From 02ec0b55cbee5de4d0fd688ce0ebddf08178dc98 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 2 Nov 2024 23:54:27 -0700 Subject: [PATCH] Replace `NNG_OPT_RAW` with `nng_socket_raw`. Similar to the other identities, this simplifies code a bit. --- docs/ref/migrate/nng1.md | 2 ++ include/nng/nng.h | 2 +- src/core/device.c | 15 +++------------ src/core/sock_test.c | 13 ------------- src/core/socket.c | 17 ++++++----------- src/core/socket.h | 3 ++- src/nng.c | 15 +++++++++++++++ src/sp/protocol/bus0/bus_test.c | 5 ++--- src/sp/protocol/pair0/pair0_test.c | 6 ++---- src/sp/protocol/pair1/pair1_test.c | 6 ++---- src/sp/protocol/pipeline0/pull_test.c | 2 +- src/sp/protocol/pipeline0/push_test.c | 2 +- src/sp/protocol/pubsub0/pub_test.c | 5 ++--- src/sp/protocol/pubsub0/sub_test.c | 2 +- src/sp/protocol/pubsub0/xsub_test.c | 2 +- src/sp/protocol/reqrep0/xrep_test.c | 2 +- src/sp/protocol/reqrep0/xreq_test.c | 2 +- src/sp/protocol/survey0/xrespond_test.c | 2 +- src/sp/protocol/survey0/xsurvey_test.c | 2 +- src/sp/transport/ipc/ipc_test.c | 2 -- 20 files changed, 45 insertions(+), 62 deletions(-) diff --git a/docs/ref/migrate/nng1.md b/docs/ref/migrate/nng1.md index 399682772..3004b8803 100644 --- a/docs/ref/migrate/nng1.md +++ b/docs/ref/migrate/nng1.md @@ -78,6 +78,8 @@ Note that the new functions provide a reference to a static string, and thus do allocation, and the returned strings should not be freed. Also the IDs are provided as `uint16_t`, matching the actual wire protocol values, instead of `int`. +The `NNG_OPT_RAW` option has aso been replaced by a function, `nng_socket_raw`. + ## Statistics Use Constified Pointers A number of the statistics functions take, or return, `const nng_stat *` instead diff --git a/include/nng/nng.h b/include/nng/nng.h index d0e478292..074850c57 100644 --- a/include/nng/nng.h +++ b/include/nng/nng.h @@ -270,6 +270,7 @@ NNG_DECL int nng_socket_proto_id(nng_socket id, uint16_t *idp); NNG_DECL int nng_socket_peer_id(nng_socket id, uint16_t *idp); NNG_DECL int nng_socket_proto_name(nng_socket id, const char **namep); NNG_DECL int nng_socket_peer_name(nng_socket id, const char **namep); +NNG_DECL int nng_socket_raw(nng_socket id, bool *rawp); // Utility function for getting a printable form of the socket address // for display in logs, etc. It is not intended to be parsed, and the @@ -729,7 +730,6 @@ NNG_DECL nng_listener nng_pipe_listener(nng_pipe); // Options. #define NNG_OPT_SOCKNAME "socket-name" -#define NNG_OPT_RAW "raw" #define NNG_OPT_RECVBUF "recv-buffer" #define NNG_OPT_SENDBUF "send-buffer" #define NNG_OPT_RECVTIMEO "recv-timeout" diff --git a/src/core/device.c b/src/core/device.c index 08c7bab56..7a6fb479d 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -1,5 +1,5 @@ // -// Copyright 2021 Staysail Systems, Inc. +// Copyright 2024 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -128,8 +128,6 @@ device_init(device_data **dp, nni_sock *s1, nni_sock *s2) { int num_paths = 2; int i; - bool raw; - size_t rsz; device_data *d; // Specifying either of these as null turns the device into @@ -149,18 +147,11 @@ device_init(device_data **dp, nni_sock *s1, nni_sock *s2) return (NNG_EINVAL); } - raw = false; - rsz = sizeof(raw); - if (((nni_sock_getopt(s1, NNG_OPT_RAW, &raw, &rsz, NNI_TYPE_BOOL) != - 0)) || - (!raw)) { + if (!nni_sock_raw(s1)) { return (NNG_EINVAL); } - rsz = sizeof(raw); - if (((nni_sock_getopt(s2, NNG_OPT_RAW, &raw, &rsz, NNI_TYPE_BOOL) != - 0)) || - (!raw)) { + if (!nni_sock_raw(s2)) { return (NNG_EINVAL); } diff --git a/src/core/sock_test.c b/src/core/sock_test.c index 7641ea1f4..5981a2e39 100644 --- a/src/core/sock_test.c +++ b/src/core/sock_test.c @@ -314,7 +314,6 @@ test_endpoint_types(void) nng_dialer d2; nng_listener l2; char *a = "inproc://mumble..."; - bool b; NUTS_OPEN(s1); @@ -324,7 +323,6 @@ test_endpoint_types(void) // Forge a listener l2.id = nng_dialer_id(d); - NUTS_FAIL(nng_listener_get_bool(l2, NNG_OPT_RAW, &b), NNG_ENOENT); NUTS_FAIL(nng_listener_close(l2), NNG_ENOENT); NUTS_PASS(nng_dialer_close(d)); @@ -334,7 +332,6 @@ test_endpoint_types(void) // Forge a dialer d2.id = nng_listener_id(l); - NUTS_FAIL(nng_dialer_get_bool(d2, NNG_OPT_RAW, &b), NNG_ENOENT); NUTS_FAIL(nng_dialer_close(d2), NNG_ENOENT); NUTS_PASS(nng_listener_close(l)); @@ -405,7 +402,6 @@ test_listener_options(void) NUTS_FAIL( nng_listener_set_string(l, NNG_OPT_SOCKNAME, "1"), NNG_ENOTSUP); - NUTS_FAIL(nng_listener_set_bool(l, NNG_OPT_RAW, true), NNG_ENOTSUP); NUTS_FAIL(nng_listener_set_ms(l, NNG_OPT_RECONNMINT, 1), NNG_ENOTSUP); NUTS_FAIL(nng_listener_set_string(l, NNG_OPT_SOCKNAME, "bogus"), NNG_ENOTSUP); @@ -440,7 +436,6 @@ test_dialer_options(void) // Cannot set inappropriate options NUTS_FAIL( nng_dialer_set_string(d, NNG_OPT_SOCKNAME, "1"), NNG_ENOTSUP); - NUTS_FAIL(nng_dialer_set_bool(d, NNG_OPT_RAW, true), NNG_ENOTSUP); NUTS_FAIL(nng_dialer_set_ms(d, NNG_OPT_SENDTIMEO, 1), NNG_ENOTSUP); NUTS_FAIL( nng_dialer_set_string(d, NNG_OPT_SOCKNAME, "bogus"), NNG_ENOTSUP); @@ -456,9 +451,7 @@ void test_endpoint_absent_options(void) { size_t s; - int i; nng_duration t; - bool b; nng_dialer d; nng_listener l; d.id = 1999; @@ -467,15 +460,9 @@ test_endpoint_absent_options(void) NUTS_FAIL(nng_dialer_set_size(d, NNG_OPT_RECVMAXSZ, 10), NNG_ENOENT); NUTS_FAIL(nng_listener_set_size(l, NNG_OPT_RECVMAXSZ, 10), NNG_ENOENT); - NUTS_FAIL(nng_dialer_get_bool(d, NNG_OPT_RAW, &b), NNG_ENOENT); - NUTS_FAIL(nng_listener_get_bool(l, NNG_OPT_RAW, &b), NNG_ENOENT); - NUTS_FAIL(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &s), NNG_ENOENT); NUTS_FAIL(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &s), NNG_ENOENT); - NUTS_FAIL(nng_dialer_get_int(d, NNG_OPT_RAW, &i), NNG_ENOENT); - NUTS_FAIL(nng_listener_get_int(l, NNG_OPT_RAW, &i), NNG_ENOENT); - NUTS_FAIL(nng_dialer_get_ms(d, NNG_OPT_RECVTIMEO, &t), NNG_ENOENT); NUTS_FAIL(nng_listener_get_ms(l, NNG_OPT_SENDTIMEO, &t), NNG_ENOENT); } diff --git a/src/core/socket.c b/src/core/socket.c index 0e9b6b17b..b01938e5f 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -136,13 +136,6 @@ sock_get_fd(nni_sock *s, unsigned flag, int *fdp) return (rv); } -static int -sock_get_raw(void *s, void *buf, size_t *szp, nni_type t) -{ - bool raw = ((nni_sock_flags(SOCK(s)) & NNI_PROTO_FLAG_RAW) != 0); - return (nni_copyout_bool(raw, buf, szp, t)); -} - static int sock_set_recvtimeo(void *s, const void *buf, size_t sz, nni_type t) { @@ -253,10 +246,6 @@ static const nni_option sock_options[] = { .o_get = sock_get_sockname, .o_set = sock_set_sockname, }, - { - .o_name = NNG_OPT_RAW, - .o_get = sock_get_raw, - }, // terminate list { .o_name = NULL, @@ -823,6 +812,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 714ad5bbc..8fab08d67 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -1,5 +1,5 @@ // -// Copyright 2021 Staysail Systems, Inc. +// Copyright 2024 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -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 *); diff --git a/src/nng.c b/src/nng.c index d5c1b9579..08606715d 100644 --- a/src/nng.c +++ b/src/nng.c @@ -1214,6 +1214,21 @@ nng_socket_peer_name(nng_socket id, const char **name) 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) { diff --git a/src/sp/protocol/bus0/bus_test.c b/src/sp/protocol/bus0/bus_test.c index 7a9e4a8ba..3fb2aa56a 100644 --- a/src/sp/protocol/bus0/bus_test.c +++ b/src/sp/protocol/bus0/bus_test.c @@ -371,14 +371,13 @@ test_bus_cooked(void) bool b; NUTS_PASS(nng_bus0_open(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(!b); - NUTS_FAIL(nng_socket_set_bool(s, NNG_OPT_RAW, true), NNG_EREADONLY); NUTS_PASS(nng_close(s)); // raw pub only differs in the option setting NUTS_PASS(nng_bus0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/pair0/pair0_test.c b/src/sp/protocol/pair0/pair0_test.c index 0cdfb789c..c2bcb7acf 100644 --- a/src/sp/protocol/pair0/pair0_test.c +++ b/src/sp/protocol/pair0/pair0_test.c @@ -225,15 +225,13 @@ test_pair0_raw(void) bool raw; NUTS_PASS(nng_pair0_open(&s1)); - NUTS_PASS(nng_socket_get_bool(s1, NNG_OPT_RAW, &raw)); + NUTS_PASS(nng_socket_raw(s1, &raw)); NUTS_TRUE(raw == false); - NUTS_FAIL(nng_socket_set_bool(s1, NNG_OPT_RAW, true), NNG_EREADONLY); NUTS_PASS(nng_close(s1)); NUTS_PASS(nng_pair0_open_raw(&s1)); - NUTS_PASS(nng_socket_get_bool(s1, NNG_OPT_RAW, &raw)); + NUTS_PASS(nng_socket_raw(s1, &raw)); NUTS_TRUE(raw == true); - NUTS_FAIL(nng_socket_set_bool(s1, NNG_OPT_RAW, false), NNG_EREADONLY); NUTS_PASS(nng_close(s1)); } diff --git a/src/sp/protocol/pair1/pair1_test.c b/src/sp/protocol/pair1/pair1_test.c index e0e6b3b29..8c6d433e3 100644 --- a/src/sp/protocol/pair1/pair1_test.c +++ b/src/sp/protocol/pair1/pair1_test.c @@ -291,15 +291,13 @@ test_pair1_raw(void) bool raw; NUTS_PASS(nng_pair1_open(&s1)); - NUTS_PASS(nng_socket_get_bool(s1, NNG_OPT_RAW, &raw)); + NUTS_PASS(nng_socket_raw(s1, &raw)); NUTS_TRUE(raw == false); - NUTS_FAIL(nng_socket_set_bool(s1, NNG_OPT_RAW, true), NNG_EREADONLY); NUTS_PASS(nng_close(s1)); NUTS_PASS(nng_pair1_open_raw(&s1)); - NUTS_PASS(nng_socket_get_bool(s1, NNG_OPT_RAW, &raw)); + NUTS_PASS(nng_socket_raw(s1, &raw)); NUTS_TRUE(raw == true); - NUTS_FAIL(nng_socket_set_bool(s1, NNG_OPT_RAW, false), NNG_EREADONLY); NUTS_PASS(nng_close(s1)); } diff --git a/src/sp/protocol/pipeline0/pull_test.c b/src/sp/protocol/pipeline0/pull_test.c index 59dcf8a85..74fd60466 100644 --- a/src/sp/protocol/pipeline0/pull_test.c +++ b/src/sp/protocol/pipeline0/pull_test.c @@ -241,7 +241,7 @@ test_pull_cooked(void) bool b; NUTS_PASS(nng_pull0_open(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(!b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/pipeline0/push_test.c b/src/sp/protocol/pipeline0/push_test.c index be60ba50a..dd3328f31 100644 --- a/src/sp/protocol/pipeline0/push_test.c +++ b/src/sp/protocol/pipeline0/push_test.c @@ -410,7 +410,7 @@ test_push_cooked(void) bool b; NUTS_PASS(nng_push0_open(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(!b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/pubsub0/pub_test.c b/src/sp/protocol/pubsub0/pub_test.c index 5f0ef01e6..733d6fb54 100644 --- a/src/sp/protocol/pubsub0/pub_test.c +++ b/src/sp/protocol/pubsub0/pub_test.c @@ -298,14 +298,13 @@ test_pub_cooked(void) bool b; NUTS_PASS(nng_pub0_open(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(!b); - NUTS_FAIL(nng_socket_set_bool(s, NNG_OPT_RAW, true), NNG_EREADONLY); NUTS_PASS(nng_close(s)); // raw pub only differs in the option setting NUTS_PASS(nng_pub0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/pubsub0/sub_test.c b/src/sp/protocol/pubsub0/sub_test.c index b823d486b..9802a35ed 100644 --- a/src/sp/protocol/pubsub0/sub_test.c +++ b/src/sp/protocol/pubsub0/sub_test.c @@ -576,7 +576,7 @@ test_sub_cooked(void) bool b; NUTS_PASS(nng_sub0_open(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(!b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/pubsub0/xsub_test.c b/src/sp/protocol/pubsub0/xsub_test.c index 8e5568c48..eec73505b 100644 --- a/src/sp/protocol/pubsub0/xsub_test.c +++ b/src/sp/protocol/pubsub0/xsub_test.c @@ -283,7 +283,7 @@ test_xsub_raw(void) bool b; NUTS_PASS(nng_sub0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/reqrep0/xrep_test.c b/src/sp/protocol/reqrep0/xrep_test.c index c1a7cb626..3c033e9b5 100644 --- a/src/sp/protocol/reqrep0/xrep_test.c +++ b/src/sp/protocol/reqrep0/xrep_test.c @@ -37,7 +37,7 @@ test_xrep_raw(void) bool b; NUTS_PASS(nng_rep0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/reqrep0/xreq_test.c b/src/sp/protocol/reqrep0/xreq_test.c index 7e09ce5a9..e8725acbf 100644 --- a/src/sp/protocol/reqrep0/xreq_test.c +++ b/src/sp/protocol/reqrep0/xreq_test.c @@ -37,7 +37,7 @@ test_xreq_raw(void) bool b; NUTS_PASS(nng_req0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/survey0/xrespond_test.c b/src/sp/protocol/survey0/xrespond_test.c index 1522d1ed8..2e4cd7431 100644 --- a/src/sp/protocol/survey0/xrespond_test.c +++ b/src/sp/protocol/survey0/xrespond_test.c @@ -37,7 +37,7 @@ test_xresp_raw(void) bool b; NUTS_PASS(nng_respondent0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/protocol/survey0/xsurvey_test.c b/src/sp/protocol/survey0/xsurvey_test.c index 41c76cb2d..d55719aff 100644 --- a/src/sp/protocol/survey0/xsurvey_test.c +++ b/src/sp/protocol/survey0/xsurvey_test.c @@ -36,7 +36,7 @@ test_xsurveyor_raw(void) bool b; NUTS_PASS(nng_surveyor0_open_raw(&s)); - NUTS_PASS(nng_socket_get_bool(s, NNG_OPT_RAW, &b)); + NUTS_PASS(nng_socket_raw(s, &b)); NUTS_TRUE(b); NUTS_CLOSE(s); } diff --git a/src/sp/transport/ipc/ipc_test.c b/src/sp/transport/ipc/ipc_test.c index 3936a7502..473246fe0 100644 --- a/src/sp/transport/ipc/ipc_test.c +++ b/src/sp/transport/ipc/ipc_test.c @@ -75,7 +75,6 @@ test_ipc_dialer_properties(void) z = 0; NUTS_PASS(nng_dialer_get_size(d, NNG_OPT_RECVMAXSZ, &z)); NUTS_TRUE(z == 8192); - NUTS_FAIL(nng_dialer_set_bool(d, NNG_OPT_RAW, true), NNG_ENOTSUP); NUTS_CLOSE(s); } @@ -141,7 +140,6 @@ test_ipc_listener_properties(void) z = 0; NUTS_PASS(nng_listener_get_size(l, NNG_OPT_RECVMAXSZ, &z)); NUTS_TRUE(z == 8192); - NUTS_FAIL(nng_listener_set_bool(l, NNG_OPT_RAW, true), NNG_ENOTSUP); NUTS_CLOSE(s); }