X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpacket.c;h=d31a631d26417db3ee5e6cd3148dc7a005f8b50a;hb=1ca901ed93a40c38c489bb6407f23991a947b56a;hp=465f11868ccdb1197577273b804f13b1e7b1da61;hpb=fab9ed5163a8f4ef5314bc67e48d1690d1126649;p=gldbg.git diff --git a/source/packet.c b/source/packet.c index 465f118..d31a631 100644 --- a/source/packet.c +++ b/source/packet.c @@ -8,6 +8,7 @@ Distributed under the GPL #include #include #include +#include #include "packet.h" #include "tmpalloc.h" @@ -38,11 +39,12 @@ struct GlPacket }; // XXX Should make this stuff truly re-entrant -static char *out_buffer = 0; -static struct iovec *iovecs = 0; +static char *out_buffer = NULL; +static struct iovec *iovecs = NULL; static GlPacket packet; -/*static char *in_buffer = 0; -static unsigned in_length;*/ +static char *in_buffer = NULL; +static unsigned in_fill = 0; +static unsigned in_offset = 0; static void next_vec(GlPacket *pkt) { @@ -270,6 +272,48 @@ GlPacket *packet_receive_str(const char *data, unsigned *len) return pkt; } +GlPacket *packet_receive(int fd) +{ + int ret; + fd_set fds; + struct timeval tv = { 0, 0 }; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + ret = select(fd+1, &fds, NULL, NULL, &tv); + if(ret>0) + { + if(!in_buffer) + in_buffer = (char *)malloc(1024); + + if(in_offset) + { + memmove(in_buffer, in_buffer+in_offset, in_fill-in_offset); + in_fill -= in_offset; + in_offset = 0; + } + + ret = read(fd, in_buffer+in_fill, 1024-in_fill); + if(ret>0) + in_fill += ret; + } + + if(ret>0 || in_fill>in_offset) + { + GlPacket *pkt; + unsigned pkt_length; + + pkt_length = in_fill; + pkt = packet_receive_str(in_buffer+in_offset, &pkt_length); + if(pkt) + in_offset += pkt_length; + + return pkt; + } + + return NULL; +} + static void next_chunk(GlPacket *pkt) { GlInPacket *in = &pkt->in; @@ -353,12 +397,12 @@ void packet_read_double(GlPacket *pkt, double *v) read_raw(pkt, (char *)v, sizeof(double), 1); } -void packet_read_pointer(GlPacket *pkt, pointer *v) +void packet_read_pointer(GlPacket *pkt, const void **v) { - read_raw(pkt, (char *)v, sizeof(pointer), 1); + read_raw(pkt, (char *)v, sizeof(const void *), 1); } -void packet_read_data(GlPacket *pkt, pointer *v) +void packet_read_data(GlPacket *pkt, const void **v) { GlInPacket *in = &pkt->in; int vlen; @@ -373,17 +417,17 @@ void packet_read_data(GlPacket *pkt, pointer *v) in->length -= vlen; } -void packet_read_string(GlPacket *pkt, string *v) +void packet_read_string(GlPacket *pkt, const char **v) { - packet_read_data(pkt, (pointer *)v); + packet_read_data(pkt, (const void **)v); } -void packet_read_string_array(GlPacket *pkt, string **v) +void packet_read_string_array(GlPacket *pkt, const char ***v) { int count; int i; packet_read_int(pkt, &count); - *v = (string *)tmpalloc(count*sizeof(string)); + *v = (const char **)tmpalloc(count*sizeof(const char *)); for(i=0; i