OpenTTD Source 20241224-master-gee860a5c8e
QueryNetworkGameSocketHandler Class Reference

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

#include <network_query.h>

Inheritance diagram for QueryNetworkGameSocketHandler:
ZeroedMemoryAllocator NetworkGameSocketHandler NetworkTCPSocketHandler NetworkSocketHandler

Public Member Functions

 QueryNetworkGameSocketHandler (SOCKET s, const std::string &connection_string)
 Create a new socket for the client side of quering game server.
 
NetworkRecvStatus CloseConnection (NetworkRecvStatus status) override
 Close the network connection due to the given status.
 
- Public Member Functions inherited from ZeroedMemoryAllocator
void * operator new (size_t size)
 Memory allocator for a single class instance.
 
void * operator new[] (size_t size)
 Memory allocator for an array of class instances.
 
void operator delete (void *ptr)
 Memory release for a single class instance.
 
void operator delete[] (void *ptr)
 Memory release for an array of class instances.
 
- 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.
 
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.
 
bool IsPendingDeletion () const
 
void DeferDeletion ()
 
- 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.
 
- Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()
 Create a new unbound socket.
 
virtual ~NetworkSocketHandler ()=default
 Close the socket when destructing the socket handler.
 
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, const std::string &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 ()
 

Protected Member Functions

NetworkRecvStatus Receive_SERVER_FULL (Packet &p) override
 Notification that the server is full.
 
NetworkRecvStatus Receive_SERVER_BANNED (Packet &p) override
 Notification that the client trying to join is banned.
 
NetworkRecvStatus Receive_SERVER_ERROR (Packet &p) override
 The client made an error: uint8_t Error code caused (see NetworkErrorCode).
 
