#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
+#include <unistd.h>
#include "packet.h"
#include "tmpalloc.h"
};
// 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_length;
static void next_vec(GlPacket *pkt)
{
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);
+
+ ret = read(fd, in_buffer+in_length, 1024-in_length);
+ if(ret>0)
+ {
+ GlPacket *pkt;
+ unsigned pkt_length;
+
+ in_length += 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);
+ }
+
+ return pkt;
+ }
+ }
+
+ return NULL;
+}
+
static void next_chunk(GlPacket *pkt)
{
GlInPacket *in = &pkt->in;
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;
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<count; ++i)
packet_read_string(pkt, *v+i);
}