OpenTTD Source  1.11.2
NetworkUDPSocketHandler Class Reference

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

#include <udp.h>

Inheritance diagram for NetworkUDPSocketHandler:
NetworkSocketHandler ClientNetworkUDPSocketHandler MasterNetworkUDPSocketHandler ServerNetworkUDPSocketHandler

Public Member Functions

 NetworkUDPSocketHandler (NetworkAddressList *bind=nullptr)
 Create an UDP socket but don't listen yet. More...
 
virtual ~NetworkUDPSocketHandler ()
 On destructing of this class, the socket needs to be closed.
 
bool Listen ()
 Start listening on the given host and port. More...
 
void Close () override
 Close the given UDP socket.
 
void SendPacket (Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
 Send a packet over UDP. More...
 
void ReceivePackets ()
 Receive a packet at UDP level.
 
- Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()
 Create a new unbound socket.
 
virtual ~NetworkSocketHandler ()
 Close the socket when destructing the socket handler.
 
bool HasClientQuit () const
 Whether the current client connected to the socket has quit. More...
 
void Reopen ()
 Reopen the socket so we can send/receive stuff again.
 
void SendCompanyInformation (Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats, uint max_len=NETWORK_COMPANY_NAME_LENGTH)
 Package some generic company information into a packet. More...
 

Protected Member Functions

NetworkRecvStatus CloseConnection (bool error=true) override
 Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just means the packet has to be dropped. More...
 
void ReceiveInvalidPacket (PacketUDPType, NetworkAddress *client_addr)
 Helper for logging receiving invalid packets. More...
 
virtual void Receive_CLIENT_FIND_SERVER (Packet *p, NetworkAddress *client_addr)
 Queries to the server for information about the game. More...
 
virtual void Receive_SERVER_RESPONSE (Packet *p, NetworkAddress *client_addr)
 Return of server information to the client. More...
 
virtual void Receive_CLIENT_DETAIL_INFO (Packet *p, NetworkAddress *client_addr)
 Query for detailed information about companies. More...
 
virtual void Receive_SERVER_DETAIL_INFO (Packet *p, NetworkAddress *client_addr)
 Reply with detailed company information. More...
 
virtual void Receive_SERVER_REGISTER (Packet *p, NetworkAddress *client_addr)
 Registers the server to the master server. More...
 
virtual void Receive_MASTER_ACK_REGISTER (Packet *p, NetworkAddress *client_addr)
 The master server acknowledges the registration. More...
 
virtual void Receive_CLIENT_GET_LIST (Packet *p, NetworkAddress *client_addr)
 The client requests a list of servers. More...
 
virtual void Receive_MASTER_RESPONSE_LIST (Packet *p, NetworkAddress *client_addr)
 The server sends a list of servers. More...
 
virtual void Receive_SERVER_UNREGISTER (Packet *p, NetworkAddress *client_addr)
 A server unregisters itself at the master server. More...
 
virtual void Receive_CLIENT_GET_NEWGRFS (Packet *p, NetworkAddress *client_addr)
 The client requests information about some NewGRFs. More...
 
virtual void Receive_SERVER_NEWGRFS (Packet *p, NetworkAddress *client_addr)
 The server returns information about some NewGRFs. More...
 
virtual void Receive_MASTER_SESSION_KEY (Packet *p, NetworkAddress *client_addr)
 The master server sends us a session key. More...
 
void HandleUDPPacket (Packet *p, NetworkAddress *client_addr)
 Handle an incoming packets by sending it to the correct function. More...
 

Protected Attributes

NetworkAddressList bind
 The address to bind to.
 
SocketList sockets
 The opened sockets.
 

Detailed Description

Base socket handler for all UDP sockets.

Definition at line 45 of file udp.h.

Constructor & Destructor Documentation

◆ NetworkUDPSocketHandler()

NetworkUDPSocketHandler::NetworkUDPSocketHandler ( NetworkAddressList bind = nullptr)

Create an UDP socket but don't listen yet.

Parameters
bindthe addresses to bind to.

Definition at line 24 of file udp.cpp.

References bind.

Member Function Documentation

◆ CloseConnection()

NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection ( bool  error = true)
overrideprotectedvirtual

Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just means the packet has to be dropped.

Parameters
errorWhether we quit under an error condition or not.
Returns
new status of the connection.

Reimplemented from NetworkSocketHandler.

Definition at line 67 of file udp.cpp.

References NetworkSocketHandler::CloseConnection(), error(), and NETWORK_RECV_STATUS_OKAY.

◆ HandleUDPPacket()

◆ Listen()

bool NetworkUDPSocketHandler::Listen ( )

Start listening on the given host and port.

Returns
true if at least one port is listening

Definition at line 44 of file udp.cpp.

References bind, Close(), and sockets.

Referenced by NetworkUDPServerListen(), and SendPacket().

◆ Receive_CLIENT_DETAIL_INFO()

void NetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

Query for detailed information about companies.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ServerNetworkUDPSocketHandler.

Definition at line 203 of file udp.cpp.

References PACKET_UDP_CLIENT_DETAIL_INFO, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_CLIENT_FIND_SERVER()

void NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

Queries to the server for information about the game.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ServerNetworkUDPSocketHandler.

Definition at line 201 of file udp.cpp.

References PACKET_UDP_CLIENT_FIND_SERVER, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_CLIENT_GET_LIST()

void NetworkUDPSocketHandler::Receive_CLIENT_GET_LIST ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The client requests a list of servers.

uint8 The protocol version. uint8 The type of server to look for: IPv4, IPv6 or based on the received packet.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Definition at line 207 of file udp.cpp.

References PACKET_UDP_CLIENT_GET_LIST, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_CLIENT_GET_NEWGRFS()

void NetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The client requests information about some NewGRFs.

uint8 The number of NewGRFs information is requested about. For each NewGRF: uint32 The GRFID. 16 * uint8 MD5 checksum of the GRF.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ServerNetworkUDPSocketHandler.

Definition at line 210 of file udp.cpp.

References PACKET_UDP_CLIENT_GET_NEWGRFS, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_MASTER_ACK_REGISTER()

void NetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The master server acknowledges the registration.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in MasterNetworkUDPSocketHandler.

Definition at line 206 of file udp.cpp.

References PACKET_UDP_MASTER_ACK_REGISTER, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_MASTER_RESPONSE_LIST()

void NetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The server sends a list of servers.

uint8 The protocol version. For each server: 4 or 16 bytes of IPv4 or IPv6 address. uint8 The port.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 208 of file udp.cpp.

References PACKET_UDP_MASTER_RESPONSE_LIST, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_MASTER_SESSION_KEY()

void NetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The master server sends us a session key.

uint64 The session key.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in MasterNetworkUDPSocketHandler.

Definition at line 212 of file udp.cpp.

References PACKET_UDP_MASTER_SESSION_KEY, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_DETAIL_INFO()

void NetworkUDPSocketHandler::Receive_SERVER_DETAIL_INFO ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

Reply with detailed company information.

uint8 Version of the packet. uint8 Number of companies. For each company: 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.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Definition at line 204 of file udp.cpp.

References PACKET_UDP_SERVER_DETAIL_INFO, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_NEWGRFS()

void NetworkUDPSocketHandler::Receive_SERVER_NEWGRFS ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

The server returns information about some NewGRFs.

uint8 The number of NewGRFs information is requested about. For each NewGRF: uint32 The GRFID. 16 * uint8 MD5 checksum of the GRF. string The name of the NewGRF.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 211 of file udp.cpp.

References PACKET_UDP_SERVER_NEWGRFS, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_REGISTER()

void NetworkUDPSocketHandler::Receive_SERVER_REGISTER ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

Registers the server to the master server.

string The "welcome" message to root out other binary packets. uint8 Version of the protocol. uint16 The port to unregister. uint64 The session key.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Definition at line 205 of file udp.cpp.

References PACKET_UDP_SERVER_REGISTER, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_RESPONSE()

void NetworkUDPSocketHandler::Receive_SERVER_RESPONSE ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

Return of server information to the client.

Serialized NetworkGameInfo. See game_info.h for details.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 202 of file udp.cpp.

References PACKET_UDP_SERVER_RESPONSE, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_UNREGISTER()

void NetworkUDPSocketHandler::Receive_SERVER_UNREGISTER ( Packet p,
NetworkAddress client_addr 
)
protectedvirtual

A server unregisters itself at the master server.

uint8 Version of the protocol. uint16 The port to unregister.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Definition at line 209 of file udp.cpp.

References PACKET_UDP_SERVER_UNREGISTER, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ ReceiveInvalidPacket()

void NetworkUDPSocketHandler::ReceiveInvalidPacket ( PacketUDPType  type,
NetworkAddress client_addr 
)
protected

◆ SendPacket()

void NetworkUDPSocketHandler::SendPacket ( Packet p,
NetworkAddress recv,
bool  all = false,
bool  broadcast = false 
)

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