fix: 重构基础通信框架为异步事件循环,并修复 KCP conv 错位与接收漏斗堵塞问题

This commit is contained in:
meiqi
2026-03-13 21:00:38 +08:00
parent cf629fa722
commit 4d475f8c92
4 changed files with 251 additions and 211 deletions

View File

@@ -38,16 +38,14 @@ static int kcp_output(const char *buf, int len, ikcpcb *kcp, void *user)
return 0;
}
static OmniContext *kcp_init(OmniRole role,
const char *bind_ip,
uint16_t bind_port,
const char *peer_ip,
uint16_t peer_port)
{
(void)role;
struct KcpContext *ctx = (struct KcpContext *)calloc(1, sizeof(*ctx));
if (!ctx) return NULL;
static OmniContext *kcp_init(OmniRole role,
const char *bind_ip,
uint16_t bind_port,
const char *peer_ip,
uint16_t peer_port)
{
struct KcpContext *ctx = (struct KcpContext *)calloc(1, sizeof(*ctx));
if (!ctx) return NULL;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
@@ -78,8 +76,9 @@ static OmniContext *kcp_init(OmniRole role,
ctx->fd = fd;
/* conv 可简单使用端口号 */
IUINT32 conv = (IUINT32)peer_port;
/* conv 必须两端一致server 用 bind_portclient 用 peer_port */
IUINT32 conv = (role == OMNI_ROLE_SERVER) ? (IUINT32)bind_port
: (IUINT32)peer_port;
ikcpcb *kcp = ikcp_create(conv, ctx);
if (!kcp) {
logger_log("ERROR", "kcp", "ikcp_create_failed");
@@ -94,11 +93,12 @@ static OmniContext *kcp_init(OmniRole role,
ikcp_nodelay(kcp, 1, 10, 2, 1);
ikcp_wndsize(kcp, 128, 128);
logger_log("INFO", "kcp",
"init bind_port=%u peer_ip=%s peer_port=%u",
(unsigned)bind_port,
peer_ip ? peer_ip : "NULL",
(unsigned)peer_port);
logger_log("INFO", "kcp",
"init bind_port=%u peer_ip=%s peer_port=%u conv=%u",
(unsigned)bind_port,
peer_ip ? peer_ip : "NULL",
(unsigned)peer_port,
(unsigned)conv);
return (OmniContext *)ctx;
}