Skip to content

Commit

Permalink
api: rename nng_send_aio and nng_recv_aio to nng_socket_send and nng_…
Browse files Browse the repository at this point in the history
…socket_recv

This aligns more closely with the nng_ctx functions.
  • Loading branch information
gdamore committed Jan 3, 2025
1 parent cefc6e1 commit d1bbf4c
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 134 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ endif ()
add_library(nng)

add_library(nng_testing STATIC EXCLUDE_FROM_ALL)
target_compile_definitions(nng_testing PUBLIC NNG_STATIC_LIB NNG_TEST_LIB NNG_PRIVATE)
target_compile_definitions(nng_testing PRIVATE NNG_STATIC_LIB NNG_TEST_LIB NNG_PRIVATE)

add_library(nng_private INTERFACE)
target_compile_definitions(nng_private INTERFACE NNG_PRIVATE)
Expand Down
12 changes: 6 additions & 6 deletions demo/raw/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ server_cb(void *arg)
switch (work->state) {
case INIT:
work->state = RECV;
nng_recv_aio(work->sock, work->aio);
nng_socket_recv(work->sock, work->aio);
break;
case RECV:
if ((rv = nng_aio_result(work->aio)) != 0) {
fatal("nng_recv_aio", rv);
fatal("nng_socket_recv", rv);
}
msg = nng_aio_get_msg(work->aio);
if ((rv = nng_msg_trim_u32(msg, &when)) != 0) {
// bad message, just ignore it.
nng_msg_free(msg);
nng_recv_aio(work->sock, work->aio);
nng_socket_recv(work->sock, work->aio);
return;
}
work->msg = msg;
Expand All @@ -89,15 +89,15 @@ server_cb(void *arg)
nng_aio_set_msg(work->aio, work->msg);
work->msg = NULL;
work->state = SEND;
nng_send_aio(work->sock, work->aio);
nng_socket_send(work->sock, work->aio);
break;
case SEND:
if ((rv = nng_aio_result(work->aio)) != 0) {
nng_msg_free(work->msg);
fatal("nng_send_aio", rv);
fatal("nng_socket_send", rv);
}
work->state = RECV;
nng_recv_aio(work->sock, work->aio);
nng_socket_recv(work->sock, work->aio);
break;
default:
fatal("bad state!", NNG_ESTATE);
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The `nng_ctx_recvmsg` function receives a message and stores a pointer to the [`
The _flags_ can contain the value [`NNG_FLAG_NONBLOCK`], indicating that the function should not wait if the socket
has no messages available to receive. In such a case, it will return [`NNG_EAGAIN`].
### nng_recv_aio
### nng_socket_recv
The `nng_ctx_send` function receives a message asynchronously, using the [`nng_aio`] _aio_, over the context _ctx_.
On success, the received message can be retrieved from the _aio_ using the [`nng_aio_get_msg`] function.
Expand Down
26 changes: 13 additions & 13 deletions docs/ref/api/sock.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ a result of [`NNG_ECLOSED`].
```c
int nng_send(nng_socket s, void *data, size_t size, int flags);
int nng_sendmsg(nng_socket s, nng_msg *msg, int flags);
void nng_send_aio(nng_socket s, nng_aio *aio);
void nng_socket_send(nng_socket s, nng_aio *aio);
```
These functions ({{i:`nng_send`}}, {{i:`nng_sendmsg`}}, and {{i:`nng_send_aio`}}) send
These functions ({{i:`nng_send`}}, {{i:`nng_sendmsg`}}, and {{i:`nng_socket_send`}}) send
messages over the socket _s_. The differences in their behaviors are as follows.
> [!NOTE]
Expand Down Expand Up @@ -198,9 +198,9 @@ cannot accept more data for sending. In such a case, it will return [`NNG_EAGAIN
> This function is preferred over [`nng_send`], as it gives access to the message structure and eliminates both
> a data copy and allocation.
### nng_send_aio
### nng_socket_send
The `nng_send_aio` function sends a message asynchronously, using the [`nng_aio`] _aio_, over the socket _s_.
The `nng_socket_send` function sends a message asynchronously, using the [`nng_aio`] _aio_, over the socket _s_.
The message to send must have been set on _aio_ using the [`nng_aio_set_msg`] function.
If the operation completes successfully, then the socket will have disposed of the message.
Expand All @@ -220,10 +220,10 @@ For example, the message may be sitting in queue, or located in TCP buffers, or
```c
int nng_recv(nng_socket s, void *data, size_t *sizep, int flags);
int nng_recvmsg(nng_socket s, nng_msg **msgp, int flags);
void nng_recv_aio(nng_socket s, nng_aio *aio);
void nng_socket_recv(nng_socket s, nng_aio *aio);
```

These functions ({{i:`nng_recv`}}, {{i:`nng_recvmsg`}}, and {{i:`nng_recv_aio`}}) receive
These functions ({{i:`nng_recv`}}, {{i:`nng_recvmsg`}}, and {{i:`nng_socket_recv`}}) receive
messages over the socket _s_. The differences in their behaviors are as follows.

> [!NOTE]
Expand Down Expand Up @@ -257,9 +257,9 @@ has no messages available to receive. In such a case, it will return [`NNG_EAGAI
> This function is preferred over [`nng_recv`], as it gives access to the message structure and eliminates both
> a data copy and allocation.
### nng_recv_aio
### nng_socket_recv

The `nng_send_aio` function receives a message asynchronously, using the [`nng_aio`] _aio_, over the socket _s_.
The `nng_socket_send` function receives a message asynchronously, using the [`nng_aio`] _aio_, over the socket _s_.
On success, the received message can be retrieved from the _aio_ using the [`nng_aio_get_msg`] function.

> [!NOTE]
Expand Down Expand Up @@ -371,7 +371,7 @@ nng_socket s = NNG_SOCKET_INITIALIZER;

### Example 2: Publishing a Timestamp

This example demonstrates the use of [`nng_aio`], [`nng_send_aio`], and [`nng_sleep_aio`] to
This example demonstrates the use of [`nng_aio`], [`nng_socket_send`], and [`nng_sleep_aio`] to
build a service that publishes a timestamp at one second intervals. Error handling is elided for the
sake of clarity.

Expand Down Expand Up @@ -403,7 +403,7 @@ void callback(void *arg) {
now = nng_clock();
nng_msg_append(msg, &now, sizeof (now)); // note: native endian
nng_aio_set_msg(state->aio, msg);
nng_send_aio(state->s, state->aio);
nng_socket_send(state->s, state->aio);
} else {
state->sleeping = true;
nng_sleep_aio(1000, state->aio); // 1000 ms == 1 second
Expand All @@ -426,7 +426,7 @@ int main(int argc, char **argv) {
### Example 3: Watching a Periodic Timestamp
This example demonstrates the use of [`nng_aio`], [`nng_recv_aio`], to build a client to
This example demonstrates the use of [`nng_aio`], [`nng_socket_recv`], to build a client to
watch for messages received from the service created in Example 2.
Error handling is elided for the sake of clarity.
Expand Down Expand Up @@ -457,7 +457,7 @@ void callback(void *arg) {
printf("Timestamp is %lu\n", (unsigned long)now);
nng_msg_free(msg);
nng_aio_set_msg(state->aio, NULL);
nng_recv_aio(state->s, state->aio);
nng_socket_recv(state->s, state->aio);
}
int main(int argc, char **argv) {
Expand All @@ -467,7 +467,7 @@ int main(int argc, char **argv) {
nng_sub0_open(&state.s);
nng_sub0_socket_subscribe(state.s, NULL, 0); // subscribe to everything
nng_dial(state.s, url, NULL, 0);
nng_recv_aio(state.s, state.aio); // kick it off right away
nng_socket_recv(state.s, state.aio); // kick it off right away
for(;;) {
nng_msleep(0x7FFFFFFF); // infinite, could use pause or sigsuspend
}
Expand Down
13 changes: 9 additions & 4 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ This is done using the [`nng_init`] function.

## Renamed Functions

The `nng_close` function has been renamed to [`nng_socket_close`] to make it clearer that
the object being closed is a socket. A compatible `nng_close` macro is available by defining `NNG1_TRANSITION`
in your compilation environment.
The following functions have been renamed as described by the following table.
The old names are available by defining the macro `NNG1_TRANSITION` in your compilation environment.

| Old Name | New Name |
| -------------- | -------------------- |
| `nng_close` | [`nng_socket_close`] |
| `nng_recv_aio` | [`nng_socket_recv`] |
| `nng_send_aio` | [`nng_socket_send`] |

## Removed Protocol Aliases

Expand Down Expand Up @@ -46,7 +51,7 @@ The `NNG_FLAG_ALLOC` flag that allowed a zero copy semantic with [`nng_send`] an
This was implemented mostly to aid legacy nanomsg applications, and it was both error prone and still a bit
suboptimal in terms of performance.

Modern code should use one of [`nng_sendmsg`], [`nng_recvmsg`], [`nng_send_aio`], or [`nng_recv_aio`] to get the maximum performance benefit.
Modern code should use one of [`nng_sendmsg`], [`nng_recvmsg`], [`nng_socket_send`], or [`nng_socket_recv`] to get the maximum performance benefit.
Working directly with [`nng_msg`] structures gives more control, reduces copies, and reduces allocation activity.

## New AIO Error Code NNG_ESTOPPED
Expand Down
4 changes: 2 additions & 2 deletions docs/ref/xref.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@
[`nng_socket_get_size`]: /api/sock.md#socket-options
[`nng_send`]: /api/sock.md#nng_send
[`nng_sendmsg`]: /api/sock.md#nng_sendmsg
[`nng_send_aio`]: /api/sock.md#nng_send_aio
[`nng_socket_send`]: /api/sock.md#nng_socket_send
[`nng_recv`]: /api/sock.md#nng_recv
[`nng_recvmsg`]: /api/sock.md#nng_recvmsg
[`nng_recv_aio`]: /api/sock.md#nng_recv_aio
[`nng_socket_recv`]: /api/sock.md#nng_socket_recv
[`nng_ctx_open`]: /api/ctx.md#creating-a-context
[`nng_ctx_id`]: /api/ctx.md#context-identity
[`nng_ctx_close`]: /api/ctx.md#closing-a-context
Expand Down
14 changes: 8 additions & 6 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,19 +405,19 @@ NNG_DECL int nng_sendmsg(nng_socket, nng_msg *, int);
// can be passed off directly to nng_sendmsg.
NNG_DECL int nng_recvmsg(nng_socket, nng_msg **, int);

// nng_send_aio sends data on the socket asynchronously. As with nng_send,
// nng_socket_send sends data on the socket asynchronously. As with nng_send,
// the completion may be executed before the data has actually been delivered,
// but only when it is accepted for delivery. The supplied AIO must have
// been initialized, and have an associated message. The message will be
// "owned" by the socket if the operation completes successfully. Otherwise,
// the caller is responsible for freeing it.
NNG_DECL void nng_send_aio(nng_socket, nng_aio *);
NNG_DECL void nng_socket_send(nng_socket, nng_aio *);

// nng_recv_aio receives data on the socket asynchronously. On a successful
// nng_socket_recv receives data on the socket asynchronously. On a successful
// result, the AIO will have an associated message, that can be obtained
// with nng_aio_get_msg(). The caller takes ownership of the message at
// this point.
NNG_DECL void nng_recv_aio(nng_socket, nng_aio *);
NNG_DECL void nng_socket_recv(nng_socket, nng_aio *);

// Context support. User contexts are not supported by all protocols,
// but for those that do, they give a way to create multiple contexts
Expand All @@ -442,7 +442,7 @@ NNG_DECL int nng_ctx_close(nng_ctx);
// A valid context is not necessarily an *open* context.
NNG_DECL int nng_ctx_id(nng_ctx);

// nng_ctx_recv receives asynchronously. It works like nng_recv_aio, but
// nng_ctx_recv receives asynchronously. It works like nng_socket_recv, but
// uses a local context instead of the socket global context.
NNG_DECL void nng_ctx_recv(nng_ctx, nng_aio *);

Expand All @@ -451,7 +451,7 @@ NNG_DECL void nng_ctx_recv(nng_ctx, nng_aio *);
// on a context instead of a socket.
NNG_DECL int nng_ctx_recvmsg(nng_ctx, nng_msg **, int);

// nng_ctx_send sends asynchronously. It works like nng_send_aio, but
// nng_ctx_send sends asynchronously. It works like nng_socket_send, but
// uses a local context instead of the socket global context.
NNG_DECL void nng_ctx_send(nng_ctx, nng_aio *);

Expand Down Expand Up @@ -1654,6 +1654,8 @@ NNG_DECL int nng_surveyor0_open_raw(nng_socket *);
do { \
} while (0)
#define nng_close(s) nng_socket_close(s)
#define nng_send_aio(s, a) nng_socket_send(s, a)
#define nng_recv_aio(s, a) nng_socket_recv(s, a)
#define nng_inproc_register() nng_nop()
#define nng_ipc_register() nng_nop()
#define nng_tls_register() nng_nop()
Expand Down
18 changes: 9 additions & 9 deletions src/core/aio_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2023 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -10,7 +10,7 @@

#include <string.h>

#include <nuts.h>
#include "nuts.h"

static void
cb_done(void *p)
Expand Down Expand Up @@ -148,7 +148,7 @@ test_consumer_cancel(void)
NUTS_TRUE(nng_aio_alloc(&a, cb_done, &done) == 0);

nng_aio_set_timeout(a, NNG_DURATION_INFINITE);
nng_recv_aio(s1, a);
nng_socket_recv(s1, a);
nng_aio_cancel(a);
nng_aio_wait(a);
NUTS_TRUE(done == 1);
Expand Down Expand Up @@ -185,10 +185,10 @@ test_traffic(void)
NUTS_PASS(nng_msg_alloc(&m, 0));
NUTS_PASS(nng_msg_append(m, "hello", strlen("hello")));

nng_recv_aio(s2, rx_aio);
nng_socket_recv(s2, rx_aio);

nng_aio_set_msg(tx_aio, m);
nng_send_aio(s1, tx_aio);
nng_socket_send(s1, tx_aio);

nng_aio_wait(tx_aio);
nng_aio_wait(rx_aio);
Expand Down Expand Up @@ -221,7 +221,7 @@ test_explicit_timeout(void)
NUTS_PASS(nng_pair1_open(&s));
NUTS_PASS(nng_aio_alloc(&a, cb_done, &done));
nng_aio_set_timeout(a, 40);
nng_recv_aio(s, a);
nng_socket_recv(s, a);
nng_aio_wait(a);
NUTS_TRUE(done == 1);
NUTS_FAIL(nng_aio_result(a), NNG_ETIMEDOUT);
Expand All @@ -242,7 +242,7 @@ test_explicit_expiration(void)
now = nng_clock();
now += 40;
nng_aio_set_expire(a, now);
nng_recv_aio(s, a);
nng_socket_recv(s, a);
nng_aio_wait(a);
NUTS_TRUE(done == 1);
NUTS_FAIL(nng_aio_result(a), NNG_ETIMEDOUT);
Expand All @@ -260,7 +260,7 @@ test_inherited_timeout(void)
NUTS_PASS(nng_pair1_open(&s));
NUTS_PASS(nng_aio_alloc(&a, cb_done, &done));
NUTS_PASS(nng_socket_set_ms(s, NNG_OPT_RECVTIMEO, 40));
nng_recv_aio(s, a);
nng_socket_recv(s, a);
nng_aio_wait(a);
NUTS_TRUE(done == 1);
NUTS_FAIL(nng_aio_result(a), NNG_ETIMEDOUT);
Expand All @@ -278,7 +278,7 @@ test_zero_timeout(void)
NUTS_PASS(nng_pair1_open(&s));
NUTS_PASS(nng_aio_alloc(&a, cb_done, &done));
nng_aio_set_timeout(a, NNG_DURATION_ZERO);
nng_recv_aio(s, a);
nng_socket_recv(s, a);
nng_aio_wait(a);
NUTS_TRUE(done == 1);
NUTS_FAIL(nng_aio_result(a), NNG_ETIMEDOUT);
Expand Down
4 changes: 2 additions & 2 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ nng_sendmsg(nng_socket s, nng_msg *msg, int flags)
}

void
nng_recv_aio(nng_socket s, nng_aio *aio)
nng_socket_recv(nng_socket s, nng_aio *aio)
{
nni_sock *sock;
int rv;
Expand All @@ -195,7 +195,7 @@ nng_recv_aio(nng_socket s, nng_aio *aio)
}

void
nng_send_aio(nng_socket s, nng_aio *aio)
nng_socket_send(nng_socket s, nng_aio *aio)
{
nni_sock *sock;
int rv;
Expand Down
Loading

0 comments on commit d1bbf4c

Please sign in to comment.