diff options
| author | Garrett D'Amore <garrett@damore.org> | 2016-12-29 08:03:16 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2016-12-29 08:03:16 -0800 |
| commit | ba8cbe2bd52199ff64270901f2981894a9b553d2 (patch) | |
| tree | 4cd75be6adeed292efc9d1087bec8123b31c809d /src/core | |
| parent | 4b53b1e31a93af8c739ba555970cb88d73063bce (diff) | |
| download | nng-ba8cbe2bd52199ff64270901f2981894a9b553d2.tar.gz nng-ba8cbe2bd52199ff64270901f2981894a9b553d2.tar.bz2 nng-ba8cbe2bd52199ff64270901f2981894a9b553d2.zip | |
Move option helpers to their own file.
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/nng_impl.h | 1 | ||||
| -rw-r--r-- | src/core/options.c | 114 | ||||
| -rw-r--r-- | src/core/options.h | 39 | ||||
| -rw-r--r-- | src/core/socket.c | 88 | ||||
| -rw-r--r-- | src/core/socket.h | 5 |
5 files changed, 154 insertions, 93 deletions
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. |
