Project

General

Profile

Feature #2071 » 0029-Rename-dout-as-d_out.patch

Marko Lindqvist, 07/09/2026 12:39 AM

View differences:

client/agents/cma_core.c
const struct cm_parameter *parameter)
{
char buffer[SAVED_PARAMETER_SIZE];
struct raw_data_out dout;
struct raw_data_out d_out;
/* Changing this function is likely to break compatibility with old
* savegames that store these values. */
dio_output_init(&dout, buffer, sizeof(buffer));
dio_output_init(&d_out, buffer, sizeof(buffer));
dio_put_uint8_raw(&dout, CMA_ATTR_VERSION);
dio_put_uint8_raw(&d_out, CMA_ATTR_VERSION);
output_type_iterate(i) {
dio_put_sint16_raw(&dout, parameter->minimal_surplus[i]);
dio_put_sint16_raw(&dout, parameter->factor[i]);
dio_put_sint16_raw(&d_out, parameter->minimal_surplus[i]);
dio_put_sint16_raw(&d_out, parameter->factor[i]);
} output_type_iterate_end;
dio_put_sint16_raw(&dout, parameter->happy_factor);
dio_put_uint8_raw(&dout, 0); /* Dummy value; used to be factor_target. */
dio_put_bool8_raw(&dout, parameter->require_happy);
dio_put_bool8_raw(&dout, parameter->max_growth);
dio_put_sint16_raw(&d_out, parameter->happy_factor);
dio_put_uint8_raw(&d_out, 0); /* Dummy value; used to be factor_target. */
dio_put_bool8_raw(&d_out, parameter->require_happy);
dio_put_bool8_raw(&d_out, parameter->max_growth);
fc_assert(dio_output_used(&dout) == SAVED_PARAMETER_SIZE);
fc_assert(dio_output_used(&d_out) == SAVED_PARAMETER_SIZE);
attr_city_set(attr, city_id, SAVED_PARAMETER_SIZE, buffer);
}
client/attribute.c
const size_t entries = attribute_hash_size(hash);
int total_length, value_lengths[entries];
void *result;
struct raw_data_out dout;
struct raw_data_out d_out;
int i;
/*
......
* Step 2: Allocate memory.
*/
result = fc_malloc(total_length);
dio_output_init(&dout, result, total_length);
dio_output_init(&d_out, result, total_length);
/*
* Step 3: Fill out the preamble.
*/
dio_put_uint32_raw(&dout, 0);
dio_put_uint8_raw(&dout, 2);
dio_put_uint32_raw(&dout, attribute_hash_size(hash));
dio_put_uint32_raw(&dout, total_length);
dio_put_uint32_raw(&d_out, 0);
dio_put_uint8_raw(&d_out, 2);
dio_put_uint32_raw(&d_out, attribute_hash_size(hash));
dio_put_uint32_raw(&d_out, total_length);
/*
* Step 4: Fill out the body.
*/
i = 0;
attribute_hash_iterate(hash, pkey, pvalue) {
dio_put_uint32_raw(&dout, value_lengths[i]);
dio_put_uint32_raw(&d_out, value_lengths[i]);
dio_put_uint32_raw(&dout, pkey->key);
dio_put_uint32_raw(&dout, pkey->id);
dio_put_sint16_raw(&dout, pkey->x);
dio_put_sint16_raw(&dout, pkey->y);
dio_put_uint32_raw(&d_out, pkey->key);
dio_put_uint32_raw(&d_out, pkey->id);
dio_put_sint16_raw(&d_out, pkey->x);
dio_put_sint16_raw(&d_out, pkey->y);
dio_put_memory_raw(&dout, ADD_TO_POINTER(pvalue, 4), value_lengths[i]);
dio_put_memory_raw(&d_out, ADD_TO_POINTER(pvalue, 4), value_lengths[i]);
i++;
} attribute_hash_iterate_end;
fc_assert(i == entries);
fc_assert(!dout.too_short);
fc_assert_msg(dio_output_used(&dout) == total_length,
fc_assert(!d_out.too_short);
fc_assert_msg(dio_output_used(&d_out) == total_length,
"serialize_hash() total_length = %lu, actual = %lu",
(long unsigned)total_length,
(long unsigned)dio_output_used(&dout));
(long unsigned)dio_output_used(&d_out));
/*
* Step 5: Return.
......
struct attr_key key;
void *pvalue;
int value_length;
struct raw_data_out dout;
struct raw_data_out d_out;
if (!dio_get_uint32_raw(&din, &value_length)) {
log_verbose("attribute.c unserialize_hash() "
......
}
pvalue = fc_malloc(value_length + 4);
dio_output_init(&dout, pvalue, value_length + 4);
dio_put_uint32_raw(&dout, value_length);
dio_output_init(&d_out, pvalue, value_length + 4);
dio_put_uint32_raw(&d_out, value_length);
if (!dio_get_memory_raw(&din, ADD_TO_POINTER(pvalue, 4), value_length)) {
log_verbose("attribute.c unserialize_hash() "
"memory dio_input_too_short");
......
if (0 != data_length) {
void *pvalue = fc_malloc(data_length + 4);
struct raw_data_out dout;
struct raw_data_out d_out;
dio_output_init(&dout, pvalue, data_length + 4);
dio_put_uint32_raw(&dout, data_length);
dio_put_memory_raw(&dout, data, data_length);
dio_output_init(&d_out, pvalue, data_length + 4);
dio_put_uint32_raw(&d_out, data_length);
dio_put_memory_raw(&d_out, data, data_length);
attribute_hash_replace(attribute_hash, &akey, pvalue);
} else {
client/servers.c
static bool begin_lanserver_scan(struct server_scan *scan)
{
union fc_sockaddr addr;
struct raw_data_out dout;
struct raw_data_out d_out;
int send_sock, opt = 1;
#ifndef FREECIV_HAVE_WINSOCK
unsigned char buffer[MAX_LEN_PACKET];
......
return FALSE;
}
dio_output_init(&dout, buffer, sizeof(buffer));
dio_put_uint8_raw(&dout, SERVER_LAN_VERSION);
size = dio_output_used(&dout);
dio_output_init(&d_out, buffer, sizeof(buffer));
dio_put_uint8_raw(&d_out, SERVER_LAN_VERSION);
size = dio_output_used(&d_out);
if (sendto(send_sock, buffer, size, 0, &addr.saddr,
sockaddr_size(&addr)) < 0) {
common/generate_packets.py
if self.constant:
return f"""\
#if {self.declared} <= MAX_UINT8
e |= DIO_PUT(uint8, &dout, &field_addr, {index});
e |= DIO_PUT(uint8, &d_out, &field_addr, {index});
#else
e |= DIO_PUT(uint16, &dout, &field_addr, {index});
e |= DIO_PUT(uint16, &d_out, &field_addr, {index});
#endif
"""
else:
return f"""\
if ({self.actual @ packet} <= MAX_UINT8) {{
e |= DIO_PUT(uint8, &dout, &field_addr, {index});
e |= DIO_PUT(uint8, &d_out, &field_addr, {index});
}} else {{
e |= DIO_PUT(uint16, &dout, &field_addr, {index});
e |= DIO_PUT(uint16, &d_out, &field_addr, {index});
}}
"""
......
def get_code_put(self, location: Location, packet: str, diff_packet: "str | None" = None) -> str:
return f"""\
e |= DIO_PUT({self.dataio_type}, &dout, &field_addr, {location @ packet});
e |= DIO_PUT({self.dataio_type}, &d_out, &field_addr, {location @ packet});
"""
def get_code_get(self, location: Location, packet: str, deep_diff: bool = False) -> str:
......
def get_code_put(self, location: Location, packet: str, diff_packet: "str | None" = None) -> str:
return f"""\
e |= DIO_PUT({self.dataio_type}, &dout, &field_addr, {location @ packet}, {self.float_factor:d});
e |= DIO_PUT({self.dataio_type}, &d_out, &field_addr, {location @ packet}, {self.float_factor:d});
"""
def get_code_get(self, location: Location, packet: str, deep_diff: bool = False) -> str:
......
def get_code_put(self, location: Location, packet: str, diff_packet: "str | None" = None) -> str:
return f"""\
e |= DIO_BV_PUT(&dout, &field_addr, {location @ packet});
e |= DIO_BV_PUT(&d_out, &field_addr, {location @ packet});
"""
def get_code_get(self, location: Location, packet: str, deep_diff: bool = False) -> str:
......
def get_code_put(self, location: Location, packet: str, diff_packet: "str | None" = None) -> str:
return f"""\
e |= DIO_PUT({self.dataio_type}, &dout, &field_addr, &{location @ packet});
e |= DIO_PUT({self.dataio_type}, &d_out, &field_addr, &{location @ packet});
"""
DEFAULT_REGISTRY.public_patterns[StructType.TYPE_PATTERN] = StructType
......
def get_code_put(self, location: Location, packet: str, diff_packet: "str | None" = None) -> str:
return f"""\
e |= DIO_PUT({self.dataio_type}, &dout, &field_addr, &{location @ packet}, {self.size.actual @ packet});
e |= DIO_PUT({self.dataio_type}, &d_out, &field_addr, &{location @ packet}, {self.size.actual @ packet});
"""
def get_code_get(self, location: Location, packet: str, deep_diff: bool = False) -> str:
......
# which we're a long way from
size_part = f"""\
fc_assert({size.actual @ packet} < MAX_UINT16);
e |= DIO_PUT(arraylen, &dout, &field_addr, {size.actual @ packet});
e |= DIO_PUT(arraylen, &d_out, &field_addr, {size.actual @ packet});
#ifdef FREECIV_JSON_CONNECTION
"""
......
size_part = f"""\
#ifdef FREECIV_JSON_CONNECTION
/* Create the array. */
e |= DIO_PUT(farray, &dout, &field_addr, {size.actual @ packet});
e |= DIO_PUT(farray, &d_out, &field_addr, {size.actual @ packet});
"""
......
head = f"""\
if ({null_condition @ packet}) {{
/* Transmit null as empty */
e |= DIO_PUT(arraylen, &dout, &field_addr, 0);
e |= DIO_PUT(arraylen, &d_out, &field_addr, 0);
}} else """
else:
# ends mid-line
......
/* Transmit null as empty */
#ifdef FREECIV_JSON_CONNECTION
/* Create the array. */
e |= DIO_PUT(farray, &dout, &field_addr, 0);
e |= DIO_PUT(farray, &d_out, &field_addr, 0);
#endif /* FREECIV_JSON_CONNECTION */
}} else """
......
size_head = f"""\
/* Create the object to hold new size and delta. */
e |= DIO_PUT(object, &dout, &field_addr);
e |= DIO_PUT(object, &d_out, &field_addr);
/* Enter object (start at size address). */
{location.json_subloc} = plocation_field_new("size");
#endif /* FREECIV_JSON_CONNECTION */
/* Write the new size */
e |= DIO_PUT(uint16, &dout, &field_addr, {safe_size.actual @ packet});
e |= DIO_PUT(uint16, &d_out, &field_addr, {safe_size.actual @ packet});
#ifdef FREECIV_JSON_CONNECTION
/* Delta address. */
......
{size_head}\
/* Create the array. */
e |= DIO_PUT(farray, &dout, &field_addr, 0);
e |= DIO_PUT(farray, &d_out, &field_addr, 0);
/* Enter array. */
{location.json_subloc} = plocation_elem_new(0);
......
{location.json_subloc}->number = -1;
/* Create the diff array element. */
e |= DIO_PUT(object, &dout, &field_addr);
e |= DIO_PUT(object, &d_out, &field_addr);
/* Enter diff array element (start at the index address). */
{location.json_subloc}->number = count_{location.index}++;
......
{location.json_subloc}->number = -1;
/* Create the terminating diff array element. */
e |= DIO_PUT(object, &dout, &field_addr);
e |= DIO_PUT(object, &d_out, &field_addr);
/* Enter diff array element (start at the index address). */
{location.json_subloc}->number = count_{location.index};
......
{{
const char *pstr = strvec_get({location @ packet}, {location.index});
e |= DIO_PUT({self.dataio_type}, &dout, &field_addr, pstr ? pstr : "");
e |= DIO_PUT({self.dataio_type}, &d_out, &field_addr, pstr ? pstr : "");
}}
"""
......
field_addr.name = "fields";
#endif /* FREECIV_JSON_CONNECTION */
e = 0;
e |= DIO_BV_PUT(&dout, &field_addr, fields);
e |= DIO_BV_PUT(&d_out, &field_addr, fields);
if (e) {{
log_packet_detailed("fields bitvector error detected");
}}
common/networking/dataio_json.c
/**********************************************************************//**
Insert 8 bit value with json.
**************************************************************************/
int dio_put_uint8_json(struct json_data_out *dout,
int dio_put_uint8_json(struct json_data_out *d_out,
const struct plocation *location,
int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_uint8_raw(&dout->raw, value);
e = dio_put_uint8_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert 8 bit value with json.
**************************************************************************/
int dio_put_sint8_json(struct json_data_out *dout,
int dio_put_sint8_json(struct json_data_out *d_out,
const struct plocation *location,
int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_sint8_raw(&dout->raw, value);
e = dio_put_sint8_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert value using 32 bits. May overflow.
**************************************************************************/
int dio_put_uint16_json(struct json_data_out *dout,
int dio_put_uint16_json(struct json_data_out *d_out,
const struct plocation *location, int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_uint16_raw(&dout->raw, value);
e = dio_put_uint16_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert value using 32 bits. May overflow.
**************************************************************************/
int dio_put_sint16_json(struct json_data_out *dout,
int dio_put_sint16_json(struct json_data_out *d_out,
const struct plocation *location, int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_sint16_raw(&dout->raw, value);
e = dio_put_sint16_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert the given cm_parameter struct
**************************************************************************/
int dio_put_cm_parameter_json(struct json_data_out *dout,
int dio_put_cm_parameter_json(struct json_data_out *d_out,
struct plocation *location,
const struct cm_parameter *param)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
json_t *obj = json_object();
json_t *min_surplus = json_array();
json_t *factor = json_array();
......
json_boolean(param->allow_specialists));
e |= json_object_set_new(obj, "happy_factor",
json_integer(param->happy_factor));
e |= plocation_write_data(dout->json, location, obj);
e |= plocation_write_data(d_out->json, location, obj);
} else {
e = dio_put_cm_parameter_raw(&dout->raw, param);
e = dio_put_cm_parameter_raw(&d_out->raw, param);
}
return e;
......
/**********************************************************************//**
Insert the given unit_order struct
**************************************************************************/
int dio_put_unit_order_json(struct json_data_out *dout,
int dio_put_unit_order_json(struct json_data_out *d_out,
struct plocation *location,
const struct unit_order *order)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
json_t *obj = json_object();
e |= json_object_set_new(obj, "order", json_integer(order->order));
......
} else {
e |= json_object_set_new(obj, "dir", json_integer(order->dir));
}
e |= plocation_write_data(dout->json, location, obj);
e |= plocation_write_data(d_out->json, location, obj);
} else {
e = dio_put_unit_order_raw(&dout->raw, order);
e = dio_put_unit_order_raw(&d_out->raw, order);
}
return e;
......
/**********************************************************************//**
Insert worklist information.
**************************************************************************/
int dio_put_worklist_json(struct json_data_out *dout,
int dio_put_worklist_json(struct json_data_out *d_out,
struct plocation *location,
const struct worklist *pwl)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
int i;
const int size = worklist_length(pwl);
/* Must create the array before insertion. */
e |= dio_put_farray_json(dout, location, size);
e |= dio_put_farray_json(d_out, location, size);
location->sub_location = plocation_elem_new(0);
......
e |= json_object_set_new(universal, "value",
json_integer(universal_number(pcp)));
e |= plocation_write_data(dout->json, location, universal);
e |= plocation_write_data(d_out->json, location, universal);
}
FC_FREE(location->sub_location);
} else {
e = dio_put_worklist_raw(&dout->raw, pwl);
e = dio_put_worklist_raw(&d_out->raw, pwl);
}
return e;
......
int **values, int stop_value)
{
if (pc->json_mode) {
/* TODO: implement */
/* TODO: Implement */
log_warn("Received unimplemeted data type uint8_vec8.");
} else {
return dio_get_uint8_vec8_raw(din, values, stop_value);
......
int stop_value)
{
if (pc->json_mode) {
/* TODO: implement */
/* TODO: Implement */
log_warn("Received unimplemeted data type uint16_vec8.");
} else {
return dio_get_uint16_vec8_raw(din, values, stop_value);
......
/**********************************************************************//**
Create an empty field array.
**************************************************************************/
int dio_put_farray_json(struct json_data_out *dout,
int dio_put_farray_json(struct json_data_out *d_out,
const struct plocation *location, int size)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
int i;
json_t *farray = json_array();
......
e |= json_array_append_new(farray, json_null());
}
e |= plocation_write_data(dout->json, location, farray);
e |= plocation_write_data(d_out->json, location, farray);
} else {
/* No caller needs this */
}
......
/**********************************************************************//**
Create an empty JSON object.
**************************************************************************/
int dio_put_object_json(struct json_data_out *dout,
int dio_put_object_json(struct json_data_out *d_out,
const struct plocation *location)
{
int e = 0;
if (dout->json) {
e |= plocation_write_data(dout->json, location, json_object());
if (d_out->json) {
e |= plocation_write_data(d_out->json, location, json_object());
} else {
/* No caller needs this */
}
......
/**********************************************************************//**
Insert uint32 value.
**************************************************************************/
int dio_put_uint32_json(struct json_data_out *dout,
int dio_put_uint32_json(struct json_data_out *d_out,
const struct plocation *location, int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_uint32_raw(&dout->raw, value);
e = dio_put_uint32_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert sint32 value.
**************************************************************************/
int dio_put_sint32_json(struct json_data_out *dout,
int dio_put_sint32_json(struct json_data_out *d_out,
const struct plocation *location, int value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_integer(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_integer(value));
} else {
e = dio_put_sint32_raw(&dout->raw, value);
e = dio_put_sint32_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert bool value.
**************************************************************************/
int dio_put_bool8_json(struct json_data_out *dout,
int dio_put_bool8_json(struct json_data_out *d_out,
const struct plocation *location, bool value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, value ? json_true() : json_false());
if (d_out->json) {
e = plocation_write_data(d_out->json, location,
value ? json_true() : json_false());
} else {
e = dio_put_bool8_raw(&dout->raw, value);
e = dio_put_bool8_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert bool value.
**************************************************************************/
int dio_put_bool32_json(struct json_data_out *dout,
int dio_put_bool32_json(struct json_data_out *d_out,
const struct plocation *location, bool value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, value ? json_true() : json_false());
if (d_out->json) {
e = plocation_write_data(d_out->json, location,
value ? json_true() : json_false());
} else {
e = dio_put_bool32_raw(&dout->raw, value);
e = dio_put_bool32_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert unsigned floating point value.
**************************************************************************/
int dio_put_ufloat_json(struct json_data_out *dout,
int dio_put_ufloat_json(struct json_data_out *d_out,
const struct plocation *location,
float value, int float_factor)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_real(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_real(value));
} else {
e = dio_put_ufloat_raw(&dout->raw, value, float_factor);
e = dio_put_ufloat_raw(&d_out->raw, value, float_factor);
}
return e;
......
/**********************************************************************//**
Insert signed floating point value.
**************************************************************************/
int dio_put_sfloat_json(struct json_data_out *dout,
int dio_put_sfloat_json(struct json_data_out *d_out,
const struct plocation *location,
float value, int float_factor)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_real(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_real(value));
} else {
e = dio_put_sfloat_raw(&dout->raw, value, float_factor);
e = dio_put_sfloat_raw(&d_out->raw, value, float_factor);
}
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,
int dio_put_arraylen_json(struct json_data_out *d_out,
const struct plocation *location, int size)
{
int e;
if (dout->json) {
e = dio_put_farray_json(dout, location, size);
if (d_out->json) {
e = dio_put_farray_json(d_out, location, size);
} else {
e = dio_put_arraylen_raw(&dout->raw, size);
e = dio_put_arraylen_raw(&d_out->raw, size);
}
return e;
......
/**********************************************************************//**
Insert vector of uint8 values, terminated by stop_value.
**************************************************************************/
int dio_put_uint8_vec8_json(struct json_data_out *dout,
int dio_put_uint8_vec8_json(struct json_data_out *d_out,
const struct plocation *location,
int *values, int stop_value)
{
int e;
if (dout->json) {
/* TODO: implement. */
if (d_out->json) {
/* TODO: Implement. */
log_error("Tried to send unimplemeted data type uint8_vec8.");
e = -1;
} else {
e = dio_put_uint8_vec8_raw(&dout->raw, values, stop_value);
e = dio_put_uint8_vec8_raw(&d_out->raw, values, stop_value);
}
return e;
......
/**********************************************************************//**
Insert vector of uint16 values, terminated by stop_value.
**************************************************************************/
int dio_put_uint16_vec8_json(struct json_data_out *dout,
int dio_put_uint16_vec8_json(struct json_data_out *d_out,
const struct plocation *location, int *values,
int stop_value)
{
int e;
if (dout->json) {
/* TODO: implement. */
if (d_out->json) {
/* TODO: Implement. */
log_error("Tried to send unimplemeted data type uint16_vec8.");
e = -1;
} else {
e = dio_put_uint16_vec8_raw(&dout->raw, values, stop_value);
e = dio_put_uint16_vec8_raw(&d_out->raw, values, stop_value);
}
return e;
......
/**********************************************************************//**
Send block of memory as byte array.
**************************************************************************/
int dio_put_memory_json(struct json_data_out *dout,
int dio_put_memory_json(struct json_data_out *d_out,
struct plocation *location,
const void *value,
size_t size)
{
int e;
if (dout->json) {
if (d_out->json) {
int i;
e = dio_put_farray_json(dout, location, size);
e = dio_put_farray_json(d_out, location, size);
location->sub_location = plocation_elem_new(0);
for (i = 0; i < size; i++) {
location->sub_location->number = i;
e |= dio_put_uint8_json(dout, location,
e |= dio_put_uint8_json(d_out, location,
((unsigned char *)value)[i]);
}
FC_FREE(location->sub_location);
} else {
e = dio_put_memory_raw(&dout->raw, value, size);
e = dio_put_memory_raw(&d_out->raw, value, size);
}
return e;
......
/**********************************************************************//**
Insert nullptr-terminated string.
**************************************************************************/
int dio_put_string_json(struct json_data_out *dout,
int dio_put_string_json(struct json_data_out *d_out,
const struct plocation *location,
const char *value)
{
int e;
if (dout->json) {
e = plocation_write_data(dout->json, location, json_string(value));
if (d_out->json) {
e = plocation_write_data(d_out->json, location, json_string(value));
} else {
e = dio_put_string_raw(&dout->raw, value);
e = dio_put_string_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Encode and write the specified string to the specified location.
**************************************************************************/
int dio_put_estring_json(struct json_data_out *dout,
int dio_put_estring_json(struct json_data_out *d_out,
const struct plocation *location,
const char *value)
{
int e;
if (dout->json) {
if (d_out->json) {
char *escaped_value;
/* Let CURL find the length itself by passing 0 */
escaped_value = curl_easy_escape(get_curl(), value, 0);
/* Handle as a regular string from now on. */
e = dio_put_string_json(dout, location, escaped_value);
e = dio_put_string_json(d_out, location, escaped_value);
/* CURL's memory management wants to free this itself. */
curl_free(escaped_value);
} else {
e = dio_put_estring_raw(&dout->raw, value);
e = dio_put_estring_raw(&d_out->raw, value);
}
return e;
......
/**********************************************************************//**
Insert a single requirement.
**************************************************************************/
int dio_put_requirement_json(struct json_data_out *dout,
int dio_put_requirement_json(struct json_data_out *d_out,
const struct plocation *location,
const struct requirement *preq)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
int kind, range, value;
bool survives, present, quiet;
......
e |= json_object_set_new(requirement, "quiet", json_boolean(quiet));
/* Put the requirement object in the packet. */
e |= plocation_write_data(dout->json, location, requirement);
e |= plocation_write_data(d_out->json, location, requirement);
} else {
e = dio_put_requirement_raw(&dout->raw, preq);
e = dio_put_requirement_raw(&d_out->raw, preq);
}
return e;
......
/**********************************************************************//**
Serialize an action probability.
**************************************************************************/
int dio_put_action_probability_json(struct json_data_out *dout,
int dio_put_action_probability_json(struct json_data_out *d_out,
const struct plocation *location,
const struct act_prob *prob)
{
int e = 0;
if (dout->json) {
if (d_out->json) {
/* Create the action probability object. */
json_t *action_probability = json_object();
......
e |= json_object_set_new(action_probability, "max", json_integer(prob->max));
/* Put the action probability object in the packet. */
e |= plocation_write_data(dout->json, location, action_probability);
e |= plocation_write_data(d_out->json, location, action_probability);
} else {
e = dio_put_action_probability_raw(&dout->raw, prob);
e = dio_put_action_probability_raw(&d_out->raw, prob);
}
return e;
common/networking/dataio_json.h
dio_get_##f##_json(pc, d, l, ## __VA_ARGS__)
/* puts */
int dio_put_farray_json(struct json_data_out *dout,
int dio_put_farray_json(struct json_data_out *d_out,
const struct plocation *location, int size);
int dio_put_object_json(struct json_data_out *dout,
int dio_put_object_json(struct json_data_out *d_out,
const struct plocation *location);
int dio_put_type_json(struct json_data_out *dout, enum data_type type,
int dio_put_type_json(struct json_data_out *d_out, enum data_type type,
const struct plocation *location,
int value);
int dio_put_uint8_json(struct json_data_out *dout,
int dio_put_uint8_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_sint8_json(struct json_data_out *dout,
int dio_put_sint8_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_uint16_json(struct json_data_out *dout,
int dio_put_uint16_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_sint16_json(struct json_data_out *dout,
int dio_put_sint16_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_uint32_json(struct json_data_out *dout,
int dio_put_uint32_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_sint32_json(struct json_data_out *dout,
int dio_put_sint32_json(struct json_data_out *d_out,
const struct plocation *location, int value);
int dio_put_bool8_json(struct json_data_out *dout,
int dio_put_bool8_json(struct json_data_out *d_out,
const struct plocation *location, bool value);
int dio_put_bool32_json(struct json_data_out *dout,
int dio_put_bool32_json(struct json_data_out *d_out,
const struct plocation *location, bool value);
int dio_put_ufloat_json(struct json_data_out *dout,
int dio_put_ufloat_json(struct json_data_out *d_out,
const struct plocation *location,
float value, int float_factor);
int dio_put_sfloat_json(struct json_data_out *dout,
int dio_put_sfloat_json(struct json_data_out *d_out,
const struct plocation *location,
float value, int float_factor);
int dio_put_memory_json(struct json_data_out *dout,
int dio_put_memory_json(struct json_data_out *d_out,
struct plocation *location,
const void *value, size_t size);
int dio_put_string_json(struct json_data_out *dout,
int dio_put_string_json(struct json_data_out *d_out,
const struct plocation *location,
const char *value);
int dio_put_estring_json(struct json_data_out *dout,
int dio_put_estring_json(struct json_data_out *d_out,
const struct plocation *location,
const char *value);
int dio_put_cm_parameter_json(struct json_data_out *dout,
int dio_put_cm_parameter_json(struct json_data_out *d_out,
struct plocation *location,
const struct cm_parameter *order);
int dio_put_unit_order_json(struct json_data_out *dout,
int dio_put_unit_order_json(struct json_data_out *d_out,
struct plocation *location,
const struct unit_order *order);
int dio_put_worklist_json(struct json_data_out *dout,
int dio_put_worklist_json(struct json_data_out *d_out,
struct plocation *location,
const struct worklist *pwl);
int dio_put_requirement_json(struct json_data_out *dout,
int dio_put_requirement_json(struct json_data_out *d_out,
const struct plocation *location,
const struct requirement *preq);
int dio_put_action_probability_json(struct json_data_out *dout,
int dio_put_action_probability_json(struct json_data_out *d_out,
const struct plocation *location,
const struct act_prob *prob);
int dio_put_arraylen_json(struct json_data_out *dout,
int dio_put_arraylen_json(struct json_data_out *d_out,
const struct plocation *location, int size);
int dio_put_uint8_vec8_json(struct json_data_out *dout,
int dio_put_uint8_vec8_json(struct json_data_out *d_out,
const struct plocation *location,
int *values, int stop_value);
int dio_put_uint16_vec8_json(struct json_data_out *dout,
int dio_put_uint16_vec8_json(struct json_data_out *d_out,
const struct plocation *location,
int *values, int stop_value);
/* Should be a function but we need some macro magic. */
#define DIO_BV_PUT(pdout, location, bv) \
dio_put_memory_json((pdout), location, (bv).vec, sizeof((bv).vec))
#define DIO_BV_PUT(pd_out, location, bv) \
dio_put_memory_json((pd_out), location, (bv).vec, sizeof((bv).vec))
#define DIO_PUT(f, d, l, ...) \
dio_put_##f##_json(d, l, ## __VA_ARGS__)
common/networking/dataio_raw.c
/**********************************************************************//**
Returns TRUE iff the output has size bytes available.
**************************************************************************/
static bool enough_space(struct raw_data_out *dout, size_t size)
static bool enough_space(struct raw_data_out *d_out, size_t size)
{
if (dout->current + size > dout->dest_size) {
dout->too_short = TRUE;
if (d_out->current + size > d_out->dest_size) {
d_out->too_short = TRUE;
return FALSE;
} else {
dout->used = MAX(dout->used, dout->current + size);
d_out->used = MAX(d_out->used, d_out->current + size);
return TRUE;
}
}
......
Initializes the output to the given output buffer and the given
buffer size.
**************************************************************************/
void dio_output_init(struct raw_data_out *dout, void *destination,
void dio_output_init(struct raw_data_out *d_out, void *destination,
size_t dest_size)
{
dout->dest = destination;
dout->dest_size = dest_size;
dout->current = 0;
dout->used = 0;
dout->too_short = FALSE;
d_out->dest = destination;
d_out->dest_size = dest_size;
d_out->current = 0;
d_out->used = 0;
d_out->too_short = FALSE;
}
/**********************************************************************//**
Return the maximum number of bytes used.
**************************************************************************/
size_t dio_output_used(struct raw_data_out *dout)
size_t dio_output_used(struct raw_data_out *d_out)
{
return dout->used;
return d_out->used;
}
/**********************************************************************//**
Rewinds the stream so that the put-functions start from the
beginning.
**************************************************************************/
void dio_output_rewind(struct raw_data_out *dout)
void dio_output_rewind(struct raw_data_out *d_out)
{
dout->current = 0;
d_out->current = 0;
}
/**********************************************************************//**
......
/**********************************************************************//**
Insert value using 8 bits. May overflow.
**************************************************************************/
int dio_put_uint8_raw(struct raw_data_out *dout, int value)
int dio_put_uint8_raw(struct raw_data_out *d_out, int value)
{
uint8_t x = value;
......
"it will result %d at receiving side.",
value, (int) x);
if (enough_space(dout, 1)) {
memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 1);
dout->current++;
if (enough_space(d_out, 1)) {
memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 1);
d_out->current++;
}
return 0;
......
/**********************************************************************//**
Insert value using 16 bits. May overflow.
**************************************************************************/
int dio_put_uint16_raw(struct raw_data_out *dout, int value)
int dio_put_uint16_raw(struct raw_data_out *d_out, int value)
{
uint16_t x = htons(value);
......
"it will result %d at receiving side.",
value, (int) ntohs(x));
if (enough_space(dout, 2)) {
memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 2);
dout->current += 2;
if (enough_space(d_out, 2)) {
memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 2);
d_out->current += 2;
}
return 0;
......
/**********************************************************************//**
Insert value using 32 bits. May overflow.
**************************************************************************/
int dio_put_uint32_raw(struct raw_data_out *dout, int value)
int dio_put_uint32_raw(struct raw_data_out *d_out, int value)
{
uint32_t x = htonl(value);
......
"it will result %d at receiving side.",
value, (int) ntohl(x));
if (enough_space(dout, 4)) {
memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 4);
dout->current += 4;
if (enough_space(d_out, 4)) {
memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), &x, 4);
d_out->current += 4;
}
return 0;
......
/**********************************************************************//**
Insert value using 'size' bits. May overflow.
**************************************************************************/
int dio_put_type_raw(struct raw_data_out *dout, enum data_type type, int value)
int dio_put_type_raw(struct raw_data_out *d_out, enum data_type type,
int value)
{
switch (type) {
case DIOT_UINT8:
return dio_put_uint8_raw(dout, value);
return dio_put_uint8_raw(d_out, value);
case DIOT_UINT16:
return dio_put_uint16_raw(dout, value);
return dio_put_uint16_raw(d_out, value);
case DIOT_UINT32:
return dio_put_uint32_raw(dout, value);
return dio_put_uint32_raw(d_out, value);
case DIOT_SINT8:
return dio_put_sint8_raw(dout, value);
return dio_put_sint8_raw(d_out, value);
case DIOT_SINT16:
return dio_put_sint16_raw(dout, value);
return dio_put_sint16_raw(d_out, value);
case DIOT_SINT32:
return dio_put_sint32_raw(dout, value);
return dio_put_sint32_raw(d_out, value);
case DIOT_LAST:
break;
}
......
/**********************************************************************//**
Insert value using 8 bits. May overflow.
**************************************************************************/
int dio_put_sint8_raw(struct raw_data_out *dout, int value)
int dio_put_sint8_raw(struct raw_data_out *d_out, int value)
{
return dio_put_uint8_raw(dout, 0 <= value ? value : value + 0x100);
return dio_put_uint8_raw(d_out, 0 <= value ? value : value + 0x100);
}
/**********************************************************************//**
Insert value using 16 bits. May overflow.
**************************************************************************/
int dio_put_sint16_raw(struct raw_data_out *dout, int value)
int dio_put_sint16_raw(struct raw_data_out *d_out, int value)
{
return dio_put_uint16_raw(dout, 0 <= value ? value : value + 0x10000);
return dio_put_uint16_raw(d_out, 0 <= value ? value : value + 0x10000);
}
/**********************************************************************//**
Insert value using 32 bits. May overflow.
**************************************************************************/
int dio_put_sint32_raw(struct raw_data_out *dout, int value)
int dio_put_sint32_raw(struct raw_data_out *d_out, int value)
{
#if SIZEOF_INT == 4
return dio_put_uint32_raw(dout, value);
return dio_put_uint32_raw(d_out, value);
#else
return dio_put_uint32_raw(dout, (0 <= value ? value : value + 0x100000000));
return dio_put_uint32_raw(d_out, (0 <= value ? value : value + 0x100000000));
#endif
}
/**********************************************************************//**
Insert value 0 or 1 using 8 bits.
**************************************************************************/
int dio_put_bool8_raw(struct raw_data_out *dout, bool value)
int dio_put_bool8_raw(struct raw_data_out *d_out, bool value)
{
FIELD_RANGE_TEST(value != TRUE && value != FALSE,
value = (value != FALSE);,
"Trying to put a non-boolean: %d", (int) value);
return dio_put_uint8_raw(dout, value ? 1 : 0);
return dio_put_uint8_raw(d_out, value ? 1 : 0);
}
/**********************************************************************//**
Insert value 0 or 1 using 32 bits.
**************************************************************************/
int dio_put_bool32_raw(struct raw_data_out *dout, bool value)
int dio_put_bool32_raw(struct raw_data_out *d_out, bool value)
{
FIELD_RANGE_TEST(value != TRUE && value != FALSE,
value = (value != FALSE);,
"Trying to put a non-boolean: %d",
(int) value);
return dio_put_uint32_raw(dout, value ? 1 : 0);
return dio_put_uint32_raw(d_out, value ? 1 : 0);
}
/**********************************************************************//**
Insert a float number, which is multiplied by 'float_factor' before
being encoded into an uint32.
**************************************************************************/
int dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
int dio_put_ufloat_raw(struct raw_data_out *d_out, float value,
int float_factor)
{
uint32_t v = value * float_factor;
......
value, float_factor, (float) v / float_factor,
fabsf((float) v / float_factor - value) * float_factor);
return dio_put_uint32_raw(dout, v);
return dio_put_uint32_raw(d_out, v);
}
/**********************************************************************//**
Insert a float number, which is multiplied by 'float_factor' before
being encoded into a sint32.
**************************************************************************/
int dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
int dio_put_sfloat_raw(struct raw_data_out *d_out, float value,
int float_factor)
{
int32_t v = value * float_factor;
......
value, float_factor, (float) v / float_factor,
fabsf((float) v / float_factor - value) * float_factor);
return dio_put_sint32_raw(dout, v);
return dio_put_sint32_raw(d_out, v);
}
/**********************************************************************//**
......
insert values using 8 bits for each. stop_value is not required to
fit in 8 bits. Actual values may overflow.
**************************************************************************/
int dio_put_uint8_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
int dio_put_uint8_vec8_raw(struct raw_data_out *d_out, int *values,
int stop_value)
{
size_t count;
int e = 0;
......
/* nothing */
}
if (enough_space(dout, 1 + count)) {
if (enough_space(d_out, 1 + count)) {
size_t i;
e |= dio_put_uint8_raw(dout, count);
e |= dio_put_uint8_raw(d_out, count);
for (i = 0; i < count; i++) {
e |= dio_put_uint8_raw(dout, values[i]);
e |= dio_put_uint8_raw(d_out, values[i]);
}
}
......
insert values using 16 bits for each. stop_value is not required to
fit in 16 bits. Actual values may overflow.
**************************************************************************/
int dio_put_uint16_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
int dio_put_uint16_vec8_raw(struct raw_data_out *d_out, int *values,
int stop_value)
{
size_t count;
int e = 0;
......
/* nothing */
}
if (enough_space(dout, 1 + 2 * count)) {
if (enough_space(d_out, 1 + 2 * count)) {
size_t i;
e |= dio_put_uint8_raw(dout, count);
e |= dio_put_uint8_raw(d_out, count);
for (i = 0; i < count; i++) {
e |= dio_put_uint16_raw(dout, values[i]);
e |= dio_put_uint16_raw(d_out, values[i]);
}
}
......
/**********************************************************************//**
Insert block directly from memory.
**************************************************************************/
int dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
int dio_put_memory_raw(struct raw_data_out *d_out, const void *value,
size_t size)
{
if (enough_space(dout, size)) {
memcpy(ADD_TO_POINTER(dout->dest, dout->current), value, size);
dout->current += size;
if (enough_space(d_out, size)) {
memcpy(ADD_TO_POINTER(d_out->dest, d_out->current), value, size);
d_out->current += size;
}
return 0;
......
/**********************************************************************//**
Insert nullptr-terminated string. Conversion callback is used if set.
**************************************************************************/
int dio_put_string_raw(struct raw_data_out *dout, const char *value)
int dio_put_string_raw(struct raw_data_out *d_out, const char *value)
{
if (put_conv_callback) {
size_t length;
......
int e = 0;
if ((buffer = (*put_conv_callback) (value, &length))) {
e = dio_put_memory_raw(dout, buffer, length + 1);
e = dio_put_memory_raw(d_out, buffer, length + 1);
free(buffer);
}
return e;
} else {
return dio_put_memory_raw(dout, value, strlen(value) + 1);
return dio_put_memory_raw(d_out, value, strlen(value) + 1);
}
}
/**********************************************************************//**
Insert cm_parameter struct.
**************************************************************************/
int dio_put_cm_parameter_raw(struct raw_data_out *dout,
int dio_put_cm_parameter_raw(struct raw_data_out *d_out,
const struct cm_parameter *param)
{
int i;
int e = 0;
for (i = 0; i < O_LAST; i++) {
e |= dio_put_sint16_raw(dout, param->minimal_surplus[i]);
e |= dio_put_sint16_raw(d_out, param->minimal_surplus[i]);
}
e |= dio_put_bool8_raw(dout, param->max_growth);
e |= dio_put_bool8_raw(dout, param->require_happy);
e |= dio_put_bool8_raw(dout, param->allow_disorder);
e |= dio_put_bool8_raw(dout, param->allow_specialists);
e |= dio_put_bool8_raw(d_out, param->max_growth);
e |= dio_put_bool8_raw(d_out, param->require_happy);
e |= dio_put_bool8_raw(d_out, param->allow_disorder);
e |= dio_put_bool8_raw(d_out, param->allow_specialists);
for (i = 0; i < O_LAST; i++) {
e |= dio_put_uint16_raw(dout, param->factor[i]);
e |= dio_put_uint16_raw(d_out, param->factor[i]);
}
e |= dio_put_uint16_raw(dout, param->happy_factor);
e |= dio_put_uint16_raw(d_out, param->happy_factor);
return e;
}
......
/**********************************************************************//**
Insert the given unit_order struct/
**************************************************************************/
int dio_put_unit_order_raw(struct raw_data_out *dout,
int dio_put_unit_order_raw(struct raw_data_out *d_out,
const struct unit_order *order)
{
int e = 0;
e |= dio_put_uint8_raw(dout, order->order);
e |= dio_put_uint8_raw(dout, order->activity);
e |= dio_put_sint32_raw(dout, order->target);
e |= dio_put_sint16_raw(dout, order->sub_target);
e |= dio_put_uint8_raw(dout, order->action);
e |= dio_put_sint8_raw(dout, order->dir);
e |= dio_put_uint8_raw(d_out, order->order);
e |= dio_put_uint8_raw(d_out, order->activity);
e |= dio_put_sint32_raw(d_out, order->target);
e |= dio_put_sint16_raw(d_out, order->sub_target);
e |= dio_put_uint8_raw(d_out, order->action);
e |= dio_put_sint8_raw(d_out, order->dir);
return e;
}
......
Insert number of worklist items as 8 bit value and then insert
8 bit kind and 8 bit number for each worklist item.
**************************************************************************/
int dio_put_worklist_raw(struct raw_data_out *dout, const struct worklist *pwl)
int dio_put_worklist_raw(struct raw_data_out *d_out, const struct worklist *pwl)
{
int i, length = worklist_length(pwl);
int e = 0;
e |= dio_put_uint8_raw(dout, length);
e |= dio_put_uint8_raw(d_out, length);
for (i = 0; i < length; i++) {
const struct universal *pcp = &(pwl->entries[i]);
e |= dio_put_uint8_raw(dout, pcp->kind);
e |= dio_put_uint8_raw(dout, universal_number(pcp));
e |= dio_put_uint8_raw(d_out, pcp->kind);
e |= dio_put_uint8_raw(d_out, universal_number(pcp));
}
return e;
......
/**********************************************************************//**
Serialize an action probability.
**************************************************************************/
int dio_put_action_probability_raw(struct raw_data_out *dout,
int dio_put_action_probability_raw(struct raw_data_out *d_out,
const struct act_prob *aprob)
{
int e = 0;
e |= dio_put_uint8_raw(dout, aprob->min);
e |= dio_put_uint8_raw(dout, aprob->max);
e |= dio_put_uint8_raw(d_out, aprob->min);
e |= dio_put_uint8_raw(d_out, aprob->max);
return e;
}
... This diff was truncated because it exceeds the maximum size that can be displayed.
(2-2/2)