问题
如何拓展之前开发的通信框架,使其支持 UDP 通信,进而成为一个完善的网络通信框架?
UDP 通信拓展
UDP 通信实体概要设计
每个 UDP Point 地位对等,可通过 IP 地址和 port 号进行通信
UDP Point 数据收发单位为:Message 或 Byte
在接口设计上,与 TcpClient 保持一致 (框架接口一致性)
目标:封装原生 socket 细节,关注 UDP 通信逻辑
UDP 通信实体接口设计
关键代码实现 - 初始化
关键代码实现 - 消息接收
由于 UDP 是以数据报的方式进行通信 (非数据流方式,报文间有明显边界)
因此,不能直接通过 MParser_ReadFd(...) 解析出消息
必须先将报文完整接收到内存中,再进行从内存中解析出消息
即通过 MParser_ReadMem(...) 间接完成消息解析
可以通过 MSG_PEEK 来获取缓冲区中的报文长度
关键代码实现 - 消息发送
关键代码实现 - 通信选项设置
由于 UdpPoint 封装了 socket 文件描述符的细节,所以需要对外提供改变 socket 属性的接口函数,以便用户使用广播和多播以及其他的功能。
UDP 通信端设计与实现
udp_point.h
#ifndef UDP_POINT_H
#define UDP_POINT_H
#include "message.h"
typedef void UdpPoint;
UdpPoint* UdpPoint_New(int port);
UdpPoint* UdpPoint_From(int fd);
int UdpPoint_SendMsg(UdpPoint* point, Message* msg, const char* remote, int port);
int UdpPoint_SendRaw(UdpPoint* point, char* buf, int length, const char* remote, int port);
Message* UdpPoint_RecvMsg(UdpPoint* point, char* remote, int* port);
int UdpPoint_RecvRaw(UdpPoint* point, char* buf, int length, char* remote, int* port);
void UdpPoint_Del(UdpPoint* point);
int UdpPoint_Available(UdpPoint* point);
void UdpPoint_SetData(UdpPoint* point, void* data);
void* UdpPoint_GetData(UdpPoint* point);
int UdpPoint_SetOpt(UdpPoint* point, int level, int optname, const void* optval, unsigned int optlen);
int UdpPoint_GetOpt(UdpPoint* point, int level, int optname, void* optval, unsigned int* optlen);
#endif
udp_point.c
#include "udp_point.h"
#include "msg_parser.h"
#include <malloc.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <string.h>
typedef struct udp_point
{
int fd;
MParser* parser;
void* data;
} Point;
char g_temp[4096] = {0};
static void ParseAddr(struct sockaddr_in addr, char* remote, int* port)
{
if(remote)
{
strcpy(remote, inet_ntoa(addr.sin_addr));
}
if(port)
{
*port = ntohs(addr.sin_port);
}
}
UdpPoint* UdpPoint_New(int port)
{
Point* ret = NULL;
int fd = -1;
struct sockaddr_in addr = {0};
int ok = 1;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
ok = ok && ((fd = socket(PF_INET, SOCK_DGRAM, 0))!= -1);
ok = ok && (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) != -1);
ok = ok && (ret = UdpPoint_From(fd));
if(!ok)
{
if(fd != -1)
{
close(fd);
}
}
return ret;
}
UdpPoint* UdpPoint_From(int fd)
{
Point* ret = (Point*)malloc(sizeof(Point));
if(ret)
{
ret->fd = fd;
ret->parser = MParser_New();
ret->data = NULL;
}
return ((ret && ret->parser) ? ret : (free(ret), ret = NULL));
}
int UdpPoint_SendMsg(UdpPoint* point, Message* msg, const char* remote, int port)
{
int ret = -1;
int length = 0;
char* buf = (char*)msg;
if(point && msg && remote)
{
length = Message_Size(msg);
Message_H2N(msg);
ret = UdpPoint_SendRaw(point, buf, length, remote, port);
Message_N2H(msg);
}
return ret;
}
int UdpPoint_SendRaw(UdpPoint* point, char* buf, int length, const char* remote, int port)
{
int ret = -1;
Point* c = (Point*)point;
if(c && buf && remote)
{
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(remote);
addr.sin_port = htons(port);
ret = sendto(c->fd, buf, length, 0, (struct sockaddr*)&addr, sizeof(addr));
}
return ret;
}
Message* UdpPoint_RecvMsg(UdpPoint* point, char* remote, int* port)
{
Message* ret = NULL;
Point* c = (Point*)point;
if(c)
{
int length = 0;
length = recvfrom(c->fd, g_temp, sizeof(g_temp), MSG_PEEK , NULL, NULL);
if(length > 0)
{
char* buf = (char*)malloc(length);
if(buf)
{
length = UdpPoint_RecvRaw(point, buf, length, remote, port);
ret = MParser_ReadMem(c->parser, buf, length);
free(buf);
}
}
}
return ret;
}
int UdpPoint_RecvRaw(UdpPoint* point, char* buf, int length, char* remote, int* port)
{
int ret = -1;
Point* c = (Point*)point;
if(c && buf)
{
struct sockaddr_in addr = {0};
socklen_t addrlen = sizeof(addr);
ret = recvfrom(c->fd, buf, length, 0, (struct sockaddr*)&addr, &addrlen);
if(ret != -1)
{
ParseAddr(addr, remote, port);
}
}
return ret;
}
void UdpPoint_Del(UdpPoint* point)
{
Point* c = (Point*)point;
if(c)
{
close(c->fd);
MParser_Del(c->parser);
free(c);
}
}
int UdpPoint_Available(UdpPoint* point)
{
int ret = -1;
Point* c = (Point*)point;
if(c)
{
ret = recvfrom(c->fd, g_temp, sizeof(g_temp), MSG_PEEK | MSG_DONTWAIT, NULL, NULL);
}
return ret;
}
void UdpPoint_SetData(UdpPoint* point, void* data)
{
Point* c = (Point*)point;
if(c)
{
c->data = data;
}
}
void* UdpPoint_GetData(UdpPoint* point)
{
Point* c = (Point*)point;
return (c ? c->data : NULL);
}
int UdpPoint_SetOpt(UdpPoint* point, int level, int optname, const void* optval, unsigned int optlen)
{
int ret = -1;
Point* c = (Point*)point;
if(c)
{
ret = setsockopt(c->fd, level, optname, optval, optlen);
}
return ret;
}
int UdpPoint_GetOpt(UdpPoint* point, int level, int optname, void* optval, unsigned int* optlen)
{
int ret = -1;
Point* c = (Point*)point;
if(c)
{
ret = getsockopt(c->fd, level, optname, optval, optlen);
}
return ret;
}
udp_test.c
#include "udp_point.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
char buf[4096] = {0};
int main(int argc, char* argv[])
{
UdpPoint* point = UdpPoint_New(6666);
Message* msg = NULL;
int i = 0;
char remote[16] = {0};
int port = 0;
printf("point = %p\n", point);
sleep(5);
i = UdpPoint_Available(point);
printf("i = %d\n", i);
// i = UdpPoint_RecvRaw(point, buf, sizeof(buf), remote, &port);
// printf("buf = %s\n", buf);
// printf("remote ip = %s, port = %d\n", remote, port);
// UdpPoint_SendRaw(point, buf, i, remote, port);
msg = UdpPoint_RecvMsg(point, remote, &port);
if(msg != NULL)
{
printf("remote ip = %s, port = %d\n", remote, port);
printf("type = %d\n", msg->type);
printf("cmd = %d\n", msg->cmd);
printf("index = %d\n", msg->index);
printf("total = %d\n", msg->total);
for(int j = 0; j < msg->length; j++)
{
printf("%02X ", msg->payload[j]);
}
printf("\n");
UdpPoint_SendMsg(point, msg, remote, port);
free(msg);
}
UdpPoint_Del(point);
return 0;
}
每个 UDP 数据报都有一个长度,接收端必须以该长度为最小单元将其所有内容一次性读出,否则数据将被截断。因为 MParser_ReadFd(...) 会先读取缓冲区的12个字节作为头部信息,然后再去读取头部中所记录的字节长度,会读取两次,所以在第一次读取到12个字节数据后,这个数据报的剩余字节数据就读取不到了。所以采用先把数据报的内容读取到我们定义的缓冲区中,然后再去通过我们定义的缓冲区去解析它,以间接的方式来获取 Message。
测试裸数据的测试结果如下图所示:
测试 Message 的测试结果如下图所示: