Commit 95594f8f authored by 马伊齐's avatar 马伊齐

Socket部分除注销注册函数的测试库完成

parent 509e1f53
......@@ -14,7 +14,7 @@
#include <mqueue.h>
#include <sys/ipc.h>
Message messageQueue [ MAX_MESSAGES ]; // 消息队列
Message messageQueue[MAX_MESSAGES]; // 消息队列
int messageCount = 0; // 消息队列中消息数量
int msgid = -1; // 全局变量,用于存储消息队列ID
......@@ -30,7 +30,7 @@ typedef struct
socket_info_t sockets[MAX_CLIENTS + 1] = {0};
// 消息通道路径数组
const char *msg_channel_paths [ MSG_CHAN_MAX ] = {
const char *msg_channel_paths[MSG_CHAN_MAX] = {
"/tmp/msg_channel_app",
"/tmp/msg_channel_lim",
"/tmp/msg_channel_iomod",
......@@ -43,7 +43,7 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
// 创建socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if ( sockfd < 0 )
if (sockfd < 0)
{
// 错误处理
perror("socket error");
......@@ -51,15 +51,15 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
}
// 如果是服务器模式
if ( server )
if (server)
{
// 初始化服务器地址结构,绑定IP地址和端口
bzero(( char * )&serv_addr, sizeof(serv_addr)); // 清零结构体
bzero((char *)&serv_addr, sizeof(serv_addr)); // 清零结构体
serv_addr.sin_family = AF_INET; // 设置服务器地址结构
serv_addr.sin_addr.s_addr = ipaddr; // 设置IP地址
serv_addr.sin_addr.s_addr = htonl(ipaddr); // 设置IP地址
serv_addr.sin_port = htons(port); // 设置端口
if ( bind(sockfd, ( struct sockaddr * )&serv_addr, sizeof(serv_addr)) < 0 )
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
// 错误处理
perror("bind error");
......@@ -67,7 +67,7 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
}
// 监听
if ( listen(sockfd, 20) < 0 )
if (listen(sockfd, 20) < 0)
{
// 错误处理
perror("listen error");
......@@ -78,12 +78,12 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
else
{
// 连接
bzero(( char * )&serv_addr, sizeof(serv_addr)); // 清零结构体
bzero((char *)&serv_addr, sizeof(serv_addr)); // 清零结构体
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = ipaddr; // 设置IP地址
serv_addr.sin_port = htons(port); // 设置端口
if ( connect(sockfd, ( struct sockaddr * )&serv_addr, sizeof(serv_addr)) < 0 )
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
// 错误处理
perror("connect error");
......@@ -101,21 +101,16 @@ void sock_exit(int fd)
int sock_write(int fd, const uint8_t *buf, int len)
{
int bytes_sent = 0; // 记录已发送的字节数
int n; // 记录每次发送的字节数
int bytes_sent; // 记录每次发送的字节数
while ( bytes_sent < len )
{
// 使用write函数发送数据,返回值是实际写入的字节数
n = write(fd, buf + bytes_sent, len - bytes_sent);
if ( n < 0 )
bytes_sent = write(fd, buf, len);
if (bytes_sent < 0)
{
// 如果write函数返回-1,表示发送失败
if ( errno == EINTR )
if (errno == EINTR)
{
// 如果是因为被信号中断,则继续发送剩余的数据
perror("Interrupted system call, write again");
continue;
perror("Interrupted system call");
}
else
{
......@@ -124,39 +119,28 @@ int sock_write(int fd, const uint8_t *buf, int len)
return -1;
}
}
else if ( n == 0 )
else if (bytes_sent == 0)
{
// 如果write函数返回0,表示对端已经关闭连接,发送失败
perror("Peer closed connection");
return -1;
}
else
{
// 更新已发送的字节数
bytes_sent += n;
}
}
return bytes_sent;
}
int sock_read(int fd, uint8_t *buf, int len)
{
int bytes_received = 0; // 记录已接收的字节数
int n;
int bytes_received;
while ( bytes_received < len )
{
// 使用read函数接收数据,返回值是实际读入的字节数
n = read(fd, buf + bytes_received, len - bytes_received);
if ( n < 0 )
bytes_received = read(fd, buf, len);
if (bytes_received < 0)
{
// 如果read函数返回-1,表示接收失败
if ( errno == EINTR )
if (errno == EINTR)
{
// 如果是因为被信号中断,则继续接收剩余的数据
perror("Interrupted system call, read again");
continue;
}
else
{
......@@ -165,17 +149,10 @@ int sock_read(int fd, uint8_t *buf, int len)
return -1;
}
}
else if ( n == 0 )
else if (bytes_received == 0)
{
// 如果read函数返回0,表示对端已经关闭连接,接收完成
printf("Peer closed connection\n");
break;
}
else
{
// 更新已接收的字节数
bytes_received += n;
}
perror("Peer closed connection");
}
return bytes_received;
......@@ -187,7 +164,7 @@ int sock_info(int fd, uint32_t *ipaddr, uint32_t *port)
socklen_t peer_addr_len = sizeof(peer_addr); // 对端地址结构体长度
// 使用getpeername函数获取对端地址信息
if ( getpeername(fd, ( struct sockaddr * )&peer_addr, &peer_addr_len) == -1 )
if (getpeername(fd, (struct sockaddr *)&peer_addr, &peer_addr_len) == -1)
{
// 如果getpeername函数返回-1,表示获取失败
perror("getpeername error");
......@@ -231,18 +208,18 @@ int sock_ev_unregister(int fd)
return -1; // 失败,未找到对应的socket
}
int init_msg_queue( )
int init_msg_queue()
{
key_t key;
key = ftok(".", 10);
if ( key == -1 )
if (key == -1)
{
perror("ftok(): ");
return -1;
}
msgid = msgget(key, IPC_CREAT | 0666);
if ( msgid == -1 )
if (msgid == -1)
{
perror("msgget(): ");
return -1;
......@@ -251,11 +228,11 @@ int init_msg_queue( )
return 0;
}
int destroy_msg_queue( )
int destroy_msg_queue()
{
if ( msgid != -1 )
if (msgid != -1)
{
if ( msgctl(msgid, IPC_RMID, NULL) == -1 )
if (msgctl(msgid, IPC_RMID, NULL) == -1)
{
perror("msgctl(): ");
return -1;
......@@ -267,26 +244,26 @@ int destroy_msg_queue( )
int msg_local_recv(msg_data_t *msg)
{
if ( msgid == -1 )
if (msgid == -1)
{
fprintf(stderr, "Message queue not initialized.\n");
return -1;
}
printf("msgid: %d\n", msgid);
ssize_t rbytes = msgrcv(msgid, ( void * )msg, sizeof(msg_data_t), 0, 0);
if ( rbytes == -1 )
ssize_t rbytes = msgrcv(msgid, (void *)msg, sizeof(msg_data_t), 0, 0);
if (rbytes == -1)
{
perror("[ERROR] msgrcv(): ");
return -1;
}
return ( int )rbytes;
return (int)rbytes;
}
int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
{
if ( msgid == -1 )
if (msgid == -1)
{
fprintf(stderr, "Message queue not initialized.\n");
return -1;
......@@ -298,7 +275,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
memcpy(msg.pad, pad, len);
msg.len = len;
if ( msgsnd(msgid, ( const void * )&msg, sizeof(msg), 0) == -1 )
if (msgsnd(msgid, (const void *)&msg, sizeof(msg), 0) == -1)
{
perror("msgsnd(): ");
return -1;
......@@ -309,14 +286,14 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
int msg_local_recv_pulse(uint16_t *type, uint32_t *code)
{
if ( messageCount > 0 )
if (messageCount > 0)
{
*type = messageQueue [ 0 ].type;
*code = messageQueue [ 0 ].code;
*type = messageQueue[0].type;
*code = messageQueue[0].code;
// 移动队列
for ( int i = 0; i < messageCount - 1; i++ )
for (int i = 0; i < messageCount - 1; i++)
{
messageQueue [ i ] = messageQueue [ i + 1 ];
messageQueue[i] = messageQueue[i + 1];
}
messageCount--;
return 0; // 成功
......@@ -330,10 +307,10 @@ int msg_local_recv_pulse(uint16_t *type, uint32_t *code)
int msg_local_send_pulse(uint16_t type, uint32_t code)
{
if ( messageCount < MAX_MESSAGES )
if (messageCount < MAX_MESSAGES)
{
messageQueue [ messageCount ].type = type;
messageQueue [ messageCount ].code = code;
messageQueue[messageCount].type = type;
messageQueue[messageCount].code = code;
messageCount++;
return 0; // 成功
}
......@@ -346,13 +323,13 @@ int msg_local_send_pulse(uint16_t type, uint32_t code)
int msg_recv(int channel, msg_data_t *msg)
{
if ( channel < 0 || channel >= MSG_CHAN_MAX || msg == NULL )
if (channel < 0 || channel >= MSG_CHAN_MAX || msg == NULL)
{
return -1;
}
int fd = open(msg_channel_paths [ channel ], O_RDONLY);
if ( fd == -1 )
int fd = open(msg_channel_paths[channel], O_RDONLY);
if (fd == -1)
{
perror("msg_recv: open");
return -1;
......@@ -366,7 +343,7 @@ 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 ( channel < 0 || channel >= MSG_CHAN_MAX || (len > 0 && pad == NULL) )
if (channel < 0 || channel >= MSG_CHAN_MAX || (len > 0 && pad == NULL))
{
return -1;
}
......@@ -377,8 +354,8 @@ int msg_send(int channel, uint16_t type, uint32_t code, void *pad, int len)
.len = len};
memcpy((void *)(msg.pad), pad, len);
int fd = open(msg_channel_paths [ channel ], O_WRONLY);
if ( fd == -1 )
int fd = open(msg_channel_paths[channel], O_WRONLY);
if (fd == -1)
{
perror("msg_send: open");
return -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