From 64db0f085be0c9efc6dca8d9e72d3e5a47cb792e Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 25 Sep 2017 12:49:10 -0700 Subject: Refactor option handling APIs. This makes the APIs use string keys, and largely eliminates the use of integer option IDs altogether. The underlying registration for options is also now a bit richer, letting protcols and transports declare the actual options they use, rather than calling down into each entry point carte blanche and relying on ENOTSUP. This code may not be as fast as the integers was, but it is more intuitive, easier to extend, and is not on any hot code paths. (If you're diddling options on a hot code path you're doing something wrong.) --- src/protocol/survey/respond.c | 68 ++++++++++++++++++++++------------------ src/protocol/survey/survey.c | 72 ++++++++++++++++++++++++------------------- 2 files changed, 78 insertions(+), 62 deletions(-) (limited to 'src/protocol/survey') diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c index 89e19e91..70dbd704 100644 --- a/src/protocol/survey/respond.c +++ b/src/protocol/survey/respond.c @@ -343,42 +343,36 @@ resp_putq_cb(void *arg) } static int -resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz) +resp_sock_setopt_raw(void *arg, const void *buf, size_t sz) { - resp_sock *s = arg; - int rv = NNG_ENOTSUP; - int oldraw; - - if (opt == nng_optid_maxttl) { - rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255); - - } else if (opt == nng_optid_raw) { - oldraw = s->raw; - rv = nni_setopt_int(&s->raw, buf, sz, 0, 1); - if (oldraw != s->raw) { - if (!s->raw) { - nni_sock_senderr(s->nsock, 0); - } else { - nni_sock_senderr(s->nsock, NNG_ESTATE); - } - } - } + resp_sock *s = arg; + int rv; + if ((rv = nni_setopt_int(&s->raw, buf, sz, 0, 1)) == 0) { + nni_sock_senderr(s->nsock, s->raw ? 0 : NNG_ESTATE); + } return (rv); } static int -resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp) +resp_sock_getopt_raw(void *arg, void *buf, size_t *szp) { - resp_sock *s = arg; - int rv = NNG_ENOTSUP; + resp_sock *s = arg; + return (nni_getopt_int(s->raw, buf, szp)); +} - if (opt == nng_optid_maxttl) { - rv = nni_getopt_int(s->ttl, buf, szp); - } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(s->raw, buf, szp); - } - return (rv); +static int +resp_sock_setopt_maxttl(void *arg, const void *buf, size_t sz) +{ + resp_sock *s = arg; + return (nni_setopt_int(&s->ttl, buf, sz, 1, 255)); +} + +static int +resp_sock_getopt_maxttl(void *arg, void *buf, size_t *szp) +{ + resp_sock *s = arg; + return (nni_getopt_int(s->ttl, buf, szp)); } static nni_msg * @@ -453,13 +447,27 @@ static nni_proto_pipe_ops resp_pipe_ops = { .pipe_stop = resp_pipe_stop, }; +static nni_proto_sock_option resp_sock_options[] = { + { + .pso_name = NNG_OPT_RAW, + .pso_getopt = resp_sock_getopt_raw, + .pso_setopt = resp_sock_setopt_raw, + }, + { + .pso_name = NNG_OPT_MAXTTL, + .pso_getopt = resp_sock_getopt_maxttl, + .pso_setopt = resp_sock_setopt_maxttl, + }, + // terminate list + { NULL, NULL, NULL }, +}; + static nni_proto_sock_ops resp_sock_ops = { .sock_init = resp_sock_init, .sock_fini = resp_sock_fini, .sock_open = resp_sock_open, .sock_close = resp_sock_close, - .sock_setopt = resp_sock_setopt, - .sock_getopt = resp_sock_getopt, + .sock_options = resp_sock_options, .sock_rfilter = resp_sock_rfilter, .sock_sfilter = resp_sock_sfilter, }; diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c index e90c7f57..361b9d37 100644 --- a/src/protocol/survey/survey.c +++ b/src/protocol/survey/survey.c @@ -267,44 +267,38 @@ failed: } static int -surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz) +surv_sock_setopt_raw(void *arg, const void *buf, size_t sz) { - surv_sock *s = arg; - int rv = NNG_ENOTSUP; - int oldraw; - - if (opt == nng_optid_surveyor_surveytime) { - rv = nni_setopt_usec(&s->survtime, buf, sz); - - } else if (opt == nng_optid_raw) { - oldraw = s->raw; - rv = nni_setopt_int(&s->raw, buf, sz, 0, 1); - if (oldraw != s->raw) { - if (s->raw) { - nni_sock_recverr(s->nsock, 0); - } else { - nni_sock_recverr(s->nsock, NNG_ESTATE); - } - s->survid = 0; - nni_timer_cancel(&s->timer); - } - } + surv_sock *s = arg; + int rv; + if ((rv = nni_setopt_int(&s->raw, buf, sz, 0, 1)) == 0) { + nni_sock_recverr(s->nsock, s->raw ? 0 : NNG_ESTATE); + s->survid = 0; + nni_timer_cancel(&s->timer); + } return (rv); } static int -surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp) +surv_sock_getopt_raw(void *arg, void *buf, size_t *szp) { - surv_sock *s = arg; - int rv = NNG_ENOTSUP; + surv_sock *s = arg; + return (nni_getopt_int(s->raw, buf, szp)); +} - if (opt == nng_optid_surveyor_surveytime) { - rv = nni_getopt_usec(s->survtime, buf, szp); - } else if (opt == nng_optid_raw) { - rv = nni_getopt_int(s->raw, buf, szp); - } - return (rv); +static int +surv_sock_setopt_surveytime(void *arg, const void *buf, size_t sz) +{ + surv_sock *s = arg; + return (nni_setopt_usec(&s->survtime, buf, sz)); +} + +static int +surv_sock_getopt_surveytime(void *arg, void *buf, size_t *szp) +{ + surv_sock *s = arg; + return (nni_getopt_usec(s->survtime, buf, szp)); } static void @@ -418,13 +412,27 @@ static nni_proto_pipe_ops surv_pipe_ops = { .pipe_stop = surv_pipe_stop, }; +static nni_proto_sock_option surv_sock_options[] = { + { + .pso_name = NNG_OPT_RAW, + .pso_getopt = surv_sock_getopt_raw, + .pso_setopt = surv_sock_setopt_raw, + }, + { + .pso_name = NNG_OPT_SURVEYOR_SURVEYTIME, + .pso_getopt = surv_sock_getopt_surveytime, + .pso_setopt = surv_sock_setopt_surveytime, + }, + // terminate list + { NULL, NULL, NULL }, +}; + static nni_proto_sock_ops surv_sock_ops = { .sock_init = surv_sock_init, .sock_fini = surv_sock_fini, .sock_open = surv_sock_open, .sock_close = surv_sock_close, - .sock_setopt = surv_sock_setopt, - .sock_getopt = surv_sock_getopt, + .sock_options = surv_sock_options, .sock_rfilter = surv_sock_rfilter, .sock_sfilter = surv_sock_sfilter, }; -- cgit v1.2.3-70-g09d2