# NetworkGameSocketHandler Class Reference

Base socket handler for all TCP sockets. More...

`#include <tcp_game.h>`

Inheritance diagram for NetworkGameSocketHandler:

## Public Member Functions
NetworkRecvStatus | CloseConnection (bool error=true)
| Functions to help ReceivePacket/SendPacket a bit A socket can make errors.
virtual NetworkRecvStatus | CloseConnection (NetworkRecvStatus status)=0
void | SetInfo (NetworkClientInfo *info)
NetworkClientInfo * | GetInfo () const
NetworkRecvStatus | ReceivePackets ()
| Do the actual receiving of packets.
const char * | ReceiveCommand (Packet *p, CommandPacket *cp)
| Receives a command from the network.
void | SendCommand (Packet *p, const CommandPacket *cp)
| Sends a command over the network.
## Data Fields
ClientID | client_id
| Client identifier.
uint32 | last_frame
| Last frame we have executed.
uint32 | last_frame_server
| Last frame the server has executed.
CommandQueue | incoming_queue
| The command-queue awaiting handling.
uint | last_packet
| Time we received the last frame.
## Protected Member Functions
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_FULL)
| Notification that the server is full.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_BANNED)
| Notification that the client trying to join is banned.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_JOIN)
| Try to join the server: string OpenTTD revision (norev000 if no revision).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_ERROR)
| The client made an error: uint8 Error code caused (see NetworkErrorCode).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_COMPANY_INFO)
| Request company information (in detail).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_COMPANY_INFO)
| Sends information about the companies (one packet per company): uint8 Version of the structure of this packet (NETWORK_COMPANY_INFO_VERSION).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_CLIENT_INFO)
| Send information about a client: uint32 ID of the client (always unique on a server.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_NEED_GAME_PASSWORD)
| Indication to the client that the server needs a game password.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_NEED_COMPANY_PASSWORD)
| Indication to the client that the server needs a company password: uint32 Generation seed.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_GAME_PASSWORD)
| Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_COMPANY_PASSWORD)
| Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_WELCOME)
| The client is joined and ready to receive his map: uint32 Own client ID.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_GETMAP)
| Request the map from the server.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_WAIT)
| Notification that another client is currently receiving the map: uint8 Number of clients awaiting the map.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_MAP_BEGIN)
| Sends that the server will begin with sending the map to the client: uint32 Current frame.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_MAP_SIZE)
| Sends the size of the map to the client.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_MAP_DATA)
| Sends the data of the map to the client: Contains a part of the map (until max size of packet).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_MAP_DONE)
| Sends that all data of the map are sent to the client:.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_MAP_OK)
| Tell the server that we are done receiving/loading the map.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_JOIN)
| A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO: uint32 ID of the client that just joined the game.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_FRAME)
| Sends the current frame counter to the client: uint32 Frame counter uint32 Frame counter max (how far may the client walk before the server?) uint32 General seed 1 (dependant on compile settings, not default).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_SYNC)
| Sends a sync-check to the client: uint32 Frame counter.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_ACK)
| Tell the server we are done with this frame: uint32 Current frame counter of the client.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_COMMAND)
| Send a DoCommand to the Server: uint8 ID of the company (0..MAX_COMPANIES-1).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_COMMAND)
| Sends a DoCommand to the client: uint8 ID of the company (0..MAX_COMPANIES-1).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_CHAT)
| Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_CHAT)
| Sends a chat-packet to the client: uint8 ID of the action (see NetworkAction).
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_SET_PASSWORD)
| Set the password for the clients current company: string The password.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_SET_NAME)
| Gives the client a new name: string New name of the client.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_QUIT)
| The client is quiting the game.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_ERROR)
| The client made an error and is quiting the game.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_QUIT)
| Notification that a client left the game: uint32 ID of the client.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_ERROR_QUIT)
| Inform all clients that one client made an error and thus has quit/been disconnected: uint32 ID of the client that caused the error.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_SHUTDOWN)
| Let the clients know that the server is closing.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_NEWGAME)
| Let the clients know that the server is loading a new map.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_RCON)
| Send the result of an issues RCon command back to the client: uint16 Colour code.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_RCON)
| Send an RCon command to the server: string RCon password.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_CHECK_NEWGRFS)
| Sends information about all used GRFs to the client: uint8 Amount of GRFs (the following data is repeated this many times, i.e.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_NEWGRFS_CHECKED)
| Tell the server that we have the required GRFs.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_MOVE)
| Move a client from one company into another: uint32 ID of the client.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_CLIENT_MOVE)
| Request the server to move this client into another company: uint8 ID of the company the client wants to join.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_COMPANY_UPDATE)
| Update the clients knowledge of which company is password protected: uint16 Bitwise representation of each company.
| DECLARE_GAME_RECEIVE_COMMAND (PACKET_SERVER_CONFIG_UPDATE)
| Update the clients knowledge of the max settings: uint8 Maximum number of companies allowed.
NetworkRecvStatus | HandlePacket (Packet *p)
| Handle the given packet, i.e.
| NetworkGameSocketHandler (SOCKET s)
| Create a new socket for the game connection.
## Private Attributes
NetworkClientInfo * | info
| Client info related to this socket.

