Skip to content

Commit 900edda

Browse files
authored
Migrate to NNG 2.0-compatible APIs (#206)
1 parent e2aee98 commit 900edda

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
^compile_commands\.json$
1313
^\.cache$
1414
^cran-comments\.md$
15+
^CLAUDE.md$

src/comms.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,13 @@ SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {
453453

454454
const int mod = nano_matcharg(mode);
455455
nng_socket *sock = (nng_socket *) NANO_PTR(con);
456+
nng_msg *msgp = NULL;
456457

457458
if (flags <= 0) {
458459

459-
if ((xc = nng_recv(*sock, &buf, &sz, NNG_FLAG_ALLOC + (flags < 0 || NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK)))
460+
if ((xc = nng_recvmsg(*sock, &msgp, (flags < 0 || NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK)))
460461
goto fail;
461-
462-
res = nano_decode(buf, sz, mod, NANO_PROT(con));
463-
nng_free(buf, sz);
464-
462+
465463
} else {
466464

467465
nng_aio *aiop = NULL;
@@ -474,13 +472,13 @@ SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {
474472
nng_aio_free(aiop);
475473
goto fail;
476474
}
477-
nng_msg *msgp = nng_aio_get_msg(aiop);
475+
msgp = nng_aio_get_msg(aiop);
478476
nng_aio_free(aiop);
479-
buf = nng_msg_body(msgp);
480-
sz = nng_msg_len(msgp);
481-
res = nano_decode(buf, sz, mod, NANO_PROT(con));
482-
nng_msg_free(msgp);
483477
}
478+
buf = nng_msg_body(msgp);
479+
sz = nng_msg_len(msgp);
480+
res = nano_decode(buf, sz, mod, NANO_PROT(con));
481+
nng_msg_free(msgp);
484482

485483
} else if (!NANO_PTR_CHECK(con, nano_ContextSymbol)) {
486484

src/thread.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ static void rnng_messenger_thread(void *args) {
7272
SEXP socket = CADR(plist);
7373
SEXP key = CADDR(plist);
7474
nng_socket *sock = (nng_socket *) NANO_PTR(socket);
75+
nng_msg *msgp = NULL;
7576
unsigned char *buf;
7677
size_t sz;
7778
time_t now;
7879
struct tm *tms;
7980
int xc;
8081

8182
while (1) {
82-
xc = nng_recv(*sock, &buf, &sz, NNG_FLAG_ALLOC);
83+
xc = nng_recvmsg(*sock, &msgp, 0);
8384
time(&now);
8485
tms = localtime(&now);
8586

@@ -91,13 +92,16 @@ static void rnng_messenger_thread(void *args) {
9192
break;
9293
}
9394

95+
buf = nng_msg_body(msgp);
96+
sz = nng_msg_len(msgp);
97+
9498
if (!strncmp((char *) buf, ":", 1)) {
9599
if (!strncmp((char *) buf, ":c ", 3)) {
96100
nano_printf(1,
97101
"| <- peer connected: %d-%02d-%02d %02d:%02d:%02d\n",
98102
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
99103
tms->tm_hour, tms->tm_min, tms->tm_sec);
100-
nng_free(buf, sz);
104+
nng_msg_free(msgp);
101105
nano_buf enc;
102106
nano_encode(&enc, key);
103107
xc = nng_send(*sock, enc.buf, enc.cur, NNG_FLAG_NONBLOCK);
@@ -115,7 +119,7 @@ static void rnng_messenger_thread(void *args) {
115119
"| -> peer disconnected: %d-%02d-%02d %02d:%02d:%02d\n",
116120
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
117121
tms->tm_hour, tms->tm_min, tms->tm_sec);
118-
nng_free(buf, sz);
122+
nng_msg_free(msgp);
119123
continue;
120124
}
121125
}
@@ -125,7 +129,7 @@ static void rnng_messenger_thread(void *args) {
125129
(char *) buf, (int) sz, "",
126130
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
127131
tms->tm_hour, tms->tm_min, tms->tm_sec);
128-
nng_free(buf, sz);
132+
nng_msg_free(msgp);
129133

130134
}
131135

0 commit comments

Comments
 (0)