From 170c0b09ed8194148e875161bd35cc8f1c2141c5 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Feb 2023 12:46:00 +0200 Subject: [PATCH 20/20] packets_json.[ch]: Improve Coding Style See osdn #46618 Signed-off-by: Marko Lindqvist --- common/networking/packets_json.c | 37 +++++++++++++--------- common/networking/packets_json.h | 54 ++++++++++++++++---------------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/common/networking/packets_json.c b/common/networking/packets_json.c index 0155be7592..f975ff8ae6 100644 --- a/common/networking/packets_json.c +++ b/common/networking/packets_json.c @@ -84,11 +84,11 @@ void *get_packet_from_connection_json(struct connection *pc, json_t *pint; if (!pc->used) { - return NULL; /* connection was closed, stop reading */ + return NULL; /* Connection was closed, stop reading */ } - + if (pc->buffer->ndata < data_type_size(pc->packet_header.length)) { - /* Not got enough for a length field yet */ + /* Not enough for a length field yet */ return NULL; } @@ -99,7 +99,7 @@ void *get_packet_from_connection_json(struct connection *pc, whole_packet_len = len_read; if ((unsigned)whole_packet_len > pc->buffer->ndata) { - return NULL; /* not all data has been read */ + return NULL; /* Not all data has been read */ } /* @@ -110,6 +110,7 @@ void *get_packet_from_connection_json(struct connection *pc, log_verbose("The packet stream is corrupt. The connection " "will be closed now."); connection_close(pc, _("decoding error")); + return NULL; } @@ -120,7 +121,8 @@ void *get_packet_from_connection_json(struct connection *pc, */ if (is_server() && pc->server.last_request_id_seen == 0) { /* Try to parse JSON packet. Note that json string has '\0' */ - pc->json_packet = json_loadb((char*)pc->buffer->data + 2, whole_packet_len - 3, 0, &error); + pc->json_packet = json_loadb((char*)pc->buffer->data + 2, + whole_packet_len - 3, 0, &error); /* Set the connection mode */ pc->json_mode = (pc->json_packet != NULL); @@ -128,7 +130,8 @@ void *get_packet_from_connection_json(struct connection *pc, if (pc->json_mode) { /* Parse JSON packet. Note that json string has '\0' */ - pc->json_packet = json_loadb((char*)pc->buffer->data + 2, whole_packet_len - 3, 0, &error); + pc->json_packet = json_loadb((char*)pc->buffer->data + 2, + whole_packet_len - 3, 0, &error); /* Log errors before we scrap the data */ if (!pc->json_packet) { @@ -140,7 +143,8 @@ void *get_packet_from_connection_json(struct connection *pc, /* Shift remaining data to the front */ pc->buffer->ndata -= whole_packet_len; - memmove(pc->buffer->data, pc->buffer->data + whole_packet_len, pc->buffer->ndata); + memmove(pc->buffer->data, pc->buffer->data + whole_packet_len, + pc->buffer->ndata); if (!pc->json_packet) { return NULL; @@ -153,8 +157,7 @@ void *get_packet_from_connection_json(struct connection *pc, return NULL; } - json_int_t packet_type = json_integer_value(pint); - utype.type = packet_type; + utype.type = json_integer_value(pint); } else { dio_get_type_raw(&din, pc->packet_header.type, &utype.itype); utype.type = utype.itype; @@ -166,6 +169,7 @@ void *get_packet_from_connection_json(struct connection *pc, "will be closed now.", utype.type, packet_name(utype.type)); connection_close(pc, _("unsupported packet type")); + return NULL; } @@ -179,7 +183,7 @@ void *get_packet_from_connection_json(struct connection *pc, pc->incoming_packet_notify(pc, utype.type, whole_packet_len); } -#if PACKET_SIZE_STATISTICS +#if PACKET_SIZE_STATISTICS { static struct { int counter; @@ -194,8 +198,8 @@ void *get_packet_from_connection_json(struct connection *pc, int i; for (i = 0; i < PACKET_LAST; i++) { - packets_stats[i].counter = 0; - packets_stats[i].size = 0; + packets_stats[i].counter = 0; + packets_stats[i].size = 0; } } @@ -208,21 +212,24 @@ void *get_packet_from_connection_json(struct connection *pc, log_test("Received packets:"); for (i = 0; i < PACKET_LAST; i++) { - if (packets_stats[i].counter == 0) - continue; - sum += packets_stats[i].size; + if (packets_stats[i].counter == 0) { + continue; + } + sum += packets_stats[i].size; log_test(" [%-25.25s %3d]: %6d packets; %8d bytes total; " "%5d bytes/packet average", packet_name(i), i, packets_stats[i].counter, packets_stats[i].size, packets_stats[i].size / packets_stats[i].counter); } + log_test("received %d bytes in %d packets;average size " "per packet %d bytes", sum, packet_counter, sum / packet_counter); } } #endif /* PACKET_SIZE_STATISTICS */ + data = receive_handler(pc); if (!data) { connection_close(pc, _("incompatible packet contents")); diff --git a/common/networking/packets_json.h b/common/networking/packets_json.h index c6da26677b..c3fcd6ae39 100644 --- a/common/networking/packets_json.h +++ b/common/networking/packets_json.h @@ -1,4 +1,4 @@ -/********************************************************************** +/*********************************************************************** Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,7 +30,7 @@ void *get_packet_from_connection_json(struct connection *pc, char *json_buffer = NULL; \ struct json_data_out dout; \ dio_output_init(&(dout.raw), buffer, sizeof(buffer)); \ - if (pc->json_mode) { \ + if (pc->json_mode) { \ dout.json = json_object(); \ dio_put_uint16_raw(&(dout.raw), 0); \ pid_addr = plocation_field_new("pid"); \ @@ -45,7 +45,7 @@ void *get_packet_from_connection_json(struct connection *pc, #define SEND_PACKET_END(packet_type) \ { \ size_t size; \ - if (pc->json_mode) { \ + if (pc->json_mode) { \ json_buffer = json_dumps(dout.json, JSON_COMPACT | JSON_ENSURE_ASCII); \ if (json_buffer) { \ dio_put_string_raw(&(dout.raw), json_buffer); \ @@ -67,29 +67,29 @@ void *get_packet_from_connection_json(struct connection *pc, return send_packet_data(pc, buffer, size, packet_type); \ } -#define RECEIVE_PACKET_START(packet_type, result) \ - struct packet_type packet_buf, *result = &packet_buf; \ - struct data_in din; \ - if (!pc->json_mode) { \ - dio_input_init(&din, pc->buffer->data, \ - data_type_size(pc->packet_header.length)); \ - { \ - int size; \ - \ - dio_get_type_raw(&din, pc->packet_header.length, &size); \ - dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \ - } \ - dio_input_skip(&din, (data_type_size(pc->packet_header.length) \ - + data_type_size(pc->packet_header.type))); \ +#define RECEIVE_PACKET_START(packet_type, result) \ + struct packet_type packet_buf, *result = &packet_buf; \ + struct data_in din; \ + if (!pc->json_mode) { \ + dio_input_init(&din, pc->buffer->data, \ + data_type_size(pc->packet_header.length)); \ + { \ + int size; \ + \ + dio_get_type_raw(&din, pc->packet_header.length, &size); \ + dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \ + } \ + dio_input_skip(&din, (data_type_size(pc->packet_header.length) \ + + data_type_size(pc->packet_header.type))); \ } -#define RECEIVE_PACKET_END(result) \ - if (pc->json_mode) { \ - json_decref(pc->json_packet); \ - result = fc_malloc(sizeof(*result)); \ - *result = packet_buf; \ - return result; \ - } else { \ +#define RECEIVE_PACKET_END(result) \ + if (pc->json_mode) { \ + json_decref(pc->json_packet); \ + result = fc_malloc(sizeof(*result)); \ + *result = packet_buf; \ + return result; \ + } else { \ if (!packet_check(&din, pc)) { \ return NULL; \ } \ @@ -99,9 +99,9 @@ void *get_packet_from_connection_json(struct connection *pc, return result; \ } -#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \ +#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \ log_packet("Error on field '" #field "'" __VA_ARGS__); \ - return NULL + return NULL; /* Utilities to exchange strings and string vectors. */ #define PACKET_STRVEC_SEPARATOR '\3' @@ -123,4 +123,4 @@ void *get_packet_from_connection_json(struct connection *pc, } #endif /* __cplusplus */ -#endif /* FC__PACKETS_JSON_H */ +#endif /* FC__PACKETS_JSON_H */ -- 2.39.1