diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-02 13:57:53 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-02 14:47:50 -0700 |
| commit | 279180c1d07fc2c4c0bfa8f5a418cb02c4b87863 (patch) | |
| tree | b451ac7f845062674d6ab45eb5d530628d3ff47c /src/core | |
| parent | 9b27984d0e2da430b78a975e59f55c96de5f6056 (diff) | |
| download | nng-279180c1d07fc2c4c0bfa8f5a418cb02c4b87863.tar.gz nng-279180c1d07fc2c4c0bfa8f5a418cb02c4b87863.tar.bz2 nng-279180c1d07fc2c4c0bfa8f5a418cb02c4b87863.zip | |
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.
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/protocol.h | 8 | ||||
| -rw-r--r-- | src/core/sock_test.c | 11 | ||||
| -rw-r--r-- | src/core/socket.c | 58 | ||||
| -rw-r--r-- | src/core/socket.h | 2 |
4 files changed, 31 insertions, 48 deletions
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 @@ -79,16 +79,6 @@ test_send_nonblock(void) } 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) { nng_socket s1 = NNG_SOCKET_INITIALIZER; @@ -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) { @@ -137,30 +137,6 @@ sock_get_fd(void *s, unsigned flag, int *fdp) } 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) { bool raw = ((nni_sock_flags(SOCK(s)) & NNI_PROTO_FLAG_RAW) != 0); @@ -287,14 +263,6 @@ static const nni_option sock_options[] = { .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, .o_set = sock_set_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 |
