From 99a78ade3a6034784e40d5dfa70cc72aa09021ca Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 21 Jan 2017 21:41:41 -0800 Subject: Adds NNG_OPT_SENDFD and NNG_OPT_RECVFD socket options (untested). --- src/core/options.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/core/options.c') diff --git a/src/core/options.c b/src/core/options.c index 32b65ebf..35411562 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -112,3 +112,60 @@ nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) *sizep = sizeof (len); return (0); } + + +static void +nni_notifyfd_push(struct nng_event *ev, void *arg) +{ + nni_notifyfd *fd = arg; + + NNI_ARG_UNUSED(ev); + + nni_plat_pipe_raise(fd->sn_wfd); +} + + +int +nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) +{ + int rv; + + if ((*szp < sizeof (int))) { + return (NNG_EINVAL); + } + + switch (mask) { + case NNG_EV_CAN_SEND: + if ((s->s_flags & NNI_PROTO_FLAG_SEND) == 0) { + return (NNG_ENOTSUP); + } + break; + case NNG_EV_CAN_RECV: + if ((s->s_flags & NNI_PROTO_FLAG_RECV) == 0) { + return (NNG_ENOTSUP); + } + break; + default: + return (NNG_ENOTSUP); + } + + // If we already inited this, just give back the same file descriptor. + if (fd->sn_init) { + memcpy(val, &fd->sn_rfd, sizeof (int)); + *szp = sizeof (int); + return (0); + } + + if ((rv = nni_plat_pipe_open(&fd->sn_wfd, &fd->sn_rfd)) != 0) { + return (rv); + } + + if (nni_add_notify(s, mask, nni_notifyfd_push, fd) == NULL) { + nni_plat_pipe_close(fd->sn_wfd, fd->sn_rfd); + return (NNG_ENOMEM); + } + + *szp = sizeof (int); + memcpy(val, &fd->sn_rfd, sizeof (int)); + return (0); +} -- cgit v1.2.3-70-g09d2