]> git.tdb.fi Git - gldbg.git/commitdiff
Properly handle reception of multiple packets at once
authorMikko Rasa <tdb@tdb.fi>
Wed, 24 Nov 2010 15:06:41 +0000 (15:06 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 24 Nov 2010 15:06:41 +0000 (15:06 +0000)
source/packet.c

index e0b687ac7c54846c2a9dd9d7710d15f5a9650d1c..d31a631d26417db3ee5e6cd3148dc7a005f8b50a 100644 (file)
@@ -43,7 +43,8 @@ static char *out_buffer = NULL;
 static struct iovec *iovecs = NULL;
 static GlPacket packet;
 static char *in_buffer = NULL;
-static unsigned in_length;
+static unsigned in_fill = 0;
+static unsigned in_offset = 0;
 
 static void next_vec(GlPacket *pkt)
 {
@@ -285,24 +286,29 @@ GlPacket *packet_receive(int fd)
                if(!in_buffer)
                        in_buffer = (char *)malloc(1024);
 
-               ret = read(fd, in_buffer+in_length, 1024-in_length);
-               if(ret>0)
+               if(in_offset)
                {
-                       GlPacket *pkt;
-                       unsigned pkt_length;
+                       memmove(in_buffer, in_buffer+in_offset, in_fill-in_offset);
+                       in_fill -= in_offset;
+                       in_offset = 0;
+               }
 
-                       in_length += ret;
+               ret = read(fd, in_buffer+in_fill, 1024-in_fill);
+               if(ret>0)
+                       in_fill += ret;
+       }
 
-                       pkt_length = in_length;
-                       pkt = packet_receive_str(in_buffer, &pkt_length);
-                       if(pkt)
-                       {
-                               in_length -= pkt_length;
-                               memmove(in_buffer, in_buffer+pkt_length, in_length);
-                       }
+       if(ret>0 || in_fill>in_offset)
+       {
+               GlPacket *pkt;
+               unsigned pkt_length;
 
-                       return pkt;
-               }
+               pkt_length = in_fill;
+               pkt = packet_receive_str(in_buffer+in_offset, &pkt_length);
+               if(pkt)
+                       in_offset += pkt_length;
+
+               return pkt;
        }
 
        return NULL;