From 3bed9dca3a3ae5b226e3bf6aee3352d7665dbcc2 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 24 Dec 2016 15:22:36 -0800 Subject: Expose nng_sendmsg. --- src/nng.c | 21 +++++++++++++++++++++ tests/sock.c | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/nng.c b/src/nng.c index 225bf074..5a0acaac 100644 --- a/src/nng.c +++ b/src/nng.c @@ -70,6 +70,27 @@ nng_recvmsg(nng_socket *s, nng_msg **msgp, int flags) return (nni_socket_recvmsg(s, msgp, expire)); } +int +nng_sendmsg(nng_socket *s, nng_msg *msg, int flags) +{ + int rv; + nni_time expire; + + if ((rv = nni_init()) != 0) { + return (rv); + } + + if ((flags == NNG_FLAG_NONBLOCK) || (s->s_sndtimeo == 0)) { + expire = NNI_TIME_ZERO; + } else if (s->s_sndtimeo < 0) { + expire = NNI_TIME_NEVER; + } else { + expire = nni_clock() + s->s_sndtimeo; + } + + return (nni_socket_sendmsg(s, msg, expire)); +} + int nng_setopt(nng_socket *s, int opt, const void *val, size_t sz) diff --git a/tests/sock.c b/tests/sock.c index 931e114a..65ed5565 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -54,6 +54,27 @@ TestMain("Socket Operations", { So(msg == NULL); }) + Convey("Send with no pipes times out correctly", { + nng_msg *msg = NULL; + int64_t when = 500000; + uint64_t now; + + // We cheat to get access to the core's clock. + So(nng_msg_alloc(&msg, 0) == 0); + So(msg != NULL); + extern uint64_t nni_clock(void); + now = nni_clock(); + + rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when, + sizeof (when)); + So(rv == 0); + rv = nng_sendmsg(sock, msg, 0); + So(rv == NNG_ETIMEDOUT); + So(nni_clock() > (now + 500000)); + So(nni_clock() < (now + 1000000)); + nng_msg_free(msg); + }) + Convey("We can set and get options", { int64_t when = 1234; int64_t check = 0; -- cgit v1.2.3-70-g09d2