Freeciv21
Develop your civilization from humble roots to a global empire
packets.cpp File Reference
#include <cstring>
#include "capability.h"
#include "connection.h"
#include "fcintl.h"
#include "log.h"
#include "support.h"
#include "dataio.h"
#include "game.h"
#include "packets.h"
#include <QRegularExpression>
#include <zlib.h>
+ Include dependency graph for packets.cpp:

Go to the source code of this file.

Macros

#define JUMBO_SIZE   0xffff
 
#define COMPRESSION_BORDER   (16 * 1024 + 1)
 
#define PACKET_STRVEC_SEPARATOR   '\3'
 
#define JUMBO_BORDER   (64 * 1024 - COMPRESSION_BORDER - 1)
 
#define log_compress   log_debug
 
#define log_compress2   log_debug
 
#define MAX_DECOMPRESSION   400
 
#define PACKET_SIZE_STATISTICS   0
 
#define MAX_LEN_COMPRESS_QUEUE   (MAX_LEN_BUFFER / 2)
 

Typedefs

typedef QHash< QString, struct packet_handlers * > packetsHash
 

Functions

static int get_compression_level ()
 Returns the compression level. More...
 
static bool conn_compression_flush (struct connection *pconn)
 Send all waiting data. More...
 
bool conn_compression_thaw (struct connection *pconn)
 Thaw the connection. More...
 
int send_packet_data (struct connection *pc, unsigned char *data, int len, enum packet_type packet_type)
 It returns the request id of the outgoing packet (or 0 if is_server()). More...
 
void * get_packet_from_connection_raw (struct connection *pc, enum packet_type *ptype)
 Read and return a packet from the connection 'pc'. More...
 
void remove_packet_from_buffer (struct socket_packet_buffer *buffer)
 Remove the packet from the buffer. More...
 
void packet_header_init (struct packet_header *packet_header)
 Set the packet header field lengths used for the login protocol, before the capability of the connection could be checked. More...
 
static void packet_header_set (struct packet_header *packet_header)
 Set the packet header field lengths used after the login protocol, after the capability of the connection could be checked. More...
 
void post_send_packet_server_join_reply (struct connection *pconn, const struct packet_server_join_reply *packet)
 Modify if needed the packet header field lengths. More...
 
void post_receive_packet_server_join_reply (struct connection *pconn, const struct packet_server_join_reply *packet)
 Modify if needed the packet header field lengths. More...
 
bool packet_check (struct data_in *din, struct connection *pc)
 Sanity check packet. More...
 
void generic_handle_player_attribute_chunk (struct player *pplayer, const struct packet_player_attribute_chunk *chunk)
 Updates pplayer->attribute_block according to the given packet. More...
 
void send_attribute_block (const struct player *pplayer, struct connection *pconn)
 Split the attribute block into chunks and send them over pconn. More...
 
void pre_send_packet_player_attribute_chunk (struct connection *pc, struct packet_player_attribute_chunk *packet)
 Test and log for sending player attribute_block. More...
 
static void packet_handlers_free ()
 Destroy the packet handler hash table. More...
 
const struct packet_handlerspacket_handlers_initial ()
 Returns the packet handlers variant with no special capability. More...
 
const struct packet_handlerspacket_handlers_get (const char *capability)
 Returns the packet handlers variant for 'capability'. More...
 
void packets_deinit ()
 Call when there is no longer a requirement for protocol processing. More...
 
void packet_strvec_compute (char str[MAX_LEN_PACKET], QVector< QString > *qstrvec)
 
QVector< QString > * packet_strvec_extract (const char *str)
 
void qstrvec_from_str (QVector< QString > *psv, char separator, const char *str)
 Build the string vector from a string until 'str_size' bytes are read. More...
 

Variables

const char *const packet_functional_capability
 
static int stat_size_alone = 0
 
static int stat_size_uncompressed = 0
 
static int stat_size_compressed = 0
 
static int stat_size_no_compression = 0
 

Macro Definition Documentation

◆ COMPRESSION_BORDER

#define COMPRESSION_BORDER   (16 * 1024 + 1)

Definition at line 39 of file packets.cpp.

◆ JUMBO_BORDER

#define JUMBO_BORDER   (64 * 1024 - COMPRESSION_BORDER - 1)

Definition at line 44 of file packets.cpp.

◆ JUMBO_SIZE

#define JUMBO_SIZE   0xffff

Definition at line 34 of file packets.cpp.

◆ log_compress

#define log_compress   log_debug

Definition at line 46 of file packets.cpp.

◆ log_compress2

#define log_compress2   log_debug

Definition at line 47 of file packets.cpp.

◆ MAX_DECOMPRESSION

#define MAX_DECOMPRESSION   400

Definition at line 49 of file packets.cpp.

◆ MAX_LEN_COMPRESS_QUEUE

#define MAX_LEN_COMPRESS_QUEUE   (MAX_LEN_BUFFER / 2)

◆ PACKET_SIZE_STATISTICS

#define PACKET_SIZE_STATISTICS   0

Definition at line 55 of file packets.cpp.

◆ PACKET_STRVEC_SEPARATOR

#define PACKET_STRVEC_SEPARATOR   '\3'

Definition at line 40 of file packets.cpp.

Typedef Documentation

◆ packetsHash

typedef QHash<QString, struct packet_handlers *> packetsHash

Definition at line 59 of file packets.cpp.

Function Documentation

◆ conn_compression_flush()

static bool conn_compression_flush ( struct connection pconn)
static

Send all waiting data.

Return TRUE on success.

Definition at line 88 of file packets.cpp.

Referenced by conn_compression_thaw(), and send_packet_data().

◆ conn_compression_thaw()

