Commit b0db938c authored by 马伊齐's avatar 马伊齐

增加通道初始话函数

parent 12008a6a
......@@ -20,6 +20,7 @@
int msgid = -1; // 全局变量,用于存储消息队列ID
int shmid;
messageQueue *queue;
static int is_initialized = 0; // 通道发送接收是否已经初始化标志
// 全局数组来保存socket信息
socket_info_t sockets[MAX_CLIENTS + 1] = {0};
......@@ -31,6 +32,12 @@ const char *msg_channel_paths[MSG_CHAN_MAX] = {
"/tmp/msg_channel_iomod",
};
/**
* @brief 通道发送接收初始化
*
*/
void init_msg_channel();
int sock_init(int server, uint32_t ipaddr, uint32_t port)
{
int sockfd;
......@@ -363,8 +370,28 @@ int msg_local_recv_pulse(uint16_t *type, uint32_t *code)
}
}
void init_msg_channel()
{
for (int i = 0; i < MSG_CHAN_MAX; i++)
{
if (access(msg_channel_paths[i], F_OK) == -1)
{
if (mkfifo(msg_channel_paths[i], 0666) == -1)
{
perror("init_msg_channel: mkfifo");
exit(EXIT_FAILURE);
}
}
}
}
int msg_recv(int channel, msg_data_t *msg)
{
if (is_initialized == 0 )
{
is_initialized = 1;
init_msg_channel();
}
if (channel < 0 || channel >= MSG_CHAN_MAX || msg == NULL)
{
return -1;
......@@ -385,6 +412,12 @@ int msg_recv(int channel, msg_data_t *msg)
int msg_send(int channel, uint16_t type, uint32_t code, void *pad, int len)
{
if (is_initialized == 0 )
{
is_initialized = 1;
init_msg_channel();
}
if (channel < 0 || channel >= MSG_CHAN_MAX || (len > 0 && pad == NULL))
{
return -1;
......@@ -393,8 +426,9 @@ int msg_send(int channel, uint16_t type, uint32_t code, void *pad, int len)
msg_data_t msg = {
.type = type,
.code = code,
.len = len};
memcpy((void *)(msg.pad), pad, len);
.len = len,
};
memcpy((void *)msg.pad, pad, msg.len);
int fd = open(msg_channel_paths[channel], O_WRONLY);
if (fd == -1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment