aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/pair
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-09-25 12:49:10 -0700
committerGarrett D'Amore <garrett@damore.org>2017-09-27 14:38:12 -0700
commit64db0f085be0c9efc6dca8d9e72d3e5a47cb792e (patch)
tree475520498d8ebe9e47e9785d8f9d209c87582400 /src/protocol/pair
parent86a96e5bf1b207a8b1aa925e1d9f73ce834505b8 (diff)
downloadnng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.tar.gz
nng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.tar.bz2
nng-64db0f085be0c9efc6dca8d9e72d3e5a47cb792e.zip
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.)
Diffstat (limited to 'src/protocol/pair')
-rw-r--r--src/protocol/pair/pair_v0.c43
-rw-r--r--src/protocol/pair/pair_v1.c128
2 files changed, 83 insertions, 88 deletions
diff --git a/src/protocol/pair/pair_v0.c b/src/protocol/pair/pair_v0.c
index 277d5cb1..9cabe3c7 100644
--- a/src/protocol/pair/pair_v0.c
+++ b/src/protocol/pair/pair_v0.c
@@ -230,31 +230,17 @@ pair0_sock_close(void *arg)
}
static int
-pair0_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
+pair0_sock_setopt_raw(void *arg, const void *buf, size_t sz)
{
pair0_sock *s = arg;
- int rv;
-
- if (opt == nng_optid_raw) {
- rv = nni_setopt_int(&s->raw, buf, sz, 0, 1);
- } else {
- rv = NNG_ENOTSUP;
- }
- return (rv);
+ return (nni_setopt_int(&s->raw, buf, sz, 0, 1));
}
static int
-pair0_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
+pair0_sock_getopt_raw(void *arg, void *buf, size_t *szp)
{
pair0_sock *s = arg;
- int rv;
-
- if (opt == nng_optid_raw) {
- rv = nni_getopt_int(s->raw, buf, szp);
- } else {
- rv = NNG_ENOTSUP;
- }
- return (rv);
+ return (nni_getopt_int(s->raw, buf, szp));
}
static nni_proto_pipe_ops pair0_pipe_ops = {
@@ -264,13 +250,22 @@ static nni_proto_pipe_ops pair0_pipe_ops = {
.pipe_stop = pair0_pipe_stop,
};
+static nni_proto_sock_option pair0_sock_options[] = {
+ {
+ .pso_name = NNG_OPT_RAW,
+ .pso_getopt = pair0_sock_getopt_raw,
+ .pso_setopt = pair0_sock_setopt_raw,
+ },
+ // terminate list
+ { NULL, NULL, NULL },
+};
+
static nni_proto_sock_ops pair0_sock_ops = {
- .sock_init = pair0_sock_init,
- .sock_fini = pair0_sock_fini,
- .sock_open = pair0_sock_open,
- .sock_close = pair0_sock_close,
- .sock_setopt = pair0_sock_setopt,
- .sock_getopt = pair0_sock_getopt,
+ .sock_init = pair0_sock_init,
+ .sock_fini = pair0_sock_fini,
+ .sock_open = pair0_sock_open,
+ .sock_close = pair0_sock_close,
+ .sock_options = pair0_sock_options,
};
// Legacy protocol (v0)
diff --git a/src/protocol/pair/pair_v1.c b/src/protocol/pair/pair_v1.c
index 68dbc6f7..2cd5782b 100644
--- a/src/protocol/pair/pair_v1.c
+++ b/src/protocol/pair/pair_v1.c
@@ -25,9 +25,9 @@ static void pair1_pipe_getq_cb(void *);
static void pair1_pipe_putq_cb(void *);
static void pair1_pipe_fini(void *);
-// These are exposed as external names for external consumers.
-int nng_optid_pair1_poly;
-const char *nng_opt_pair1_poly = "pair1-polyamorous";
+// This is exposed as an external name for external consumers.
+#define NNG_OPT_PAIR1_POLY "pair1-polyamorous"
+const char *nng_opt_pair1_poly = NNG_OPT_PAIR1_POLY;
// pair1_sock is our per-socket protocol private structure.
struct pair1_sock {
@@ -394,74 +394,57 @@ pair1_sock_close(void *arg)
}
static int
-pair1_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
+pair1_sock_setopt_raw(void *arg, const void *buf, size_t sz)
{
- pair1_sock *s = arg;
- int rv = NNG_ENOTSUP;
-
- if (opt == nng_optid_raw) {
- nni_mtx_lock(&s->mtx);
- if (s->started) {
- rv = NNG_ESTATE;
- } else {
- rv = nni_setopt_int(&s->raw, buf, sz, 0, 1);
- }
- nni_mtx_unlock(&s->mtx);
- } else if (opt == nng_optid_maxttl) {
- nni_mtx_lock(&s->mtx);
- rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255);
- nni_mtx_unlock(&s->mtx);
- } else if (opt == nng_optid_pair1_poly) {
- nni_mtx_lock(&s->mtx);
- if (s->started) {
- rv = NNG_ESTATE;
- } else {
- rv = nni_setopt_int(&s->poly, buf, sz, 0, 1);
- }
- nni_mtx_unlock(&s->mtx);
- }
-
+ pair1_sock *s = arg;
+ int rv;
+ nni_mtx_lock(&s->mtx);
+ rv = s->started ? NNG_ESTATE : nni_setopt_int(&s->raw, buf, sz, 0, 1);
+ nni_mtx_unlock(&s->mtx);
return (rv);
}
static int
-pair1_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
+pair1_sock_getopt_raw(void *arg, void *buf, size_t *szp)
{
- pair1_sock *s = arg;
- int rv = NNG_ENOTSUP;
+ pair1_sock *s = arg;
+ return (nni_getopt_int(s->raw, buf, szp));
+}
- if (opt == nng_optid_raw) {
- nni_mtx_lock(&s->mtx);
- rv = nni_getopt_int(s->raw, buf, szp);
- nni_mtx_unlock(&s->mtx);
- } else if (opt == nng_optid_maxttl) {
- nni_mtx_lock(&s->mtx);
- rv = nni_getopt_int(s->ttl, buf, szp);
- nni_mtx_unlock(&s->mtx);
- } else if (opt == nng_optid_pair1_poly) {
- nni_mtx_lock(&s->mtx);
- rv = nni_getopt_int(s->poly, buf, szp);
- nni_mtx_unlock(&s->mtx);
- }
+static int
+pair1_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
+{
+ pair1_sock *s = arg;
+ int rv;
+ nni_mtx_lock(&s->mtx); // Have to be locked against recv cb.
+ rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255);
+ nni_mtx_unlock(&s->mtx);
return (rv);
}
-static void
-pair1_fini(void)
+static int
+pair1_sock_getopt_maxttl(void *arg, void *buf, size_t *szp)
{
- nng_optid_pair1_poly = -1;
+ pair1_sock *s = arg;
+ return (nni_getopt_int(s->ttl, buf, szp));
}
static int
-pair1_init(void)
+pair1_sock_setopt_poly(void *arg, const void *buf, size_t sz)
{
- int rv;
- if ((rv = nni_option_register(
- nng_opt_pair1_poly, &nng_optid_pair1_poly)) != 0) {
- pair1_fini();
- return (rv);
- }
- return (0);
+ pair1_sock *s = arg;
+ int rv;
+ nni_mtx_lock(&s->mtx);
+ rv = s->started ? NNG_ESTATE : nni_setopt_int(&s->poly, buf, sz, 0, 1);
+ nni_mtx_unlock(&s->mtx);
+ return (rv);
+}
+
+static int
+pair1_sock_getopt_poly(void *arg, void *buf, size_t *szp)
+{
+ pair1_sock *s = arg;
+ return (nni_getopt_int(s->poly, buf, szp));
}
static nni_proto_pipe_ops pair1_pipe_ops = {
@@ -471,13 +454,32 @@ static nni_proto_pipe_ops pair1_pipe_ops = {
.pipe_stop = pair1_pipe_stop,
};
+static nni_proto_sock_option pair1_sock_options[] = {
+ {
+ .pso_name = NNG_OPT_RAW,
+ .pso_getopt = pair1_sock_getopt_raw,
+ .pso_setopt = pair1_sock_setopt_raw,
+ },
+ {
+ .pso_name = NNG_OPT_MAXTTL,
+ .pso_getopt = pair1_sock_getopt_maxttl,
+ .pso_setopt = pair1_sock_setopt_maxttl,
+ },
+ {
+ .pso_name = NNG_OPT_PAIR1_POLY,
+ .pso_getopt = pair1_sock_getopt_poly,
+ .pso_setopt = pair1_sock_setopt_poly,
+ },
+ // terminate list
+ { NULL, NULL, NULL },
+};
+
static nni_proto_sock_ops pair1_sock_ops = {
- .sock_init = pair1_sock_init,
- .sock_fini = pair1_sock_fini,
- .sock_open = pair1_sock_open,
- .sock_close = pair1_sock_close,
- .sock_setopt = pair1_sock_setopt,
- .sock_getopt = pair1_sock_getopt,
+ .sock_init = pair1_sock_init,
+ .sock_fini = pair1_sock_fini,
+ .sock_open = pair1_sock_open,
+ .sock_close = pair1_sock_close,
+ .sock_options = pair1_sock_options,
};
static nni_proto pair1_proto = {
@@ -487,8 +489,6 @@ static nni_proto pair1_proto = {
.proto_flags = NNI_PROTO_FLAG_SNDRCV,
.proto_sock_ops = &pair1_sock_ops,
.proto_pipe_ops = &pair1_pipe_ops,
- .proto_init = &pair1_init,
- .proto_fini = &pair1_fini,
};
int