Freeciv-3.2
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);
106
109 const struct packet_server_join_reply
110 *packet);
112 const struct
114
117 *packet);
118
119const struct packet_handlers *packet_handlers_initial(void);
120const struct packet_handlers *packet_handlers_get(const char *capability);
121
122void packets_deinit(void);
123
124#ifdef FREECIV_JSON_CONNECTION
125#include "packets_json.h"
126#else
127
128#define SEND_PACKET_START(packet_type) \
129 unsigned char buffer[MAX_LEN_PACKET]; \
130 struct raw_data_out dout; \
131 \
132 dio_output_init(&dout, buffer, sizeof(buffer)); \
133 dio_put_type_raw(&dout, pc->packet_header.length, 0); \
134 dio_put_type_raw(&dout, pc->packet_header.type, packet_type);
135
136#define SEND_PACKET_END(packet_type) \
137 { \
138 size_t size = dio_output_used(&dout); \
139 \
140 dio_output_rewind(&dout); \
141 dio_put_type_raw(&dout, pc->packet_header.length, size); \
142 fc_assert(!dout.too_short); \
143 return send_packet_data(pc, buffer, size, packet_type); \
144 }
145
146#define SEND_PACKET_DISCARD() \
147 return 0
148
149#define RECEIVE_PACKET_START(packet_type, result) \
150 struct data_in din; \
151 struct packet_type packet_buf, *result = &packet_buf; \
152 \
153 dio_input_init(&din, pc->buffer->data, \
154 data_type_size(pc->packet_header.length)); \
155 { \
156 int size; \
157 \
158 dio_get_type_raw(&din, pc->packet_header.length, &size); \
159 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
160 } \
161 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
162 + data_type_size(pc->packet_header.type)));
163
164#define RECEIVE_PACKET_END(result) \
165 if (!packet_check(&din, pc)) { \
166 return NULL; \
167 } \
168 remove_packet_from_buffer(pc->buffer); \
169 result = fc_malloc(sizeof(*result)); \
170 *result = packet_buf; \
171 return result;
172
173#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
174 log_packet("Error on field '" #field "'" __VA_ARGS__); \
175 return NULL
176
177#endif /* FREECIV_JSON_PROTOCOL */
178
179int send_packet_data(struct connection *pc, unsigned char *data, int len,
181bool packet_check(struct data_in *din, struct connection *pc);
182
183/* Utilities to exchange strings and string vectors. */
184#define PACKET_STRVEC_SEPARATOR '\3'
185#define PACKET_STRVEC_COMPUTE(str, strvec) \
186 if (NULL != strvec) { \
187 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
188 } else { \
189 str[0] = '\0'; \
190 }
191#define PACKET_STRVEC_EXTRACT(strvec, str) \
192 if ('\0' != str[0]) { \
193 strvec = strvec_new(); \
194 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
195 } else { \
196 strvec = NULL; \
197 }
198
199#ifdef __cplusplus
200}
201#endif /* __cplusplus */
202
203#endif /* FC__PACKETS_H */
char * incite_cost
Definition comments.c:75
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:48
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 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::@68 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