bool conn_compression_thaw ( struct connection pconn)

Thaw the connection.

Then maybe compress the data waiting to send them to the connection. Returns TRUE on success. See also conn_compression_freeze().

Definition at line 160 of file packets.cpp.

Referenced by conn_list_compression_thaw(), connection_attach_real(), establish_new_connection(), and finish_processing_request().

◆ generic_handle_player_attribute_chunk()

void generic_handle_player_attribute_chunk ( struct player pplayer,
const struct packet_player_attribute_chunk *  chunk 
)

Updates pplayer->attribute_block according to the given packet.

Definition at line 639 of file packets.cpp.

Referenced by handle_player_attribute_chunk().

◆ get_compression_level()

static int get_compression_level ( )
inlinestatic

Returns the compression level.

Initilialize it if needed.

Definition at line 70 of file packets.cpp.

Referenced by conn_compression_flush().

◆ get_packet_from_connection_raw()

void* get_packet_from_connection_raw ( struct connection pc,
enum packet_type *  ptype 
)

Read and return a packet from the connection 'pc'.

The type of the packet is written in 'ptype'. On error, the connection is closed and the function returns nullptr.

Definition at line 329 of file packets.cpp.

◆ packet_check()

bool packet_check ( struct data_in din,
struct connection pc 
)

Sanity check packet.

Definition at line 617 of file packets.cpp.

◆ packet_handlers_free()

static void packet_handlers_free ( )
static

Destroy the packet handler hash table.

Definition at line 744 of file packets.cpp.

Referenced by packets_deinit().

◆ packet_handlers_get()

const struct packet_handlers* packet_handlers_get ( const char *  capability)

Returns the packet handlers variant for 'capability'.

Definition at line 766 of file packets.cpp.

Referenced by conn_set_capability().

◆ packet_handlers_initial()

const struct packet_handlers* packet_handlers_initial ( )

Returns the packet handlers variant with no special capability.

Definition at line 749 of file packets.cpp.

Referenced by init_packet_hashs(), and packet_handlers_get().

◆ packet_header_init()

void packet_header_init ( struct packet_header packet_header)

Set the packet header field lengths used for the login protocol, before the capability of the connection could be checked.

NB: These values cannot be changed for backward compatibility reasons.

Definition at line 572 of file packets.cpp.

Referenced by connection_common_init().

◆ packet_header_set()

static void packet_header_set ( struct packet_header packet_header)
inlinestatic

Set the packet header field lengths used after the login protocol, after the capability of the connection could be checked.

Definition at line 582 of file packets.cpp.

Referenced by post_receive_packet_server_join_reply(), and post_send_packet_server_join_reply().

◆ packet_strvec_compute()

◆ packet_strvec_extract()

◆ packets_deinit()

void packets_deinit ( )

Call when there is no longer a requirement for protocol processing.

All connections must have been closed.

Definition at line 804 of file packets.cpp.

Referenced by at_exit(), and close_connections_and_socket().

◆ post_receive_packet_server_join_reply()

void post_receive_packet_server_join_reply ( struct connection pconn,
const struct packet_server_join_reply *  packet 
)

Modify if needed the packet header field lengths.

Definition at line 606 of file packets.cpp.

◆ post_send_packet_server_join_reply()

void post_send_packet_server_join_reply ( struct connection pconn,
const struct packet_server_join_reply *  packet 
)

Modify if needed the packet header field lengths.

Definition at line 595 of file packets.cpp.

◆ pre_send_packet_player_attribute_chunk()

void pre_send_packet_player_attribute_chunk ( struct connection pc,
struct packet_player_attribute_chunk *  packet 
)

Test and log for sending player attribute_block.

Definition at line 725 of file packets.cpp.

◆ qstrvec_from_str()

void qstrvec_from_str ( QVector< QString > *  psv,
char  separator,
const char *  str 
)

Build the string vector from a string until 'str_size' bytes are read.

Passing -1 for 'str_size' will assume 'str' as the expected format. Note it's a bit dangerous.

This string format is a list of strings separated by 'separator'.

See also strvec_to_str().

Definition at line 838 of file packets.cpp.

Referenced by packet_strvec_extract().

◆ remove_packet_from_buffer()

void remove_packet_from_buffer ( struct socket_packet_buffer buffer)

Remove the packet from the buffer.

Definition at line 553 of file packets.cpp.

◆ send_attribute_block()

void send_attribute_block ( const struct player pplayer,
struct connection pconn 
)

Split the attribute block into chunks and send them over pconn.

Definition at line 681 of file packets.cpp.

Referenced by attribute_flush(), handle_player_attribute_block(), and send_all_info().

◆ send_packet_data()

int send_packet_data ( struct connection pc,
unsigned char *  data,
int  len,
enum packet_type packet_type packet_type   
)

It returns the request id of the outgoing packet (or 0 if is_server()).

Definition at line 176 of file packets.cpp.

Variable Documentation

◆ packet_functional_capability

const char* const packet_functional_capability

Definition at line 57 of file packets.cpp.

Referenced by packet_handlers_get().

◆ stat_size_alone

int stat_size_alone = 0
static

Definition at line 62 of file packets.cpp.

Referenced by send_packet_data().

◆ stat_size_compressed

int stat_size_compressed = 0
static

Definition at line 64 of file packets.cpp.

Referenced by conn_compression_flush(), and send_packet_data().

◆ stat_size_no_compression

int stat_size_no_compression = 0
static

Definition at line 65 of file packets.cpp.

Referenced by conn_compression_flush(), and send_packet_data().

◆ stat_size_uncompressed

int stat_size_uncompressed = 0
static

Definition at line 63 of file packets.cpp.

Referenced by conn_compression_flush(), and send_packet_data().