diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-09 08:32:33 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-11 10:31:44 -0800 |
| commit | db5467e6c3b502f6e93a6f6c5d1f2a8f86a8eaa5 (patch) | |
| tree | 71fb23ed0f0e2eb980245fcdf72cf2fce5d152be /src | |
| parent | 768eaa4d50a6a436b714188ec9c6b98fd729b306 (diff) | |
| download | nng-db5467e6c3b502f6e93a6f6c5d1f2a8f86a8eaa5.tar.gz nng-db5467e6c3b502f6e93a6f6c5d1f2a8f86a8eaa5.tar.bz2 nng-db5467e6c3b502f6e93a6f6c5d1f2a8f86a8eaa5.zip | |
Add new functions for socket identity.
These functions were added in the main branch for NNG 2.0, and
we add them here to facilitate converting any code that happened
to use the options (`NNG_OPT_PROTO`, `NNG_OPT_PROTONAME`,
`NNG_OPT_PEER`, `NNG_OPT_PEERNAME`, and `NNG_OPT_RAW`.
Its unlikely that many applications need this, but providing these
as a transition aid may help applications start adopting new APIs now.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/socket.c | 6 | ||||
| -rw-r--r-- | src/core/socket.h | 1 | ||||
| -rw-r--r-- | src/nng.c | 79 | ||||
| -rw-r--r-- | src/sp/protocol/bus0/bus_test.c | 22 |
4 files changed, 97 insertions, 11 deletions
diff --git a/src/core/socket.c b/src/core/socket.c index c4e16f70..bf550a24 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -877,6 +877,12 @@ nni_sock_peer_name(nni_sock *sock) return (sock->s_peer_id.p_name); } +bool +nni_sock_raw(nni_sock *sock) +{ + return ((nni_sock_flags(sock) & NNI_PROTO_FLAG_RAW) != 0); +} + struct nni_proto_pipe_ops * nni_sock_proto_pipe_ops(nni_sock *sock) { diff --git a/src/core/socket.h b/src/core/socket.h index c4037e96..343310ca 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -22,6 +22,7 @@ extern uint16_t nni_sock_proto_id(nni_sock *); extern uint16_t nni_sock_peer_id(nni_sock *); extern const char *nni_sock_proto_name(nni_sock *); extern const char *nni_sock_peer_name(nni_sock *); +extern bool nni_sock_raw(nni_sock *); extern void *nni_sock_proto_data(nni_sock *); extern void nni_sock_add_stat(nni_sock *, nni_stat_item *); @@ -1146,6 +1146,85 @@ nng_socket_get_addr(nng_socket id, const char *n, nng_sockaddr *v) } int +nng_socket_proto_id(nng_socket id, uint16_t *idp) +{ + int rv; + nni_sock *sock; + + if (((rv = nni_init()) != 0) || + ((rv = nni_sock_find(&sock, id.id)) != 0)) { + return (rv); + } + + *idp = nni_sock_proto_id(sock); + nni_sock_rele(sock); + return (0); +} + +int +nng_socket_peer_id(nng_socket id, uint16_t *idp) +{ + int rv; + nni_sock *sock; + + if (((rv = nni_init()) != 0) || + ((rv = nni_sock_find(&sock, id.id)) != 0)) { + return (rv); + } + + *idp = nni_sock_peer_id(sock); + nni_sock_rele(sock); + return (0); +} + +int +nng_socket_proto_name(nng_socket id, const char **name) +{ + int rv; + nni_sock *sock; + + if (((rv = nni_init()) != 0) || + ((rv = nni_sock_find(&sock, id.id)) != 0)) { + return (rv); + } + + *name = nni_sock_proto_name(sock); + nni_sock_rele(sock); + return (0); +} + +int +nng_socket_peer_name(nng_socket id, const char **name) +{ + int rv; + nni_sock *sock; + + if (((rv = nni_init()) != 0) || + ((rv = nni_sock_find(&sock, id.id)) != 0)) { + return (rv); + } + + *name = nni_sock_peer_name(sock); + nni_sock_rele(sock); + return (0); +} + +int +nng_socket_raw(nng_socket id, bool *rawp) +{ + int rv; + nni_sock *sock; + + if (((rv = nni_init()) != 0) || + ((rv = nni_sock_find(&sock, id.id)) != 0)) { + return (rv); + } + *rawp = nni_sock_raw(sock); + nni_sock_rele(sock); + return (0); +} + +int nng_pipe_notify(nng_socket s, nng_pipe_ev ev, nng_pipe_cb cb, void *arg) { int rv; diff --git a/src/sp/protocol/bus0/bus_test.c b/src/sp/protocol/bus0/bus_test.c index 6a214972..f9c1c092 100644 --- a/src/sp/protocol/bus0/bus_test.c +++ b/src/sp/protocol/bus0/bus_test.c @@ -1,5 +1,5 @@ // -// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech> // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -16,21 +16,19 @@ void test_bus_identity(void) { - nng_socket s; - int p; - char *n; + nng_socket s; + uint16_t p; + const char *n; NUTS_PASS(nng_bus0_open(&s)); - NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PROTO, &p)); + NUTS_PASS(nng_socket_proto_id(s, &p)); NUTS_TRUE(p == NNG_BUS0_SELF); - NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PEER, &p)); + NUTS_PASS(nng_socket_peer_id(s, &p)); NUTS_TRUE(p == NNG_BUS0_PEER); // 49 - NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PROTONAME, &n)); + NUTS_PASS(nng_socket_proto_name(s, &n)); NUTS_MATCH(n, NNG_BUS0_SELF_NAME); - nng_strfree(n); - NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PEERNAME, &n)); + NUTS_PASS(nng_socket_peer_name(s, &n)); NUTS_MATCH(n, NNG_BUS0_PEER_NAME); - nng_strfree(n); NUTS_CLOSE(s); } @@ -70,7 +68,7 @@ test_bus_device(void) { nng_socket s1, s2, s3; nng_socket none = NNG_SOCKET_INITIALIZER; - nng_aio *aio; + nng_aio *aio; NUTS_PASS(nng_bus0_open_raw(&s1)); NUTS_PASS(nng_bus0_open(&s2)); @@ -372,6 +370,8 @@ test_bus_cooked(void) bool b; NUTS_PASS(nng_bus0_open(&s)); + NUTS_PASS(nng_socket_raw(s, &b)); + NUTS_TRUE(!b); NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); NUTS_TRUE(!b); NUTS_FAIL(nng_socket_set_bool(s, NNG_OPT_RAW, true), NNG_EREADONLY); |