NetworkRecvStatus Receive_SERVER_GAME_INFO (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 Receive_CLIENT_JOIN (Packet &p)
 Try to join the server: string OpenTTD revision (norev0000 if no revision).
 
virtual NetworkRecvStatus Receive_CLIENT_GAME_INFO (Packet &p)
 Request game information.
 
virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO (Packet &p)
 Send information about a client: uint32_t ID of the client (always unique on a server.
 
virtual NetworkRecvStatus Receive_CLIENT_IDENTIFY (Packet &p)
 The client tells the server about the identity of the client: string Name of the client (max NETWORK_NAME_LENGTH).
 
virtual NetworkRecvStatus Receive_SERVER_AUTH_REQUEST (Packet &p)
 Indication to the client that it needs to authenticate: uint8_t The NetworkAuthenticationMethod to use.
 
virtual NetworkRecvStatus Receive_CLIENT_AUTH_RESPONSE (Packet &p)
 Send the response to the authentication request: 32 * uint8_t Public key of the client.
 
virtual NetworkRecvStatus Receive_SERVER_ENABLE_ENCRYPTION (Packet &p)
 Indication to the client that authentication is complete and encryption has to be used from here on forward.
 
virtual NetworkRecvStatus Receive_SERVER_WELCOME (Packet &p)
 The client is joined and ready to receive their map: uint32_t Own client ID.
 
virtual NetworkRecvStatus Receive_CLIENT_GETMAP (Packet &p)
 Request the map from the server.
 
virtual NetworkRecvStatus Receive_SERVER_WAIT (Packet &p)
 Notification that another client is currently receiving the map: uint8_t Number of clients waiting in front of you.
 
virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN (Packet &p)
 Sends that the server will begin with sending the map to the client: uint32_t Current frame.
 
virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE (Packet &p)
 Sends the size of the map to the client.
 
virtual NetworkRecvStatus Receive_SERVER_MAP_DATA (Packet &p)
 Sends the data of the map to the client: Contains a part of the map (until max size of packet).
 
virtual NetworkRecvStatus Receive_SERVER_MAP_DONE (Packet &p)
 Sends that all data of the map are sent to the client:
 
virtual NetworkRecvStatus Receive_CLIENT_MAP_OK (Packet &p)
 Tell the server that we are done receiving/loading the map.
 
virtual NetworkRecvStatus Receive_SERVER_JOIN (Packet &p)
 A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO: uint32_t ID of the client that just joined the game.
 
virtual NetworkRecvStatus Receive_SERVER_FRAME (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 Receive_SERVER_SYNC (Packet &p)
 Sends a sync-check to the client: uint32_t Frame counter.
 
virtual NetworkRecvStatus Receive_CLIENT_ACK (Packet &p)
 Tell the server we are done with this frame: uint32_t Current frame counter of the client.
 
virtual NetworkRecvStatus Receive_CLIENT_COMMAND (Packet &p)
 Send a DoCommand to the Server: uint8_t ID of the company (0..MAX_COMPANIES-1).
 
virtual NetworkRecvStatus Receive_SERVER_COMMAND (Packet &p)
 Sends a DoCommand to the client: uint8_t ID of the company (0..MAX_COMPANIES-1).
 
virtual NetworkRecvStatus Receive_CLIENT_CHAT (Packet &p)
 Sends a chat-packet to the server: uint8_t ID of the action (see NetworkAction).
 
virtual NetworkRecvStatus Receive_SERVER_CHAT (Packet &p)
 Sends a chat-packet to the client: uint8_t ID of the action (see NetworkAction).
 
virtual NetworkRecvStatus Receive_SERVER_EXTERNAL_CHAT (Packet &p)
 Sends a chat-packet for external source to the client: string Name of the source this message came from.
 
virtual NetworkRecvStatus Receive_CLIENT_SET_NAME (Packet &p)
 Gives the client a new name: string New name of the client.
 
virtual NetworkRecvStatus Receive_CLIENT_QUIT (Packet &p)
 The client is quitting the game.
 
virtual NetworkRecvStatus Receive_CLIENT_ERROR (Packet &p)
 The client made an error and is quitting the game.
 
virtual NetworkRecvStatus Receive_SERVER_QUIT (Packet &p)
 Notification that a client left the game: uint32_t ID of the client.
 
virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT (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 Receive_SERVER_SHUTDOWN (Packet &p)
 Let the clients know that the server is closing.
 
virtual NetworkRecvStatus Receive_SERVER_NEWGAME (Packet &p)
 Let the clients know that the server is loading a new map.
 
virtual NetworkRecvStatus Receive_SERVER_RCON (Packet &p)
 Send the result of an issues RCon command back to the client: uint16_t Colour code.
 
virtual NetworkRecvStatus Receive_CLIENT_RCON (Packet &p)
 Send an RCon command to the server: string RCon password.
 
virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS (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 Receive_CLIENT_NEWGRFS_CHECKED (Packet &p)
 Tell the server that we have the required GRFs.
 
virtual NetworkRecvStatus Receive_SERVER_MOVE (Packet &p)
 Move a client from one company into another: uint32_t ID of the client.
 
virtual NetworkRecvStatus Receive_CLIENT_MOVE (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 Receive_SERVER_CONFIG_UPDATE (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
 Client identifier.
 
uint32_t last_frame
 Last frame we have executed.
 
uint32_t last_frame_server
 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
 The socket currently connected to.
 
bool writable
 Can we write to this socket?
 
- Protected Attributes inherited from NetworkSocketHandler
std::unique_ptr< class NetworkEncryptionHandlerreceive_encryption_handler
 The handler for decrypting received packets.
 
std::unique_ptr< class NetworkEncryptionHandlersend_encryption_handler
 The handler for encrypting sent packets.
 

Detailed Description

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

Definition at line 16 of file network_query.h.

Constructor & Destructor Documentation

◆ QueryNetworkGameSocketHandler()

QueryNetworkGameSocketHandler::QueryNetworkGameSocketHandler ( SOCKET  s,
const std::string &  connection_string 
)
inline

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

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

Definition at line 39 of file network_query.h.

Member Function Documentation

◆ CheckConnection()

bool QueryNetworkGameSocketHandler::CheckConnection ( )
protected

Check the connection's state, i.e.

is the connection still up?

Definition at line 42 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.

Implements NetworkGameSocketHandler.

Definition at line 22 of file network_query.cpp.

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

Referenced by CheckConnection(), and Receive().

◆ QueryServer()

static void QueryNetworkGameSocketHandler::QueryServer ( SOCKET  s,
const std::string &  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 61 of file network_query.cpp.

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

◆ Receive_SERVER_BANNED()

NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_BANNED ( Packet p)
overrideprotectedvirtual

Notification that the client trying to join is banned.

Parameters
pThe packet that was just received.

Reimplemented from NetworkGameSocketHandler.

Definition at line 103 of file network_query.cpp.

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

◆ Receive_SERVER_ERROR()

NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_ERROR ( Packet p)
overrideprotectedvirtual

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

Parameters
pThe packet that was just received.

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, Packet::Recv_uint8(), NetworkGameList::refreshing, NetworkGameList::status, and UpdateNetworkGameWindow().

◆ Receive_SERVER_FULL()

NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_FULL ( Packet p)
overrideprotectedvirtual

Notification that the server is full.

Parameters
pThe packet that was just received.

Reimplemented from NetworkGameSocketHandler.

Definition at line 90 of file network_query.cpp.

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

◆ Receive_SERVER_GAME_INFO()

NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_GAME_INFO ( Packet p)
overrideprotectedvirtual

Sends information about the game.

Serialized NetworkGameInfo. See game_info.h for details.

Parameters
pThe packet that was just received.

Reimplemented from NetworkGameSocketHandler.

Definition at line 116 of file network_query.cpp.

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

◆ Send()

void QueryNetworkGameSocketHandler::Send ( )
protected

Send the packets of this socket handler.

Definition at line 74 of file network_query.cpp.

References NetworkTCPSocketHandler::SendPackets().

◆ SendGameInfo()

NetworkRecvStatus QueryNetworkGameSocketHandler::SendGameInfo ( )
protected

Query the server for server information.

Definition at line 82 of file network_query.cpp.

References Debug, NETWORK_RECV_STATUS_OKAY, PACKET_CLIENT_GAME_INFO, 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 20 of file network_query.h.

Referenced by QueryServer(), and SendReceive().


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