Skip to content

Commit

Permalink
Replace NNG_OPT_RAW with nng_socket_raw.
Browse files Browse the repository at this point in the history
Similar to the other identities, this simplifies code a bit.
  • Loading branch information
gdamore committed Nov 3, 2024
1 parent 8bac5cc commit 02ec0b5
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 62 deletions.
2 changes: 2 additions & 0 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
15 changes: 3 additions & 12 deletions src/core/device.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
13 changes: 0 additions & 13 deletions src/core/sock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ test_endpoint_types(void)
nng_dialer d2;
nng_listener l2;
char *a = "inproc://mumble...";
bool b;

NUTS_OPEN(s1);

Expand All @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
17 changes: 6 additions & 11 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/socket.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -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 *);

Expand Down
15 changes: 15 additions & 0 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 1225 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L1225

Added line #L1225 was not covered by tests
}
*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)
{
Expand Down
5 changes: 2 additions & 3 deletions src/sp/protocol/bus0/bus_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/sp/protocol/pair0/pair0_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 2 additions & 4 deletions src/sp/protocol/pair1/pair1_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/pipeline0/pull_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/pipeline0/push_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sp/protocol/pubsub0/pub_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/pubsub0/sub_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/pubsub0/xsub_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/reqrep0/xrep_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/reqrep0/xreq_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/survey0/xrespond_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp/protocol/survey0/xsurvey_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 0 additions & 2 deletions src/sp/transport/ipc/ipc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 02ec0b5

Please sign in to comment.