Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/networking/packets_json.h Source File

Freeciv-3.1

Loading...
Searching...
No Matches
packets_json.h
Go to the documentation of this file.
1/**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifndef FC__PACKETS_JSON_H
14#define FC__PACKETS_JSON_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <jansson.h>
21
22#define log_packet_json log_debug
23
24 void *get_packet_from_connection_json(struct connection *pc,
25 enum packet_type *ptype);
26
27#define SEND_PACKET_START(packet_type) \
28 unsigned char buffer[MAX_LEN_PACKET * 5]; \
29 struct plocation *pid_addr; \
30 char *json_buffer = NULL; \
31 struct json_data_out dout; \
32 dio_output_init(&(dout.raw), buffer, sizeof(buffer)); \
33 if (pc->json_mode) { \
34 dout.json = json_object(); \
35 dio_put_uint16_raw(&(dout.raw), 0); \
36 pid_addr = plocation_field_new("pid"); \
37 dio_put_uint8_json(&dout, pid_addr, packet_type); \
38 FC_FREE(pid_addr); \
39 } else { \
40 dout.json = NULL; \
41 dio_put_type_raw(&dout.raw, pc->packet_header.length, 0); \
42 dio_put_type_raw(&dout.raw, pc->packet_header.type, packet_type); \
43 }
44
45#define SEND_PACKET_END(packet_type) \
46 { \
47 size_t size; \
48 if (pc->json_mode) { \
49 json_buffer = json_dumps(dout.json, JSON_COMPACT | JSON_ENSURE_ASCII); \
50 if (json_buffer) { \
51 dio_put_string_raw(&(dout.raw), json_buffer); \
52 log_packet_json("Json out: %s", json_buffer); \
53 } \
54 size = dio_output_used(&dout.raw); \
55 \
56 dio_output_rewind(&(dout.raw)); \
57 dio_put_uint16_raw(&(dout.raw), size); \
58 free(json_buffer); \
59 json_decref(dout.json); \
60 } else { \
61 size = dio_output_used(&dout.raw); \
62 \
63 dio_output_rewind(&dout.raw); \
64 dio_put_type_raw(&dout.raw, pc->packet_header.length, size); \
65 } \
66 fc_assert(!dout.raw.too_short); \
67 return send_packet_data(pc, buffer, size, packet_type); \
68 }
69
70#define RECEIVE_PACKET_START(packet_type, result) \
71 struct packet_type packet_buf, *result = &packet_buf; \
72 struct data_in din; \
73 if (!pc->json_mode) { \
74 dio_input_init(&din, pc->buffer->data, \
75 data_type_size(pc->packet_header.length)); \
76 { \
77 int size; \
78 \
79 dio_get_type_raw(&din, pc->packet_header.length, &size); \
80 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
81 } \
82 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
83 + data_type_size(pc->packet_header.type))); \
84 }
85
86#define RECEIVE_PACKET_END(result) \
87 if (pc->json_mode) { \
88 json_decref(pc->json_packet); \
89 result = fc_malloc(sizeof(*result)); \
90 *result = packet_buf; \
91 return result; \
92 } else { \
93 if (!packet_check(&din, pc)) { \
94 return NULL; \
95 } \
96 remove_packet_from_buffer(pc->buffer); \
97 result = fc_malloc(sizeof(*result)); \
98 *result = packet_buf; \
99 return result; \
100 }
101
102#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
103 log_packet("Error on field '" #field "'" __VA_ARGS__); \
104 return NULL
105
106/* Utilities to exchange strings and string vectors. */
107#define PACKET_STRVEC_SEPARATOR '\3'
108#define PACKET_STRVEC_COMPUTE(str, strvec) \
109 if (NULL != strvec) { \
110 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
111 } else { \
112 str[0] = '\0'; \
113 }
114#define PACKET_STRVEC_EXTRACT(strvec, str) \
115 if ('\0' != str[0]) { \
116 strvec = strvec_new(); \
117 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
118 } else { \
119 strvec = NULL; \
120 }
121
122#ifdef __cplusplus
123}
124#endif /* __cplusplus */
125
126#endif /* FC__PACKETS_JSON_H */
packet_type
packet_type
Definition packets_gen.h:1883
get_packet_from_connection_json
void * get_packet_from_connection_json(struct connection *pc, enum packet_type *ptype)
connection
Definition connection.h:141