Fixed bug where messages were mangled in reception

This commit is contained in:
j4nk 2023-09-11 02:02:18 -04:00
parent 0d2a9150a4
commit 3d3155fc46
2 changed files with 9 additions and 5 deletions

View File

@ -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;
}

View File

@ -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;