Skip to content

Commit

Permalink
Add dataio arraylen type
Browse files Browse the repository at this point in the history
In JSON mode this is just encoded in the length of an array rather than needing extra space

See RM #466

Signed-off-by: Alina Lenk <[email protected]>
  • Loading branch information
alien-valkyrie committed Apr 17, 2024
1 parent 2a35f8a commit 488b18b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions common/networking/dataio_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,29 @@ bool dio_get_worklist_json(struct connection *pc, struct data_in *din,
return TRUE;
}

/**********************************************************************//**
Receive array length value to dest with json. In json mode, this will
simply read the length of the array at that location.
**************************************************************************/
bool dio_get_arraylen_json(struct connection *pc, struct data_in *din,
const struct plocation *location, int *dest)
{
if (pc->json_mode) {
const json_t *arr = plocation_read_data(pc->json_packet, location);

if (!json_is_array(arr)) {
log_packet("Not an array");
return FALSE;
}

*dest = json_array_size(arr);
} else {
return dio_get_arraylen_raw(din, dest);
}

return TRUE;
}

/**********************************************************************//**
Receive vector of 8 bit values, terminated by stop_value.
**************************************************************************/
Expand Down Expand Up @@ -1002,6 +1025,24 @@ int dio_put_sfloat_json(struct json_data_out *dout,
return e;
}

/**********************************************************************//**
Insert array length. In json mode, this will create an array at that
location.
**************************************************************************/
int dio_put_arraylen_json(struct json_data_out *dout,
const struct plocation *location, int size)
{
int e;

if (dout->json) {
e = dio_put_farray_json(dout, location, size);
} else {
e = dio_put_arraylen_raw(&dout->raw, size);
}

return e;
}

/**********************************************************************//**
Insert vector of uint8 values, terminated by stop_value.
**************************************************************************/
Expand Down
5 changes: 5 additions & 0 deletions common/networking/dataio_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ bool dio_get_action_probability_json(struct connection *pc, struct data_in *din,
struct act_prob *prob)
fc__attribute((nonnull (4)));

bool dio_get_arraylen_json(struct connection *pc, struct data_in *din,
const struct plocation *location, int *dest)
fc__attribute((nonnull (4)));
bool dio_get_uint8_vec8_json(struct connection *pc, struct data_in *din,
const struct plocation *location,
int **values, int stop_value)
Expand Down Expand Up @@ -180,6 +183,8 @@ int dio_put_action_probability_json(struct json_data_out *dout,
const struct plocation *location,
const struct act_prob *prob);

int dio_put_arraylen_json(struct json_data_out *dout,
const struct plocation *location, int size);
int dio_put_uint8_vec8_json(struct json_data_out *dout,
const struct plocation *location,
int *values, int stop_value);
Expand Down
4 changes: 4 additions & 0 deletions common/networking/dataio_raw.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
#define dio_get_estring_raw dio_get_string_raw
#define dio_put_estring_raw dio_put_string_raw

/* In the binary protocol, just encode the array lengths. */
#define dio_get_arraylen_raw dio_get_uint16_raw
#define dio_put_arraylen_raw dio_put_uint16_raw

#ifndef FREECIV_JSON_CONNECTION

/* Should be a function but we need some macro magic. */
Expand Down

0 comments on commit 488b18b

Please sign in to comment.