18#ifdef FREECIV_JSON_CONNECTION
27#ifdef HAVE_ARPA_INET_H
30#ifdef HAVE_NETINET_IN_H
31#include <netinet/in.h>
56#define PACKET_SIZE_STATISTICS 0
60#define SPECHASH_TAG packet_handler
61#define SPECHASH_ASTR_KEY_TYPE
62#define SPECHASH_IDATA_TYPE struct packet_handlers *
63#define SPECHASH_IDATA_FREE (packet_handler_hash_data_free_fn_t) free
99 whole_packet_len = len_read;
101 if ((
unsigned)whole_packet_len > pc->
buffer->
ndata) {
110 log_verbose(
"The packet stream is corrupt. The connection "
111 "will be closed now.");
123 pc->json_packet = json_loadb((
char*)pc->
buffer->
data + 2, whole_packet_len - 3, 0, &error);
126 pc->json_mode = (pc->json_packet != NULL);
131 pc->json_packet = json_loadb((
char*)pc->
buffer->
data + 2, whole_packet_len - 3, 0, &error);
134 if (!pc->json_packet) {
145 if (!pc->json_packet) {
149 pint = json_object_get(pc->json_packet,
"pid");
152 log_error(
"ERROR: Unable to get packet type.");
160 utype.type = utype.itype;
165 log_verbose(
"Received unsupported packet type %d (%s). The connection "
166 "will be closed now.",
172 log_packet(
"got packet type=(%s) len=%d from %s",
182#if PACKET_SIZE_STATISTICS
188 static int packet_counter = 0;
191 int size = whole_packet_len;
193 if (!packet_counter) {
197 packets_stats[i].counter = 0;
198 packets_stats[i].size = 0;
206 if (packet_counter % 100 == 0) {
211 if (packets_stats[i].counter == 0)
213 sum += packets_stats[i].size;
214 log_test(
" [%-25.25s %3d]: %6d packets; %8d bytes total; "
215 "%5d bytes/packet average",
217 packets_stats[i].
size,
218 packets_stats[i].
size / packets_stats[i].counter);
220 log_test(
"received %d bytes in %d packets;average size "
221 "per packet %d bytes",
222 sum, packet_counter, sum / packet_counter);
226 data = receive_handler(pc);
void connection_close(struct connection *pconn, const char *reason)
bool dio_get_uint16_raw(struct data_in *din, int *dest)
bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
size_t data_type_size(enum data_type type)
void dio_input_init(struct data_in *din, const void *src, size_t src_size)
static bool is_server(void)
#define log_verbose(message,...)
#define log_error(message,...)
const char *const packet_functional_capability
const char * packet_name(enum packet_type type)
void * get_packet_from_connection_json(struct connection *pc, enum packet_type *ptype)
const struct packet_handlers * handlers
struct connection::@59 phs
struct packet_header packet_header
char username[MAX_LEN_NAME]
struct connection::@57::@63 server
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
struct socket_packet_buffer * buffer
void *(* receive[PACKET_LAST])(struct connection *pconn)