OpenTTD Source 20260421-master-gc2fbc6fdeb
QueryNetworkGameSocketHandler Class Reference

Class for handling the client side of querying a game server. More...

#include <network_query.h>

Inheritance diagram for QueryNetworkGameSocketHandler:
NetworkGameSocketHandler NetworkTCPSocketHandler NetworkSocketHandler

Public Member Functions

 QueryNetworkGameSocketHandler (SOCKET s, std::string_view connection_string)
 Create a new socket for the client side of querying game server.
NetworkRecvStatus CloseConnection (NetworkRecvStatus status) override
 Close the network connection due to the given status.
Public Member Functions inherited from NetworkGameSocketHandler
NetworkRecvStatus CloseConnection (bool error=true) override
 Functions to help ReceivePacket/SendPacket a bit A socket can make errors.
void SetInfo (NetworkClientInfo *info)
 Sets the client info for this socket handler.
NetworkClientInfoGetInfo () const
 Gets the client info of this socket handler.
NetworkRecvStatus ReceivePackets ()
 Do the actual receiving of packets.
std::optional< std::string_view > ReceiveCommand (Packet &p, CommandPacket &cp)
 Receives a command from the network.
void SendCommand (Packet &p, const CommandPacket &cp)
 Sends a command over the network.
bool IsPendingDeletion () const
 Is this pending for deletion and as such should not be accessed anymore.
void DeferDeletion ()
 Mark this socket handler for deletion, once iterating the socket handlers is done.
Public Member Functions inherited from NetworkTCPSocketHandler
bool IsConnected () const
 Whether this socket is currently bound to a socket.
void CloseSocket ()
 Close the actual socket of the connection.
virtual void SendPacket (std::unique_ptr< Packet > &&packet)
 This function puts the packet in the send-queue and it is send as soon as possible.
SendPacketsState SendPackets (bool closing_down=false)
 Sends all the buffered packets out for this client.
virtual std::unique_ptr< PacketReceivePacket ()
 Receives a packet for the given client.
bool CanSendReceive ()
 Check whether this socket can send or receive something.
bool HasSendQueue ()
 Whether there is something pending in the send queue.
 NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET)
 Construct a socket handler for a TCP connection.
 ~NetworkTCPSocketHandler () override
 Close the socket.
Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()=default
 Create a new unbound socket.
virtual ~NetworkSocketHandler ()=default
 Ensure the destructor of the sub classes are called as well.
void MarkClosed ()
 Mark the connection as closed.
bool HasClientQuit () const
 Whether the current client connected to the socket has quit.
void Reopen ()
 Reopen the socket so we can send/receive stuff again.

Static Public Member Functions

static void QueryServer (SOCKET s, std::string_view connection_string)
 Start to query a server based on an open socket.
static void SendReceive ()
 Check if any query needs to send or receive.
Static Public Member Functions inherited from NetworkGameSocketHandler
static void ProcessDeferredDeletions ()
 Actually delete the socket handlers that were marked for deletion.

Protected Member Functions

NetworkRecvStatus ReceiveServerFull (Packet &p) override
 Notification that the server is full.
NetworkRecvStatus ReceiveServerBanned (Packet &p) override
 Notification that the client trying to join is banned.
NetworkRecvStatus ReceiveServerError (Packet &p) override
 The client made an error: uint8_t Error code caused (see NetworkErrorCode).
NetworkRecvStatus ReceiveServerGameInfo (Packet &p) override
 Sends information about the game.
NetworkRecvStatus SendGameInfo ()
 Query the server for server information.
bool CheckConnection ()
 Check the connection's state, i.e.
void Send ()
 Send the packets of this socket handler.
bool Receive ()
 Check whether we received/can send some data from/to the server and when that's the case handle it appropriately.
Protected Member Functions inherited from NetworkGameSocketHandler
NetworkRecvStatus ReceiveInvalidPacket (PacketGameType type)
 Helper for logging receiving invalid packets.
virtual NetworkRecvStatus ReceiveClientJoin (Packet &p)
 Try to join the server: string OpenTTD revision (norev0000 if no revision).
virtual NetworkRecvStatus ReceiveClientGameInfo (Packet &p)
 Request game information.
