Freeciv-3.3
Loading...
Searching...
No Matches
packets.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_H
14#define FC__PACKETS_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20/* utility */
21#include "shared.h" /* MAX_LEN_ADDR */
22
23/* common */
24#include "diptreaty.h"
25#include "effects.h"
26#include "events.h"
27#include "improvement.h" /* bv_imprs */
28#include "player.h"
29#include "requirements.h"
30#include "spaceship.h"
31#include "team.h"
32#include "tile.h"
33#include "traderoutes.h"
34#include "unittype.h"
35#include "worklist.h"
36
37struct connection;
38struct data_in;
39
40
41#ifdef FREECIV_WEB
42#define web_send_packet(packetname, pconn, ...) \
43do { \
44 if (conn_is_webclient(pconn)) { \
45 send_packet_web_ ##packetname(pconn, __VA_ARGS__ ); \
46 } \
47} while (FALSE)
48#define web_lsend_packet(packetname, pconn, pack, ...) \
49do { \
50 const struct packet_web_ ##packetname *_pptr_ = pack; \
51 if (_pptr_ != NULL) { \
52 lsend_packet_web_ ##packetname(pconn, _pptr_, ##__VA_ARGS__ ); \
53 } \
54} while (FALSE);
55#else /* FREECIV_WEB */
56#define web_send_packet(packetname, pconn, ...)
57#define web_lsend_packet(packetname, ...)
58#endif /* FREECIV_WEB */
59
60/* Indicates that the player initiated a request.
61 *
62 * Used in network protocol. */
63#define REQEST_PLAYER_INITIATED (0)
64
65/* Used in network protocol. */
71
72#include "packets_gen.h"
73
75 union {
77 int (*packet)(struct connection *pconn, const void *packet);
78 int (*force_to_send)(struct connection *pconn, const void *packet,
79 bool force_to_send);
81 void *(*receive[PACKET_LAST])(struct connection *pconn);
82};
83
85 enum packet_type *ptype);
86
87#ifdef FREECIV_JSON_CONNECTION
88#define get_packet_from_connection(pc, ptype) get_packet_from_connection_json(pc, ptype)
89#else
90#define get_packet_from_connection(pc, ptype) get_packet_from_connection_raw(pc, ptype)
91#endif
92
94
95void send_attribute_block(const struct player *pplayer,
96 struct connection *pconn);
98 const struct
100 *chunk);
103 const char *capability);
104const char *packet_name(enum packet_type type);
106void packet_destroy(void *packet, enum packet_type type);
107
110 const struct packet_server_join_reply
111 *packet);
113 const struct
115
118 *packet);
119
120const struct packet_handlers *packet_handlers_initial(void);
121const struct packet_handlers *packet_handlers_get(const char *capability);
122
123void packets_deinit(void);
124
125#ifdef FREECIV_JSON_CONNECTION
126#include "packets_json.h"
127#else
128
129#define SEND_PACKET_START(packet_type) \
130 unsigned char buffer[MAX_LEN_PACKET]; \
131 struct raw_data_out dout; \
132 \
133 dio_output_init(&dout, buffer, sizeof(buffer)); \
134 dio_put_type_raw(&dout, pc->packet_header.length, 0); \
135 dio_put_type_raw(&dout, pc->packet_header.type, packet_type);
136
137#define SEND_PACKET_END(packet_type) \
138 { \
139 size_t size = dio_output_used(&dout); \
140 \
141 dio_output_rewind(&dout); \
142 dio_put_type_raw(&dout, pc->packet_header.length, size); \
143 fc_assert(!dout.too_short); \
144 return send_packet_data(pc, buffer, size, packet_type); \
145 }
146
147#define SEND_PACKET_DISCARD() \
148 return 0
149
150#define RECEIVE_PACKET_START(packet_type, result) \
151 struct data_in din; \
152 struct packet_type packet_buf, *result = &packet_buf; \
153 \
154 init_ ##packet_type (&packet_buf); \
155 dio_input_init(&din, pc->buffer->data, \
156 data_type_size(pc->packet_header.length)); \
157 { \
158 int size; \
159 \
160 dio_get_type_raw(&din, pc->packet_header.length, &size); \
161 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
162 } \
163 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
164 + data_type_size(pc->packet_header.type)));
165
166#define RECEIVE_PACKET_END(result) \
167 if (!packet_check(&din, pc)) { \
168 FREE_PACKET_STRUCT(&packet_buf); \
169 return NULL; \
170 } \
171 remove_packet_from_buffer(pc->buffer); \
172 result = fc_malloc(sizeof(*result)); \
173 *result = packet_buf; \
174 return result;
175
176#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
177 log_packet("Error on field '" #field "'" __VA_ARGS__); \
178 FREE_PACKET_STRUCT(&packet_buf); \
179 return NULL
180
181#endif /* FREECIV_JSON_PROTOCOL */
182
183int send_packet_data(struct connection *pc, unsigned char *data, int len,
185bool packet_check(struct data_in *din, struct connection *pc);
186
187/* Utilities to move string vectors in and out of packets. */
188#define PACKET_STRVEC_INSERT(dest, src) \
189 dest = src
190#define PACKET_STRVEC_EXTRACT(dest, src) \
191 if (src != nullptr && strvec_size(src) > 0) { \
192 dest = strvec_new(); \
193 strvec_copy(dest, src); \
194 } else { \
195 dest = nullptr; \
196 }
197
198#ifdef __cplusplus
199}
200#endif /* __cplusplus */
201
202#endif /* FC__PACKETS_H */
char * incite_cost
Definition comments.c:76
GType type
Definition repodlgs.c:1313
void post_receive_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:652
void * get_packet_from_connection_raw(struct connection *pc, enum packet_type *ptype)
Definition packets.c:373
bool packet_has_game_info_flag(enum packet_type type)
bool packet_check(struct data_in *din, struct connection *pc)
Definition packets.c:665
void packet_header_init(struct packet_header *packet_header)
Definition packets.c:617
void packet_handlers_fill_capability(struct packet_handlers *phandlers, const char *capability)
const struct packet_handlers * packet_handlers_initial(void)
Definition packets.c:823
void packets_deinit(void)
Definition packets.c:900
void pre_send_packet_player_attribute_chunk(struct connection *pc, struct packet_player_attribute_chunk *packet)
Definition packets.c:793
void remove_packet_from_buffer(struct socket_packet_buffer *buffer)
Definition packets.c:598
const char * packet_name(enum packet_type type)
Definition packets_gen.c:47
void post_send_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:640
int send_packet_data(struct connection *pc, unsigned char *data, int len, enum packet_type packet_type)
Definition packets.c:213
void packet_destroy(void *packet, enum packet_type type)
void generic_handle_player_attribute_chunk(struct player *pplayer, const struct packet_player_attribute_chunk *chunk)
Definition packets.c:690
void send_attribute_block(const struct player *pplayer, struct connection *pconn)
Definition packets.c:746
unit_info_use
Definition packets.h:66
@ UNIT_INFO_IDENTITY
Definition packets.h:67
@ UNIT_INFO_CITY_PRESENT
Definition packets.h:69
@ UNIT_INFO_CITY_SUPPORTED
Definition packets.h:68
const struct packet_handlers * packet_handlers_get(const char *capability)
Definition packets.c:840
void packet_handlers_fill_initial(struct packet_handlers *phandlers)
packet_type
@ PACKET_LAST
int len
Definition packhand.c:127
char capability[MAX_LEN_CAPSTR]
Definition connection.h:171
struct socket_packet_buffer * buffer
Definition connection.h:153
union packet_handlers::@71 send[PACKET_LAST]
int(* force_to_send)(struct connection *pconn, const void *packet, bool force_to_send)
Definition packets.h:78
int(* no_packet)(struct connection *pconn)
Definition packets.h:76
int(* packet)(struct connection *pconn, const void *packet)
Definition packets.h:77
void *(* receive[PACKET_LAST])(struct connection *pconn)
Definition packets.h:81