aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-12-31 17:57:12 -0800
committerGarrett D'Amore <garrett@damore.org>2024-12-31 17:57:12 -0800
commitf0a65566a0879015cf75323ef4c3495e5ce3867e (patch)
tree59d53a6e8585bb8228ee1ea617349ba01d6fd33b
parent3fd24aae784e12dc83c8344b8459c34200ab077c (diff)
downloadnng-f0a65566a0879015cf75323ef4c3495e5ce3867e.tar.gz
nng-f0a65566a0879015cf75323ef4c3495e5ce3867e.tar.bz2
nng-f0a65566a0879015cf75323ef4c3495e5ce3867e.zip
docs: add an example of nng_recv_aio with subv2.0.0-alpha.2
-rw-r--r--docs/ref/api/sock.md53
1 files changed, 52 insertions, 1 deletions
diff --git a/docs/ref/api/sock.md b/docs/ref/api/sock.md
index 54aa6580..3ea056fb 100644
--- a/docs/ref/api/sock.md
+++ b/docs/ref/api/sock.md
@@ -352,6 +352,7 @@ sake of clarity.
```c
#include <stdlib.h>
+#include <stdio.h>
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
@@ -368,7 +369,7 @@ void callback(void *arg) {
nng_time now;
struct state *state = arg;
if (nng_aio_result(state->aio) != 0) {
- nng_log_err("Error %s occurred", nng_strerror(nng_aio_result(state->aio)));
+ fprintf(stderr, "Error %s occurred", nng_strerror(nng_aio_result(state->aio)));
return; // terminate the callback loop
}
if (state->sleeping) {
@@ -398,4 +399,54 @@ 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
+watch for messages received from the service created in Example 2.
+Error handling is elided for the sake of clarity.
+
+```c
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>>
+#include <nng/nng.h>
+#include <nng/protocol/pubsub0/sub.h>
+
+struct state {
+ nng_socket s;
+ nng_aio *aio;
+};
+
+static struct state state;
+
+void callback(void *arg) {
+ nng_msg *msg;
+ nng_time now;
+ struct state *state = arg;
+ if (nng_aio_result(state->aio) != 0) {
+ fprintf(stderr, "Error %s occurred", nng_strerror(nng_aio_result(state->aio)));
+ return; // terminate the callback loop
+ }
+ msg = nng_aio_get_msg(state->aio);
+ memcpy(&now, nng_msg_body(msg), sizeof (now)); // should check the length!
+ 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);
+}
+
+int main(int argc, char **argv) {
+ const char *url = argv[1]; // should check this
+
+ nng_aio_alloc(&state.aio, NULL, NULL);
+ 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
+ for(;;) {
+ nng_msleep(0x7FFFFFFF); // infinite, could use pause or sigsuspend
+ }
+}
+```
+
{{#include ../xref.md}}