## Detailed Description

Base socket handler for all TCP sockets.

Definition at line 155 of file tcp_game.h.

## Constructor & Destructor Documentation

NetworkGameSocketHandler::NetworkGameSocketHandler | ( | SOCKET | s | ) | `[protected]`

Create a new socket for the game connection.

Parameters:

  | s | The socket to connect with.

Definition at line 28 of file tcp_game.cpp.

References _realtime_tick, last_frame, last_frame_server, last_packet, and NetworkTCPSocketHandler::sock.

## Member Function Documentation

NetworkRecvStatus NetworkGameSocketHandler::CloseConnection | ( | bool | error = `true` | ) | `[virtual]`

Functions to help ReceivePacket/SendPacket a bit A socket can make errors.

When that happens this handles what to do. For clients: close connection and drop back to main-menu For servers: close connection and that is it

Returns:
  the new status

Reimplemented from NetworkTCPSocketHandler.

Definition at line 43 of file tcp_game.cpp.

References _network_server, _networking, _switch_mode, NETWORK_RECV_STATUS_CONN_LOST, and NETWORK_RECV_STATUS_SERVER_ERROR.

Referenced by ClientNetworkGameSocketHandler::CheckConnection(), and HandlePacket().

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_CONFIG_UPDATE |  | ) | `[protected]`

Update the clients knowledge of the max settings: uint8 Maximum number of companies allowed.

uint8 Maximum number of spectators allowed.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_MOVE |  | ) | `[protected]`

Request the server to move this client into another company: uint8 ID of the company the client wants to join.

string Password, if the company is password protected.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_MOVE |  | ) | `[protected]`

Move a client from one company into another: uint32 ID of the client.

uint8 ID of the new company.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_CHECK_NEWGRFS |  | ) | `[protected]`

Sends information about all used GRFs to the client: uint8 Amount of GRFs (the following data is repeated this many times, i.e.

per GRF data). uint32 GRF ID 16 * uint8 MD5 checksum of the GRF

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_RCON |  | ) | `[protected]`

Send an RCon command to the server: string RCon password.

string Command to be executed.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_RCON |  | ) | `[protected]`

Send the result of an issues RCon command back to the client: uint16 Colour code.

string Output of the RCon command

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_ERROR_QUIT |  | ) | `[protected]`

Inform all clients that one client made an error and thus has quit/been disconnected: uint32 ID of the client that caused the error.

uint8 Code of the error caused (see NetworkErrorCode).

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_ERROR |  | ) | `[protected]`

The client made an error and is quiting the game.

uint8 Error of the code caused (see NetworkErrorCode).

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_CHAT |  | ) | `[protected]`

Sends a chat-packet to the client: uint8 ID of the action (see NetworkAction).

uint32 ID of the client (origin of the chat). string Message (max NETWORK_CHAT_LENGTH). uint64 data (used e.g. for 'give money' actions).

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_CHAT |  | ) | `[protected]`

Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).

uint8 ID of the destination type (see DestType). uint32 ID of the client or company (destination of the chat). string Message (max NETWORK_CHAT_LENGTH). uint64 data (used e.g. for 'give money' actions).

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_COMMAND |  | ) | `[protected]`

