Revision: 7295
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7295&view=rev
Author:   thjc
Date:     2009-01-24 02:25:59 +0000 (Sat, 24 Jan 2009)

Log Message:
-----------
fixed unpack/pack regressions introduced by the elimination of empty structures 
in the interfaces.

Modified Paths:
--------------
    code/player/trunk/client_libs/libplayerc/client.c
    code/player/trunk/libplayertcp/playertcp.cc
    code/player/trunk/libplayertcp/playerudp.cc

Modified: code/player/trunk/client_libs/libplayerc/client.c
===================================================================
--- code/player/trunk/client_libs/libplayerc/client.c   2009-01-24 01:07:34 UTC 
(rev 7294)
+++ code/player/trunk/client_libs/libplayerc/client.c   2009-01-24 02:25:59 UTC 
(rev 7295)
@@ -1139,33 +1139,39 @@
     client->read_xdrdata_len += nbytes;
   }
 
+  if (header->size)
+  {
   // Locate the appropriate unpacking function for the message body
-  if(!(packfunc = playerxdr_get_packfunc(header->addr.interf, header->type,
+    if(!(packfunc = playerxdr_get_packfunc(header->addr.interf, header->type,
                                          header->subtype)))
-  {
-    // TODO: Allow the user to register a callback to handle unsupported
-    // messages
-    PLAYERC_ERR4("skipping message from %s:%u with unsupported type %s:%u",
+    {
+      // TODO: Allow the user to register a callback to handle unsupported
+      // messages
+      PLAYERC_ERR4("skipping message from %s:%u with unsupported type %s:%u",
                  interf_to_str(header->addr.interf), header->addr.index, 
msgtype_to_str(header->type), header->subtype);
 
-    // Slide over the body
-    memmove(client->read_xdrdata,
+      // Slide over the body
+      memmove(client->read_xdrdata,
             client->read_xdrdata + header->size,
             client->read_xdrdata_len - header->size);
-    client->read_xdrdata_len -= header->size;
+      client->read_xdrdata_len -= header->size;
 
-    return(-1);
-  }
+      return(-1);
+    }
 
-  // Unpack the body
-  if((decode_msglen = (*packfunc)(client->read_xdrdata,
+    // Unpack the body
+    if((decode_msglen = (*packfunc)(client->read_xdrdata,
                                   header->size, data, PLAYERXDR_DECODE)) < 0)
-  {
-    PLAYERC_ERR4("decoding failed on message from %s:%u with type %s:%u",
+    {
+      PLAYERC_ERR4("decoding failed on message from %s:%u with type %s:%u",
                  interf_to_str(header->addr.interf), header->addr.index, 
msgtype_to_str(header->type), header->subtype);
-    return(-1);
+      return(-1);
+    }
   }
-
+  else
+  {
+    decode_msglen = 0;
+  }
   // Slide over the body
   memmove(client->read_xdrdata,
           client->read_xdrdata + header->size,
@@ -1273,8 +1279,15 @@
 
   item = client->qitems + (client->qfirst + client->qlen) % client->qsize;
   item->header = *header;
-  item->data = malloc(header->size);
-  memcpy(item->data, data, header->size);
+  if (header->size && data)
+  {
+    item->data = malloc(header->size);
+    memcpy(item->data, data, header->size);
+  }
+  else
+  {
+    item->data = NULL;
+  }
 
   client->qlen +=1;
 

Modified: code/player/trunk/libplayertcp/playertcp.cc
===================================================================
--- code/player/trunk/libplayertcp/playertcp.cc 2009-01-24 01:07:34 UTC (rev 
7294)
+++ code/player/trunk/libplayertcp/playertcp.cc 2009-01-24 02:25:59 UTC (rev 
7295)
@@ -567,6 +567,7 @@
   player_msghdr_t hdr;
   void* payload;
   int encode_msglen;
+
 #if HAVE_Z
   player_map_data_t* zipped_data=NULL;
 #endif
@@ -581,6 +582,8 @@
                          client->writebuffer,
                          MIN(client->writebufferlen,
                              PLAYERTCP_WRITEBUFFER_SIZE));
+
+
       if(numwritten < 0)
       {
         if(errno == EAGAIN)
@@ -612,79 +615,79 @@
       // instances of the message on other queues.
       hdr = *msg->GetHeader();
       payload = msg->GetPayload();
-      // Locate the appropriate packing function
-      if(!(packfunc = playerxdr_get_packfunc(hdr.addr.interf,
-                                         hdr.type, hdr.subtype)))
+
+      // Make sure there's room in the buffer for the encoded messsage.
+      // 4 times the message (including dynamic data) is a safe upper bound
+      size_t maxsize = PLAYERXDR_MSGHDR_SIZE + (4 * msg->GetDataSize());
+      if(maxsize > (size_t)(client->writebuffersize))
       {
-        // TODO: Allow the user to register a callback to handle unsupported
-        // messages
-        PLAYER_WARN4("skipping message from %s:%u with unsupported type %s:%u",
-                     interf_to_str(hdr.addr.interf), hdr.addr.index, 
msgtype_to_str(hdr.type), hdr.subtype);
-      }
-      else
-      {
-        // Make sure there's room in the buffer for the encoded messsage.
-        // 4 times the message (including dynamic data) is a safe upper bound
-        size_t maxsize = PLAYERXDR_MSGHDR_SIZE + (4 * msg->GetDataSize());
-        if(maxsize > (size_t)(client->writebuffersize))
+        // Get at least twice as much space
+        client->writebuffersize = MAX((size_t)(client->writebuffersize * 2),
+                                        maxsize);
+        // Did we hit the limit (or overflow and become negative)?
+        if((client->writebuffersize >= PLAYERXDR_MAX_MESSAGE_SIZE) ||
+           (client->writebuffersize < 0))
         {
-          // Get at least twice as much space
-          client->writebuffersize = MAX((size_t)(client->writebuffersize * 2),
-                                        maxsize);
-          // Did we hit the limit (or overflow and become negative)?
-          if((client->writebuffersize >= PLAYERXDR_MAX_MESSAGE_SIZE) ||
-             (client->writebuffersize < 0))
-          {
-            PLAYER_WARN1("allocating maximum %d bytes to outgoing message 
buffer",
+          PLAYER_WARN1("allocating maximum %d bytes to outgoing message 
buffer",
                          PLAYERXDR_MAX_MESSAGE_SIZE);
-            client->writebuffersize = PLAYERXDR_MAX_MESSAGE_SIZE;
-          }
-          client->writebuffer = (char*)realloc(client->writebuffer,
+          client->writebuffersize = PLAYERXDR_MAX_MESSAGE_SIZE;
+        }
+        client->writebuffer = (char*)realloc(client->writebuffer,
                                                client->writebuffersize);
-          assert(client->writebuffer);
-          memset(client->writebuffer, 0, client->writebuffersize);
-        }
+        assert(client->writebuffer);
+        memset(client->writebuffer, 0, client->writebuffersize);
+      }
 
-        // HACK: special handling for map data to compress it before sending
-        // them out over the network.
-        if((hdr.addr.interf == PLAYER_MAP_CODE) &&
-           (hdr.type == PLAYER_MSGTYPE_RESP_ACK) &&
-           (hdr.subtype == PLAYER_MAP_REQ_GET_DATA))
-        {
+      // HACK: special handling for map data to compress it before sending
+      // them out over the network.
+      if((hdr.addr.interf == PLAYER_MAP_CODE) &&
+         (hdr.type == PLAYER_MSGTYPE_RESP_ACK) &&
+         (hdr.subtype == PLAYER_MAP_REQ_GET_DATA))
+      {
 #if HAVE_Z
-          player_map_data_t* raw_data = (player_map_data_t*)payload;
-          zipped_data = 
(player_map_data_t*)calloc(1,sizeof(player_map_data_t));
-          assert(zipped_data);
+        player_map_data_t* raw_data = (player_map_data_t*)payload;
+        zipped_data = (player_map_data_t*)calloc(1,sizeof(player_map_data_t));
+        assert(zipped_data);
 
-          // copy the metadata
-          *zipped_data = *raw_data;
-          uLongf count = compressBound(raw_data->data_count);
-          zipped_data->data = (int8_t*)malloc(count);
+        // copy the metadata
+        *zipped_data = *raw_data;
+        uLongf count = compressBound(raw_data->data_count);
+        zipped_data->data = (int8_t*)malloc(count);
 
-          // compress the tile
-          int ret;
-          ret = compress((Bytef*)zipped_data->data,&count,
+        // compress the tile
+        int ret;
+        ret = compress((Bytef*)zipped_data->data,&count,
                          (const Bytef*)raw_data->data, raw_data->data_count);
-          if((ret != Z_OK) && (ret != Z_STREAM_END))
-          {
-            PLAYER_ERROR("failed to compress map data");
-            free(zipped_data);
-            client->writebufferlen = 0;
-            delete msg;
-            return(0);
-          }
+        if((ret != Z_OK) && (ret != Z_STREAM_END))
+        {
+          PLAYER_ERROR("failed to compress map data");
+          free(zipped_data);
+          client->writebufferlen = 0;
+          delete msg;
+          return(0);
+        }
 
-          zipped_data->data_count = count;
+        zipped_data->data_count = count;
 
-          // swap the payload pointer to point at the zipped version
-          payload = (void*)zipped_data;
+        // swap the payload pointer to point at the zipped version
+        payload = (void*)zipped_data;
 #else
-          PLAYER_WARN("not compressing map data, because zlib was not found at 
compile time");
+        PLAYER_WARN("not compressing map data, because zlib was not found at 
compile time");
 #endif
+      }
+
+      if (payload)
+      {
+        // Locate the appropriate packing function
+        if(!(packfunc = playerxdr_get_packfunc(hdr.addr.interf,
+                                               hdr.type, hdr.subtype)))
+        {
+          // TODO: Allow the user to register a callback to handle unsupported 
messages
+          PLAYER_WARN4("skipping message from %s:%u with unsupported type 
%s:%u",
+                           interf_to_str(hdr.addr.interf), hdr.addr.index, 
msgtype_to_str(hdr.type), hdr.subtype);
         }
-
-        if (payload)
-          {
+        else
+        {
           // Encode the body first
           if((encode_msglen =
               (*packfunc)(client->writebuffer + PLAYERXDR_MSGHDR_SIZE,
@@ -706,33 +709,34 @@
             return(0);
           }
         }
-        else
-        {
-          encode_msglen = 0;
-        }
-        // Rewrite the size in the header with the length of the encoded
-        // body, then encode the header.
-        hdr.size = encode_msglen;
-        if((encode_msglen = player_msghdr_pack(client->writebuffer,
+      }
+      else
+      {
+        encode_msglen = 0;
+      }
+      // Rewrite the size in the header with the length of the encoded
+      // body, then encode the header.
+      hdr.size = encode_msglen;
+      if((encode_msglen = player_msghdr_pack(client->writebuffer,
                    PLAYERXDR_MSGHDR_SIZE, &hdr,
                    PLAYERXDR_ENCODE)) < 0)
+      {
+        PLAYER_ERROR("failed to encode msg header");
+#if HAVE_Z
+        if(zipped_data)
         {
-          PLAYER_ERROR("failed to encode msg header");
-#if HAVE_Z
-          if(zipped_data)
-          {
-            free(zipped_data->data);
-            free(zipped_data);
-            zipped_data=NULL;
-          }
+          free(zipped_data->data);
+          free(zipped_data);
+          zipped_data=NULL;
+        }
 #endif
-          client->writebufferlen = 0;
-          delete msg;
-          return(0);
-        }
+        client->writebufferlen = 0;
+        delete msg;
+        return(0);
+      }
 
-        client->writebufferlen = PLAYERXDR_MSGHDR_SIZE + hdr.size;
-      }
+      client->writebufferlen = PLAYERXDR_MSGHDR_SIZE + hdr.size;
+
       delete msg;
 #if HAVE_Z
       if(zipped_data)

Modified: code/player/trunk/libplayertcp/playerudp.cc
===================================================================
--- code/player/trunk/libplayertcp/playerudp.cc 2009-01-24 01:07:34 UTC (rev 
7294)
+++ code/player/trunk/libplayertcp/playerudp.cc 2009-01-24 02:25:59 UTC (rev 
7295)
@@ -517,8 +517,6 @@
   player_map_data_t* zipped_data=NULL;
 #endif
 
-  //printf("WriteClient(%d)\n", cli);
-
   client = this->clients + cli;
   for(;;)
   {
@@ -562,120 +560,128 @@
       // instances of the message on other queues.
       hdr = *msg->GetHeader();
       payload = msg->GetPayload();
-      // Locate the appropriate packing function
-      if(!(packfunc = playerxdr_get_packfunc(hdr.addr.interf,
-                                         hdr.type, hdr.subtype)))
+
+      // Make sure there's room in the buffer for the encoded messsage.
+      // 4 times the message (including dynamic data) is a safe upper bound
+      size_t maxsize = PLAYERXDR_MSGHDR_SIZE + (4 * msg->GetDataSize());
+      if(maxsize > (size_t)(client->writebuffersize))
       {
-        // TODO: Allow the user to register a callback to handle unsupported
-        // messages
-        PLAYER_WARN4("skipping message from %s:%u with unsupported type %s:%u",
-                     interf_to_str(hdr.addr.interf), hdr.addr.index, 
msgtype_to_str(hdr.type), hdr.subtype);
-      }
-      else
-      {
-        // Make sure there's room in the buffer for the encoded messsage.
-        // 4 times the message is a safe upper bound
-        size_t maxsize = PLAYERXDR_MSGHDR_SIZE + (4 * msg->GetDataSize());
-        if(maxsize > (size_t)(client->writebuffersize))
+        // Get at least twice as much space
+        client->writebuffersize = MAX((size_t)(client->writebuffersize * 2),
+                                        maxsize);
+        // Did we hit the limit (or overflow and become negative)?
+        if((client->writebuffersize >= PLAYERXDR_MAX_MESSAGE_SIZE) ||
+           (client->writebuffersize < 0))
         {
-          // Get at least twice as much space
-          client->writebuffersize = MAX((size_t)(client->writebuffersize * 2),
-                                        maxsize);
-          // Did we hit the limit (or overflow and become negative)?
-          if((client->writebuffersize >= PLAYERXDR_MAX_MESSAGE_SIZE) ||
-             (client->writebuffersize < 0))
-          {
-            PLAYER_WARN1("allocating maximum %d bytes to outgoing message 
buffer",
+          PLAYER_WARN1("allocating maximum %d bytes to outgoing message 
buffer",
                          PLAYERXDR_MAX_MESSAGE_SIZE);
-            client->writebuffersize = PLAYERXDR_MAX_MESSAGE_SIZE;
-          }
-          client->writebuffer = (char*)realloc(client->writebuffer,
+          client->writebuffersize = PLAYERXDR_MAX_MESSAGE_SIZE;
+        }
+        client->writebuffer = (char*)realloc(client->writebuffer,
                                                client->writebuffersize);
-          assert(client->writebuffer);
-          memset(client->writebuffer, 0, client->writebuffersize);
-        }
+        assert(client->writebuffer);
+        memset(client->writebuffer, 0, client->writebuffersize);
+      }
 
-        // HACK: special handling for map data to compress it before sending
-        // them out over the network.
-        if((hdr.addr.interf == PLAYER_MAP_CODE) &&
-           (hdr.type == PLAYER_MSGTYPE_RESP_ACK) &&
-           (hdr.subtype == PLAYER_MAP_REQ_GET_DATA))
-        {
+      // HACK: special handling for map data to compress it before sending
+      // them out over the network.
+      if((hdr.addr.interf == PLAYER_MAP_CODE) &&
+         (hdr.type == PLAYER_MSGTYPE_RESP_ACK) &&
+         (hdr.subtype == PLAYER_MAP_REQ_GET_DATA))
+      {
 #if HAVE_Z
-            player_map_data_t* raw_data = (player_map_data_t*)payload;
-            zipped_data = 
(player_map_data_t*)calloc(1,sizeof(player_map_data_t));
-            assert(zipped_data);
+        player_map_data_t* raw_data = (player_map_data_t*)payload;
+        zipped_data = (player_map_data_t*)calloc(1,sizeof(player_map_data_t));
+        assert(zipped_data);
 
-            // copy the metadata
-            *zipped_data = *raw_data;
-            uLongf count = compressBound(raw_data->data_count);
-            zipped_data->data = (int8_t*)malloc(count);
+        // copy the metadata
+        *zipped_data = *raw_data;
+        uLongf count = compressBound(raw_data->data_count);
+        zipped_data->data = (int8_t*)malloc(count);
 
-            // compress the tile
-            if(compress((Bytef*)zipped_data->data,&count,
-                        (const Bytef*)raw_data->data, raw_data->data_count) != 
Z_OK)
-            {
-              PLAYER_ERROR("failed to compress map data");
-              free(zipped_data);
-              client->writebufferlen = 0;
-              delete msg;
-              return(0);
-            }
+        // compress the tile
+        int ret;
+        ret = compress((Bytef*)zipped_data->data,&count,
+                         (const Bytef*)raw_data->data, raw_data->data_count);
+        if((ret != Z_OK) && (ret != Z_STREAM_END))
+        {
+          PLAYER_ERROR("failed to compress map data");
+          free(zipped_data);
+          client->writebufferlen = 0;
+          delete msg;
+          return(0);
+        }
 
-            zipped_data->data_count = count;
+        zipped_data->data_count = count;
 
-            // swap the payload pointer to point at the zipped version
-            payload = (void*)zipped_data;
+        // swap the payload pointer to point at the zipped version
+        payload = (void*)zipped_data;
 #else
-          PLAYER_WARN("not compressing map data, because zlib was not found at 
compile time");
+        PLAYER_WARN("not compressing map data, because zlib was not found at 
compile time");
 #endif
+      }
+
+      if (payload)
+      {
+        // Locate the appropriate packing function
+        if(!(packfunc = playerxdr_get_packfunc(hdr.addr.interf,
+                                               hdr.type, hdr.subtype)))
+        {
+          // TODO: Allow the user to register a callback to handle unsupported 
messages
+          PLAYER_WARN4("skipping message from %s:%u with unsupported type 
%s:%u",
+                           interf_to_str(hdr.addr.interf), hdr.addr.index, 
msgtype_to_str(hdr.type), hdr.subtype);
         }
-
-        // Encode the body first
-        if((encode_msglen =
-            (*packfunc)(client->writebuffer + PLAYERXDR_MSGHDR_SIZE,
+        else
+        {
+          // Encode the body first
+          if((encode_msglen =
+              (*packfunc)(client->writebuffer + PLAYERXDR_MSGHDR_SIZE,
                         maxsize - PLAYERXDR_MSGHDR_SIZE,
                         payload, PLAYERXDR_ENCODE)) < 0)
-        {
-          PLAYER_WARN4("encoding failed on message from %s:%u with type %s:%u",
+          {
+            PLAYER_WARN4("encoding failed on message from %s:%u with type 
%s:%u",
                        interf_to_str(hdr.addr.interf), hdr.addr.index, 
msgtype_to_str(hdr.type), hdr.subtype);
 #if HAVE_Z
-          if(zipped_data)
-          {
-            free(zipped_data->data);
-            free(zipped_data);
-            zipped_data=NULL;
+            if(zipped_data)
+            {
+              free(zipped_data->data);
+              free(zipped_data);
+              zipped_data=NULL;
+            }
+#endif
+            client->writebufferlen = 0;
+            delete msg;
+            return(0);
           }
-#endif
-          client->writebufferlen = 0;
-          delete msg;
-          return(0);
         }
-
-        // Rewrite the size in the header with the length of the encoded
-        // body, then encode the header.
-        hdr.size = encode_msglen;
-       if((encode_msglen =
-           player_msghdr_pack(client->writebuffer,
-                              PLAYERXDR_MSGHDR_SIZE, &hdr,
-                              PLAYERXDR_ENCODE)) < 0)
+      }
+      else
+      {
+        encode_msglen = 0;
+      }
+      // Rewrite the size in the header with the length of the encoded
+      // body, then encode the header.
+      hdr.size = encode_msglen;
+      if((encode_msglen = player_msghdr_pack(client->writebuffer,
+                   PLAYERXDR_MSGHDR_SIZE, &hdr,
+                   PLAYERXDR_ENCODE)) < 0)
+      {
+        PLAYER_ERROR("failed to encode msg header");
+#if HAVE_Z
+        if(zipped_data)
         {
-          PLAYER_ERROR("failed to encode msg header");
-#if HAVE_Z
-          if(zipped_data)
-          {
-            free(zipped_data->data);
-            free(zipped_data);
-            zipped_data=NULL;
-          }
+          free(zipped_data->data);
+          free(zipped_data);
+          zipped_data=NULL;
+        }
 #endif
-          client->writebufferlen = 0;
-          delete msg;
-          return(0);
-        }
+        client->writebufferlen = 0;
+        delete msg;
+        return(0);
+      }
 
-        client->writebufferlen = PLAYERXDR_MSGHDR_SIZE + hdr.size;
-      }
+      client->writebufferlen = PLAYERXDR_MSGHDR_SIZE + hdr.size;
+
       delete msg;
 #if HAVE_Z
       if(zipped_data)
@@ -687,10 +693,7 @@
 #endif
     }
     else
-    {
-      //puts("no messages");
       return(0);
-    }
   }
 }
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to