fix:更新客户端功能

This commit is contained in:
nnbcccscdscdsc
2026-03-16 22:28:05 +08:00
parent 7f2f79e672
commit 6c975d9ae3
18 changed files with 8067 additions and 33092 deletions

View File

@@ -18,6 +18,7 @@
#include <string.h>
#include <unistd.h>
/* 打印测试程序命令行帮助。 */
static void usage(const char *prog)
{
fprintf(stderr,
@@ -27,6 +28,7 @@ static void usage(const char *prog)
prog, prog);
}
/* 解析协议名,非法输入时默认回退 TCP。 */
static OmniProtocol parse_proto(const char *s)
{
if (strcmp(s, "tcp") == 0) return OMNI_PROTO_TCP;
@@ -35,6 +37,10 @@ static OmniProtocol parse_proto(const char *s)
return OMNI_PROTO_TCP;
}
/*
* 测试服务端:
* 持续收消息并原样 echo 回去,主要用于验证双向收发路径是否通畅。
*/
static void run_server(OmniProtocol proto, uint16_t port)
{
OmniContext *ctx = omni_init(OMNI_ROLE_SERVER, proto,
@@ -56,6 +62,7 @@ static void run_server(OmniProtocol proto, uint16_t port)
break;
}
if (n == 0) {
/* KCP 在“暂时没拼出完整消息”时可能返回 0因此这里不能立刻判定连接结束。 */
if (proto == OMNI_PROTO_KCP) {
usleep(10 * 1000);
continue;
@@ -73,6 +80,10 @@ static void run_server(OmniProtocol proto, uint16_t port)
omni_close(ctx);
}
/*
* 测试客户端:
* 连续发 100 条消息,每发一条就等一条 echo用来观测协议往返行为。
*/
static void run_client(OmniProtocol proto, const char *host, uint16_t port)
{
if (!host) {
@@ -111,6 +122,7 @@ static void run_client(OmniProtocol proto, const char *host, uint16_t port)
break;
}
if (m == 0) {
/* 对 KCP 来说0 更像“此刻没取到完整消息”,而不是 socket 关闭。 */
if (proto == OMNI_PROTO_KCP) {
usleep(10 * 1000);
--i;
@@ -129,6 +141,7 @@ static void run_client(OmniProtocol proto, const char *host, uint16_t port)
omni_close(ctx);
}
/* 程序入口:根据角色参数派发到测试客户端或测试服务端。 */
int main(int argc, char **argv)
{
const char *role_str = NULL;