Sends a DoCommand to the client: uint8 ID of the company (0..MAX_COMPANIES-1).

uint32 ID of the command (see command.h). uint32 P1 (free variable used in DoCommand). uint32 P2. uint32 Tile where this is taking place. string Text. uint8 ID of the callback. uint32 Frame of execution.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_COMMAND |  | ) | `[protected]`

Send a DoCommand to the Server: uint8 ID of the company (0..MAX_COMPANIES-1).

uint32 ID of the command (see command.h). uint32 P1 (free variables used in DoCommand). uint32 P2 uint32 Tile where this is taking place. string Text. uint8 ID of the callback.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_ACK |  | ) | `[protected]`

Tell the server we are done with this frame: uint32 Current frame counter of the client.

uint8 The random token that the server sent in the PACKET_SERVER_FRAME packet.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_SYNC |  | ) | `[protected]`

Sends a sync-check to the client: uint32 Frame counter.

uint32 General seed 1. uint32 General seed 2 (dependant on compile settings, not default).

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_FRAME |  | ) | `[protected]`

Sends the current frame counter to the client: uint32 Frame counter uint32 Frame counter max (how far may the client walk before the server?) uint32 General seed 1 (dependant on compile settings, not default).

uint32 General seed 2 (dependant on compile settings, not default). uint8 Random token to validate the client is actually listening (only occasionally present).

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_MAP_SIZE |  | ) | `[protected]`

Sends the size of the map to the client.

uint32 Size of the (compressed) map (in bytes).

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_GETMAP |  | ) | `[protected]`

Request the map from the server.

uint32 NewGRF version (release versions of OpenTTD only).

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_WELCOME |  | ) | `[protected]`

The client is joined and ready to receive his map: uint32 Own client ID.

uint32 Generation seed. string Network ID of the server.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_COMPANY_PASSWORD |  | ) | `[protected]`

Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).

string The password.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_GAME_PASSWORD |  | ) | `[protected]`

Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).

string The password.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_NEED_COMPANY_PASSWORD |  | ) | `[protected]`

Indication to the client that the server needs a company password: uint32 Generation seed.

string Network ID of the server.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_CLIENT_INFO |  | ) | `[protected]`

Send information about a client: uint32 ID of the client (always unique on a server.

1 = server, 0 is invalid). uint8 ID of the company the client is playing as (255 for spectators). string Name of the client.

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_SERVER_COMPANY_INFO |  | ) | `[protected]`

Sends information about the companies (one packet per company): uint8 Version of the structure of this packet (NETWORK_COMPANY_INFO_VERSION).

bool Contains data (false marks the end of updates). uint8 ID of the company. string Name of the company. uint32 Year the company was inaugurated. uint64 Value. uint64 Money. uint64 Income. uint16 Performance (last quarter). bool Company is password protected. uint16 Number of trains. uint16 Number of lorries. uint16 Number of busses. uint16 Number of planes. uint16 Number of ships. uint16 Number of train stations. uint16 Number of lorry stations. uint16 Number of bus stops. uint16 Number of airports and heliports. uint16 Number of harbours. bool Company is an AI. string Client names (comma separated list)

Reimplemented in ClientNetworkGameSocketHandler.

NetworkGameSocketHandler::DECLARE_GAME_RECEIVE_COMMAND | ( | PACKET_CLIENT_JOIN |  | ) | `[protected]`

Try to join the server: string OpenTTD revision (norev000 if no revision).

string Name of the client (max NETWORK_NAME_LENGTH). uint8 ID of the company to play as (1..MAX_COMPANIES). uint8 ID of the clients Language.

Reimplemented in ServerNetworkGameSocketHandler.

NetworkRecvStatus NetworkGameSocketHandler::HandlePacket | ( | Packet * | p | ) | `[protected]`

Handle the given packet, i.e.

pass it to the right parser receive command.

Parameters:

  | p | the packet to handle

Returns:
  NetworkRecvStatus of handling.

Definition at line 70 of file tcp_game.cpp.

