]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldecoder.c
Use a centralized packet framework
[gldbg.git] / source / gldecoder.c
index 8d0f15a1287f57b555bdd6ca18b1269f72f1aa9d..999acccc9cec140d141f5db97f878af136282cdf 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the GPL
 */
 
@@ -9,12 +9,10 @@ Distributed under the GPL
 #include <string.h>
 #include "functions.h"
 #include "gldecoder.h"
-#include "tmpalloc.h"
+#include "packet.h"
 
-static unsigned read_short(short *, const char *);
-static unsigned read_int(int *, const char *);
-static int decode_func(GlDecoder *, unsigned short, const char *);
-static int decode_gldfunc(GlDecoder *, unsigned short, const char *);
+static int decode_func(GlDecoder *, unsigned short, GlPacket *);
+static int decode_gldfunc(GlDecoder *, unsigned short, GlPacket *);
 
 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
 {
@@ -37,127 +35,46 @@ void gldecoder_delete(GlDecoder *dec)
 
 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
 {
-       unsigned pos = 0;
-       unsigned pktlen;
+       GlPacket *pkt;
        unsigned short func;
-       int ret;
 
-       if(len<sizeof(int)+sizeof(short))
+       pkt = packet_receive_str(data, &len);
+       if(!pkt)
                return -1;
-       pos += read_int((int *)&pktlen, data);
-       if(len<pktlen)
-               return -1;
-       pos += read_short((short *)&func, data+pos);
+
+       packet_read_short(pkt, (short *)&func);
+
        if(dec)
        {
+               int ret = 0;
+
                if(func&0x8000)
-                       ret = decode_gldfunc(dec, func, data+pos);
+                       ret = decode_gldfunc(dec, func, pkt);
                else
-                       ret = decode_func(dec, func, data+pos);
+                       ret = decode_func(dec, func, pkt);
                if(ret<0)
                        return -1;
        }
-       return pktlen;
-}
-
-static unsigned read_char(char *v, const char *data)
-{
-       *v = *data;
-       return 1;
-}
-
-static unsigned read_short(short *v, const char *data)
-{
-       *v = *(short *)data;
-       return sizeof(short);
-}
-
-static unsigned read_int(int *v, const char *data)
-{
-       *v = *(int *)data;
-       return sizeof(int);
-}
-
-static unsigned read_long(long *v, const char *data)
-{
-       *v = *(long *)data;
-       return sizeof(long);
-}
-
-static unsigned read_long_long(long long *v, const char *data)
-{
-       *v = *(long long *)data;
-       return sizeof(long long);
-}
-
-static unsigned read_float(float *v, const char *data)
-{
-       *v = *(float *)data;
-       return sizeof(float);
-}
-
-static unsigned read_double(double *v, const char *data)
-{
-       *v = *(double *)data;
-       return sizeof(double);
-}
-
-typedef void *pointer;
 
-static unsigned read_pointer(pointer *v, const char *data)
-{
-       *v = *(void **)data;
-       return sizeof(void *);
-}
-
-static unsigned read_data(const void **v, const char *data)
-{
-       int vlen;
-       unsigned pos = 0;
-       pos += read_int(&vlen, data);
-       if(vlen)
-               *v = data+pos;
-       else
-               *v = NULL;
-       return pos+vlen;
-}
-
-typedef const char *string;
-
-static unsigned read_string(string *v, const char *data)
-{
-       return read_data((const void **)v, data);
-}
-
-static unsigned read_string_array(string **v, const char *data)
-{
-       int count;
-       unsigned pos = 0;
-       int i;
-       pos += read_int(&count, data);
-       *v = (string *)tmpalloc(count*sizeof(string));
-       for(i=0; i<count; ++i)
-               pos += read_string(*v+i, data+pos);
-       return pos;
+       return len;
 }
 
-#include "gldecoder.funcs"
+#include "gensrc/gldecoder.funcs"
 
-static int decode_gldError(GlDecoder *dec, const char *data)
+static void decode_gldError(GlDecoder *dec, GlPacket *pkt)
 {
-       unsigned pos = 0;
        GLenum code;
-       pos += read_int((int *)&code, data);
+       packet_read_int(pkt, (int *)&code);
        if(dec->gldError)
                dec->gldError(dec->user_data, code);
-       return pos;
 }
 
-static int decode_gldfunc(GlDecoder *dec, unsigned short func, const char *data)
+static int decode_gldfunc(GlDecoder *dec, unsigned short func, GlPacket *pkt)
 {
        switch(func)
        {
-       case FUNC_GLDERROR: return decode_gldError(dec, data);
+       case FUNC_GLDERROR: decode_gldError(dec, pkt); break;
        default: return -1;
        }
+       return 0;
 }