Skip to content

Commit d48eaf1

Browse files
committed
nanocoap_server: use zero-copy network buffer for parsing request
1 parent db345a7 commit d48eaf1

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

sys/include/net/nanocoap_sock.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,17 @@ static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
476476
}
477477

478478
/**
479-
* @brief Start a nanocoap server instance
479+
* @brief Start a nanoCoAP server instance
480480
*
481-
* This function only returns if there's an error binding to @p local, or if
482-
* receiving of UDP packets fails.
481+
* This function only returns if there's an error binding to @p local.
483482
*
484483
* @param[in] local local UDP endpoint to bind to
485-
* @param[in] buf input buffer to use
484+
* @param[in] buf response buffer to use
486485
* @param[in] bufsize size of @p buf
487486
*
488-
* @returns -1 on error
487+
* @returns return code of @see sock_udp_create on error
489488
*/
490-
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
489+
int nanocoap_server(sock_udp_ep_t *local, void *buf, size_t bufsize);
491490

492491
/**
493492
* @brief Create and start the nanoCoAP server thread

sys/net/application_layer/nanocoap/sock.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path,
11311131
return (res < 0) ? (ssize_t)res : (ssize_t)_buf.len;
11321132
}
11331133

1134-
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
1134+
int nanocoap_server(sock_udp_ep_t *local, void *rsp_buf, size_t rsp_buf_len)
11351135
{
11361136
sock_udp_t sock;
11371137
sock_udp_ep_t remote;
@@ -1145,11 +1145,20 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11451145

11461146
ssize_t res = sock_udp_create(&sock, local, NULL, 0);
11471147
if (res != 0) {
1148-
return -1;
1148+
return res;
11491149
}
11501150

1151+
void *buf;
1152+
void *buf_ctx = NULL;
1153+
11511154
while (1) {
11521155

1156+
if (buf_ctx) {
1157+
/* free the buffer */
1158+
res = sock_udp_recv_buf_aux(&sock, &buf, &buf_ctx, 0, NULL, NULL);
1159+
assert(res == 0);
1160+
}
1161+
11531162
sock_udp_aux_rx_t *aux_in_ptr = NULL;
11541163
#ifdef MODULE_SOCK_AUX_LOCAL
11551164
sock_udp_aux_rx_t aux_in = {
@@ -1158,14 +1167,14 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11581167
aux_in_ptr = &aux_in;
11591168
#endif
11601169

1161-
res = sock_udp_recv_aux(&sock, buf, bufsize, SOCK_NO_TIMEOUT,
1162-
&remote, aux_in_ptr);
1170+
res = sock_udp_recv_buf_aux(&sock, &buf, &buf_ctx, SOCK_NO_TIMEOUT,
1171+
&remote, aux_in_ptr);
11631172
if (res <= 0) {
11641173
DEBUG("nanocoap: error receiving UDP packet %" PRIdSIZE "\n", res);
11651174
continue;
11661175
}
11671176
coap_pkt_t pkt;
1168-
if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
1177+
if (coap_parse(&pkt, buf, res) < 0) {
11691178
DEBUG("nanocoap: error parsing packet\n");
11701179
continue;
11711180
}
@@ -1183,12 +1192,12 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11831192
}
11841193
ctx.local = &aux_in.local;
11851194
#endif
1186-
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) <= 0) {
1195+
if ((res = coap_handle_req(&pkt, rsp_buf, rsp_buf_len, &ctx)) <= 0) {
11871196
DEBUG("nanocoap: error handling request %" PRIdSIZE "\n", res);
11881197
continue;
11891198
}
11901199

1191-
sock_udp_send_aux(&sock, buf, res, &remote, aux_out_ptr);
1200+
sock_udp_send_aux(&sock, rsp_buf, res, &remote, aux_out_ptr);
11921201
}
11931202

11941203
return 0;

0 commit comments

Comments
 (0)