diff options
| author | Garrett D'Amore <garrett@damore.org> | 2025-01-01 17:06:39 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2025-01-01 17:06:39 -0800 |
| commit | f7bf24f429cbc488b861ab1b1e4cf1983af56295 (patch) | |
| tree | 2196361a906bd5db1148c0e177d69854a99b7b58 /demo/reqrep/reqrep.c | |
| parent | ee5c8437f8c2a811c0eaef9b00c149b93c095391 (diff) | |
| download | nng-f7bf24f429cbc488b861ab1b1e4cf1983af56295.tar.gz nng-f7bf24f429cbc488b861ab1b1e4cf1983af56295.tar.bz2 nng-f7bf24f429cbc488b861ab1b1e4cf1983af56295.zip | |
api: Remove the NNG_FLAG_ALLOC
This flag failed to provide real zero copy that it was intended for,
and it also involved extra allocations. Further, the API for it was
brittle and error prone.
Modern code should just work directly with nng_msg structures.
Diffstat (limited to 'demo/reqrep/reqrep.c')
| -rw-r--r-- | demo/reqrep/reqrep.c | 79 |
1 files changed, 27 insertions, 52 deletions
diff --git a/demo/reqrep/reqrep.c b/demo/reqrep/reqrep.c index 5eb96266..9d7110f7 100644 --- a/demo/reqrep/reqrep.c +++ b/demo/reqrep/reqrep.c @@ -29,28 +29,6 @@ #define SERVER "server" #define DATECMD 1 -#define PUT64(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint64_t) (u)) >> 56); \ - (ptr)[1] = (uint8_t) (((uint64_t) (u)) >> 48); \ - (ptr)[2] = (uint8_t) (((uint64_t) (u)) >> 40); \ - (ptr)[3] = (uint8_t) (((uint64_t) (u)) >> 32); \ - (ptr)[4] = (uint8_t) (((uint64_t) (u)) >> 24); \ - (ptr)[5] = (uint8_t) (((uint64_t) (u)) >> 16); \ - (ptr)[6] = (uint8_t) (((uint64_t) (u)) >> 8); \ - (ptr)[7] = (uint8_t) ((uint64_t) (u)); \ - } while (0) - -#define GET64(ptr, v) \ - v = (((uint64_t) ((uint8_t) (ptr)[0])) << 56) + \ - (((uint64_t) ((uint8_t) (ptr)[1])) << 48) + \ - (((uint64_t) ((uint8_t) (ptr)[2])) << 40) + \ - (((uint64_t) ((uint8_t) (ptr)[3])) << 32) + \ - (((uint64_t) ((uint8_t) (ptr)[4])) << 24) + \ - (((uint64_t) ((uint8_t) (ptr)[5])) << 16) + \ - (((uint64_t) ((uint8_t) (ptr)[6])) << 8) + \ - (((uint64_t) (uint8_t) (ptr)[7])) - void fatal(const char *func, int rv) { @@ -88,36 +66,34 @@ server(const char *url) nng_listener_start(listener, 0); for (;;) { - char *buf = NULL; - size_t sz; uint64_t val; + nng_msg *msg; count++; - if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { + if ((rv = nng_recvmsg(sock, &msg, 0)) != 0) { fatal("nng_recv", rv); } - if ((sz == sizeof(uint64_t)) && - ((GET64(buf, val)) == DATECMD)) { + if ((nng_msg_trim_u64(msg, &val) == 0) && (val == DATECMD)) { time_t now; printf("SERVER: RECEIVED DATE REQUEST\n"); now = time(&now); if (count == 6) { printf("SERVER: SKIP SENDING REPLY\n"); - nng_free(buf, sz); + nng_msg_free(msg); continue; } printf("SERVER: SENDING DATE: "); showdate(now); - // Reuse the buffer. We know it is big enough. - PUT64(buf, (uint64_t) now); - rv = nng_send(sock, buf, sz, NNG_FLAG_ALLOC); + // Reuse the message. We know it is big enough. + nng_msg_append_u64(msg, now); + rv = nng_sendmsg(sock, msg, 0); if (rv != 0) { fatal("nng_send", rv); } - continue; + } else { + // Unrecognized command, so toss the message. + nng_msg_free(msg); } - // Unrecognized command, so toss the buffer. - nng_free(buf, sz); } } @@ -128,12 +104,8 @@ client(const char *url) nng_dialer dialer; int rv; size_t sz; - char *buf = NULL; - uint8_t cmd[sizeof(uint64_t)]; int sleep = 0; - PUT64(cmd, DATECMD); - if ((rv = nng_init(NULL)) != 0) { fatal("nng_init", rv); } @@ -151,22 +123,27 @@ client(const char *url) while (1) { + nng_msg *msg; + uint64_t now; + if ((rv = nng_msg_alloc(&msg, 0)) != 0) { + fatal("nng_msg_alloc", rv); + } + if ((rv = nng_msg_append_u64(msg, DATECMD)) != 0) { + fatal("nng_msg_append", rv); + } printf("CLIENT: SENDING DATE REQUEST\n"); - if ((rv = nng_send(sock, cmd, sizeof(cmd), 0)) != 0) { - fatal("nng_send", rv); + if ((rv = nng_sendmsg(sock, msg, 0)) != 0) { + fatal("nng_sendmsg", rv); } - if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { - fatal("nng_recv", rv); + if ((rv = nng_recvmsg(sock, &msg, 0)) != 0) { + fatal("nng_recvmsg", rv); } - - if (sz == sizeof(uint64_t)) { - uint64_t now; - GET64(buf, now); - printf("CLIENT: RECEIVED DATE: "); - showdate((time_t) now); - } else { - printf("CLIENT: GOT WRONG SIZE!\n"); + if ((rv = nng_msg_trim_u64(msg, &now)) != 0) { + fatal("nng_msg_trim_u64", rv); } + nng_msg_free(msg); + printf("CLIENT: RECEIVED DATE: "); + showdate((time_t) now); nng_msleep(sleep); sleep++; if (sleep == 4) { @@ -174,8 +151,6 @@ client(const char *url) } } - // This assumes that buf is ASCIIZ (zero terminated). - nng_free(buf, sz); nng_socket_close(sock); return (0); } |