virtual NetworkRecvStatus ReceiveServerClientInfo (Packet &p)
 Send information about a client: uint32_t ID of the client (always unique on a server.
virtual NetworkRecvStatus ReceiveClientIdentify (Packet &p)
 The client tells the server about the identity of the client: string Name of the client (max NETWORK_NAME_LENGTH).
virtual NetworkRecvStatus ReceiveServerAuthenticationRequest (Packet &p)
 Indication to the client that it needs to authenticate: uint8_t The NetworkAuthenticationMethod to use.
virtual NetworkRecvStatus ReceiveClientAuthenticationResponse (Packet &p)
 Send the response to the authentication request: 32 * uint8_t Public key of the client.
virtual NetworkRecvStatus ReceiveServerEnableEncryption (Packet &p)
 Indication to the client that authentication is complete and encryption has to be used from here on forward.
virtual NetworkRecvStatus ReceiveServerWelcome (Packet &p)
 The client is joined and ready to receive their map: uint32_t Own client ID.
virtual NetworkRecvStatus ReceiveClientGetMap (Packet &p)
 Request the map from the server.
virtual NetworkRecvStatus ReceiveServerWaitForMap (Packet &p)
 Notification that another client is currently receiving the map: uint8_t Number of clients waiting in front of you.
virtual NetworkRecvStatus ReceiveServerMapBegin (Packet &p)
 Sends that the server will begin with sending the map to the client: uint32_t Current frame.
virtual NetworkRecvStatus ReceiveServerMapSize (Packet &p)
 Sends the size of the map to the client.
virtual NetworkRecvStatus ReceiveServerMapData (Packet &p)
 Sends the data of the map to the client: Contains a part of the map (until max size of packet).
virtual NetworkRecvStatus ReceiveServerMapDone (Packet &p)
 Sends that all data of the map are sent to the client:
virtual NetworkRecvStatus ReceiveClientMapOk (Packet &p)
 Tell the server that we are done receiving/loading the map.
virtual NetworkRecvStatus ReceiveServerClientJoined (Packet &p)
 A client joined (PacketGameType::ClientMapOk), what usually directly follows is a PacketGameType::ServerClientInfo: uint32_t ID of the client that just joined the game.
virtual NetworkRecvStatus ReceiveServerFrame (Packet &p)
 Sends the current frame counter to the client: uint32_t Frame counter uint32_t Frame counter max (how far may the client walk before the server?) uint32_t General seed 1 (dependent on compile settings, not default).
virtual NetworkRecvStatus ReceiveServerSync (Packet &p)
 Sends a sync-check to the client: uint32_t Frame counter.
virtual NetworkRecvStatus ReceiveClientAck (Packet &p)
 Tell the server we are done with this frame: uint32_t Current frame counter of the client.
virtual NetworkRecvStatus ReceiveClientCommand (Packet &p)
 Send a DoCommand to the Server: uint8_t ID of the company (0..MAX_COMPANIES-1).
virtual NetworkRecvStatus ReceiveServerCommand (Packet &p)
 Sends a DoCommand to the client: uint8_t ID of the company (0..MAX_COMPANIES-1).
virtual NetworkRecvStatus ReceiveClientChat (Packet &p)
 Sends a chat-packet to the server: uint8_t ID of the action (see NetworkAction).
virtual NetworkRecvStatus ReceiveServerChat (Packet &p)
 Sends a chat-packet to the client: uint8_t ID of the action (see NetworkAction).
virtual NetworkRecvStatus ReceiveServerExternalChat (Packet &p)
 Sends a chat-packet for external source to the client: string Name of the source this message came from.
virtual NetworkRecvStatus ReceiveClientSetName (Packet &p)
 Gives the client a new name: string New name of the client.
virtual NetworkRecvStatus ReceiveClientQuit (Packet &p)
 The client is quitting the game.
virtual NetworkRecvStatus ReceiveClientError (Packet &p)
 The client made an error and is quitting the game.
virtual NetworkRecvStatus ReceiveServerQuit (Packet &p)
 Notification that a client left the game: uint32_t ID of the client.
virtual NetworkRecvStatus ReceiveServerErrorQuit (Packet &p)
 Inform all clients that one client made an error and thus has quit/been disconnected: uint32_t ID of the client that caused the error.
virtual NetworkRecvStatus ReceiveServerShutdown (Packet &p)
 Let the clients know that the server is closing.
virtual NetworkRecvStatus ReceiveServerNewGame (Packet &p)
 Let the clients know that the server is loading a new map.
virtual NetworkRecvStatus ReceiveServerRemoteConsoleCommand (Packet &p)
 Send the result of an issues RCon command back to the client: uint16_t Colour code.
virtual NetworkRecvStatus ReceiveClientRemoteConsoleCommand (Packet &p)
 Send an RCon command to the server: string RCon password.
virtual NetworkRecvStatus ReceiveServerCheckNewGRFs (Packet &p)
 Sends information about all used GRFs to the client: uint8_t Amount of GRFs (the following data is repeated this many times, i.e.
virtual NetworkRecvStatus ReceiveClientNewGRFsChecked (Packet &p)
 Tell the server that we have the required GRFs.
virtual NetworkRecvStatus ReceiveServerMove (Packet &p)
 Move a client from one company into another: uint32_t ID of the client.
virtual NetworkRecvStatus ReceiveClientMove (Packet &p)
 Request the server to move this client into another company: uint8_t ID of the company the client wants to join.
virtual NetworkRecvStatus ReceiveServerConfigurationUpdate (Packet &p)
 Update the clients knowledge of the max settings: uint8_t 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

std::string connection_string
 Address we are connected to.

Static Private Attributes

static std::vector< std::unique_ptr< QueryNetworkGameSocketHandler > > queries = {}
 Pending queries.

Additional Inherited Members

Data Fields inherited from NetworkGameSocketHandler
ClientID client_id = INVALID_CLIENT_ID
 Client identifier.
uint32_t last_frame = 0
 Last frame we have executed.
uint32_t last_frame_server = 0
 Last frame the server has executed.
CommandQueue incoming_queue
 The command-queue awaiting handling.
std::chrono::steady_clock::time_point last_packet {}
 Time we received the last frame.
Data Fields inherited from NetworkTCPSocketHandler
SOCKET sock = INVALID_SOCKET
 The socket currently connected to.
bool writable = false
 Can we write to this socket?
Protected Attributes inherited from NetworkSocketHandler
std::unique_ptr< class NetworkEncryptionHandlerreceive_encryption_handler = nullptr
 The handler for decrypting received packets.
std::unique_ptr< class NetworkEncryptionHandlersend_encryption_handler = nullptr
 The handler for encrypting sent packets.

Detailed Description

Class for handling the client side of querying a game server.

Definition at line 16 of file network_query.h.

Constructor & Destructor Documentation

◆ QueryNetworkGameSocketHandler()

QueryNetworkGameSocketHandler::QueryNetworkGameSocketHandler ( SOCKET s,
std::string_view connection_string )
inline

Create a new socket for the client side of querying game server.

Parameters
sThe socket to connect with.
connection_stringThe connection string of the server.

Definition at line 39 of file network_query.h.

References connection_string, and NetworkGameSocketHandler::NetworkGameSocketHandler().

Member Function Documentation

◆ CheckConnection()

bool QueryNetworkGameSocketHandler::CheckConnection ( )
protected

Check the connection's state, i.e.

is the connection still up?

Returns
true if the connection remains valid, otherwise it will be closed.

Definition at line 41 of file network_query.cpp.

References CloseConnection(), connection_string, Debug, NetworkGameSocketHandler::last_packet, and NETWORK_RECV_STATUS_CONNECTION_LOST.

◆ CloseConnection()

NetworkRecvStatus QueryNetworkGameSocketHandler::CloseConnection ( NetworkRecvStatus status)
overridevirtual

Close the network connection due to the given status.

Parameters
statusThe reason the connection got closed.
Returns
The state the network should have.

Implements NetworkGameSocketHandler.

Definition at line 20 of file network_query.cpp.

References connection_string, NETWORK_RECV_STATUS_OKAY, NetworkGameListAddItem(), NGLS_OFFLINE, NetworkGame::refreshing, NetworkTCPSocketHandler::sock, NetworkGame::status, and UpdateNetworkGameWindow().

Referenced by CheckConnection(), and Receive().

◆ QueryServer()

void QueryNetworkGameSocketHandler::QueryServer ( SOCKET s,
std::string_view connection_string )
inlinestatic

Start to query a server based on an open socket.

Parameters
sThe socket to connect with.
connection_stringThe connection string of the server.

Definition at line 46 of file network_query.h.

References connection_string, and queries.

Referenced by TCPQueryConnecter::OnConnect().

◆ Receive()

bool QueryNetworkGameSocketHandler::Receive ( )
protected

Check whether we received/can send some data from/to the server and when that's the case handle it appropriately.

Returns
true when everything went okay.

Definition at line 60 of file network_query.cpp.

References NetworkTCPSocketHandler::CanSendReceive(), CloseConnection(), NETWORK_RECV_STATUS_OKAY, and NetworkGameSocketHandler::ReceivePackets().

◆ ReceiveServerBanned()

NetworkRecvStatus QueryNetworkGameSocketHandler::ReceiveServerBanned ( Packet & p)
overrideprotectedvirtual

Notification that the client trying to join is banned.

Parameters
pThe packet that was just received.
Returns
The state the network should have.

Reimplemented from NetworkGameSocketHandler.

Definition at line 103 of file network_query.cpp.

References connection_string, Debug, NETWORK_RECV_STATUS_CLOSE_QUERY, NetworkGameListAddItem(), NGLS_BANNED, NetworkGame::refreshing, NetworkGame::status, and UpdateNetworkGameWindow().

◆ ReceiveServerError()

NetworkRecvStatus QueryNetworkGameSocketHandler::ReceiveServerError ( Packet & p)
overrideprotectedvirtual

The client made an error: uint8_t Error code caused (see NetworkErrorCode).

Parameters
pThe packet that was just received.
Returns
The state the network should have.

Reimplemented from NetworkGameSocketHandler.

Definition at line 137 of file network_query.cpp.

References connection_string, Debug, NETWORK_RECV_STATUS_CLOSE_QUERY, NetworkGameListAddItem(), NGLS_OFFLINE, NGLS_TOO_OLD, NotExpected, Packet::Recv_uint8(), NetworkGame::refreshing, NetworkGame::status, and UpdateNetworkGameWindow().

◆ ReceiveServerFull()

NetworkRecvStatus QueryNetworkGameSocketHandler::ReceiveServerFull ( Packet & p)
overrideprotectedvirtual

Notification that the server is full.

Parameters
pThe packet that was just received.
Returns
The state the network should have.

Reimplemented from NetworkGameSocketHandler.

Definition at line 90 of file network_query.cpp.

References connection_string, Debug, NETWORK_RECV_STATUS_CLOSE_QUERY, NetworkGameListAddItem(), NGLS_FULL, NetworkGame::refreshing, NetworkGame::status, and UpdateNetworkGameWindow().

◆ ReceiveServerGameInfo()

NetworkRecvStatus QueryNetworkGameSocketHandler::ReceiveServerGameInfo ( Packet & p)
overrideprotectedvirtual

Sends information about the game.

Serialized NetworkGameInfo. See game_info.h for details.

Parameters
pThe packet that was just received.
Returns
The state the network should have.

Reimplemented from NetworkGameSocketHandler.

Definition at line 116 of file network_query.cpp.

References CheckGameCompatibility(), ClearGRFConfigList(), connection_string, Debug, DeserializeNetworkGameInfo(), NetworkServerGameInfo::grfconfig, NetworkGame::info, NETWORK_RECV_STATUS_CLOSE_QUERY, NetworkGameListAddItem(), NGLS_ONLINE, NetworkGame::refreshing, NetworkGame::status, and UpdateNetworkGameWindow().

◆ Send()

void QueryNetworkGameSocketHandler::Send ( )
protected

Send the packets of this socket handler.

Definition at line 73 of file network_query.cpp.

References NetworkTCPSocketHandler::SendPackets().

◆ SendGameInfo()

NetworkRecvStatus QueryNetworkGameSocketHandler::SendGameInfo ( )
protected

Query the server for server information.

Returns
The status the network should have.

Definition at line 82 of file network_query.cpp.

References ClientGameInfo, Debug, NETWORK_RECV_STATUS_OKAY, and NetworkTCPSocketHandler::SendPacket().

◆ SendReceive()

void QueryNetworkGameSocketHandler::SendReceive ( )
static

Check if any query needs to send or receive.

Definition at line 164 of file network_query.cpp.

References queries.

Referenced by NetworkBackgroundLoop().

Field Documentation

◆ connection_string

std::string QueryNetworkGameSocketHandler::connection_string
private

◆ queries

std::vector< std::unique_ptr< QueryNetworkGameSocketHandler > > QueryNetworkGameSocketHandler::queries = {}
staticprivate

Pending queries.

Definition at line 18 of file network_query.h.

Referenced by QueryServer(), and SendReceive().


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