feat: 实现并完成核心功能测试套件

- 编译系统:支持通过 `make clean all` 进行全量编译,生成可执行文件 `omni_client`、`omni_server`、`omni_relay` 和 `omni_test`。
- 客户端-服务端文件传输:支持 TCP/UDP/KCP 协议,已验证文件收发功能(使用 `/tmp/input.bin` 作为测试文件)。
- 服务端指令驱动:服务端可通过控制台发送 ASCII 指令(如 `hello-client`)实时驱动客户端。
- 动态转发功能 (Relay):实现 UDP 协议下的动态目标切换,支持 `show` 查询和 `set` 命令实时修改转发目标(如从 9102 端口切换到 9103 端口)。
- 所有功能已在本地环境(127.0.0.1)通过完整流程验证。
This commit is contained in:
nnbcccscdscdsc
2026-03-13 22:39:41 +08:00
parent 4d475f8c92
commit 7ecd8a4ef4
9 changed files with 34061 additions and 152 deletions

View File

@@ -1,40 +1,94 @@
/*
* common.h
* 全局公共定义:消息头、错误码、通用宏
*/
#ifndef OMNISOCKET_COMMON_H
#define OMNISOCKET_COMMON_H
#include <stdint.h>
#include <time.h>
/* 统一的 16 字节消息头(解决 TCP 粘包用) */
typedef struct MsgHeader {
uint32_t magic; /* 固定魔数,用于快速校验 */
uint32_t length; /* 后续负载长度(字节数) */
uint64_t seq; /* 序列号或会话内消息 ID */
} MsgHeader;
#define MSG_HEADER_SIZE (sizeof(MsgHeader)) /* 16 字节 */
#define MSG_MAGIC 0x4F4D4E49u /* 'OMNI' */
/* 通用错误码(负数返回表示出错) */
enum {
OMNI_OK = 0,
OMNI_ERR_GENERIC = -1,
OMNI_ERR_PARAM = -2,
OMNI_ERR_IO = -3,
OMNI_ERR_TIMEOUT = -4
};
/* 获取当前单调时间(毫秒),用于延迟统计 */
static inline uint64_t omni_now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000u + (uint64_t)(ts.tv_nsec / 1000000u);
}
#endif /* OMNISOCKET_COMMON_H */
/*
* common.h
. * 全局公共定义:消息头、错误码、通用宏
*/
#ifndef OMNISOCKET_COMMON_H
#define OMNISOCKET_COMMON_H
#include <arpa/inet.h>
#include <stdint.h>
#include <time.h>
/* 统一的 16 字节消息头(应用层消息头) */
typedef struct MsgHeader {
uint32_t type; /* 消息类型:文件块/控制指令等 */
uint32_t len; /* 后续负载长度(字节数) */
uint64_t timestamp; /* 发送时间戳(毫秒) */
} MsgHeader;
#define MSG_HEADER_SIZE (sizeof(MsgHeader)) /* 16 字节 */
enum {
MSG_TYPE_FILE_CHUNK = 1,
MSG_TYPE_FILE_END = 2,
MSG_TYPE_COMMAND = 3,
MSG_TYPE_RAW = 100
};
#define OMNI_DEFAULT_MTU 1400u
/* 通用错误码(负数返回表示出错) */
enum {
OMNI_OK = 0,
OMNI_ERR_GENERIC = -1,
OMNI_ERR_PARAM = -2,
OMNI_ERR_IO = -3,
OMNI_ERR_TIMEOUT = -4
};
/* 获取当前单调时间(毫秒),用于延迟统计 */
static inline uint64_t omni_now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000u + (uint64_t)(ts.tv_nsec / 1000000u);
}
// 64 位整数主机序与网络序转换工具
static inline uint64_t omni_bswap64(uint64_t x)
{
return ((x & 0x00000000000000FFull) << 56) |
((x & 0x000000000000FF00ull) << 40) |
((x & 0x0000000000FF0000ull) << 24) |
((x & 0x00000000FF000000ull) << 8) |
((x & 0x000000FF00000000ull) >> 8) |
((x & 0x0000FF0000000000ull) >> 24) |
((x & 0x00FF000000000000ull) >> 40) |
((x & 0xFF00000000000000ull) >> 56);
}
static inline uint64_t omni_htonll(uint64_t x)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return omni_bswap64(x);
#else
return x;
#endif
}
static inline uint64_t omni_ntohll(uint64_t x)
{
return omni_htonll(x);
}
static inline void omni_msg_header_encode(MsgHeader *out_hdr,
uint32_t type,
uint32_t len,
uint64_t timestamp_ms)
{
out_hdr->type = htonl(type);
out_hdr->len = htonl(len);
out_hdr->timestamp = omni_htonll(timestamp_ms);
}
static inline void omni_msg_header_decode(const MsgHeader *net_hdr,
MsgHeader *host_hdr)
{
host_hdr->type = ntohl(net_hdr->type);
host_hdr->len = ntohl(net_hdr->len);
host_hdr->timestamp = omni_ntohll(net_hdr->timestamp);
}
#endif /* OMNISOCKET_COMMON_H */