From 8c78b7ef530eb715c06c83ceb8df7783865ce48e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 23 Feb 2022 10:51:01 +0000 Subject: [PATCH] Refactor mg_alloc_conn() --- mongoose.c | 15 ++++++++------- mongoose.h | 2 +- src/net.c | 9 ++++----- src/net.h | 2 +- src/sock.c | 6 ++++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/mongoose.c b/mongoose.c index 0a2f0a28..1091786a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2738,11 +2738,9 @@ bool mg_aton(struct mg_str str, struct mg_addr *addr) { mg_aton6(str, addr); } -struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr, bool clnt, void *fd) { +struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr) { struct mg_connection *c = (struct mg_connection *) calloc(1, sizeof(*c)); if (c != NULL) { - c->is_client = clnt; - c->fd = fd; c->mgr = mgr; c->id = ++mgr->nextid; } @@ -2771,12 +2769,13 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, struct mg_connection *c = NULL; if (url == NULL || url[0] == '\0') { MG_ERROR(("null url")); - } else if ((c = mg_alloc_conn(mgr, true, NULL)) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("OOM")); } else { LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->is_udp = (strncmp(url, "udp:", 4) == 0); c->fn = fn; + c->is_client = true; c->fn_data = fn_data; MG_DEBUG(("%lu -> %s", c->id, url)); mg_call(c, MG_EV_OPEN, NULL); @@ -2788,7 +2787,7 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data) { struct mg_connection *c = NULL; - if ((c = mg_alloc_conn(mgr, false, NULL)) == NULL) { + if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("OOM %s", url)); } else if (!mg_open_listener(c, url)) { MG_ERROR(("Failed: %s, errno %d", url, errno)); @@ -3533,7 +3532,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { MG_ERROR(("%ld > %ld", (long) fd, (long) FD_SETSIZE)); closesocket(fd); #endif - } else if ((c = mg_alloc_conn(mgr, false, S2PTR(fd))) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("%lu OOM", lsn->id)); closesocket(fd); } else { @@ -3544,6 +3543,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { mg_set_non_blocking_mode(FD(c)); setsockopts(c); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); + c->fd = S2PTR(fd); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; c->pfn = lsn->pfn; @@ -3604,13 +3604,14 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn, struct mg_connection *c = NULL; if (!mg_socketpair(sp, usa)) { MG_ERROR(("Cannot create socket pair")); - } else if ((c = mg_alloc_conn(mgr, false, S2PTR(sp[1]))) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { closesocket(sp[0]); closesocket(sp[1]); MG_ERROR(("OOM")); } else { MG_DEBUG(("pipe %lu", (unsigned long) sp[0])); tomgaddr(&usa[0], &c->rem, false); + c->fd = S2PTR(sp[1]); c->is_udp = 1; c->pfn = pf1; c->pfn_data = (void *) (size_t) sp[0]; diff --git a/mongoose.h b/mongoose.h index 7cdba101..ad0ee86a 100644 --- a/mongoose.h +++ b/mongoose.h @@ -884,7 +884,7 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *); void mg_mgr_wakeup(struct mg_connection *pipe, const void *buf, size_t len); // These functions are used to integrate with custom network stacks -struct mg_connection *mg_alloc_conn(struct mg_mgr *, bool is_client, void *fd); +struct mg_connection *mg_alloc_conn(struct mg_mgr *); void mg_close_conn(struct mg_connection *c); bool mg_open_listener(struct mg_connection *c, const char *url); diff --git a/src/net.c b/src/net.c index 66ad4095..debbb62b 100644 --- a/src/net.c +++ b/src/net.c @@ -137,11 +137,9 @@ bool mg_aton(struct mg_str str, struct mg_addr *addr) { mg_aton6(str, addr); } -struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr, bool clnt, void *fd) { +struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr) { struct mg_connection *c = (struct mg_connection *) calloc(1, sizeof(*c)); if (c != NULL) { - c->is_client = clnt; - c->fd = fd; c->mgr = mgr; c->id = ++mgr->nextid; } @@ -170,12 +168,13 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, struct mg_connection *c = NULL; if (url == NULL || url[0] == '\0') { MG_ERROR(("null url")); - } else if ((c = mg_alloc_conn(mgr, true, NULL)) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("OOM")); } else { LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->is_udp = (strncmp(url, "udp:", 4) == 0); c->fn = fn; + c->is_client = true; c->fn_data = fn_data; MG_DEBUG(("%lu -> %s", c->id, url)); mg_call(c, MG_EV_OPEN, NULL); @@ -187,7 +186,7 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data) { struct mg_connection *c = NULL; - if ((c = mg_alloc_conn(mgr, false, NULL)) == NULL) { + if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("OOM %s", url)); } else if (!mg_open_listener(c, url)) { MG_ERROR(("Failed: %s, errno %d", url, errno)); diff --git a/src/net.h b/src/net.h index d722d10d..aa895d6a 100644 --- a/src/net.h +++ b/src/net.h @@ -80,6 +80,6 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *); void mg_mgr_wakeup(struct mg_connection *pipe, const void *buf, size_t len); // These functions are used to integrate with custom network stacks -struct mg_connection *mg_alloc_conn(struct mg_mgr *, bool is_client, void *fd); +struct mg_connection *mg_alloc_conn(struct mg_mgr *); void mg_close_conn(struct mg_connection *c); bool mg_open_listener(struct mg_connection *c, const char *url); diff --git a/src/sock.c b/src/sock.c index 59bd8c53..b2e61042 100644 --- a/src/sock.c +++ b/src/sock.c @@ -365,7 +365,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { MG_ERROR(("%ld > %ld", (long) fd, (long) FD_SETSIZE)); closesocket(fd); #endif - } else if ((c = mg_alloc_conn(mgr, false, S2PTR(fd))) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { MG_ERROR(("%lu OOM", lsn->id)); closesocket(fd); } else { @@ -376,6 +376,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { mg_set_non_blocking_mode(FD(c)); setsockopts(c); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); + c->fd = S2PTR(fd); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; c->pfn = lsn->pfn; @@ -436,13 +437,14 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn, struct mg_connection *c = NULL; if (!mg_socketpair(sp, usa)) { MG_ERROR(("Cannot create socket pair")); - } else if ((c = mg_alloc_conn(mgr, false, S2PTR(sp[1]))) == NULL) { + } else if ((c = mg_alloc_conn(mgr)) == NULL) { closesocket(sp[0]); closesocket(sp[1]); MG_ERROR(("OOM")); } else { MG_DEBUG(("pipe %lu", (unsigned long) sp[0])); tomgaddr(&usa[0], &c->rem, false); + c->fd = S2PTR(sp[1]); c->is_udp = 1; c->pfn = pf1; c->pfn_data = (void *) (size_t) sp[0];