Assuring the send/recv loops, which didn't pay attention to data sent in
authorllbatlle@taga
Mon, 11 Aug 2008 20:27:29 +0200
changeset 235 97064e7d2969
parent 234 cde13285c573
child 236 cb9c3b46c874
Assuring the send/recv loops, which didn't pay attention to data sent in multiple packets (several recv() calls).
main.h
msg.c
--- a/main.h	Wed Jul 16 22:06:44 2008 +0200
+++ b/main.h	Mon Aug 11 20:27:29 2008 +0200
@@ -244,8 +244,8 @@
 void unblock_sigint_and_install_handler();
 
 /* msg.c */
-void send_bytes(const int fd, const char *data, const int bytes);
-int recv_bytes(const int fd, char *data, const int bytes);
+void send_bytes(const int fd, const char *data, int bytes);
+int recv_bytes(const int fd, char *data, int bytes);
 void send_msg(const int fd, const struct msg *m);
 int recv_msg(const int fd, struct msg *m);
 
--- a/msg.c	Wed Jul 16 22:06:44 2008 +0200
+++ b/msg.c	Mon Aug 11 20:27:29 2008 +0200
@@ -11,22 +11,44 @@
 #include <stdlib.h>
 #include "main.h"
 
-void send_bytes(const int fd, const char *data, const int bytes)
+void send_bytes(const int fd, const char *data, int bytes)
 {
     int res;
+    int offset = 0;
     /* Send the message */
-    res = send(fd, data, bytes, 0);
-    if(res == -1)
-        warning("Sending %i bytes to %i.", bytes, fd);
+    while(1)
+    {
+        res = send(fd, data + offset, bytes, 0);
+        if(res == -1)
+        {
+            warning("Sending %i bytes to %i.", bytes, fd);
+            break;
+        }
+        if(res == bytes)
+            break;
+        offset += res;
+        bytes -= res;
+    }
 }
 
-int recv_bytes(const int fd, char *data, const int bytes)
+int recv_bytes(const int fd, char *data, int bytes)
 {
     int res;
+    int offset = 0;
     /* Send the message */
-    res = recv(fd, data, bytes, 0);
-    if(res == -1)
-        warning("Receiving %i bytes from %i.", bytes, fd);
+    while(1)
+    {
+        res = recv(fd, data + offset, bytes, 0);
+        if(res == -1)
+        {
+            warning("Receiving %i bytes from %i.", bytes, fd);
+            break;
+        }
+        if(res == bytes)
+            break;
+        offset += res;
+        bytes -= res;
+    }
 
     return res;
 }
@@ -35,11 +57,13 @@
 {
     int res;
     /* Send the message */
-    if (0)
+    if (1)
         msgdump(stderr, m);
     res = send(fd, m, sizeof(*m), 0);
-    if(res == -1 && 0)
-        warning_msg(m, "Sending a message to %i.", fd);
+    if(res == -1 || res != sizeof(*m))
+        warning_msg(m, "Sending a message to %i, sent %i bytes, should "
+                "send %i.", fd,
+                res, sizeof(*m));
 }
 
 int recv_msg(const int fd, struct msg *m)
@@ -49,8 +73,12 @@
     res = recv(fd, m, sizeof(*m), 0);
     if(res == -1)
         warning_msg(m, "Receiving a message from %i.", fd);
-    if (res == sizeof(*m) && 0)
+    if (res == sizeof(*m))
         msgdump(stderr, m);
+    else if (res > 0)
+        warning_msg(m, "Receiving a message from %i, received %i bytes, "
+                "should have received %i.", fd,
+                res, sizeof(*m));
 
     return res;
 }