From 3d3155fc46db027ba2274b547a270ed43eb70f46 Mon Sep 17 00:00:00 2001 From: j4nk Date: Mon, 11 Sep 2023 02:02:18 -0400 Subject: [PATCH] Fixed bug where messages were mangled in reception --- oct_networking.c | 8 ++++++-- test_client.lua | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/oct_networking.c b/oct_networking.c index 8ccd306..b25a42b 100644 --- a/oct_networking.c +++ b/oct_networking.c @@ -214,8 +214,11 @@ int oct_network_recv_msgs() { // recvfrom returns -1 if nothing was received // Need MSG_DONTWAIT flag for nonblocking int i = 0; - while (recvfrom(oct_network_node.sfd, tmp_buffer, BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen) > 0 && i < OCT_NETWORK_MAX_RECVS) { - OCT_LOG_INFO("Received message!"); + ssize_t msg_size = recvfrom(oct_network_node.sfd, tmp_buffer, BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen); + while ((msg_size > 0) && (i < OCT_NETWORK_MAX_RECVS)) { + // recvfrom does NOT place the null byte - place it ourselves + tmp_buffer[msg_size] = '\0'; + OCT_LOG_DEBUG("Received message!"); int s = getnameinfo((struct sockaddr *) &peer_addr, peer_addrlen, tmp_addr, NI_MAXHOST, tmp_port, NI_MAXSERV, NI_NUMERICSERV); if (s != 0) { @@ -224,6 +227,7 @@ int oct_network_recv_msgs() { } oct_network_q_enqueue(&oct_network_node.recv_queue, tmp_addr, tmp_port, tmp_buffer); i++; + msg_size = recvfrom(oct_network_node.sfd, tmp_buffer, BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen); } return i; } diff --git a/test_client.lua b/test_client.lua index ad038a9..34ce472 100644 --- a/test_client.lua +++ b/test_client.lua @@ -15,9 +15,9 @@ end counter = 0; function oct_loop(key) - if counter == 1000000 then - oct_send("First message!", "127.0.0.1", "1234"); - oct_send("Second message!", "127.0.0.1", "1234"); + if counter == 10000000 then + oct_send("First message!\nnewline\nnewline\nnewline", "127.0.0.1", "1234"); + oct_send("Second message!\nhey!", "127.0.0.1", "1234"); counter = 0; end counter = counter + 1;