diff --git a/asnw/nw_clt.c b/asnw/nw_clt.c index 8376ec3..412facb 100644 --- a/asnw/nw_clt.c +++ b/asnw/nw_clt.c @@ -152,13 +152,19 @@ nw_clt *nw_clt_create(nw_clt_cfg *cfg, nw_clt_type *type, void *privdata) memset(clt, 0, sizeof(nw_clt)); clt->type = *type; clt->reconnect_timeout = cfg->reconnect_timeout == 0 ? 1.0 : cfg->reconnect_timeout; - clt->buf_pool = nw_buf_pool_create(cfg->max_pkg_size); + if (cfg->buf_pool) { + clt->custom_buf_pool = true; + clt->buf_pool = cfg->buf_pool; + } else { + clt->custom_buf_pool = false; + clt->buf_pool = nw_buf_pool_create(cfg->max_pkg_size); + if (clt->buf_pool == NULL) { + nw_clt_release(clt); + return NULL; + } + } clt->read_mem = cfg->read_mem; clt->write_mem = cfg->write_mem; - if (clt->buf_pool == NULL) { - nw_clt_release(clt); - return NULL; - } nw_addr_t *host_addr = malloc(sizeof(nw_addr_t)); if (host_addr == NULL) { @@ -241,7 +247,7 @@ void nw_clt_release(nw_clt *clt) if (clt->ses.write_buf) { nw_ses_release(&clt->ses); } - if (clt->buf_pool) { + if (!clt->custom_buf_pool && clt->buf_pool) { nw_buf_pool_release(clt->buf_pool); } free(clt->ses.host_addr); diff --git a/asnw/nw_clt.h b/asnw/nw_clt.h index cbc8ff3..2b9d00b 100644 --- a/asnw/nw_clt.h +++ b/asnw/nw_clt.h @@ -22,6 +22,7 @@ typedef struct nw_clt_cfg { uint32_t read_mem; uint32_t write_mem; double reconnect_timeout; + nw_buf_pool *buf_pool; } nw_clt_cfg; typedef struct nw_clt_type { @@ -36,6 +37,7 @@ typedef struct nw_clt_type { typedef struct nw_clt { nw_ses ses; nw_clt_type type; + bool custom_buf_pool; nw_buf_pool *buf_pool; nw_timer timer; bool connected; diff --git a/asnw/nw_ses.c b/asnw/nw_ses.c index 3974a03..8223e27 100644 --- a/asnw/nw_ses.c +++ b/asnw/nw_ses.c @@ -322,15 +322,13 @@ static void on_can_connect(nw_ses *ses) { if (ses->sockfd < 0) return; - errno = nw_sock_errno(ses->sockfd); if (errno != 0) { ses->on_connect(ses, false); return; } - - ses->on_connect(ses, true); watch_read(ses); + ses->on_connect(ses, true); } static void libev_on_read_write_evt(struct ev_loop *loop, ev_io *watcher, int events) @@ -378,8 +376,8 @@ int nw_ses_connect(nw_ses *ses, nw_addr_t *addr) { int ret = connect(ses->sockfd, NW_SOCKADDR(addr), addr->addrlen); if (ret == 0) { - ses->on_connect(ses, true); watch_read(ses); + ses->on_connect(ses, true); return 0; } if (errno == EINPROGRESS) {