From a287fd97e4a5194ec0c2e6b74a4f6d5e48fa5ff1 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 22 Feb 2022 21:27:08 +0000 Subject: [PATCH] Factor mg_connect() to net.c --- mongoose.c | 37 ++++++++++++++++++------------------- src/net.c | 18 ++++++++++++++++++ src/sock.c | 19 ------------------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/mongoose.c b/mongoose.c index 32ef44c9..1af02e0e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2747,6 +2747,24 @@ struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr, bool clnt, void *fd) { } return c; } +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + 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) { + 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->fn_data = fn_data; + MG_DEBUG(("%lu -> %s", c->id, url)); + mg_call(c, MG_EV_OPEN, NULL); + mg_resolve(c, url); + } + return c; +} void mg_mgr_free(struct mg_mgr *mgr) { struct mg_connection *c; @@ -3471,25 +3489,6 @@ void mg_connect_resolved(struct mg_connection *c) { } } -struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, - mg_event_handler_t fn, void *fn_data) { - struct mg_connection *c = NULL; - if (url == NULL || url[0] == '\0') { - MG_ERROR(("null url")); - } else if ((c = mg_alloc_conn(mgr, true, S2PTR(INVALID_SOCKET))) == 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->fn_data = fn_data; - MG_DEBUG(("%lu -> %s", c->id, url)); - mg_call(c, MG_EV_OPEN, NULL); - mg_resolve(c, url); - } - return c; -} - static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { struct mg_connection *c = NULL; union usa usa; diff --git a/src/net.c b/src/net.c index 2e6ad0ee..46a3e17e 100644 --- a/src/net.c +++ b/src/net.c @@ -145,6 +145,24 @@ struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr, bool clnt, void *fd) { } return c; } +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + 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) { + 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->fn_data = fn_data; + MG_DEBUG(("%lu -> %s", c->id, url)); + mg_call(c, MG_EV_OPEN, NULL); + mg_resolve(c, url); + } + return c; +} void mg_mgr_free(struct mg_mgr *mgr) { struct mg_connection *c; diff --git a/src/sock.c b/src/sock.c index 5fc31a1f..8c954329 100644 --- a/src/sock.c +++ b/src/sock.c @@ -360,25 +360,6 @@ void mg_connect_resolved(struct mg_connection *c) { } } -struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, - mg_event_handler_t fn, void *fn_data) { - struct mg_connection *c = NULL; - if (url == NULL || url[0] == '\0') { - MG_ERROR(("null url")); - } else if ((c = mg_alloc_conn(mgr, true, S2PTR(INVALID_SOCKET))) == 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->fn_data = fn_data; - MG_DEBUG(("%lu -> %s", c->id, url)); - mg_call(c, MG_EV_OPEN, NULL); - mg_resolve(c, url); - } - return c; -} - static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { struct mg_connection *c = NULL; union usa usa;