aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/sock_test.c24
-rw-r--r--src/nng.c35
-rw-r--r--src/sp/nonblock_test.c14
3 files changed, 21 insertions, 52 deletions
diff --git a/src/core/sock_test.c b/src/core/sock_test.c
index e311634d..f6f1bd7d 100644
--- a/src/core/sock_test.c
+++ b/src/core/sock_test.c
@@ -102,8 +102,8 @@ test_send_recv(void)
int len;
size_t sz;
nng_duration to = 3000; // 3 seconds
- char *buf;
- char *a = "inproc://t1";
+ char *a = "inproc://t1";
+ char rxbuf[32];
NUTS_OPEN(s1);
NUTS_OPEN(s2);
@@ -124,11 +124,10 @@ test_send_recv(void)
NUTS_PASS(nng_dial(s2, a, NULL, 0));
NUTS_PASS(nng_send(s1, "abc", 4, 0));
- NUTS_PASS(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC));
- NUTS_TRUE(buf != NULL);
+ sz = sizeof(rxbuf);
+ NUTS_PASS(nng_recv(s2, rxbuf, &sz, 0));
NUTS_TRUE(sz == 4);
- NUTS_TRUE(memcmp(buf, "abc", 4) == 0);
- nng_free(buf, sz);
+ NUTS_TRUE(memcmp(rxbuf, "abc", 4) == 0);
NUTS_CLOSE(s1);
NUTS_CLOSE(s2);
@@ -142,7 +141,7 @@ test_send_recv_zero_length(void)
int len;
size_t sz;
nng_duration to = 3000; // 3 seconds
- char *buf;
+ char buf[32];
char *a = "inproc://send-recv-zero-length";
NUTS_OPEN(s1);
@@ -164,10 +163,9 @@ test_send_recv_zero_length(void)
NUTS_PASS(nng_dial(s2, a, NULL, 0));
NUTS_PASS(nng_send(s1, "", 0, 0));
- NUTS_PASS(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC));
- NUTS_TRUE(buf == NULL);
+ sz = sizeof(buf);
+ NUTS_PASS(nng_recv(s2, buf, &sz, 0));
NUTS_TRUE(sz == 0);
- nng_free(buf, sz);
NUTS_CLOSE(s1);
NUTS_CLOSE(s2);
@@ -186,7 +184,7 @@ test_connection_refused(void)
void
test_late_connection(void)
{
- char *buf;
+ char buf[32];
size_t sz;
nng_socket s1;
nng_socket s2;
@@ -202,10 +200,10 @@ test_late_connection(void)
NUTS_PASS(nng_listen(s2, a, NULL, 0));
nng_msleep(100);
NUTS_PASS(nng_send(s1, "abc", 4, 0));
- NUTS_PASS(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC));
+ sz = sizeof(buf);
+ NUTS_PASS(nng_recv(s2, &buf, &sz, 0));
NUTS_TRUE(sz == 4);
NUTS_TRUE(memcmp(buf, "abc", 4) == 0);
- nng_free(buf, sz);
NUTS_CLOSE(s1);
NUTS_CLOSE(s2);
diff --git a/src/nng.c b/src/nng.c
index 55e23214..c4bad311 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -79,35 +79,12 @@ nng_recv(nng_socket s, void *buf, size_t *szp, int flags)
// Note that while it would be nice to make this a zero copy operation,
// its not normally possible if a size was specified.
- if ((rv = nng_recvmsg(s, &msg, flags & ~(NNG_FLAG_ALLOC))) != 0) {
+ if ((rv = nng_recvmsg(s, &msg, flags)) != 0) {
return (rv);
}
- if (!(flags & NNG_FLAG_ALLOC)) {
- memcpy(buf, nng_msg_body(msg),
- *szp > nng_msg_len(msg) ? nng_msg_len(msg) : *szp);
- *szp = nng_msg_len(msg);
- } else {
- // We'd really like to avoid a separate data copy, but since
- // we have allocated messages with headroom, we can't really
- // make free() work on the base pointer. We'd have to have
- // some other API for this. Folks that want zero copy had
- // better use nng_recvmsg() instead.
- void *nbuf;
-
- if (nng_msg_len(msg) != 0) {
- if ((nbuf = nni_alloc(nng_msg_len(msg))) == NULL) {
- nng_msg_free(msg);
- return (NNG_ENOMEM);
- }
-
- *(void **) buf = nbuf;
- memcpy(nbuf, nni_msg_body(msg), nni_msg_len(msg));
- *szp = nng_msg_len(msg);
- } else {
- *(void **) buf = NULL;
- *szp = 0;
- }
- }
+ memcpy(buf, nng_msg_body(msg),
+ *szp > nng_msg_len(msg) ? nng_msg_len(msg) : *szp);
+ *szp = nng_msg_len(msg);
nni_msg_free(msg);
return (0);
}
@@ -159,10 +136,6 @@ nng_send(nng_socket s, void *buf, size_t len, int flags)
if ((rv = nng_sendmsg(s, msg, flags)) != 0) {
// If nng_sendmsg() succeeded, then it took ownership.
nng_msg_free(msg);
- } else {
- if (flags & NNG_FLAG_ALLOC) {
- nni_free(buf, len);
- }
}
return (rv);
}
diff --git a/src/sp/nonblock_test.c b/src/sp/nonblock_test.c
index 7b2251c4..c06fa64d 100644
--- a/src/sp/nonblock_test.c
+++ b/src/sp/nonblock_test.c
@@ -1,5 +1,5 @@
//
-// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2025 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
@@ -46,8 +46,7 @@ repthr(void *arg)
for (;;) {
fd_set fset;
struct timeval tmo;
- char *msgbuf;
- size_t msglen;
+ nng_msg *msg;
FD_ZERO(&fset);
FD_SET(fd, &fset);
@@ -59,14 +58,13 @@ repthr(void *arg)
for (;;) {
int rv;
- rv = nng_recv(rep, &msgbuf, &msglen,
- NNG_FLAG_NONBLOCK | NNG_FLAG_ALLOC);
+ rv = nng_recvmsg(rep, &msg, NNG_FLAG_NONBLOCK);
if (rv != 0) {
return;
}
- nng_free(msgbuf, msglen);
- int ok = 0;
- rv = nng_send(rep, &ok, 4, NNG_FLAG_NONBLOCK);
+ nng_msg_clear(msg);
+ nng_msg_append_u32(msg, 0);
+ rv = nng_sendmsg(rep, msg, NNG_FLAG_NONBLOCK);
if (rv == NNG_ECLOSED) {
return;
}