OpenTTD Source 20241224-master-gee860a5c8e
NetworkUDPSocketHandler Class Reference

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

#include <udp.h>

Inheritance diagram for NetworkUDPSocketHandler:
NetworkSocketHandler ClientNetworkUDPSocketHandler ServerNetworkUDPSocketHandler

Public Member Functions

 NetworkUDPSocketHandler (NetworkAddressList *bind=nullptr)
 Create an UDP socket but don't listen yet.
 
virtual ~NetworkUDPSocketHandler ()
 On destructing of this class, the socket needs to be closed.
 
bool Listen ()
 Start listening on the given host and port.
 
void CloseSocket ()
 Close the actual UDP socket.
 
void SendPacket (Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
 Send a packet over UDP.
 
void ReceivePackets ()
 Receive a packet at UDP level.
 
- 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.
 

Protected Member Functions

void ReceiveInvalidPacket (PacketUDPType, NetworkAddress &client_addr)
 Helper for logging receiving invalid packets.
 
virtual void Receive_CLIENT_FIND_SERVER (Packet &p, NetworkAddress &client_addr)
 Queries to the server for information about the game.
 
virtual void Receive_SERVER_RESPONSE (Packet &p, NetworkAddress &client_addr)
 Response to a query letting the client know we are here.
 
void HandleUDPPacket (Packet &p, NetworkAddress &client_addr)
 Handle an incoming packets by sending it to the correct function.
 

Protected Attributes

NetworkAddressList bind
 The address to bind to.
 
SocketList sockets
 The opened sockets.
 
- 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

Base socket handler for all UDP sockets.

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

◆ ~NetworkUDPSocketHandler()

virtual NetworkUDPSocketHandler::~NetworkUDPSocketHandler ( )
inlinevirtual

On destructing of this class, the socket needs to be closed.

Definition at line 54 of file udp.h.

References CloseSocket().

Member Function Documentation

◆ CloseSocket()

void NetworkUDPSocketHandler::CloseSocket ( )

Close the actual UDP socket.

Definition at line 59 of file udp.cpp.

References sockets.

Referenced by Listen(), and ~NetworkUDPSocketHandler().

◆ HandleUDPPacket()

void NetworkUDPSocketHandler::HandleUDPPacket ( Packet p,
NetworkAddress client_addr 
)
protected

Handle an incoming packets by sending it to the correct function.

Parameters
pthe received packet
client_addrthe sender of the packet

Definition at line 156 of file udp.cpp.

References Debug, NetworkAddress::GetAddressAsString(), NetworkSocketHandler::HasClientQuit(), PACKET_UDP_CLIENT_FIND_SERVER, PACKET_UDP_END, PACKET_UDP_SERVER_RESPONSE, Receive_CLIENT_FIND_SERVER(), Receive_SERVER_RESPONSE(), Packet::Recv_uint8(), and NetworkSocketHandler::Reopen().

Referenced by ReceivePackets().

◆ 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, CloseSocket(), and sockets.

Referenced by NetworkUDPServerListen(), and SendPacket().

◆ 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 189 of file udp.cpp.

References PACKET_UDP_CLIENT_FIND_SERVER, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ Receive_SERVER_RESPONSE()

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

Response to a query letting the client know we are here.

Parameters
pThe received packet.
client_addrThe origin of the packet.

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 190 of file udp.cpp.

References PACKET_UDP_SERVER_RESPONSE, and ReceiveInvalidPacket().

Referenced by HandleUDPPacket().

◆ ReceiveInvalidPacket()

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

Helper for logging receiving invalid packets.

Parameters
typeThe received packet type.
client_addrThe address we received the packet from.

Definition at line 184 of file udp.cpp.

References Debug, and NetworkAddress::GetAddressAsString().

Referenced by Receive_CLIENT_FIND_SERVER(), and Receive_SERVER_RESPONSE().

◆ ReceivePackets()

void NetworkUDPSocketHandler::ReceivePackets ( )

◆ SendPacket()

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

Send a packet over UDP.

Parameters
pthe packet to send
recvthe receiver (target) of the packet
allsend the packet using all sockets that can send it
broadcastwhether to send a broadcast message

Definition at line 74 of file udp.cpp.

References NetworkError::AsString(), Debug, NetworkAddress::GetAddress(), NetworkAddress::GetAddressAsString(), NetworkAddress::GetAddressLength(), NetworkError::GetLast(), NetworkAddress::IsFamily(), Listen(), Packet::PrepareToSend(), sockets, and Packet::TransferOut().

Referenced by NetworkUDPBroadCast(), and ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER().

Field Documentation

◆ bind

NetworkAddressList NetworkUDPSocketHandler::bind
protected

The address to bind to.

Definition at line 29 of file udp.h.

Referenced by Listen(), and NetworkUDPSocketHandler().

◆ sockets

SocketList NetworkUDPSocketHandler::sockets
protected

The opened sockets.

Definition at line 31 of file udp.h.

Referenced by CloseSocket(), Listen(), ReceivePackets(), and SendPacket().


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