References _realtime_tick, client_id, CloseConnection(), DEBUG, GAME_COMMAND, NetworkSocketHandler::HasClientQuit(), last_packet, NETWORK_RECV_STATUS_MALFORMED_PACKET, PACKET_CLIENT_ACK, PACKET_CLIENT_CHAT, PACKET_CLIENT_COMMAND, PACKET_CLIENT_COMPANY_INFO, PACKET_CLIENT_COMPANY_PASSWORD, PACKET_CLIENT_ERROR, PACKET_CLIENT_GAME_PASSWORD, PACKET_CLIENT_GETMAP, PACKET_CLIENT_JOIN, PACKET_CLIENT_MAP_OK, PACKET_CLIENT_MOVE, PACKET_CLIENT_NEWGRFS_CHECKED, PACKET_CLIENT_QUIT, PACKET_CLIENT_RCON, PACKET_CLIENT_SET_NAME, PACKET_CLIENT_SET_PASSWORD, PACKET_END, PACKET_SERVER_BANNED, PACKET_SERVER_CHAT, PACKET_SERVER_CHECK_NEWGRFS, PACKET_SERVER_CLIENT_INFO, PACKET_SERVER_COMMAND, PACKET_SERVER_COMPANY_INFO, PACKET_SERVER_COMPANY_UPDATE, PACKET_SERVER_CONFIG_UPDATE, PACKET_SERVER_ERROR, PACKET_SERVER_ERROR_QUIT, PACKET_SERVER_FRAME, PACKET_SERVER_FULL, PACKET_SERVER_JOIN, PACKET_SERVER_MAP_BEGIN, PACKET_SERVER_MAP_DATA, PACKET_SERVER_MAP_DONE, PACKET_SERVER_MAP_SIZE, PACKET_SERVER_MOVE, PACKET_SERVER_NEED_COMPANY_PASSWORD, PACKET_SERVER_NEED_GAME_PASSWORD, PACKET_SERVER_NEWGAME, PACKET_SERVER_QUIT, PACKET_SERVER_RCON, PACKET_SERVER_SHUTDOWN, PACKET_SERVER_SYNC, PACKET_SERVER_WAIT, and PACKET_SERVER_WELCOME.

Referenced by ReceivePackets().

const char * NetworkGameSocketHandler::ReceiveCommand | ( | Packet * | p,
|  | CommandPacket * | cp |
| ) |  |  |

Receives a command from the network.

Parameters:

  | p | the packet to read from.
  | cp | the struct to write the data to.

Returns:
  an error message. When NULL there has been no error.

Definition at line 296 of file network_command.cpp.

References _callback_table, CommandContainer::callback, CommandContainer::cmd, CMD_FLAGS_MASK, CMD_OFFLINE, CommandPacket::company, GetCommandFlags(), IsValidCommand(), lengthof, CommandContainer::p1, CommandContainer::p2, Packet::Recv_string(), CommandContainer::text, and CommandContainer::tile.

NetworkRecvStatus NetworkGameSocketHandler::ReceivePackets | ( |  | ) |

Do the actual receiving of packets.

As long as HandlePacket returns OKAY packets are handled. Upon failure, or no more packets to process the last result of HandlePacket is returned.

Returns:
  NetworkRecvStatus of the last handled packet.

Definition at line 140 of file tcp_game.cpp.

References HandlePacket(), NETWORK_RECV_STATUS_OKAY, and NetworkTCPSocketHandler::ReceivePacket().

Referenced by ClientNetworkGameSocketHandler::Receive().

void NetworkGameSocketHandler::SendCommand | ( | Packet * | p,
|  | const CommandPacket * | cp |
| ) |  |  |

Sends a command over the network.

Parameters:

  | p | the packet to send it in.
  | cp | the packet to actually send.

Definition at line 321 of file network_command.cpp.

References _callback_table, CommandContainer::callback, CommandContainer::cmd, CommandPacket::company, DEBUG, lengthof, CommandContainer::p1, CommandContainer::p2, Packet::Send_string(), CommandContainer::text, and CommandContainer::tile.

The documentation for this class was generated from the following files:

- src/network/core/tcp_game.h
- src/network/core/tcp_game.cpp
- src/network/network_command.cpp