From: Mikko Rasa Date: Wed, 24 Nov 2010 15:06:41 +0000 (+0000) Subject: Properly handle reception of multiple packets at once X-Git-Url: http://git.tdb.fi/?p=gldbg.git;a=commitdiff_plain;h=1ca901ed93a40c38c489bb6407f23991a947b56a Properly handle reception of multiple packets at once --- diff --git a/source/packet.c b/source/packet.c index e0b687a..d31a631 100644 --- a/source/packet.c +++ b/source/packet.c @@ -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;