13#ifndef FC__PACKETS_JSON_H
14#define FC__PACKETS_JSON_H
22#define log_packet_json log_debug
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); \
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); \
45#define SEND_PACKET_END(packet_type) \
48 if (pc->json_mode) { \
49 json_buffer = json_dumps(dout.json, JSON_COMPACT | JSON_ENSURE_ASCII); \
51 dio_put_string_raw(&(dout.raw), json_buffer); \
52 log_packet_json("Json out: %s", json_buffer); \
54 size = dio_output_used(&dout.raw); \
56 dio_output_rewind(&(dout.raw)); \
57 dio_put_uint16_raw(&(dout.raw), size); \
59 json_decref(dout.json); \
61 size = dio_output_used(&dout.raw); \
63 dio_output_rewind(&dout.raw); \
64 dio_put_type_raw(&dout.raw, pc->packet_header.length, size); \
66 fc_assert(!dout.raw.too_short); \
67 return send_packet_data(pc, buffer, size, packet_type); \
70#define SEND_PACKET_DISCARD() \
72 if (pc->json_mode) { \
73 json_decref(dout.json); \
78#define RECEIVE_PACKET_START(packet_type, result) \
79 struct packet_type packet_buf, *result = &packet_buf; \
81 if (!pc->json_mode) { \
82 dio_input_init(&din, pc->buffer->data, \
83 data_type_size(pc->packet_header.length)); \
87 dio_get_type_raw(&din, pc->packet_header.length, &size); \
88 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
90 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
91 + data_type_size(pc->packet_header.type))); \
94#define RECEIVE_PACKET_END(result) \
95 if (pc->json_mode) { \
96 json_decref(pc->json_packet); \
97 result = fc_malloc(sizeof(*result)); \
98 *result = packet_buf; \
101 if (!packet_check(&din, pc)) { \
104 remove_packet_from_buffer(pc->buffer); \
105 result = fc_malloc(sizeof(*result)); \
106 *result = packet_buf; \
110#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
111 log_packet("Error on field '" #field "'" __VA_ARGS__); \
115#define PACKET_STRVEC_SEPARATOR '\3'
116#define PACKET_STRVEC_COMPUTE(str, strvec) \
117 if (NULL != strvec) { \
118 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
122#define PACKET_STRVEC_EXTRACT(strvec, str) \
123 if ('\0' != str[0]) { \
124 strvec = strvec_new(); \
125 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
void * get_packet_from_connection_json(struct connection *pc, enum packet_type *ptype)