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

Freeciv-3.1

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
20struct connection;
21struct data_in;
22
23/* utility */
24#include "shared.h" /* MAX_LEN_ADDR */
25
26/* common */
27#include "connection.h" /* struct connection, MAX_LEN_* */
28#include "diptreaty.h"
29#include "effects.h"
30#include "events.h"
31#include "improvement.h" /* bv_imprs */
32#include "player.h"
33#include "requirements.h"
34#include "spaceship.h"
35#include "team.h"
36#include "tile.h"
37#include "traderoutes.h"
38#include "unittype.h"
39#include "worklist.h"
40
41
42/* Used in network protocol. */
43#define MAX_LEN_MSG 1536
44#define MAX_LEN_ROUTE 2000 /* MAX_LEN_PACKET / 2 - header */
45
46#ifdef FREECIV_WEB
47#define web_send_packet(packetname, ...) \
48 send_packet_web_ ##packetname( __VA_ARGS__ )
49#define web_lsend_packet(packetname, ...) \
50 lsend_packet_web_ ##packetname( __VA_ARGS__ )
51#else /* FREECIV_WEB */
52#define web_send_packet(packetname, ...)
53#define web_lsend_packet(packetname, ...)
54#endif /* FREECIV_WEB */
55
56/* The size of opaque (void *) data sent in the network packet. To avoid
57 * fragmentation issues, this SHOULD NOT be larger than the standard
58 * ethernet or PPP 1500 byte frame size (with room for headers).
59 *
60 * Do not spend much time optimizing, you have no idea of the actual dynamic
61 * path characteristics between systems, such as VPNs and tunnels.
62 *
63 * Used in network protocol.
64 */
65#define ATTRIBUTE_CHUNK_SIZE (1400)
66
67/* Indicates that the player initiated a request.
68 *
69 * Used in network protocol. */
70#define REQEST_PLAYER_INITIATED (0)
71
72/* Used in network protocol. */
73 enum report_type {
74 REPORT_WONDERS_OF_THE_WORLD,
75 REPORT_TOP_5_CITIES,
76 REPORT_DEMOGRAPHIC,
77 REPORT_ACHIEVEMENTS
78};
79
80/* Used in network protocol. */
81 enum unit_info_use {
82 UNIT_INFO_IDENTITY,
83 UNIT_INFO_CITY_SUPPORTED,
84 UNIT_INFO_CITY_PRESENT
85};
86
87/* Used in network protocol. */
88 enum authentication_type {
89 AUTH_LOGIN_FIRST, /* request a password for a returning user */
90 AUTH_NEWUSER_FIRST, /* request a password for a new user */
91 AUTH_LOGIN_RETRY, /* inform the client to try a different password */
92 AUTH_NEWUSER_RETRY /* inform the client to try a different [new] password */
93};
94
95#include "packets_gen.h"
96
97 struct packet_handlers {
98 union {
99 int (*no_packet)(struct connection *pconn);
100 int (*packet)(struct connection *pconn, const void *packet);
101 int (*force_to_send)(struct connection *pconn, const void *packet,
102 bool force_to_send);
103 } send[PACKET_LAST];
104 void *(*receive[PACKET_LAST])(struct connection *pconn);
105};
106
107void *get_packet_from_connection_raw(struct connection *pc,
108 enum packet_type *ptype);
109
110#ifdef FREECIV_JSON_CONNECTION
111#define get_packet_from_connection(pc, ptype) get_packet_from_connection_json(pc, ptype)
112#else
113#define get_packet_from_connection(pc, ptype) get_packet_from_connection_raw(pc, ptype)
114#endif
115
116void remove_packet_from_buffer(struct socket_packet_buffer *buffer);
117
118void send_attribute_block(const struct player *pplayer,
119 struct connection *pconn);
120void generic_handle_player_attribute_chunk(struct player *pplayer,
121 const struct
122 packet_player_attribute_chunk
123 *chunk);
124void packet_handlers_fill_initial(struct packet_handlers *phandlers);
125void packet_handlers_fill_capability(struct packet_handlers *phandlers,
126 const char *capability);
127const char *packet_name(enum packet_type type);
128bool packet_has_game_info_flag(enum packet_type type);
129
130void packet_header_init(struct packet_header *packet_header);
131void post_send_packet_server_join_reply(struct connection *pconn,
132 const struct packet_server_join_reply
133 *packet);
134void post_receive_packet_server_join_reply(struct connection *pconn,
135 const struct
136 packet_server_join_reply *packet);
137
138void pre_send_packet_player_attribute_chunk(struct connection *pc,
139 struct packet_player_attribute_chunk
140 *packet);
141
142const struct packet_handlers *packet_handlers_initial(void);
143const struct packet_handlers *packet_handlers_get(const char *capability);
144
145void packets_deinit(void);
146
147#ifdef FREECIV_JSON_CONNECTION
148#include "packets_json.h"
149#else
150
151#define SEND_PACKET_START(packet_type) \
152 unsigned char buffer[MAX_LEN_PACKET]; \
153 struct raw_data_out dout; \
154 \
155 dio_output_init(&dout, buffer, sizeof(buffer)); \
156 dio_put_type_raw(&dout, pc->packet_header.length, 0); \
157 dio_put_type_raw(&dout, pc->packet_header.type, packet_type);
158
159#define SEND_PACKET_END(packet_type) \
160 { \
161 size_t size = dio_output_used(&dout); \
162 \
163 dio_output_rewind(&dout); \
164 dio_put_type_raw(&dout, pc->packet_header.length, size); \
165 fc_assert(!dout.too_short); \
166 return send_packet_data(pc, buffer, size, packet_type); \
167 }
168
169#define RECEIVE_PACKET_START(packet_type, result) \
170 struct data_in din; \
171 struct packet_type packet_buf, *result = &packet_buf; \
172 \
173 dio_input_init(&din, pc->buffer->data, \
174 data_type_size(pc->packet_header.length)); \
175 { \
176 int size; \
177 \
178 dio_get_type_raw(&din, pc->packet_header.length, &size); \
179 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
180 } \
181 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
182 + data_type_size(pc->packet_header.type)));
183
184#define RECEIVE_PACKET_END(result) \
185 if (!packet_check(&din, pc)) { \
186 return NULL; \
187 } \
188 remove_packet_from_buffer(pc->buffer); \
189 result = fc_malloc(sizeof(*result)); \
190 *result = packet_buf; \
191 return result;
192
193#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
194 log_packet("Error on field '" #field "'" __VA_ARGS__); \
195 return NULL
196
197#endif /* FREECIV_JSON_PROTOCOL */
198
199int send_packet_data(struct connection *pc, unsigned char *data, int len,
200 enum packet_type packet_type);
201bool packet_check(struct data_in *din, struct connection *pc);
202
203/* Utilities to exchange strings and string vectors. */
204#define PACKET_STRVEC_SEPARATOR '\3'
205#define PACKET_STRVEC_COMPUTE(str, strvec) \
206 if (NULL != strvec) { \
207 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
208 } else { \
209 str[0] = '\0'; \
210 }
211#define PACKET_STRVEC_EXTRACT(strvec, str) \
212 if ('\0' != str[0]) { \
213 strvec = strvec_new(); \
214 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
215 } else { \
216 strvec = NULL; \
217 }
218
219#ifdef __cplusplus
220}
221#endif /* __cplusplus */
222
223#endif /* FC__PACKETS_H */
connection.h
diptreaty.h
effects.h
events.h
type
GType type
Definition repodlgs.c:1312
improvement.h
post_receive_packet_server_join_reply
void post_receive_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:652
get_packet_from_connection_raw
void * get_packet_from_connection_raw(struct connection *pc, enum packet_type *ptype)
Definition packets.c:373
packet_has_game_info_flag
bool packet_has_game_info_flag(enum packet_type type)
Definition packets_gen.c:570
packet_check
bool packet_check(struct data_in *din, struct connection *pc)
Definition packets.c:665
packet_header_init
void packet_header_init(struct packet_header *packet_header)
Definition packets.c:617
packet_handlers_fill_capability
void packet_handlers_fill_capability(struct packet_handlers *phandlers, const char *capability)
Definition packets_gen.c:95970
packet_handlers_initial
const struct packet_handlers * packet_handlers_initial(void)
Definition packets.c:823
packets_deinit
void packets_deinit(void)
Definition packets.c:900
report_type
report_type
Definition packets.h:73
REPORT_WONDERS_OF_THE_WORLD
@ REPORT_WONDERS_OF_THE_WORLD
Definition packets.h:74
REPORT_DEMOGRAPHIC
@ REPORT_DEMOGRAPHIC
Definition packets.h:76
REPORT_TOP_5_CITIES
@ REPORT_TOP_5_CITIES
Definition packets.h:75
REPORT_ACHIEVEMENTS
@ REPORT_ACHIEVEMENTS
Definition packets.h:77
pre_send_packet_player_attribute_chunk
void pre_send_packet_player_attribute_chunk(struct connection *pc, struct packet_player_attribute_chunk *packet)
Definition packets.c:793
remove_packet_from_buffer
void remove_packet_from_buffer(struct socket_packet_buffer *buffer)
Definition packets.c:598
packet_name
const char * packet_name(enum packet_type type)
Definition packets_gen.c:49
post_send_packet_server_join_reply
void post_send_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:640
authentication_type
authentication_type
Definition packets.h:88
AUTH_NEWUSER_RETRY
@ AUTH_NEWUSER_RETRY
Definition packets.h:92
AUTH_NEWUSER_FIRST
@ AUTH_NEWUSER_FIRST
Definition packets.h:90
AUTH_LOGIN_RETRY
@ AUTH_LOGIN_RETRY
Definition packets.h:91
AUTH_LOGIN_FIRST
@ AUTH_LOGIN_FIRST
Definition packets.h:89
send_packet_data
int send_packet_data(struct connection *pc, unsigned char *data, int len, enum packet_type packet_type)
Definition packets.c:213
generic_handle_player_attribute_chunk
void generic_handle_player_attribute_chunk(struct player *pplayer, const struct packet_player_attribute_chunk *chunk)
Definition packets.c:690
send_attribute_block
void send_attribute_block(const struct player *pplayer, struct connection *pconn)
Definition packets.c:746
unit_info_use
unit_info_use
Definition packets.h:81
UNIT_INFO_IDENTITY
@ UNIT_INFO_IDENTITY
Definition packets.h:82
UNIT_INFO_CITY_PRESENT
@ UNIT_INFO_CITY_PRESENT
Definition packets.h:84
UNIT_INFO_CITY_SUPPORTED
@ UNIT_INFO_CITY_SUPPORTED
Definition packets.h:83
packet_handlers_get
const struct packet_handlers * packet_handlers_get(const char *capability)
Definition packets.c:840
packet_handlers_fill_initial
void packet_handlers_fill_initial(struct packet_handlers *phandlers)
Definition packets_gen.c:95655
packets_gen.h
packet_type
packet_type
Definition packets_gen.h:1883
PACKET_LAST
@ PACKET_LAST
Definition packets_gen.h:2078
packets_json.h
len
int len
Definition packhand.c:125
player.h
requirements.h
shared.h
spaceship.h
connection
Definition connection.h:141
connection::capability
char capability[MAX_LEN_CAPSTR]
Definition connection.h:176
connection::buffer
struct socket_packet_buffer * buffer
Definition connection.h:158
data_in
Definition dataio_raw.h:30
packet_handlers
Definition packets.h:97
packet_handlers::force_to_send
int(* force_to_send)(struct connection *pconn, const void *packet, bool force_to_send)
Definition packets.h:101
packet_handlers::no_packet
int(* no_packet)(struct connection *pconn)
Definition packets.h:99
packet_handlers::packet
int(* packet)(struct connection *pconn, const void *packet)
Definition packets.h:100
packet_handlers::send
union packet_handlers::@67 send[PACKET_LAST]
packet_handlers::receive
void *(* receive[PACKET_LAST])(struct connection *pconn)
Definition packets.h:104
packet_header
Definition connection.h:128
packet_player_attribute_chunk
Definition packets_gen.h:591
packet_server_join_reply
Definition packets_gen.h:54
player
Definition player.h:249
socket_packet_buffer
Definition connection.h:121
team.h
tile.h
traderoutes.h
unittype.h
worklist.h