Class for handling the server side of the game connection. More...
#include <network_server.h>
Public Types | |
enum | ClientStatus { STATUS_INACTIVE, STATUS_NEWGRFS_CHECK, STATUS_AUTH_GAME, STATUS_AUTH_COMPANY, STATUS_AUTHORIZED, STATUS_MAP_WAIT, STATUS_MAP, STATUS_DONE_MAP, STATUS_PRE_ACTIVE, STATUS_ACTIVE, STATUS_END } |
Status of a client. More... | |
Public Member Functions | |
ServerNetworkGameSocketHandler (SOCKET s) | |
Create a new socket for the server side of the game connection. | |
~ServerNetworkGameSocketHandler () | |
Clear everything related to this client. | |
virtual Packet * | ReceivePacket () |
NetworkRecvStatus | CloseConnection (NetworkRecvStatus status) |
void | GetClientName (char *client_name, size_t size) const |
Get the name of the client, if the user did not send it yet, Client #<no> is used. | |
NetworkRecvStatus | SendMap () |
This sends the map to the client. | |
NetworkRecvStatus | SendErrorQuit (ClientID client_id, NetworkErrorCode errorno) |
Tell the client another client quit with an error. | |
NetworkRecvStatus | SendQuit (ClientID client_id) |
Tell the client another client quit. | |
NetworkRecvStatus | SendShutdown () |
Tell the client we're shutting down. | |
NetworkRecvStatus | SendNewGame () |
Tell the client we're starting a new game. | |
NetworkRecvStatus | SendRConResult (uint16 colour, const char *command) |
Send the result of a console action. | |
NetworkRecvStatus | SendMove (ClientID client_id, CompanyID company_id) |
Tell that a client moved to another company. | |
NetworkRecvStatus | SendClientInfo (NetworkClientInfo *ci) |
Send the client information about a client. | |
NetworkRecvStatus | SendError (NetworkErrorCode error) |
Send an error to the client, and close its connection. | |
NetworkRecvStatus | SendChat (NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data) |
Send a chat message. | |
NetworkRecvStatus | SendJoin (ClientID client_id) |
Tell that a client joined. | |
NetworkRecvStatus | SendFrame () |
Tell the client that they may run to a particular frame. | |
NetworkRecvStatus | SendSync () |
Request the client to sync. | |
NetworkRecvStatus | SendCommand (const CommandPacket *cp) |
Send a command to the client to execute. | |
NetworkRecvStatus | SendCompanyUpdate () |
Send an update about the company password states. | |
NetworkRecvStatus | SendConfigUpdate () |
Send an update about the max company/spectator counts. | |
const char * | GetClientIP () |
Get the IP address/hostname of the connected client. | |
Static Public Member Functions | |
static void | Send () |
Send the packets for the server sockets. | |
static void | AcceptConnection (SOCKET s, const NetworkAddress &address) |
Handle the accepting of a connection to the server. | |
static bool | AllowConnection () |
Whether an connection is allowed or not at this moment. | |
static const char * | GetName () |
Get the name used by the listener. | |
static ServerNetworkGameSocketHandler * | GetByClientID (ClientID client_id) |
Return the client state given it's client-identifier. | |
Data Fields | |
byte | lag_test |
Byte used for lag-testing the client. | |
byte | last_token |
The last random token we did send to verify the client is listening. | |
uint32 | last_token_frame |
The last frame we received the right token. | |
ClientStatus | status |
Status of this client. | |
CommandQueue | outgoing_queue |
The command-queue awaiting delivery. | |
int | receive_limit |
Amount of bytes that we can receive at this moment. | |
struct PacketWriter * | savegame |
Writer used to write the savegame. | |
NetworkAddress | client_address |
IP-address of the client (so he can be banned). | |
Protected Member Functions | |
virtual NetworkRecvStatus | Receive_CLIENT_JOIN (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_COMPANY_INFO (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_GAME_PASSWORD (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_COMPANY_PASSWORD (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_GETMAP (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_MAP_OK (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_ACK (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_COMMAND (Packet *p) |
The client has done a command and wants us to handle it. | |
virtual NetworkRecvStatus | Receive_CLIENT_CHAT (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_SET_PASSWORD (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_SET_NAME (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_QUIT (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_ERROR (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_RCON (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_NEWGRFS_CHECKED (Packet *p) |
virtual NetworkRecvStatus | Receive_CLIENT_MOVE (Packet *p) |
NetworkRecvStatus | SendCompanyInfo () |
Send the client information about the companies. | |
NetworkRecvStatus | SendNewGRFCheck () |
Send the check for the NewGRFs. | |
NetworkRecvStatus | SendWelcome () |
Send the client a welcome message with some basic information. | |
NetworkRecvStatus | SendWait () |
Tell the client that its put in a waiting queue. | |
NetworkRecvStatus | SendNeedGamePassword () |
Request the game password. | |
NetworkRecvStatus | SendNeedCompanyPassword () |
Request the company password. |
Class for handling the server side of the game connection.
Definition at line 29 of file network_server.h.
Status of a client.
Definition at line 57 of file network_server.h.
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler | ( | SOCKET | s | ) |
Create a new socket for the server side of the game connection.
s | The socket to connect with. |
Definition at line 221 of file network_server.cpp.
References _network_client_id, _settings_client, NetworkSettings::bytes_per_frame_burst, NetworkGameSocketHandler::client_id, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::MAX_SIZE, ClientSettings::network, receive_limit, and status.
Referenced by AcceptConnection().
void ServerNetworkGameSocketHandler::AcceptConnection | ( | SOCKET | s, | |
const NetworkAddress & | address | |||
) | [static] |
Handle the accepting of a connection to the server.
s | The socket of the new connection. | |
address | The address of the peer. |
Definition at line 495 of file network.cpp.
References client_address, ServerNetworkGameSocketHandler(), SetWindowDirty(), and WC_CLIENT_LIST.
bool ServerNetworkGameSocketHandler::AllowConnection | ( | ) | [static] |
Whether an connection is allowed or not at this moment.
Definition at line 312 of file network_server.cpp.
References _network_clients_connected, _network_game_info, _settings_client, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientsocket_pool >::CanAllocateItem(), NetworkServerGameInfo::clients_on, NetworkSettings::max_clients, MAX_CLIENTS, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::MAX_SIZE, and ClientSettings::network.
ServerNetworkGameSocketHandler * ServerNetworkGameSocketHandler::GetByClientID | ( | ClientID | client_id | ) | [static] |
Return the client state given it's client-identifier.
client_id | the ClientID to search for |
Definition at line 129 of file network.cpp.
References FOR_ALL_CLIENT_SOCKETS.
Referenced by SendCompanyInfo().
const char * ServerNetworkGameSocketHandler::GetClientIP | ( | ) |
Get the IP address/hostname of the connected client.
Definition at line 1967 of file network_server.cpp.
References client_address, and NetworkAddress::GetHostname().
void ServerNetworkGameSocketHandler::GetClientName | ( | char * | client_name, | |
size_t | size | |||
) | const |
Get the name of the client, if the user did not send it yet, Client #<no> is used.
client_name | The variable to write the name to. | |
size | The amount of bytes we can write. |
Definition at line 2155 of file network_server.cpp.
References NetworkGameSocketHandler::client_id, NetworkClientInfo::client_name, NetworkGameSocketHandler::GetInfo(), StrEmpty(), and ttd_strlcpy().
Referenced by SendError().
static const char* ServerNetworkGameSocketHandler::GetName | ( | ) | [inline, static] |
Get the name used by the listener.
Definition at line 114 of file network_server.h.
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND | ( | Packet * | p | ) | [protected, virtual] |
The client has done a command and wants us to handle it.
p | the packet in which the command was sent |
Only CMD_COMPANY_CTRL is always allowed, for the rest, playas needs to match the company in the packet. If it doesn't, the client has done something pretty naughty (or a bug), and will be kicked
Definition at line 1099 of file network_server.cpp.
References _settings_client, CommandQueue::Append(), CC_ERROR, NetworkGameSocketHandler::client_id, NetworkClientInfo::client_id, CLIENT_ID_SERVER, NetworkClientInfo::client_playas, CommandContainer::cmd, CMD_CLIENT_ID, CMD_COMPANY_CTRL, CMD_SERVER, CMD_SPECTATOR, CommandPacket::company, COMPANY_NEW_COMPANY, COMPANY_SPECTATOR, CommandQueue::Count(), DESTTYPE_CLIENT, GetCommandFlags(), NetworkGameSocketHandler::GetInfo(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetNumItems(), NetworkSocketHandler::HasClientQuit(), IConsolePrintF(), NetworkGameSocketHandler::incoming_queue, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), NetworkSettings::max_commands_in_queue, NetworkSettings::max_companies, ClientSettings::network, NetworkServerSendChat(), CommandContainer::p1, CommandContainer::p2, NetworkGameSocketHandler::ReceiveCommand(), and SendError().
void ServerNetworkGameSocketHandler::Send | ( | ) | [static] |
Send the packets for the server sockets.
Definition at line 325 of file network_server.cpp.
References FOR_ALL_CLIENT_SOCKETS, and SPS_CLOSED.
NetworkRecvStatus ServerNetworkGameSocketHandler::SendChat | ( | NetworkAction | action, | |
ClientID | client_id, | |||
bool | self_send, | |||
const char * | msg, | |||
int64 | data | |||
) |
Send a chat message.
action | The action associated with the message. | |
client_id | The origin of the chat message. | |
self_send | Whether we did send the message. | |
msg | The actual message. | |
data | Arbitrary extra data. |
Definition at line 763 of file network_server.cpp.
References PACKET_SERVER_CHAT, Packet::Send_bool(), Packet::Send_string(), Packet::Send_uint32(), Packet::Send_uint64(), Packet::Send_uint8(), NetworkTCPSocketHandler::SendPacket(), and STATUS_PRE_ACTIVE.
NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo | ( | NetworkClientInfo * | ci | ) |
Send the client information about a client.
ci | The client to send information about. |
Definition at line 349 of file network_server.cpp.
References NetworkClientInfo::client_id, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, INVALID_CLIENT_ID, PACKET_SERVER_CLIENT_INFO, Packet::Send_string(), Packet::Send_uint32(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
Referenced by SendWelcome().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand | ( | const CommandPacket * | cp | ) |
Send a command to the client to execute.
cp | The command to send. |
Definition at line 743 of file network_server.cpp.
References CommandPacket::frame, CommandPacket::my_cmd, PACKET_SERVER_COMMAND, Packet::Send_bool(), Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo | ( | ) | [protected] |
Send the client information about the companies.
Definition at line 363 of file network_server.cpp.
References CLIENT_ID_SERVER, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, FOR_ALL_CLIENT_SOCKETS, GetByClientID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), lastof, NETWORK_CLIENT_NAME_LENGTH, NETWORK_CLIENTS_LENGTH, NETWORK_COMPANY_INFO_VERSION, NetworkPopulateCompanyStats(), PACKET_SERVER_COMPANY_INFO, Packet::Send_bool(), Packet::Send_string(), Packet::Send_uint8(), NetworkTCPSocketHandler::SendPacket(), strecat(), strecpy(), and StrEmpty().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyUpdate | ( | ) |
Send an update about the company password states.
Definition at line 856 of file network_server.cpp.
References _network_company_passworded, PACKET_SERVER_COMPANY_UPDATE, Packet::Send_uint16(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendConfigUpdate | ( | ) |
Send an update about the max company/spectator counts.
Definition at line 866 of file network_server.cpp.
References _settings_client, NetworkSettings::max_companies, NetworkSettings::max_spectators, ClientSettings::network, PACKET_SERVER_CONFIG_UPDATE, Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError | ( | NetworkErrorCode | error | ) |
Send an error to the client, and close its connection.
error | The error to disconnect for. |
Definition at line 429 of file network_server.cpp.
References CC_DEFAULT, NetworkGameSocketHandler::client_id, DEBUG, FOR_ALL_CLIENT_SOCKETS, GetClientName(), GetNetworkErrorMsg(), lastof, NETWORK_CLIENT_NAME_LENGTH, NETWORK_RECV_STATUS_SERVER_ERROR, NetworkAdminClientError(), PACKET_SERVER_ERROR, Packet::Send_uint8(), NetworkTCPSocketHandler::SendPacket(), and STATUS_AUTHORIZED.
Referenced by Receive_CLIENT_COMMAND(), and SendMap().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendErrorQuit | ( | ClientID | client_id, | |
NetworkErrorCode | errorno | |||
) |
Tell the client another client quit with an error.
client_id | The client that quit. | |
errorno | The reason the client quit. |
Definition at line 784 of file network_server.cpp.
References PACKET_SERVER_ERROR_QUIT, Packet::Send_uint32(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendFrame | ( | ) |
Tell the client that they may run to a particular frame.
Definition at line 703 of file network_server.cpp.
References _frame_counter, _frame_counter_max, _sync_seed_1, last_token, PACKET_SERVER_FRAME, Packet::Send_uint32(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendJoin | ( | ClientID | client_id | ) |
Tell that a client joined.
client_id | The client that joined. |
Definition at line 692 of file network_server.cpp.
References PACKET_SERVER_JOIN, Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendMove | ( | ClientID | client_id, | |
CompanyID | company_id | |||
) |
Tell that a client moved to another company.
client_id | The client that moved. | |
company_id | The company the client moved to. |
Definition at line 845 of file network_server.cpp.
References PACKET_SERVER_MOVE, Packet::Send_uint32(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedCompanyPassword | ( | ) | [protected] |
Request the company password.
Definition at line 507 of file network_server.cpp.
References _frame_counter, _settings_client, _settings_game, GameSettings::game_creation, GameCreationSettings::generation_seed, NetworkGameSocketHandler::last_frame, NetworkGameSocketHandler::last_frame_server, ClientSettings::network, NetworkSettings::network_id, NETWORK_RECV_STATUS_MALFORMED_PACKET, PACKET_SERVER_NEED_COMPANY_PASSWORD, Packet::Send_string(), Packet::Send_uint32(), NetworkTCPSocketHandler::SendPacket(), and STATUS_AUTH_COMPANY.
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedGamePassword | ( | ) | [protected] |
Request the game password.
Definition at line 492 of file network_server.cpp.
References _frame_counter, NetworkGameSocketHandler::last_frame, NetworkGameSocketHandler::last_frame_server, NETWORK_RECV_STATUS_MALFORMED_PACKET, PACKET_SERVER_NEED_GAME_PASSWORD, NetworkTCPSocketHandler::SendPacket(), and STATUS_AUTH_GAME.
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGame | ( | ) |
Tell the client we're starting a new game.
Definition at line 818 of file network_server.cpp.
References PACKET_SERVER_NEWGAME, and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck | ( | ) | [protected] |
Send the check for the NewGRFs.
Definition at line 472 of file network_server.cpp.
References _grfconfig, GRFConfig::flags, GCF_STATIC, HasBit(), GRFConfig::ident, GRFConfig::next, PACKET_SERVER_CHECK_NEWGRFS, Packet::Send_uint8(), NetworkSocketHandler::SendGRFIdentifier(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendQuit | ( | ClientID | client_id | ) |
Tell the client another client quit.
client_id | The client that quit. |
Definition at line 799 of file network_server.cpp.
References PACKET_SERVER_QUIT, Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendRConResult | ( | uint16 | colour, | |
const char * | command | |||
) |
Send the result of a console action.
colour | The colour of the result. | |
command | The command that was executed. |
Definition at line 830 of file network_server.cpp.
References PACKET_SERVER_RCON, Packet::Send_string(), Packet::Send_uint16(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendShutdown | ( | ) |
Tell the client we're shutting down.
Definition at line 810 of file network_server.cpp.
References PACKET_SERVER_SHUTDOWN, and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync | ( | ) |
Request the client to sync.
Definition at line 726 of file network_server.cpp.
References _frame_counter, _sync_seed_1, PACKET_SERVER_SYNC, Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait | ( | ) | [protected] |
Tell the client that its put in a waiting queue.
Definition at line 555 of file network_server.cpp.
References NetworkGameSocketHandler::client_id, FOR_ALL_CLIENT_SOCKETS, NetworkGameSocketHandler::GetInfo(), NetworkClientInfo::join_date, PACKET_SERVER_WAIT, Packet::Send_uint8(), NetworkTCPSocketHandler::SendPacket(), and STATUS_MAP_WAIT.
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome | ( | ) | [protected] |
Send the client a welcome message with some basic information.
Definition at line 524 of file network_server.cpp.
References _frame_counter, _network_game_info, _settings_client, _settings_game, NetworkGameSocketHandler::client_id, CLIENT_ID_SERVER, NetworkServerGameInfo::clients_on, FOR_ALL_CLIENT_SOCKETS, GameSettings::game_creation, GameCreationSettings::generation_seed, NetworkClientInfo::GetByClientID(), NetworkGameSocketHandler::last_frame, NetworkGameSocketHandler::last_frame_server, ClientSettings::network, NetworkSettings::network_id, NETWORK_RECV_STATUS_MALFORMED_PACKET, PACKET_SERVER_WELCOME, Packet::Send_string(), Packet::Send_uint32(), SendClientInfo(), NetworkTCPSocketHandler::SendPacket(), and STATUS_AUTHORIZED.