mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 15:01:03 +08:00
Refactor mg_alloc_conn()
This commit is contained in:
parent
c650fdcdfc
commit
8c78b7ef53
15
mongoose.c
15
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];
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
|
@ -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];
|
||||
|
Loading…
x
Reference in New Issue
Block a user