fix: 重构基础通信框架为异步事件循环,并修复 KCP conv 错位与接收漏斗堵塞问题
This commit is contained in:
@@ -51,7 +51,15 @@ static void run_server(OmniProtocol proto, uint16_t port)
|
|||||||
char buf[4096];
|
char buf[4096];
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ssize_t n = omni_recv(ctx, buf, sizeof(buf));
|
ssize_t n = omni_recv(ctx, buf, sizeof(buf));
|
||||||
if (n <= 0) {
|
if (n < 0) {
|
||||||
|
logger_log("INFO", "test", "server_recv_end n=%zd", n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
if (proto == OMNI_PROTO_KCP) {
|
||||||
|
usleep(10 * 1000);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
logger_log("INFO", "test", "server_recv_end n=%zd", n);
|
logger_log("INFO", "test", "server_recv_end n=%zd", n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -98,7 +106,16 @@ static void run_client(OmniProtocol proto, const char *host, uint16_t port)
|
|||||||
if (n <= 0) break;
|
if (n <= 0) break;
|
||||||
|
|
||||||
ssize_t m = omni_recv(ctx, recv_buf, sizeof(recv_buf));
|
ssize_t m = omni_recv(ctx, recv_buf, sizeof(recv_buf));
|
||||||
if (m <= 0) {
|
if (m < 0) {
|
||||||
|
logger_log("INFO", "test", "client_recv_end i=%d bytes=%zd", i, m);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (m == 0) {
|
||||||
|
if (proto == OMNI_PROTO_KCP) {
|
||||||
|
usleep(10 * 1000);
|
||||||
|
--i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
logger_log("INFO", "test", "client_recv_end i=%d bytes=%zd", i, m);
|
logger_log("INFO", "test", "client_recv_end i=%d bytes=%zd", i, m);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -158,4 +175,3 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,15 +10,27 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
static OmniStats g_stats;
|
static OmniStats g_stats;
|
||||||
static FILE *g_json_fp = NULL;
|
static FILE *g_json_fp = NULL;
|
||||||
|
static int g_min_level = 1; /* default INFO */
|
||||||
|
|
||||||
static uint64_t now_ms(void)
|
static uint64_t now_ms(void)
|
||||||
{
|
{
|
||||||
return omni_now_ms();
|
return omni_now_ms();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int level_to_int(const char *level)
|
||||||
|
{
|
||||||
|
if (!level) return 1;
|
||||||
|
if (strcmp(level, "DEBUG") == 0) return 0;
|
||||||
|
if (strcmp(level, "INFO") == 0) return 1;
|
||||||
|
if (strcmp(level, "WARN") == 0) return 2;
|
||||||
|
if (strcmp(level, "ERROR") == 0) return 3;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static void ewma_update(double *avg, double sample, double alpha)
|
static void ewma_update(double *avg, double sample, double alpha)
|
||||||
{
|
{
|
||||||
if (*avg <= 0.0) {
|
if (*avg <= 0.0) {
|
||||||
@@ -36,6 +48,9 @@ void logger_init(void)
|
|||||||
g_stats.send_call_min_ms = UINT64_MAX;
|
g_stats.send_call_min_ms = UINT64_MAX;
|
||||||
g_stats.recv_call_min_ms = UINT64_MAX;
|
g_stats.recv_call_min_ms = UINT64_MAX;
|
||||||
|
|
||||||
|
const char *lvl_env = getenv("OMNI_LOG_LEVEL");
|
||||||
|
g_min_level = level_to_int(lvl_env);
|
||||||
|
|
||||||
if (!g_json_fp) {
|
if (!g_json_fp) {
|
||||||
g_json_fp = fopen("omni_logs.jsonl", "a");
|
g_json_fp = fopen("omni_logs.jsonl", "a");
|
||||||
}
|
}
|
||||||
@@ -230,6 +245,10 @@ void logger_log(const char *level, const char *component,
|
|||||||
const char *lvl = level ? level : "INFO";
|
const char *lvl = level ? level : "INFO";
|
||||||
const char *comp = component ? component : "general";
|
const char *comp = component ? component : "general";
|
||||||
|
|
||||||
|
if (level_to_int(lvl) < g_min_level) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
FILE *fp = stderr;
|
FILE *fp = stderr;
|
||||||
print_timestamp(fp);
|
print_timestamp(fp);
|
||||||
fprintf(fp, "level=%s component=%s ", lvl, comp);
|
fprintf(fp, "level=%s component=%s ", lvl, comp);
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ ssize_t omni_send(OmniContext *ctx, const void *buf, size_t len)
|
|||||||
}
|
}
|
||||||
logger_log("DEBUG", "network", "omni_send proto=%d bytes=%zd call_ms=%llu",
|
logger_log("DEBUG", "network", "omni_send proto=%d bytes=%zd call_ms=%llu",
|
||||||
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
||||||
|
if (n > 0) {
|
||||||
|
logger_print_performance_log("on_send");
|
||||||
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,6 +117,9 @@ ssize_t omni_recv(OmniContext *ctx, void *buf, size_t len)
|
|||||||
}
|
}
|
||||||
logger_log("DEBUG", "network", "omni_recv proto=%d bytes=%zd call_ms=%llu",
|
logger_log("DEBUG", "network", "omni_recv proto=%d bytes=%zd call_ms=%llu",
|
||||||
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
||||||
|
if (n > 0) {
|
||||||
|
logger_print_performance_log("on_recv");
|
||||||
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,4 +132,3 @@ void omni_close(OmniContext *ctx)
|
|||||||
logger_print_performance_log("final");
|
logger_print_performance_log("final");
|
||||||
free(ctx);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,6 @@ static OmniContext *kcp_init(OmniRole role,
|
|||||||
const char *peer_ip,
|
const char *peer_ip,
|
||||||
uint16_t peer_port)
|
uint16_t peer_port)
|
||||||
{
|
{
|
||||||
(void)role;
|
|
||||||
|
|
||||||
struct KcpContext *ctx = (struct KcpContext *)calloc(1, sizeof(*ctx));
|
struct KcpContext *ctx = (struct KcpContext *)calloc(1, sizeof(*ctx));
|
||||||
if (!ctx) return NULL;
|
if (!ctx) return NULL;
|
||||||
|
|
||||||
@@ -78,8 +76,9 @@ static OmniContext *kcp_init(OmniRole role,
|
|||||||
|
|
||||||
ctx->fd = fd;
|
ctx->fd = fd;
|
||||||
|
|
||||||
/* conv 可简单使用端口号 */
|
/* conv 必须两端一致:server 用 bind_port,client 用 peer_port */
|
||||||
IUINT32 conv = (IUINT32)peer_port;
|
IUINT32 conv = (role == OMNI_ROLE_SERVER) ? (IUINT32)bind_port
|
||||||
|
: (IUINT32)peer_port;
|
||||||
ikcpcb *kcp = ikcp_create(conv, ctx);
|
ikcpcb *kcp = ikcp_create(conv, ctx);
|
||||||
if (!kcp) {
|
if (!kcp) {
|
||||||
logger_log("ERROR", "kcp", "ikcp_create_failed");
|
logger_log("ERROR", "kcp", "ikcp_create_failed");
|
||||||
@@ -95,10 +94,11 @@ static OmniContext *kcp_init(OmniRole role,
|
|||||||
ikcp_wndsize(kcp, 128, 128);
|
ikcp_wndsize(kcp, 128, 128);
|
||||||
|
|
||||||
logger_log("INFO", "kcp",
|
logger_log("INFO", "kcp",
|
||||||
"init bind_port=%u peer_ip=%s peer_port=%u",
|
"init bind_port=%u peer_ip=%s peer_port=%u conv=%u",
|
||||||
(unsigned)bind_port,
|
(unsigned)bind_port,
|
||||||
peer_ip ? peer_ip : "NULL",
|
peer_ip ? peer_ip : "NULL",
|
||||||
(unsigned)peer_port);
|
(unsigned)peer_port,
|
||||||
|
(unsigned)conv);
|
||||||
|
|
||||||
return (OmniContext *)ctx;
|
return (OmniContext *)ctx;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user