From 279180c1d07fc2c4c0bfa8f5a418cb02c4b87863 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 2 Nov 2024 13:57:53 -0700 Subject: NNG_OPT_RECVFD and NNG_OPT_SENDFD converted to functions. These options are removed entirely, and their functionality is now available via special functions, `nng_socket_get_send_poll_fd` and `nng_socket_get_recv_poll_fd`, making these first class methods on the socket. This eliminates a bit of wasteful code, and provides type safety for these methods. --- src/core/protocol.h | 8 +++++++- src/core/sock_test.c | 11 ---------- src/core/socket.c | 58 ++++++++++++++++++++-------------------------------- src/core/socket.h | 2 ++ 4 files changed, 31 insertions(+), 48 deletions(-) (limited to 'src/core') diff --git a/src/core/protocol.h b/src/core/protocol.h index c7bfb1ad..0d4d12dc 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -108,6 +108,12 @@ struct nni_proto_sock_ops { // Receive a message. void (*sock_recv)(void *, nni_aio *); + // Return the receive poll FD. + int (*sock_recv_poll_fd)(void *, int *); + + // Return the send poll FD. + int (*sock_send_poll_fd)(void *, int *); + // Options. Must not be NULL. Final entry should have NULL name. nni_option *sock_options; }; @@ -124,7 +130,7 @@ struct nni_proto { uint32_t proto_flags; // Protocol flags const nni_proto_sock_ops *proto_sock_ops; // Per-socket operations const nni_proto_pipe_ops *proto_pipe_ops; // Per-pipe operations - const nni_proto_ctx_ops * proto_ctx_ops; // Context operations + const nni_proto_ctx_ops *proto_ctx_ops; // Context operations }; // We quite intentionally use a signature where the upper word is nonzero, diff --git a/src/core/sock_test.c b/src/core/sock_test.c index f785d9c0..7641ea1f 100644 --- a/src/core/sock_test.c +++ b/src/core/sock_test.c @@ -78,16 +78,6 @@ test_send_nonblock(void) NUTS_CLOSE(s1); } -void -test_readonly_options(void) -{ - nng_socket s1; - NUTS_OPEN(s1); - NUTS_FAIL(nng_socket_set_int(s1, NNG_OPT_RECVFD, 0), NNG_EREADONLY); - NUTS_FAIL(nng_socket_set_int(s1, NNG_OPT_SENDFD, 0), NNG_EREADONLY); - NUTS_CLOSE(s1); -} - void test_socket_base(void) { @@ -596,7 +586,6 @@ NUTS_TESTS = { { "recv non-block", test_recv_nonblock }, { "send timeout", test_send_timeout }, { "send non-block", test_send_nonblock }, - { "read only options", test_readonly_options }, { "socket base", test_socket_base }, { "socket name", test_socket_name }, { "socket name oversize", test_socket_name_oversize }, diff --git a/src/core/socket.c b/src/core/socket.c index 88a11382..9376f9de 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -114,19 +114,19 @@ static void nni_ctx_destroy(nni_ctx *); #define SOCK(s) ((nni_sock *) (s)) static int -sock_get_fd(void *s, unsigned flag, int *fdp) +sock_get_fd(nni_sock *s, unsigned flag, int *fdp) { int rv; nni_pollable *p; - if ((flag & nni_sock_flags(SOCK(s))) == 0) { + if ((flag & nni_sock_flags(s)) == 0) { return (NNG_ENOTSUP); } if (flag == NNI_PROTO_FLAG_SND) { - rv = nni_msgq_get_sendable(SOCK(s)->s_uwq, &p); + rv = nni_msgq_get_sendable(s->s_uwq, &p); } else { - rv = nni_msgq_get_recvable(SOCK(s)->s_urq, &p); + rv = nni_msgq_get_recvable(s->s_urq, &p); } if (rv == 0) { @@ -136,30 +136,6 @@ sock_get_fd(void *s, unsigned flag, int *fdp) return (rv); } -static int -sock_get_sendfd(void *s, void *buf, size_t *szp, nni_type t) -{ - int fd; - int rv; - - if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_SND, &fd)) != 0) { - return (rv); - } - return (nni_copyout_int(fd, buf, szp, t)); -} - -static int -sock_get_recvfd(void *s, void *buf, size_t *szp, nni_type t) -{ - int fd; - int rv; - - if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_RCV, &fd)) != 0) { - return (rv); - } - return (nni_copyout_int(fd, buf, szp, t)); -} - static int sock_get_raw(void *s, void *buf, size_t *szp, nni_type t) { @@ -286,14 +262,6 @@ static const nni_option sock_options[] = { .o_get = sock_get_sendtimeo, .o_set = sock_set_sendtimeo, }, - { - .o_name = NNG_OPT_RECVFD, - .o_get = sock_get_recvfd, - }, - { - .o_name = NNG_OPT_SENDFD, - .o_get = sock_get_sendfd, - }, { .o_name = NNG_OPT_RECVBUF, .o_get = sock_get_recvbuf, @@ -353,6 +321,24 @@ nni_sock_id(nni_sock *s) return (s->s_id); } +int +nni_sock_get_send_fd(nni_sock *s, int *fdp) +{ + if (s->s_sock_ops.sock_send_poll_fd != NULL) { + return (s->s_sock_ops.sock_send_poll_fd(s->s_data, fdp)); + } + return (sock_get_fd(s, NNI_PROTO_FLAG_SND, fdp)); +} + +int +nni_sock_get_recv_fd(nni_sock *s, int *fdp) +{ + if (s->s_sock_ops.sock_recv_poll_fd != NULL) { + return (s->s_sock_ops.sock_recv_poll_fd(s->s_data, fdp)); + } + return (sock_get_fd(s, NNI_PROTO_FLAG_RCV, fdp)); +} + // nni_sock_sendq and nni_sock_recvq are called by the protocol to obtain // the upper read and write queues. nni_msgq * diff --git a/src/core/socket.h b/src/core/socket.h index c4037e96..714ad5bb 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -34,6 +34,8 @@ extern int nni_sock_getopt( extern void nni_sock_send(nni_sock *, nni_aio *); extern void nni_sock_recv(nni_sock *, nni_aio *); extern uint32_t nni_sock_id(nni_sock *); +extern int nni_sock_get_send_fd(nni_sock *s, int *fdp); +extern int nni_sock_get_recv_fd(nni_sock *s, int *fdp); // These are socket methods that protocol operations can expect to call. // Note that each of these should be called without any locks held, since -- cgit v1.2.3-70-g09d2