diff options
| -rw-r--r-- | src/core/socket.c | 60 | ||||
| -rw-r--r-- | src/nng.c | 12 | ||||
| -rw-r--r-- | tests/sock.c | 26 |
3 files changed, 88 insertions, 10 deletions
diff --git a/src/core/socket.c b/src/core/socket.c index f3afb7bb..adf2e082 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -228,16 +228,6 @@ nni_socket_recvmsg(nni_socket *sock, nni_msg **msgp, nni_time expire) int rv; nni_msg *msg; -#if 0 - if (tmout > 0) { - expire = nni_clock() + tmout; - } else if (tmout < 0) { - expire = NNI_TIME_NEVER; - } else { - expire = NNI_TIME_ZERO; - } -#endif - nni_mutex_enter(&sock->s_mx); if (sock->s_closing) { nni_mutex_exit(&sock->s_mx); @@ -567,6 +557,18 @@ nni_setopt_duration(nni_duration *ptr, const void *val, size_t size) return (0); } +static int +nni_getopt_duration(nni_duration *ptr, void *val, size_t *sizep) +{ + size_t sz = sizeof (nni_duration); + + if (sz > *sizep) { + sz = *sizep; + } + *sizep = sizeof (nni_duration); + memcpy(val, ptr, sz); + return (0); +} int nni_socket_setopt(nni_socket *sock, int opt, const void *val, size_t size) @@ -603,3 +605,41 @@ nni_socket_setopt(nni_socket *sock, int opt, const void *val, size_t size) nni_mutex_exit(&sock->s_mx); return (rv); } + +int +nni_socket_getopt(nni_socket *sock, int opt, void *val, size_t *sizep) +{ + size_t rsz; + void *ptr; + int rv = ENOTSUP; + + nni_mutex_enter(&sock->s_mx); + if (sock->s_ops.proto_getopt != NULL) { + rv = sock->s_ops.proto_getopt(sock->s_data, opt, val, sizep); + if (rv != NNG_ENOTSUP) { + nni_mutex_exit(&sock->s_mx); + return (rv); + } + } + switch (opt) { + case NNG_OPT_LINGER: + rv = nni_getopt_duration(&sock->s_linger, val, sizep); + break; + case NNG_OPT_SNDTIMEO: + rv = nni_getopt_duration(&sock->s_sndtimeo, val, sizep); + break; + case NNG_OPT_RCVTIMEO: + rv = nni_getopt_duration(&sock->s_rcvtimeo, val, sizep); + break; + case NNG_OPT_RECONN_TIME: + rv = nni_getopt_duration(&sock->s_reconn, val, sizep); + break; + case NNG_OPT_RECONN_MAXTIME: + rv = nni_getopt_duration(&sock->s_reconnmax, val, sizep); + break; + } + nni_mutex_exit(&sock->s_mx); + return (rv); +} + + @@ -83,6 +83,18 @@ nng_setopt(nng_socket *s, int opt, const void *val, size_t sz) } +int +nng_getopt(nng_socket *s, int opt, void *val, size_t *szp) +{ + int rv; + + if ((rv == nni_init()) != 0) { + return (rv); + } + return (nni_socket_getopt(s, opt, val, szp)); +} + + // Misc. const char * nng_strerror(int num) diff --git a/tests/sock.c b/tests/sock.c index 81361f8b..931e114a 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -53,5 +53,31 @@ TestMain("Socket Operations", { So(rv == NNG_EAGAIN); So(msg == NULL); }) + + Convey("We can set and get options", { + int64_t when = 1234; + int64_t check = 0; + size_t sz; + rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when, + sizeof (when)); + So(rv == 0); + sz = sizeof (check); + Convey("Short size is not copied", { + sz = 0; + rv = nng_getopt(sock, NNG_OPT_SNDTIMEO, + &check, &sz); + So(rv == 0); + So(sz == sizeof (check)); + So(check == 0); + }) + Convey("Correct size is copied", { + sz = sizeof (check); + rv = nng_getopt(sock, NNG_OPT_SNDTIMEO, &check, + &sz); + So(rv == 0); + So(sz == sizeof (check)); + So(check == 1234); + }) + }) }) })
\ No newline at end of file |
