diff options
| -rw-r--r-- | src/platform/posix/posix_epdesc.c | 2 | ||||
| -rw-r--r-- | src/platform/windows/win_tcp.c | 10 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 9 | ||||
| -rw-r--r-- | src/supplemental/http/http.c | 7 | ||||
| -rw-r--r-- | src/supplemental/http/server.c | 68 | ||||
| -rw-r--r-- | src/supplemental/mbedtls/tls.c | 8 | ||||
| -rw-r--r-- | tests/httpclient.c | 12 | ||||
| -rw-r--r-- | tests/ws.c | 1 | ||||
| -rw-r--r-- | tests/zt.c | 8 |
9 files changed, 56 insertions, 69 deletions
diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 80711ed0..7b168679 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -318,6 +318,7 @@ nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) // connection is ready for us. There isn't anything else for us to // do really, as that will have been done in listen. nni_mtx_lock(&ed->mtx); + aio->a_pipe = NULL; // If we can't start, it means that the AIO was stopped. if ((rv = nni_aio_start(aio, nni_posix_epdesc_cancel, ed)) != 0) { nni_mtx_unlock(&ed->mtx); @@ -343,6 +344,7 @@ nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) int fd; nni_mtx_lock(&ed->mtx); + aio->a_pipe = NULL; // If we can't start, it means that the AIO was stopped. if ((rv = nni_aio_start(aio, nni_posix_epdesc_cancel, ed)) != 0) { nni_mtx_unlock(&ed->mtx); diff --git a/src/platform/windows/win_tcp.c b/src/platform/windows/win_tcp.c index 01818af4..93cead91 100644 --- a/src/platform/windows/win_tcp.c +++ b/src/platform/windows/win_tcp.c @@ -163,6 +163,12 @@ nni_win_tcp_pipe_cancel(nni_win_event *evt) static void nni_win_tcp_pipe_finish(nni_win_event *evt, nni_aio *aio) { + if ((evt->status == 0) && (evt->count == 0)) { + // Windows sometimes returns a zero read. Convert these + // into an NNG_ECLOSED. (We are never supposed to come + // back with zero length read.) + evt->status = NNG_ECLOSED; + } nni_aio_finish(aio, evt->status, evt->count); } @@ -263,10 +269,10 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa, ep->s = INVALID_SOCKET; - if (rsa->s_un.s_family != NNG_AF_UNSPEC) { + if ((rsa != NULL) && (rsa->s_un.s_family != NNG_AF_UNSPEC)) { ep->remlen = nni_win_nn2sockaddr(&ep->remaddr, rsa); } - if (lsa->s_un.s_family != NNG_AF_UNSPEC) { + if ((lsa != NULL) && (lsa->s_un.s_family != NNG_AF_UNSPEC)) { ep->loclen = nni_win_nn2sockaddr(&ep->locaddr, lsa); } diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 0d9e7387..6cfb4162 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -14,20 +14,19 @@ #ifdef NNG_PLATFORM_WINDOWS +#include <stdlib.h> + void * nni_alloc(size_t sz) { - void *v; - - v = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz); - return (v); + return (calloc(sz, 1)); } void nni_free(void *b, size_t z) { NNI_ARG_UNUSED(z); - HeapFree(GetProcessHeap(), 0, b); + free(b); } void diff --git a/src/supplemental/http/http.c b/src/supplemental/http/http.c index 08a8eb13..f414c5c7 100644 --- a/src/supplemental/http/http.c +++ b/src/supplemental/http/http.c @@ -299,11 +299,12 @@ http_rd_cancel(nni_aio *aio, int rv) nni_mtx_lock(&http->mtx); if (nni_aio_list_active(aio)) { - nni_aio_list_remove(aio); if (aio == nni_list_first(&http->rdq)) { - http_close(http); + nni_aio_cancel(http->rd_aio, NNG_ECANCELED); + } else { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, rv); } - nni_aio_finish_error(aio, rv); } nni_mtx_unlock(&http->mtx); } diff --git a/src/supplemental/http/server.c b/src/supplemental/http/server.c index 92853898..ea7f15ce 100644 --- a/src/supplemental/http/server.c +++ b/src/supplemental/http/server.c @@ -51,6 +51,7 @@ typedef struct http_sconn { nni_aio * txaio; nni_aio * txdataio; nni_http_tran tran; + nni_reap_item reap; } http_sconn; struct nni_http_server { @@ -60,12 +61,10 @@ struct nni_http_server { int starts; nni_list handlers; nni_list conns; - nni_list reaps; nni_mtx mtx; nni_cv cv; bool closed; bool tls; - nni_task cleanup; nni_aio * accaio; nni_plat_tcp_ep *tep; }; @@ -74,7 +73,7 @@ static nni_list http_servers; static nni_mtx http_servers_lk; static void -http_sconn_fini(void *arg) +http_sconn_reap(void *arg) { http_sconn *sc = arg; NNI_ASSERT(!sc->finished); @@ -100,6 +99,12 @@ http_sconn_fini(void *arg) } static void +http_sconn_fini(http_sconn *sc) +{ + nni_reap(&sc->reap, http_sconn_reap, sc); +} + +static void http_sconn_close(http_sconn *sc) { nni_http_server *s; @@ -111,16 +116,16 @@ http_sconn_close(http_sconn *sc) nni_http *h; sc->closed = true; // Close the underlying transport. - if ((h = sc->http) != NULL) { - nni_http_close(h); - } if (nni_list_node_active(&sc->node)) { nni_list_remove(&s->conns, sc); - nni_list_append(&s->reaps, sc); + if (nni_list_empty(&s->conns)) { + nni_cv_wake(&s->cv); + } } - nni_task_dispatch(&s->cleanup); - } else { - nni_panic("DOUBLE CLOSE!\n"); + if ((h = sc->http) != NULL) { + nni_http_close(h); + } + http_sconn_fini(sc); } nni_mtx_unlock(&s->mtx); } @@ -544,34 +549,14 @@ http_server_acccb(void *arg) nni_mtx_lock(&s->mtx); sc->server = s; if (s->closed) { - nni_http_close(sc->http); - sc->closed = true; - nni_list_append(&s->reaps, sc); nni_mtx_unlock(&s->mtx); + http_sconn_close(sc); return; } nni_list_append(&s->conns, sc); - nni_mtx_unlock(&s->mtx); nni_http_read_req(sc->http, sc->req, sc->rxaio); nni_plat_tcp_ep_accept(s->tep, s->accaio); -} - -static void -http_server_cleanup(void *arg) -{ - nni_http_server *s = arg; - http_sconn * sc; - nni_mtx_lock(&s->mtx); - while ((sc = nni_list_first(&s->reaps)) != NULL) { - nni_list_remove(&s->reaps, sc); - nni_mtx_unlock(&s->mtx); - http_sconn_fini(sc); - nni_mtx_lock(&s->mtx); - } - if (nni_list_empty(&s->reaps) && nni_list_empty(&s->conns)) { - nni_cv_wake(&s->cv); - } nni_mtx_unlock(&s->mtx); } @@ -593,7 +578,8 @@ http_server_fini(nni_http_server *s) http_handler *h; nni_mtx_lock(&s->mtx); - while ((!nni_list_empty(&s->conns)) || (!nni_list_empty(&s->reaps))) { + nni_aio_stop(s->accaio); + while (!nni_list_empty(&s->conns)) { nni_cv_wait(&s->cv); } if (s->tep != NULL) { @@ -604,7 +590,6 @@ http_server_fini(nni_http_server *s) http_handler_fini(h); } nni_mtx_unlock(&s->mtx); - nni_task_wait(&s->cleanup); nni_aio_fini(s->accaio); nni_cv_fini(&s->cv); nni_mtx_fini(&s->mtx); @@ -634,10 +619,8 @@ http_server_init(nni_http_server **serverp, nng_sockaddr *sa) } nni_mtx_init(&s->mtx); nni_cv_init(&s->cv, &s->mtx); - nni_task_init(NULL, &s->cleanup, http_server_cleanup, s); NNI_LIST_INIT(&s->handlers, http_handler, node); NNI_LIST_INIT(&s->conns, http_sconn, node); - NNI_LIST_INIT(&s->reaps, http_sconn, node); if ((rv = nni_aio_init(&s->accaio, http_server_acccb, s)) != 0) { http_server_fini(s); return (rv); @@ -740,15 +723,16 @@ http_server_stop(nni_http_server *s) nni_plat_tcp_ep_close(s->tep); } - // This marks the server as "shutting down" -- existing - // connections finish their activity and close. - // - // XXX: figure out how to shut down connections that are - // blocked waiting to receive a request. We won't do this for - // upgraded connections... + // Stopping the server is a hard stop -- it aborts any work being + // done by clients. (No graceful shutdown). NNI_LIST_FOREACH (&s->conns, sc) { - sc->close = true; + nni_list_remove(&s->conns, sc); + if (sc->http != NULL) { + nni_http_close(sc->http); + } + http_sconn_fini(sc); } + nni_cv_wake(&s->cv); } void diff --git a/src/supplemental/mbedtls/tls.c b/src/supplemental/mbedtls/tls.c index 94503df5..d64447ac 100644 --- a/src/supplemental/mbedtls/tls.c +++ b/src/supplemental/mbedtls/tls.c @@ -76,11 +76,11 @@ struct nni_tls { bool tls_closed; // upper TLS layer closed bool tcp_closed; // underlying TCP buffer closed uint8_t * sendbuf; // send buffer - int sendlen; // amount of data in send buffer - int sendoff; // offset of start of send data + size_t sendlen; // amount of data in send buffer + size_t sendoff; // offset of start of send data uint8_t * recvbuf; // recv buffer - int recvlen; // amount of data in recv buffer - int recvoff; // offset of start of recv data + size_t recvlen; // amount of data in recv buffer + size_t recvoff; // offset of start of recv data nni_list sends; // upper side sends nni_list recvs; // upper recv aios nni_aio * handshake; // handshake aio (upper) diff --git a/tests/httpclient.c b/tests/httpclient.c index ab4f46a2..6ea6fa6e 100644 --- a/tests/httpclient.c +++ b/tests/httpclient.c @@ -30,13 +30,11 @@ TestMain("HTTP Client", { atexit(nng_fini); Convey("Given a TCP connection to httpbin.org", { - nni_plat_tcp_ep * ep; - nni_plat_tcp_pipe *p; - nng_aio * aio; - nni_aio * iaio; - nng_sockaddr rsa; - nni_http_client * cli; - nni_http * http; + nng_aio * aio; + nni_aio * iaio; + nng_sockaddr rsa; + nni_http_client *cli; + nni_http * http; So(nng_aio_alloc(&aio, NULL, NULL) == 0); iaio = (nni_aio *) aio; @@ -104,6 +104,7 @@ TestMain("WebSocket Transport", { }); trantest_next_address(addr, "ws://*:%u/test"); So(nng_listen(s1, addr, NULL, 0) == 0); + nng_msleep(100); // reset port back one trantest_prev_address(addr, "ws://127.0.0.1:%u/test"); So(nng_dial(s2, addr, NULL, 0) == 0); @@ -32,6 +32,7 @@ int mkdir(const char *path, int mode) { CreateDirectory(path, NULL); + return (0); } #else #include <sys/stat.h> @@ -146,8 +147,7 @@ check_props(nng_msg *msg, nng_listener l, nng_dialer d) So(nng_dialer_getopt_ms(d, NNG_OPT_ZT_PING_TIME, &t) == 0); So(t > 1000 && t < 3600000); // 1 sec - 1 hour - int rv = nng_dialer_setopt_int(d, NNG_OPT_ZT_PING_COUNT, 20); - + So(nng_dialer_setopt_int(d, NNG_OPT_ZT_PING_COUNT, 20) == 0); So(nng_dialer_setopt_int(d, NNG_OPT_ZT_PING_COUNT, 20) == 0); So(nng_dialer_setopt_ms(d, NNG_OPT_ZT_PING_TIME, 2000) == 0); So(nng_listener_setopt_int(l, NNG_OPT_ZT_PING_COUNT, 0) == 0); @@ -203,7 +203,6 @@ TestMain("ZeroTier Transport", { nng_listener l; nng_socket s; char addr[NNG_MAXADDRLEN]; - int rv; So(nng_zt_register() == 0); @@ -246,7 +245,6 @@ TestMain("ZeroTier Transport", { nng_dialer d; nng_socket s; char addr[NNG_MAXADDRLEN]; - int rv; // uint64_t node = 0xb000072fa6ull; // my personal host uint64_t node = 0x2d2f619cccull; // my personal host @@ -266,7 +264,6 @@ TestMain("ZeroTier Transport", { nng_listener l; nng_socket s; char addr[NNG_MAXADDRLEN]; - int rv; uint64_t node1 = 0; uint64_t node2 = 0; @@ -315,7 +312,6 @@ TestMain("ZeroTier Transport", { nng_socket s2; char addr1[NNG_MAXADDRLEN]; char addr2[NNG_MAXADDRLEN]; - int rv; uint64_t node; port = 9944; |
