X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpacket.c;h=95190c56cb0c148a7c9fc65dbd57c132b9445712;hb=d72ef6d75a11f6cc05ab8ec039520719e1044741;hp=465f11868ccdb1197577273b804f13b1e7b1da61;hpb=fab9ed5163a8f4ef5314bc67e48d1690d1126649;p=gldbg.git diff --git a/source/packet.c b/source/packet.c index 465f118..95190c5 100644 --- a/source/packet.c +++ b/source/packet.c @@ -1,13 +1,7 @@ -/* $Id$ - -This file is part of gldbg -Copyright © 2010 Mikko Rasa, Mikkosoft Productions -Distributed under the GPL -*/ - #include #include #include +#include #include "packet.h" #include "tmpalloc.h" @@ -38,11 +32,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) { @@ -191,7 +186,14 @@ void packet_write_pointer(GlPacket *pkt, const void *p) void packet_write_data(GlPacket *pkt, const void *data, unsigned size) { - if(data) + if(!data) + packet_write_int(pkt, 0); + else if((unsigned long)data<0x100000) + { + packet_write_int(pkt, ~0); + packet_write_pointer(pkt, data); + } + else { GlOutPacket *out = &pkt->out; @@ -203,8 +205,6 @@ void packet_write_data(GlPacket *pkt, const void *data, unsigned size) ++out->vec; out->vec->iov_base = out->ptr; } - else - packet_write_int(pkt, 0); } void packet_write_string(GlPacket *pkt, const char *s) @@ -270,6 +270,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,37 +395,41 @@ 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; packet_read_int(pkt, &vlen); - if(vlen) + if(vlen==~0) + packet_read_pointer(pkt, v); + else if(vlen) + { *v = in->ptr; + in->ptr += vlen; + in->chunk -= vlen; + in->length -= vlen; + } else *v = NULL; - in->ptr += vlen; - in->chunk -= vlen; - 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