Author: sveinung
Date: Fri Nov 27 10:41:07 2015
New Revision: 30802

URL: http://svn.gna.org/viewcvs/freeciv?rev=30802&view=rev
Log:
JSON protocol: encode memory as a JSON array

This replaces sending each memory byte as a separate field with the byte
number added to the original field name.

This changes the JSON network protocol but not the binary protocol.

See patch #6634

Modified:
    trunk/common/dataio_json.c
    trunk/common/dataio_json.h

Modified: trunk/common/dataio_json.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/dataio_json.c?rev=30802&r1=30801&r2=30802&view=diff
==============================================================================
--- trunk/common/dataio_json.c  (original)
+++ trunk/common/dataio_json.c  Fri Nov 27 10:41:07 2015
@@ -555,23 +555,26 @@
   Send block of memory as byte array
 **************************************************************************/
 void dio_put_memory_json(struct json_data_out *dout, char *key,
-                         const struct plocation* location,
+                         struct plocation* location,
                          const void *value,
                          size_t size)
 {
   int i;
   char fullkey[512];
-  struct plocation ploc;
-
-  /* TODO: Should probably be a JSON array. */
-  ploc = *plocation_field_new(NULL);
+
+  dio_put_farray_json(dout, key, location, size);
+
+  location->sub_location = plocation_elem_new(0);
 
   for (i = 0; i < size; i++) {
     fc_snprintf(fullkey, sizeof(fullkey), "%s_%d", key, i);
-    ploc.name = fullkey;
-
-    dio_put_uint8_json(dout, fullkey, &ploc, ((unsigned char *)value)[i]);
-  }
+    location->sub_location->number = i;
+
+    dio_put_uint8_json(dout, fullkey, location,
+                       ((unsigned char *)value)[i]);
+  }
+
+  FC_FREE(location->sub_location);
 }
 
 /**************************************************************************
@@ -772,28 +775,29 @@
   Receive block of memory as byte array
 **************************************************************************/
 bool dio_get_memory_json(json_t *json_packet, char *key,
-                         const struct plocation* location,
+                         struct plocation* location,
                          void *dest, size_t dest_size)
 {
    int i;
   char fullkey[512];
-  struct plocation ploc;
-
-  /* TODO: Should probably be a JSON array. */
-  ploc = *plocation_field_new(NULL);
+
+  location->sub_location = plocation_elem_new(0);
 
   for (i = 0; i < dest_size; i++) {
     int val;
 
     fc_snprintf(fullkey, sizeof(fullkey), "%s_%d", key, i);
-    ploc.name = fullkey;
-
-    if (!dio_get_uint8_json(json_packet, fullkey, &ploc, &val)) {
+    location->sub_location->number = i;
+
+    if (!dio_get_uint8_json(json_packet, fullkey, location, &val)) {
+      free(location->sub_location);
       return FALSE;
     }
     ((unsigned char *)dest)[i] = val;
   }
 
+  FC_FREE(location->sub_location);
+
   return TRUE;
 }
 

Modified: trunk/common/dataio_json.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/dataio_json.h?rev=30802&r1=30801&r2=30802&view=diff
==============================================================================
--- trunk/common/dataio_json.h  (original)
+++ trunk/common/dataio_json.h  Fri Nov 27 10:41:07 2015
@@ -72,7 +72,7 @@
                          const struct plocation* location,
                          float *dest, int float_factor);
 bool dio_get_memory_json(json_t *json_packet, char *key,
-                         const struct plocation* location,
+                         struct plocation* location,
                          void *dest, size_t dest_size);
 bool dio_get_string_json(json_t *json_packet, char *key,
                          const struct plocation* location,
@@ -140,7 +140,7 @@
                          float value, int float_factor);
 
 void dio_put_memory_json(struct json_data_out *dout, char *key,
-                         const struct plocation* location,
+                         struct plocation* location,
                          const void *value, size_t size);
 void dio_put_string_json(struct json_data_out *dout, char *key,
                          const struct plocation* location,


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to