summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2016-12-29 08:03:16 -0800
committerGarrett D'Amore <garrett@damore.org>2016-12-29 08:03:16 -0800
commitba8cbe2bd52199ff64270901f2981894a9b553d2 (patch)
tree4cd75be6adeed292efc9d1087bec8123b31c809d
parent4b53b1e31a93af8c739ba555970cb88d73063bce (diff)
downloadnng-ba8cbe2bd52199ff64270901f2981894a9b553d2.tar.gz
nng-ba8cbe2bd52199ff64270901f2981894a9b553d2.tar.bz2
nng-ba8cbe2bd52199ff64270901f2981894a9b553d2.zip
Move option helpers to their own file.
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/core/nng_impl.h1
-rw-r--r--src/core/options.c114
-rw-r--r--src/core/options.h39
-rw-r--r--src/core/socket.c88
-rw-r--r--src/core/socket.h5
-rw-r--r--src/protocol/reqrep/req.c2
7 files changed, 157 insertions, 94 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f68afa32..0f10d2ac 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -40,6 +40,8 @@ set (NNG_SOURCES
core/msgqueue.c
core/msgqueue.h
core/nng_impl.h
+ core/options.c
+ core/options.h
core/panic.c
core/panic.h
core/pipe.c
diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h
index a0a1a2e6..46341863 100644
--- a/src/core/nng_impl.h
+++ b/src/core/nng_impl.h
@@ -27,6 +27,7 @@
#include "core/init.h"
#include "core/message.h"
#include "core/msgqueue.h"
+#include "core/options.h"
#include "core/panic.h"
#include "core/platform.h"
#include "core/protocol.h"
diff --git a/src/core/options.c b/src/core/options.c
new file mode 100644
index 00000000..b970a0e7
--- /dev/null
+++ b/src/core/options.c
@@ -0,0 +1,114 @@
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#include "core/nng_impl.h"
+
+#include <string.h>
+
+int
+nni_setopt_duration(nni_duration *ptr, const void *val, size_t size)
+{
+ nni_duration dur;
+
+ if (size != sizeof (*ptr)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&dur, val, sizeof (dur));
+ if (dur < -1) {
+ return (NNG_EINVAL);
+ }
+ *ptr = dur;
+ return (0);
+}
+
+
+int
+nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval)
+{
+ int v;
+
+ if (size != sizeof (v)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&v, val, sizeof (v));
+ if (v > maxval) {
+ return (NNG_EINVAL);
+ }
+ if (v < minval) {
+ return (NNG_EINVAL);
+ }
+ *ptr = v;
+ return (0);
+}
+
+
+int
+nni_getopt_duration(nni_duration *ptr, void *val, size_t *sizep)
+{
+ size_t sz = sizeof (*ptr);
+
+ if (sz > *sizep) {
+ sz = *sizep;
+ }
+ *sizep = sizeof (*ptr);
+ memcpy(val, ptr, sz);
+ return (0);
+}
+
+
+int
+nni_getopt_int(int *ptr, void *val, size_t *sizep)
+{
+ size_t sz = sizeof (*ptr);
+
+ if (sz > *sizep) {
+ sz = *sizep;
+ }
+ *sizep = sizeof (*ptr);
+ memcpy(val, ptr, sz);
+ return (0);
+}
+
+
+int
+nni_setopt_buf(nni_msgqueue *mq, const void *val, size_t sz)
+{
+ int len;
+
+ if (sz < sizeof (len)) {
+ return (NNG_EINVAL);
+ }
+ memcpy(&len, val, sizeof (len));
+ if (len < 0) {
+ return (NNG_EINVAL);
+ }
+ if (len > 8192) {
+ // put a reasonable uppper limit on queue depth.
+ // This is a count in messages, so the total queue
+ // size could be quite large indeed in this case.
+ return (NNG_EINVAL);
+ }
+ return (nni_msgqueue_resize(mq, len));
+}
+
+
+int
+nni_getopt_buf(nni_msgqueue *mq, void *val, size_t *sizep)
+{
+ int len = nni_msgqueue_cap(mq);
+
+ int sz = *sizep;
+
+ if (sz > sizeof (len)) {
+ sz = sizeof (len);
+ }
+ memcpy(val, &len, sz);
+ *sizep = sizeof (len);
+ return (0);
+}
diff --git a/src/core/options.h b/src/core/options.h
new file mode 100644
index 00000000..7319bdf4
--- /dev/null
+++ b/src/core/options.h
@@ -0,0 +1,39 @@
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#ifndef CORE_OPTIONS_H
+#define CORE_OPTIONS_H
+
+// Option helpers. These can be called from protocols or transports
+// in their own option handling, centralizing the logic for dealing with
+// variable sized options.
+
+// nni_setopt_buf sets the queue size for the message queue.
+extern int nni_setopt_buf(nni_msgqueue *, const void *, size_t);
+
+// nni_getopt_buf gets the queue size for the message queue.
+extern int nni_getopt_buf(nni_msgqueue *, void *, size_t *);
+
+// nni_setopt_duration sets the duration. Durations must be legal,
+// either a positive value, 0, or -1 to indicate forever.
+extern int nni_setopt_duration(nni_duration *, const void *, size_t);
+
+// nni_getopt_duration gets the duration.
+extern int nni_getopt_duration(nni_duration *, void *, size_t *);
+
+// nni_setopt_int sets an integer, which must be between the minimum and
+// maximum values (inclusive).
+extern int nni_setopt_int(int *, const void *, size_t, int, int);
+#define NNI_MAXINT ((int)2147483647)
+#define NNI_MININT ((int)-2147483648)
+
+// nni_getopt_int gets an integer.
+extern int nni_getopt_int(int *, void *, size_t *);
+
+#endif // CORE_OPTIONS_H
diff --git a/src/core/socket.c b/src/core/socket.c
index c9b9175f..8c4cd339 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -452,94 +452,6 @@ nni_socket_senderr(nni_socket *sock, int err)
int
-nni_setopt_duration(nni_duration *ptr, const void *val, size_t size)
-{
- nni_duration dur;
-
- if (size != sizeof (*ptr)) {
- return (NNG_EINVAL);
- }
- memcpy(&dur, val, sizeof (dur));
- if (dur < -1) {
- return (NNG_EINVAL);
- }
- *ptr = dur;
- return (0);
-}
-
-
-int
-nni_setopt_int(int *ptr, const void *val, size_t size)
-{
- if (size != sizeof (*ptr)) {
- return (NNG_EINVAL);
- }
- memcpy(ptr, val, sizeof (*ptr));
- return (0);
-}
-
-
-int
-nni_getopt_duration(nni_duration *ptr, void *val, size_t *sizep)
-{
- size_t sz = sizeof (*ptr);
-
- if (sz > *sizep) {
- sz = *sizep;
- }
- *sizep = sizeof (*ptr);
- memcpy(val, ptr, sz);
- return (0);
-}
-
-
-int
-nni_getopt_int(int *ptr, void *val, size_t *sizep)
-{
- size_t sz = sizeof (*ptr);
-
- if (sz > *sizep) {
- sz = *sizep;
- }
- *sizep = sizeof (*ptr);
- memcpy(val, ptr, sz);
- return (0);
-}
-
-
-static int
-nni_setopt_buf(nni_msgqueue *mq, const void *val, size_t sz)
-{
- int len;
-
- if (sz < sizeof (len)) {
- return (NNG_EINVAL);
- }
- memcpy(&len, val, sizeof (len));
- if (len < 0) {
- return (NNG_EINVAL);
- }
- return (nni_msgqueue_resize(mq, len));
-}
-
-
-static int
-nni_getopt_buf(nni_msgqueue *mq, void *val, size_t *sizep)
-{
- int len = nni_msgqueue_cap(mq);
-
- int sz = *sizep;
-
- if (sz > sizeof (len)) {
- sz = sizeof (len);
- }
- memcpy(val, &len, sz);
- *sizep = sizeof (len);
- return (0);
-}
-
-
-int
nni_socket_setopt(nni_socket *sock, int opt, const void *val, size_t size)
{
size_t rsz;
diff --git a/src/core/socket.h b/src/core/socket.h
index c80af22e..7b49e85f 100644
--- a/src/core/socket.h
+++ b/src/core/socket.h
@@ -56,11 +56,6 @@ extern int nni_socket_sendmsg(nni_socket *, nni_msg *, nni_time);
extern int nni_socket_dial(nni_socket *, const char *, nni_endpt **, int);
extern int nni_socket_listen(nni_socket *, const char *, nni_endpt **, int);
-extern int nni_setopt_duration(nni_duration *, const void *, size_t);
-extern int nni_getopt_duration(nni_duration *, void *, size_t *);
-extern int nni_setopt_int(int *, const void *, size_t);
-extern int nni_getopt_int(int *, void *, size_t *);
-
// Set error codes for applications. These are only ever
// called from the filter functions in protocols, and thus
// already have the socket lock held.
diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c
index 3514047b..b0225e90 100644
--- a/src/protocol/reqrep/req.c
+++ b/src/protocol/reqrep/req.c
@@ -264,7 +264,7 @@ nni_req_setopt(void *arg, int opt, const void *buf, size_t sz)
break;
case NNG_OPT_RAW:
nni_mutex_enter(&req->mx);
- rv = nni_setopt_int(&req->raw, buf, sz);
+ rv = nni_setopt_int(&req->raw, buf, sz, 0, 1);
nni_mutex_exit(&req->mx);
break;
default: