diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-30 21:41:40 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-05-01 12:27:19 -0700 |
| commit | 4998964a435fe0f02a2d81b01fdb837214674e72 (patch) | |
| tree | dc5fe29da0574fc1510ab6f9b23b54ffd68cd97b | |
| parent | 63bdb2c28bc185096e579d1922d57cb71ecaa36b (diff) | |
| download | nng-4998964a435fe0f02a2d81b01fdb837214674e72.tar.gz nng-4998964a435fe0f02a2d81b01fdb837214674e72.tar.bz2 nng-4998964a435fe0f02a2d81b01fdb837214674e72.zip | |
fixes #381 Want comparators for various types
| -rw-r--r-- | docs/man/libnng.3.adoc | 5 | ||||
| -rw-r--r-- | docs/man/nng_ctx.5.adoc | 9 | ||||
| -rw-r--r-- | docs/man/nng_ctx_id.3.adoc | 50 | ||||
| -rw-r--r-- | docs/man/nng_dialer.5.adoc | 7 | ||||
| -rw-r--r-- | docs/man/nng_dialer_id.3.adoc | 51 | ||||
| -rw-r--r-- | docs/man/nng_listener.5.adoc | 7 | ||||
| -rw-r--r-- | docs/man/nng_listener_id.3.adoc | 51 | ||||
| -rw-r--r-- | docs/man/nng_pipe.5.adoc | 14 | ||||
| -rw-r--r-- | docs/man/nng_pipe_close.3.adoc | 2 | ||||
| -rw-r--r-- | docs/man/nng_pipe_id.3.adoc | 50 | ||||
| -rw-r--r-- | docs/man/nng_socket.5.adoc | 14 | ||||
| -rw-r--r-- | docs/man/nng_socket_id.3.adoc | 50 | ||||
| -rw-r--r-- | src/core/pipe.c | 15 | ||||
| -rw-r--r-- | src/nng.c | 62 | ||||
| -rw-r--r-- | src/nng.h | 27 | ||||
| -rw-r--r-- | tests/message.c | 12 | ||||
| -rw-r--r-- | tests/pair1.c | 36 | ||||
| -rw-r--r-- | tests/reconnect.c | 2 | ||||
| -rw-r--r-- | tests/sock.c | 17 | ||||
| -rw-r--r-- | tests/surveyctx.c | 10 | ||||
| -rw-r--r-- | tests/tcp.c | 2 | ||||
| -rw-r--r-- | tests/tcp6.c | 4 | ||||
| -rw-r--r-- | tests/tls.c | 6 | ||||
| -rw-r--r-- | tests/trantest.h | 78 | ||||
| -rw-r--r-- | tests/ws.c | 2 | ||||
| -rw-r--r-- | tests/wss.c | 2 | ||||
| -rw-r--r-- | tests/wssfile.c | 6 | ||||
| -rw-r--r-- | tests/zt.c | 2 |
28 files changed, 494 insertions, 99 deletions
diff --git a/docs/man/libnng.3.adoc b/docs/man/libnng.3.adoc index 8df29861..149dc9f4 100644 --- a/docs/man/libnng.3.adoc +++ b/docs/man/libnng.3.adoc @@ -49,6 +49,7 @@ The following functions operate on sockets. |<<nng_recv.3#,nng_recv()>>|receive data |<<nng_send.3#,nng_send()>>|send data |<<nng_setopt.3#,nng_setopt()>>|set socket option +|<<nng_socket_id.3#,nng_socket_id()>>|get numeric socket identifier |=== === Connection Management @@ -61,16 +62,19 @@ Listeners accept incoming connection requets, and dialers make them. |<<nng_dialer_close.3#,nng_dialer_close()>>|close dialer |<<nng_dialer_create.3#,nng_dialer_create()>>|create dialer |<<nng_dialer_getopt.3#,nng_dialer_getopt()>>|get dialer option +|<<nng_dialer_id.3#,nng_dialer_id()>>|get numeric dialer identifier |<<nng_dialer_setopt.3#,nng_dialer_setopt()>>|set dialer option |<<nng_dialer_start.3#,nng_dialer_start()>>|start dialer |<<nng_listen.3#,nng_listen()>>|create and start listener |<<nng_listener_close.3#,nng_listener_close()>>|close listener |<<nng_listener_create.3#,nng_listener_create()>>|create listener |<<nng_listener_getopt.3#,nng_listener_getopt()>>|get listener option +|<<nng_listener_id.3#,nng_listener_id()>>|get numeric listener identifier |<<nng_listener_setopt.3#,nng_listener_setopt()>>|set listener option |<<nng_listener_start.3#,nng_listener_start()>>|start listener |<<nng_pipe_close.3#,nng_pipe_close()>>|close pipe |<<nng_pipe_getopt.3#,nng_pipe_getopt()>>|get pipe option +|<<nng_pipe_id.3#,nng_pipe_id()>>|get numeric pipe identifier |=== === Message Handling Functions @@ -205,6 +209,7 @@ concurrent applications. |=== |<<nng_ctx_close.3#,nng_ctx_close()>>|close context |<<nng_ctx_getopt.3#,nng_ctx_getopt()>>|get context option +|<<nng_ctx_id.3#,nng_ctx_id()>>|get numeric context identifier |<<nng_ctx_open.3#,nng_ctx_open()>>|create context |<<nng_ctx_recv.3#,nng_ctx_recv()>>|receive message using context asynchronously |<<nng_ctx_send.3#,nng_ctx_send()>>|send message using context asynchronously diff --git a/docs/man/nng_ctx.5.adoc b/docs/man/nng_ctx.5.adoc index 98402738..ada117c3 100644 --- a/docs/man/nng_ctx.5.adoc +++ b/docs/man/nng_ctx.5.adoc @@ -81,6 +81,12 @@ are in use on the same socket is not supported, and may lead to unpredictable behavior. These asynchronous methods should not be mixed on the same socket. +[[NNG_CTX_INITIALIZER]] +=== Initialization + +A context may be initialized using the macro `NNG_CTX_INITIALIZER` +before it is opened, to prevent confusion with valid open contexts. + == EXAMPLE The following program fragment demonstrates the use of contexts to implement @@ -91,7 +97,7 @@ to the sender. ---- struct echo_context { - nng_ctx *ctx; + nng_ctx ctx; nng_aio *aio; enum { INIT, RECV, SEND } state; }; @@ -156,6 +162,7 @@ start_echo_service(nng_socket rep_socket) <<nng_ctx_close.3#,nng_ctx_close(3)>>, <<nng_ctx_open.3#,nng_ctx_open(3)>>, <<nng_ctx_getopt.3#,nng_ctx_getopt(3)>>, +<<nng_ctx_id.3#,nng_ctx_id(3)>>, <<nng_ctx_recv.3#,nng_ctx_recv(3)>>, <<nng_ctx_send.3#,nng_ctx_send(3)>>, <<nng_ctx_setopt.3#,nng_ctx_setopt(3)>>, diff --git a/docs/man/nng_ctx_id.3.adoc b/docs/man/nng_ctx_id.3.adoc new file mode 100644 index 00000000..56d9d7bd --- /dev/null +++ b/docs/man/nng_ctx_id.3.adoc @@ -0,0 +1,50 @@ += nng_ctx_id(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_ctx_id - return numeric context identifier + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +int nng_ctx_id(nng_ctx c); +---- + +== DESCRIPTION + +The `nng_ctx_id()` function returns a positive identifier for the context _c_, +if it is valid. +Otherwise it returns `-1`. + +NOTE: A context is considered valid if it was ever opened with +<<nng_ctx_open.3#,`nng_ctx_open`>> function. +Contexts that are allocated on the stack or statically should be +initialized with the macro +<<nng_ctx.5#NNG_CTX_INITIALIZER,`NNG_CTX_INITIALIZER`>> to ensure that +they cannot be confused with a valid context before they are opened. + +== RETURN VALUES + +This function returns the positive value for the context identifier, or +`-1` if the context is invalid. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_ctx.5#,nng_ctx(5)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_dialer.5.adoc b/docs/man/nng_dialer.5.adoc index 45c60bc9..3f742c21 100644 --- a/docs/man/nng_dialer.5.adoc +++ b/docs/man/nng_dialer.5.adoc @@ -58,12 +58,19 @@ Dialer objects may be destroyed by the <<nng_dialer_close.3#,`nng_pipe_close()`>> function. They are also closed when their "`owning`" socket is closed. +[[NNG_DIALER_INITIALIZER]] +=== Initialization + +A dialer may be initialized using the macro `NNG_DIALER_INITIALIZER` +before it is opened, to prevent confusion with valid open dialers. + == SEE ALSO <<nng_dial.3#,nng_dial(3)>>, <<nng_dialer_close.3#,nng_dialer_close(3)>>, <<nng_dialer_create.3#,nng_dialer_create(3)>>, <<nng_dialer_getopt.3#,nng_dialer_getopt(3)>>, +<<nng_dialer_id.3#,nng_dialer_id(3)>>, <<nng_dialer_setopt.3#,nng_dialer_setopt(3)>>, <<nng_dialer_start.3#,nng_dialer_start(3)>>, <<nng_listener.5#,nng_listener(5)>>, diff --git a/docs/man/nng_dialer_id.3.adoc b/docs/man/nng_dialer_id.3.adoc new file mode 100644 index 00000000..cf8936b5 --- /dev/null +++ b/docs/man/nng_dialer_id.3.adoc @@ -0,0 +1,51 @@ += nng_dialer_id(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_dialer_id - return numeric dialer identifier + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +int nng_dialer_id(nng_dialer d); +---- + +== DESCRIPTION + +The `nng_dialer_id()` function returns a positive identifier for the dialer _d_, +if it is valid. +Otherwise it returns `-1`. + +NOTE: A dialer is considered valid if it was ever created with the +<<nng_dialer_create.3#,`nng_dialer_create`>> or +<<nng_dial.3#,`nng_dial`>> functions. +Dialers that are allocated on the stack or statically should be +initialized with the macro +<<nng_ctx.5#NNG_DIALER_INITIALIZER,`NNG_DIALER_INITIALIZER`>> to ensure that +they cannot be confused with a valid dialer before they are created properly. + +== RETURN VALUES + +This function returns the positive value for the dialer identifier, or +`-1` if the dialer is invalid. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_dialer.5#,nng_dialer(5)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_listener.5.adoc b/docs/man/nng_listener.5.adoc index 558eb920..e7187a61 100644 --- a/docs/man/nng_listener.5.adoc +++ b/docs/man/nng_listener.5.adoc @@ -55,12 +55,19 @@ Listener objects may be destroyed by the <<nng_listener_close.3#,`nng_listener_close()`>> function. They are also closed when their "`owning`" socket is closed. +[[NNG_LISTENER_INITIALIZER]] +=== Initialization + +A listener may be initialized using the macro `NNG_LISTENER_INITIALIZER` +before it is opened, to prevent confusion with valid open listener. + == SEE ALSO <<nng_listen.3#,nng_listen(3)>>, <<nng_listener_close.3#,nng_listener_close(3)>>, <<nng_listener_create.3#,nng_listener_create(3)>>, <<nng_listener_getopt.3#,nng_listener_getopt(3)>>, +<<nng_listener_id.3#,nng_listener_id(3)>>, <<nng_listener_setopt.3#,nng_listener_setopt(3)>>, <<nng_listener_start.3#,nng_listener_start(3)>>, <<nng_dialer.5#,nng_dialer(5)>>, diff --git a/docs/man/nng_listener_id.3.adoc b/docs/man/nng_listener_id.3.adoc new file mode 100644 index 00000000..44404e14 --- /dev/null +++ b/docs/man/nng_listener_id.3.adoc @@ -0,0 +1,51 @@ += nng_listener_id(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_listener_id - return numeric listener identifier + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +int nng_listener_id(nng_listener l); +---- + +== DESCRIPTION + +The `nng_listener_id()` function returns a positive identifier for the +listener _l_, if it is valid. +Otherwise it returns `-1`. + +NOTE: A listener is considered valid if it was ever created with the +<<nng_listener_create.3#,`nng_listener_create`>> or +<<nng_listen.3#,`nng_listen`>> functions. +listeners that are allocated on the stack or statically should be +initialized with the macro +<<nng_ctx.5#NNG_LISTENER_INITIALIZER,`NNG_LISTENER_INITIALIZER`>> to ensure that +they cannot be confused with a valid listener before they are created properly. + +== RETURN VALUES + +This function returns the positive value for the listener identifier, or +`-1` if the listener is invalid. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_listener.5#,nng_listener(5)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_pipe.5.adoc b/docs/man/nng_pipe.5.adoc index 39247fbd..a7423884 100644 --- a/docs/man/nng_pipe.5.adoc +++ b/docs/man/nng_pipe.5.adoc @@ -51,11 +51,25 @@ Pipe objects may be destroyed by the They are also closed when their "`owning`" dialer or listener is closed, or when the remote peer closes the underlying connection. +[[NNG_PIPE_INITIALIZER]] +=== Initialization + +A pipe may be initialized using the macro `NNG_PIPE_INITIALIZER` +before it is opened, to prevent confusion with valid open pipes. + +For example: + +[source, c] +---- +nng_pipe p = NNG_PIPE_INITIALIZER; +---- + == SEE ALSO <<nng_msg_get_pipe.3#,nng_msg_get_pipe(3)>>, <<nng_pipe_close.3#,nng_pipe_close(3)>>, <<nng_pipe_getopt.3#,nng_pipe_getopt(3)>>, +<<nng_pipe_id.3#,nng_pipe_id(3)>>, <<nng_dialer.5#,nng_dialer(5)>>, <<nng_listener.5#,nng_listener(5)>>, <<nng_options.5#,nng_options(5)>>, diff --git a/docs/man/nng_pipe_close.3.adoc b/docs/man/nng_pipe_close.3.adoc index 598e143a..2128caec 100644 --- a/docs/man/nng_pipe_close.3.adoc +++ b/docs/man/nng_pipe_close.3.adoc @@ -19,7 +19,7 @@ nng_pipe_close - close pipe ---- #include <nng/nng.h> -int nng_pipe_close(nng_socket s); +int nng_pipe_close(nng_pipe p); ---- == DESCRIPTION diff --git a/docs/man/nng_pipe_id.3.adoc b/docs/man/nng_pipe_id.3.adoc new file mode 100644 index 00000000..b327dac2 --- /dev/null +++ b/docs/man/nng_pipe_id.3.adoc @@ -0,0 +1,50 @@ += nng_pipe_id(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_pipe_id - return numeric pipe identifier + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +int nng_pipe_id(nng_pipe p); +---- + +== DESCRIPTION + +The `nng_pipe_id()` function returns a positive identifier for the pipe _p_, +if it is valid. +Otherwise it returns `-1`. + +NOTE: A pipe is considered valid if it was ever opened with +<<nng_pipe_open.3#,`nng_pipe_open`>> function. +Pipes that are allocated on the stack or statically should be +initialized with the macro +<<nng_pipe.5#NNG_PIPE_INITIALIZER,`NNG_PIPE_INITIALIZER`>> to ensure that +they cannot be confused with a valid pipe before they are opened. + +== RETURN VALUES + +This function returns the positive value for the pipe identifier, or +`-1` if the pipe is invalid. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_pipe.5#,nng_pipe(5)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_socket.5.adoc b/docs/man/nng_socket.5.adoc index 4711495f..2d8d7256 100644 --- a/docs/man/nng_socket.5.adoc +++ b/docs/man/nng_socket.5.adoc @@ -44,12 +44,26 @@ Each `nng_socket` is created by a protocol-specific constructor, such as When the socket is no longer needed, it can be closed with <<nng_close.3#,`nng_close()`>>. +[[NNG_SOCKET_INITIALIZER]] +=== Initialization + +A socket may be initialized using the macro `NNG_SOCKET_INITIALIZER` +before it is opened, to prevent confusion with valid open sockets. + +For example: + +[source, c] +---- +nng_socket s = NNG_SOCKET_INITIALIZER; +---- + == SEE ALSO <<libnng.3#,libnng(3)>>, <<nng_close.3#,nng_close(3)>>, <<nng_getopt.3#,nng_getopt(3)>>, <<nng_setopt.3#,nng_setopt(3)>>, +<<nng_socket_id.3#,nng_socket_id(3)>>, <<nng_dialer.5#,nng_dialer(5)>>, <<nng_listener.5#,nng_listener(5)>>, <<nng_options.5#,nng_options(5)>>, diff --git a/docs/man/nng_socket_id.3.adoc b/docs/man/nng_socket_id.3.adoc new file mode 100644 index 00000000..b5acc98b --- /dev/null +++ b/docs/man/nng_socket_id.3.adoc @@ -0,0 +1,50 @@ += nng_socket_id(3) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This document is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +== NAME + +nng_socket_id - return numeric socket identifier + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> + +int nng_socket_id(nng_socket s); +---- + +== DESCRIPTION + +The `nng_socket_id()` function returns a positive identifier for the socket _s_, +if it is valid. +Otherwise it returns `-1`. + +NOTE: A socket is considered valid if it was ever opened with a protocol +constructor, such as the <<nng_req0_open.3#,`nng_req0_open`>> function. +Sockets that are allocated on the stack or statically should be +initialized with the macro +<<nng_socket.5#NNG_SOCKET_INITIALIZER,`NNG_SOCKET_INITIALIZER`>> to ensure that +they cannot be confused with a valid socket before they are opened. + +== RETURN VALUES + +This function returns the positive value for the socket identifier, or +`-1` if the socket is invalid. + +== ERRORS + +None. + +== SEE ALSO + +<<nng_socket.5#,nng_socket(5)>>, +<<nng.7#,nng(7)>> diff --git a/src/core/pipe.c b/src/core/pipe.c index a4148c65..c619fca0 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -26,7 +26,7 @@ struct nni_pipe { nni_list_node p_ep_node; nni_sock * p_sock; nni_ep * p_ep; - int p_reap; + bool p_reap; int p_stop; int p_refcnt; nni_mtx p_mtx; @@ -142,8 +142,12 @@ nni_pipe_find(nni_pipe **pp, uint32_t id) nni_pipe *p; nni_mtx_lock(&nni_pipe_lk); if ((rv = nni_idhash_find(nni_pipes, id, (void **) &p)) == 0) { - p->p_refcnt++; - *pp = p; + if (p->p_reap) { + rv = NNG_ECLOSED; + } else { + p->p_refcnt++; + *pp = p; + } } nni_mtx_unlock(&nni_pipe_lk); return (rv); @@ -186,12 +190,12 @@ void nni_pipe_close(nni_pipe *p) { nni_mtx_lock(&p->p_mtx); - if (p->p_reap == 1) { + if (p->p_reap) { // We already did a close. nni_mtx_unlock(&p->p_mtx); return; } - p->p_reap = 1; + p->p_reap = true; // Close the underlying transport. if (p->p_tran_data != NULL) { @@ -267,6 +271,7 @@ nni_pipe_create(nni_ep *ep, void *tdata) p->p_proto_data = NULL; p->p_ep = ep; p->p_sock = sock; + p->p_reap = false; NNI_LIST_NODE_INIT(&p->p_reap_node); NNI_LIST_NODE_INIT(&p->p_sock_node); @@ -46,6 +46,12 @@ nng_close(nng_socket s) return (rv); } +int +nng_socket_id(nng_socket s) +{ + return (((int) s.id > 0) ? (int) s.id : -1); +} + void nng_closeall(void) { @@ -230,12 +236,12 @@ nng_send_aio(nng_socket s, nng_aio *aio) } int -nng_ctx_open(nng_ctx *idp, nng_socket s) +nng_ctx_open(nng_ctx *cp, nng_socket s) { nni_sock *sock; nni_ctx * ctx; int rv; - nng_ctx id; + nng_ctx c; if ((rv = nni_sock_find(&sock, s.id)) != 0) { return (rv); @@ -244,20 +250,20 @@ nng_ctx_open(nng_ctx *idp, nng_socket s) nni_sock_rele(sock); return (rv); } - id.id = nni_ctx_id(ctx); - *idp = id; + c.id = nni_ctx_id(ctx); nni_ctx_rele(ctx); nni_sock_rele(sock); + *cp = c; return (0); } int -nng_ctx_close(nng_ctx cid) +nng_ctx_close(nng_ctx c) { int rv; nni_ctx *ctx; - if ((rv = nni_ctx_find(&ctx, cid.id, true)) != 0) { + if ((rv = nni_ctx_find(&ctx, c.id, true)) != 0) { return (rv); } // no release, close releases implicitly. @@ -265,6 +271,12 @@ nng_ctx_close(nng_ctx cid) return (0); } +int +nng_ctx_id(nng_ctx c) +{ + return (((int) c.id > 0) ? (int) c.id : -1); +} + void nng_ctx_recv(nng_ctx cid, nng_aio *aio) { @@ -497,6 +509,12 @@ nng_listener_start(nng_listener l, int flags) } int +nng_listener_id(nng_listener l) +{ + return (((int) l.id > 0) ? (int) l.id : -1); +} + +int nng_dialer_create(nng_dialer *dp, nng_socket s, const char *addr) { nni_sock * sock; @@ -532,6 +550,12 @@ nng_dialer_start(nng_dialer d, int flags) return (rv); } +int +nng_dialer_id(nng_dialer d) +{ + return (((int) d.id > 0) ? (int) d.id : -1); +} + static int nng_ep_setx( uint32_t id, const char *n, const void *v, size_t sz, int mode, int t) @@ -1088,19 +1112,19 @@ nng_strerror(int num) } static int -nng_pipe_getx(nng_pipe id, const char *name, void *val, size_t *szp, int t) +nng_pipe_getx(nng_pipe p, const char *name, void *val, size_t *szp, int t) { int rv; - nni_pipe *p; + nni_pipe *pipe; if ((rv = nni_init()) < 0) { return (rv); } - if ((rv = nni_pipe_find(&p, id.id)) != 0) { + if ((rv = nni_pipe_find(&pipe, p.id)) != 0) { return (rv); } - rv = nni_pipe_getopt(p, name, val, szp, t); - nni_pipe_rele(p); + rv = nni_pipe_getopt(pipe, name, val, szp, t); + nni_pipe_rele(pipe); return (rv); } @@ -1167,19 +1191,25 @@ nng_pipe_getopt_string(nng_pipe p, const char *name, char **valp) } int -nng_pipe_close(nng_pipe pid) +nng_pipe_close(nng_pipe p) { int rv; - nni_pipe *p; + nni_pipe *pipe; - if ((rv = nni_pipe_find(&p, pid.id)) != 0) { + if ((rv = nni_pipe_find(&pipe, p.id)) != 0) { return (rv); } - nni_pipe_close(p); - nni_pipe_rele(p); + nni_pipe_close(pipe); + nni_pipe_rele(pipe); return (0); } +int +nng_pipe_id(nng_pipe p) +{ + return (((int) p.id > 0) ? (int) p.id : -1); +} + // Message handling. int nng_msg_alloc(nng_msg **msgp, size_t size) @@ -92,6 +92,15 @@ typedef struct nng_snapshot nng_snapshot; typedef struct nng_stat nng_stat; typedef struct nng_aio nng_aio; +// Initializers. +// clang-format off +#define NNG_PIPE_INITIALIZER { 0 } +#define NNG_SOCKET_INITIALIZER { 0 } +#define NNG_DIALER_INITIALIZER { 0 } +#define NNG_LISTENER_INITIALIZER { 0 } +#define NNG_CTX_INITIALIZER { 0 } +// clang-format on + // Some address details. This is in some ways like a traditional sockets // sockaddr, but we have our own to cope with our unique families, etc. // The details of this structure are directly exposed to applications. @@ -181,6 +190,10 @@ NNG_DECL void nng_fini(void); // resources. NNG_DECL int nng_close(nng_socket); +// nng_socket_id returns the positive socket id for the socket, or -1 +// if the socket is not valid. +NNG_DECL int nng_socket_id(nng_socket); + // nng_closeall closes all open sockets. Do not call this from // a library; it will affect all sockets. NNG_DECL void nng_closeall(void); @@ -249,6 +262,14 @@ NNG_DECL int nng_dialer_close(nng_dialer); // connections and releasing all associated resources. NNG_DECL int nng_listener_close(nng_listener); +// nng_dialer_id returns the positive dialer ID, or -1 if the dialer is +// invalid. +NNG_DECL int nng_dialer_id(nng_dialer); + +// nng_listener_id returns the positive listener ID, or -1 if the listener is +// invalid. +NNG_DECL int nng_listener_id(nng_listener); + // nng_dialer_setopt sets an option for a specific dialer. Note // dialer options may not be altered on a running dialer. NNG_DECL int nng_dialer_setopt(nng_dialer, const char *, const void *, size_t); @@ -379,6 +400,11 @@ NNG_DECL int nng_ctx_open(nng_ctx *, nng_socket); // nng_ctx_close closes the context. NNG_DECL int nng_ctx_close(nng_ctx); +// nng_ctx_id returns the numeric id for the context; this will be +// a postive value for a valid context, or < 0 for an invalid context. +// A valid context is not necessarily an *open* context. +NNG_DECL int nng_ctx_id(nng_ctx); + // nng_ctx_recv receives asynchronously. It works like nng_recv_aio, but // uses a local context instead of the socket global context. NNG_DECL void nng_ctx_recv(nng_ctx, nng_aio *); @@ -573,6 +599,7 @@ NNG_DECL int nng_pipe_getopt_uint64(nng_pipe, const char *, uint64_t *); NNG_DECL int nng_pipe_getopt_ptr(nng_pipe, const char *, void **); NNG_DECL int nng_pipe_getopt_string(nng_pipe, const char *, char **); NNG_DECL int nng_pipe_close(nng_pipe); +NNG_DECL int nng_pipe_id(nng_pipe); // Flags. enum nng_flag_enum { diff --git a/tests/message.c b/tests/message.c index bf212b48..7f40cd38 100644 --- a/tests/message.c +++ b/tests/message.c @@ -113,13 +113,17 @@ TestMain("Message Tests", { }); Convey("Pipe retrievals work", { - nng_pipe p; + nng_pipe p = NNG_PIPE_INITIALIZER; + nng_pipe p0 = NNG_PIPE_INITIALIZER; + + So(nng_pipe_id(p0) < 0); p = nng_msg_get_pipe(msg); - So(p.id == 0); - p.id = 45; + So(nng_pipe_id(p) < 0); + memset(&p, 0x22, sizeof(p)); nng_msg_set_pipe(msg, p); p = nng_msg_get_pipe(msg); - So(p.id == 45); + So(nng_pipe_id(p) != nng_pipe_id(p0)); + So(nng_pipe_id(p) == 0x22222222); }); Convey("Message realloc works", { diff --git a/tests/pair1.c b/tests/pair1.c index 8a27bc6e..c777725c 100644 --- a/tests/pair1.c +++ b/tests/pair1.c @@ -28,14 +28,13 @@ TestMain("PAIRv1 protocol", { const char * templ = "inproc://pairv1/%u"; char addr[NNG_MAXADDRLEN + 1]; - nng_socket s1; - nng_socket c1; - nng_socket c2; + nng_socket s1 = NNG_SOCKET_INITIALIZER; + nng_socket c1 = NNG_SOCKET_INITIALIZER; + nng_socket c2 = NNG_SOCKET_INITIALIZER; nng_duration tmo; uint32_t v; size_t sz; - s1.id = c1.id = c2.id = 0; atexit(nng_fini); Convey("Given a few sockets", { @@ -44,6 +43,13 @@ TestMain("PAIRv1 protocol", { So(nng_pair1_open(&c1) == 0); So(nng_pair1_open(&c2) == 0); + So(nng_socket_id(s1) > 0); + So(nng_socket_id(c1) > 0); + So(nng_socket_id(c2) > 0); + So(nng_socket_id(s1) != nng_socket_id(c1)); + So(nng_socket_id(s1) != nng_socket_id(c2)); + So(nng_socket_id(c1) != nng_socket_id(c2)); + Reset({ nng_close(s1); nng_close(c1); @@ -211,7 +217,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1.id != 0); + So(nng_pipe_id(p1) > 0); nng_msg_free(msg); So(nng_msg_alloc(&msg, 0) == 0); @@ -220,10 +226,10 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "TWO"); p2 = nng_msg_get_pipe(msg); - So(p2.id != 0); + So(nng_pipe_id(p2) > 0); nng_msg_free(msg); - So(p1.id != p2.id); + So(nng_pipe_id(p1) != nng_pipe_id(p2)); So(nng_msg_alloc(&msg, 0) == 0); @@ -308,7 +314,7 @@ TestMain("PAIRv1 protocol", { nng_msleep(20); Convey("Send/recv work", { - nng_pipe p; + nng_pipe p = NNG_PIPE_INITIALIZER; So(nng_msg_alloc(&msg, 0) == 0); APPENDSTR(msg, "GAMMA"); So(nng_msg_header_append_u32(msg, 1) == 0); @@ -316,7 +322,8 @@ TestMain("PAIRv1 protocol", { So(nng_sendmsg(c1, msg, 0) == 0); So(nng_recvmsg(s1, &msg, 0) == 0); p = nng_msg_get_pipe(msg); - So(p.id != 0); + So(nng_pipe_id(p) > 0); + CHECKSTR(msg, "GAMMA"); So(nng_msg_header_len(msg) == sizeof(uint32_t)); So(nng_msg_header_trim_u32(msg, &hops) == 0); @@ -332,7 +339,8 @@ TestMain("PAIRv1 protocol", { So(nng_msg_header_len(msg) == sizeof(uint32_t)); So(nng_msg_header_trim_u32(msg, &hops) == 0); p = nng_msg_get_pipe(msg); - So(p.id != 0); + So(nng_pipe_id(p) > 0); + So(hops == 2); nng_msg_free(msg); }); @@ -474,7 +482,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1.id != 0); + So(nng_pipe_id(p1) > 0); So(nng_msg_header_trim_u32(msg, &hops) == 0); So(hops == 1); nng_msg_free(msg); @@ -485,12 +493,12 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "TWO"); p2 = nng_msg_get_pipe(msg); - So(p2.id != 0); + So(nng_pipe_id(p2) > 0); So(nng_msg_header_trim_u32(msg, &hops) == 0); So(hops == 1); nng_msg_free(msg); - So(p1.id != p2.id); + So(nng_pipe_id(p1) != nng_pipe_id(p2)); So(nng_msg_alloc(&msg, 0) == 0); nng_msg_set_pipe(msg, p1); @@ -518,7 +526,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1.id != 0); + So(nng_pipe_id(p1) > 0); nng_msg_free(msg); nng_close(c1); diff --git a/tests/reconnect.c b/tests/reconnect.c index 4cd6cb3a..6b4e8586 100644 --- a/tests/reconnect.c +++ b/tests/reconnect.c @@ -96,7 +96,7 @@ TestMain("Reconnect works", { CHECKSTR(msg, "again"); p2 = nng_msg_get_pipe(msg); nng_msg_free(msg); - So(p2.id != p1.id); + So(nng_pipe_id(p2) != nng_pipe_id(p1)); }); }); }); diff --git a/tests/sock.c b/tests/sock.c index d6e36235..37b713dd 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -333,10 +333,12 @@ TestMain("Socket Operations", { Convey("Listening works", { char * a = "inproc://here"; - nng_listener l; + nng_listener l = NNG_LISTENER_INITIALIZER; + + So(nng_listener_id(l) < 0); rv = nng_listen(s1, a, &l, 0); So(rv == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); Convey("Second listen fails ADDRINUSE", { rv = nng_listen(s1, a, NULL, 0); @@ -347,7 +349,8 @@ TestMain("Socket Operations", { { So(nng_listener_start(l, 0) == NNG_ESTATE); }); Convey("We can connect to it", { - nng_socket s2; + nng_socket s2 = NNG_SOCKET_INITIALIZER; + So(nng_socket_id(s2) < 0); So(nng_pair_open(&s2) == 0); Reset({ nng_close(s2); }); So(nng_dial(s2, a, NULL, 0) == 0); @@ -356,9 +359,13 @@ TestMain("Socket Operations", { }); Convey("Dialer creation ok", { - nng_dialer ep; - char * a = "tcp://127.0.0.1:2929"; + nng_dialer ep = NNG_DIALER_INITIALIZER; + char * a = "tcp://127.0.0.1:2929"; + + So(nng_dialer_id(ep) < 0); So(nng_dialer_create(&ep, s1, a) == 0); + So(nng_dialer_id(ep) > 0); + Convey("Options work", { size_t sz; So(nng_dialer_setopt_size( diff --git a/tests/surveyctx.c b/tests/surveyctx.c index f4d8e703..aa36eca1 100644 --- a/tests/surveyctx.c +++ b/tests/surveyctx.c @@ -92,7 +92,7 @@ TestMain("Surveyor concurrent contexts", { atexit(nng_fini); Convey("We can use Surveyor contexts concurrently", { - nng_socket surv; + nng_socket surv = NNG_SOCKET_INITIALIZER; So(nng_aio_alloc(&resp_state.aio, (void *) resp_cb, NULL) == 0); @@ -145,6 +145,14 @@ TestMain("Surveyor concurrent contexts", { if ((rv = nng_ctx_open(&ctxs[i], surv)) != 0) { break; } + if (nng_ctx_id(ctxs[i]) < 0) { + Fail("Invalid context ID"); + break; + } + if ((i > 0) && + (nng_ctx_id(ctxs[i]) == nng_ctx_id(ctxs[i - 1]))) { + Fail("Context IDs not different"); + } } So(rv == 0); So(i == NCTX); diff --git a/tests/tcp.c b/tests/tcp.c index 3d2ecfc8..d29ceb8d 100644 --- a/tests/tcp.c +++ b/tests/tcp.c @@ -30,7 +30,7 @@ check_props_v4(nng_msg *msg) bool b; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); So(la.s_family == NNG_AF_INET); So(la.s_in.sa_port == htons(trantest_port - 1)); diff --git a/tests/tcp6.c b/tests/tcp6.c index 80135b33..c023997f 100644 --- a/tests/tcp6.c +++ b/tests/tcp6.c @@ -49,7 +49,7 @@ check_props_v6(nng_msg *msg) nng_sockaddr la; z = sizeof(nng_sockaddr); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); So(z == sizeof(la)); So(la.s_family == NNG_AF_INET6); @@ -62,7 +62,7 @@ check_props_v6(nng_msg *msg) nng_sockaddr ra; z = sizeof(nng_sockaddr); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == 0); So(z == sizeof(ra)); So(ra.s_family == NNG_AF_INET6); diff --git a/tests/tls.c b/tests/tls.c index 71e28deb..8ecf342a 100644 --- a/tests/tls.c +++ b/tests/tls.c @@ -114,7 +114,7 @@ check_props_v4(nng_msg *msg) nng_sockaddr ra; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); // Typed access So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); @@ -426,7 +426,7 @@ TestMain("TLS Transport", { So(nng_msg_len(msg) == 6); So(strcmp(nng_msg_body(msg), "hello") == 0); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_bool(p, NNG_OPT_TLS_VERIFIED, &b) == 0); So(b == false); nng_msg_free(msg); @@ -468,7 +468,7 @@ TestMain("TLS Transport", { So(nng_msg_len(msg) == 6); So(strcmp(nng_msg_body(msg), "hello") == 0); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_bool(p, NNG_OPT_TLS_VERIFIED, &b) == 0); So(b == true); int i; diff --git a/tests/trantest.h b/tests/trantest.h index ed1eccb7..de82099c 100644 --- a/tests/trantest.h +++ b/tests/trantest.h @@ -170,9 +170,8 @@ trantest_fini(trantest *tt) int trantest_dial(trantest *tt, nng_dialer *dp) { - nng_dialer d; + nng_dialer d = NNG_DIALER_INITIALIZER; int rv; - d.id = 0; rv = nng_dialer_create(&d, tt->reqsock, tt->addr); if (rv != 0) { @@ -196,8 +195,7 @@ int trantest_listen(trantest *tt, nng_listener *lp) { int rv; - nng_listener l; - l.id = 0; + nng_listener l = NNG_LISTENER_INITIALIZER; rv = nng_listener_create(&l, tt->repsock, tt->addr); if (rv != 0) { @@ -231,13 +229,12 @@ void trantest_conn_refused(trantest *tt) { Convey("Connection refused works", { - nng_dialer d; - d.id = 0; + nng_dialer d = NNG_DIALER_INITIALIZER; So(trantest_dial(tt, &d) == NNG_ECONNREFUSED); - So(d.id == 0); + So(nng_dialer_id(d) < 0); So(trantest_dial(tt, &d) == NNG_ECONNREFUSED); - So(d.id == 0); + So(nng_dialer_id(d) < 0); }); } @@ -245,14 +242,15 @@ void trantest_duplicate_listen(trantest *tt) { Convey("Duplicate listen rejected", { - nng_listener l; + nng_listener l1 = NNG_LISTENER_INITIALIZER; + nng_listener l2 = NNG_LISTENER_INITIALIZER; int rv; - rv = trantest_listen(tt, &l); + rv = trantest_listen(tt, &l1); So(rv == 0); - So(l.id != 0); - l.id = 0; - So(trantest_listen(tt, &l) == NNG_EADDRINUSE); - So(l.id == 0); + So(nng_listener_id(l1) > 0); + So(trantest_listen(tt, &l2) == NNG_EADDRINUSE); + So(nng_listener_id(l2) < 0); + So(nng_listener_id(l1) != nng_listener_id(l2)); }); } @@ -260,15 +258,16 @@ void trantest_listen_accept(trantest *tt) { Convey("Listen and accept", { - nng_listener l; - nng_dialer d; + nng_listener l = NNG_LISTENER_INITIALIZER; + nng_dialer d = NNG_DIALER_INITIALIZER; + nng_dialer d0 = NNG_DIALER_INITIALIZER; So(trantest_listen(tt, &l) == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); nng_msleep(200); - d.id = 0; So(trantest_dial(tt, &d) == 0); - So(d.id != 0); + So(nng_dialer_id(d) > 0); + So(nng_dialer_id(d0) < 0); }); } @@ -276,18 +275,19 @@ void trantest_send_recv(trantest *tt) { Convey("Send and recv", { - nng_listener l; - nng_dialer d; + nng_listener l = NNG_LISTENER_INITIALIZER; + nng_dialer d = NNG_DIALER_INITIALIZER; + nng_pipe p = NNG_PIPE_INITIALIZER; nng_msg * send; nng_msg * recv; size_t len; - nng_pipe p; char * url; So(trantest_listen(tt, &l) == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); + So(trantest_dial(tt, &d) == 0); - So(d.id != 0); + So(nng_dialer_id(d) > 0); nng_msleep(200); // listener may be behind slightly @@ -313,7 +313,7 @@ trantest_send_recv(trantest *tt) So(nng_msg_len(recv) == strlen("acknowledge")); So(strcmp(nng_msg_body(recv), "acknowledge") == 0); p = nng_msg_get_pipe(recv); - So(p.id != 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_string(p, NNG_OPT_URL, &url) == 0); So(strcmp(url, tt->addr) == 0); nng_strfree(url); @@ -325,19 +325,19 @@ void trantest_send_recv_multi(trantest *tt) { Convey("Send and recv multi", { - nng_listener l; - nng_dialer d; + nng_listener l = NNG_LISTENER_INITIALIZER; + nng_dialer d = NNG_DIALER_INITIALIZER; + nng_pipe p = NNG_PIPE_INITIALIZER; nng_msg * send; nng_msg * recv; - nng_pipe p; char * url; int i; char msgbuf[16]; So(trantest_listen(tt, &l) == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); So(trantest_dial(tt, &d) == 0); - So(d.id != 0); + So(nng_dialer_id(d) > 0); nng_msleep(200); // listener may be behind slightly @@ -367,7 +367,7 @@ trantest_send_recv_multi(trantest *tt) So(nng_msg_len(recv) == strlen(msgbuf) + 1); So(strcmp(nng_msg_body(recv), msgbuf) == 0); p = nng_msg_get_pipe(recv); - So(p.id != 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_string(p, NNG_OPT_URL, &url) == 0); So(strcmp(url, tt->addr) == 0); nng_strfree(url); @@ -380,16 +380,16 @@ void trantest_check_properties(trantest *tt, trantest_proptest_t f) { Convey("Properties test", { - nng_listener l; - nng_dialer d; + nng_listener l = NNG_LISTENER_INITIALIZER; + nng_dialer d = NNG_DIALER_INITIALIZER; nng_msg * send; nng_msg * recv; int rv; So(trantest_listen(tt, &l) == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); So(trantest_dial(tt, &d) == 0); - So(d.id != 0); + So(nng_dialer_id(d) > 0); nng_msleep(200); // listener may be behind slightly @@ -415,8 +415,8 @@ void trantest_send_recv_large(trantest *tt) { Convey("Send and recv large data", { - nng_listener l; - nng_dialer d; + nng_listener l = NNG_LISTENER_INITIALIZER; + nng_dialer d = NNG_DIALER_INITIALIZER; nng_msg * send; nng_msg * recv; char * data; @@ -430,9 +430,9 @@ trantest_send_recv_large(trantest *tt) } So(trantest_listen(tt, &l) == 0); - So(l.id != 0); + So(nng_listener_id(l) > 0); So(trantest_dial(tt, &d) == 0); - So(d.id != 0); + So(nng_dialer_id(d) > 0); nng_msleep(200); // listener may be behind slightly @@ -32,7 +32,7 @@ check_props_v4(nng_msg *msg) size_t len; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_sockaddr(p, NNG_OPT_LOCADDR, &la) == 0); So(la.s_family == NNG_AF_INET); diff --git a/tests/wss.c b/tests/wss.c index 333af56c..fac33398 100644 --- a/tests/wss.c +++ b/tests/wss.c @@ -141,7 +141,7 @@ check_props(nng_msg *msg) size_t len; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); z = sizeof(nng_sockaddr); So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); diff --git a/tests/wssfile.c b/tests/wssfile.c index 6b1d1a7b..9117c277 100644 --- a/tests/wssfile.c +++ b/tests/wssfile.c @@ -142,7 +142,7 @@ check_props(nng_msg *msg) size_t len; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); // Typed z = sizeof(nng_sockaddr); @@ -349,7 +349,7 @@ TestMain("WebSocket Secure (TLS) Transport (file based)", { So(nng_msg_len(msg) == 6); So(strcmp(nng_msg_body(msg), "hello") == 0); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_bool(p, NNG_OPT_TLS_VERIFIED, &b) == 0); So(b == false); nng_msg_free(msg); @@ -391,7 +391,7 @@ TestMain("WebSocket Secure (TLS) Transport (file based)", { So(nng_msg_len(msg) == 6); So(strcmp(nng_msg_body(msg), "hello") == 0); p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); So(nng_pipe_getopt_bool(p, NNG_OPT_TLS_VERIFIED, &b) == 0); So(b == true); nng_msg_free(msg); @@ -44,7 +44,7 @@ check_props(nng_msg *msg) { nng_pipe p; p = nng_msg_get_pipe(msg); - So(p.id > 0); + So(nng_pipe_id(p) > 0); // Check local address. Convey("Local address property works", { |
