From 45f455064b5704f3d5ed8ecf9f197a18fe72ee59 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 4 Apr 2018 12:37:34 -0700 Subject: fixes #331 replace NNG_OPT_RAW option with constructor This makes the raw mode something that is immutable, determined at socket construction. This is an enabling change for the separate context support coming soon. As a result, this is an API breaking change for users of the raw mode option (NNG_OPT_RAW). There aren't many of them out there. Cooked mode is entirely unaffected. There are changes to tests and documentation included. --- src/protocol/pair1/pair.c | 65 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'src/protocol/pair1/pair.c') diff --git a/src/protocol/pair1/pair.c b/src/protocol/pair1/pair.c index becbbfa7..a3c01d46 100644 --- a/src/protocol/pair1/pair.c +++ b/src/protocol/pair1/pair.c @@ -72,7 +72,7 @@ pair1_sock_fini(void *arg) } static int -pair1_sock_init(void **sp, nni_sock *nsock) +pair1_sock_init_impl(void **sp, nni_sock *nsock, bool raw) { pair1_sock *s; int rv; @@ -94,7 +94,7 @@ pair1_sock_init(void **sp, nni_sock *nsock) return (rv); } - s->raw = false; + s->raw = raw; s->poly = false; s->uwq = nni_sock_sendq(nsock); s->urq = nni_sock_recvq(nsock); @@ -104,6 +104,18 @@ pair1_sock_init(void **sp, nni_sock *nsock) return (0); } +static int +pair1_sock_init(void **sp, nni_sock *nsock) +{ + return (pair1_sock_init_impl(sp, nsock, false)); +} + +static int +pair1_sock_init_raw(void **sp, nni_sock *nsock) +{ + return (pair1_sock_init_impl(sp, nsock, true)); +} + static void pair1_pipe_fini(void *arg) { @@ -396,24 +408,6 @@ pair1_sock_close(void *arg) NNI_ARG_UNUSED(arg); } -static int -pair1_sock_setopt_raw(void *arg, const void *buf, size_t sz, int typ) -{ - pair1_sock *s = arg; - int rv; - nni_mtx_lock(&s->mtx); - rv = s->started ? NNG_ESTATE : nni_copyin_bool(&s->raw, buf, sz, typ); - nni_mtx_unlock(&s->mtx); - return (rv); -} - -static int -pair1_sock_getopt_raw(void *arg, void *buf, size_t *szp, int typ) -{ - pair1_sock *s = arg; - return (nni_copyout_bool(s->raw, buf, szp, typ)); -} - static int pair1_sock_setopt_maxttl(void *arg, const void *buf, size_t sz, int typ) { @@ -474,12 +468,6 @@ static nni_proto_pipe_ops pair1_pipe_ops = { }; static nni_proto_sock_option pair1_sock_options[] = { - { - .pso_name = NNG_OPT_RAW, - .pso_type = NNI_TYPE_BOOL, - .pso_getopt = pair1_sock_getopt_raw, - .pso_setopt = pair1_sock_setopt_raw, - }, { .pso_name = NNG_OPT_MAXTTL, .pso_type = NNI_TYPE_INT32, @@ -522,3 +510,28 @@ nng_pair1_open(nng_socket *sidp) { return (nni_proto_open(sidp, &pair1_proto)); } + +static nni_proto_sock_ops pair1_sock_ops_raw = { + .sock_init = pair1_sock_init_raw, + .sock_fini = pair1_sock_fini, + .sock_open = pair1_sock_open, + .sock_close = pair1_sock_close, + .sock_recv = pair1_sock_recv, + .sock_send = pair1_sock_send, + .sock_options = pair1_sock_options, +}; + +static nni_proto pair1_proto_raw = { + .proto_version = NNI_PROTOCOL_VERSION, + .proto_self = { NNI_PROTO_PAIR_V1, "pair1" }, + .proto_peer = { NNI_PROTO_PAIR_V1, "pair1" }, + .proto_flags = NNI_PROTO_FLAG_SNDRCV | NNI_PROTO_FLAG_RAW, + .proto_sock_ops = &pair1_sock_ops_raw, + .proto_pipe_ops = &pair1_pipe_ops, +}; + +int +nng_pair1_open_raw(nng_socket *sidp) +{ + return (nni_proto_open(sidp, &pair1_proto_raw)); +} -- cgit v1.2.3-70-g09d2