OpenTTD Source
20241121-master-g67a0fccfad
|
Base socket handler for all UDP sockets. More...
#include <udp.h>
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 | CloseSocket () |
Close the actual 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 ()=default |
Close the socket when destructing the socket handler. | |
void | MarkClosed () |
Mark the connection as closed. More... | |
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. | |
Protected Member Functions | |
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) |
Response to a query letting the client know we are here. 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. | |
Protected Attributes inherited from NetworkSocketHandler | |
std::unique_ptr< class NetworkEncryptionHandler > | receive_encryption_handler |
The handler for decrypting received packets. | |
std::unique_ptr< class NetworkEncryptionHandler > | send_encryption_handler |
The handler for encrypting sent packets. | |
NetworkUDPSocketHandler::NetworkUDPSocketHandler | ( | NetworkAddressList * | bind = nullptr | ) |
|
protected |
Handle an incoming packets by sending it to the correct function.
p | the received packet |
client_addr | the 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().
bool NetworkUDPSocketHandler::Listen | ( | ) |
Start listening on the given host and port.
Definition at line 44 of file udp.cpp.
References bind, CloseSocket(), and sockets.
Referenced by NetworkUDPServerListen(), and SendPacket().
|
protectedvirtual |
Queries to the server for information about the game.
p | The received packet. |
client_addr | The 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().
|
protectedvirtual |
Response to a query letting the client know we are here.
p | The received packet. |
client_addr | The 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().
|
protected |
Helper for logging receiving invalid packets.
type | The received packet type. |
client_addr | The 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().
void NetworkUDPSocketHandler::SendPacket | ( | Packet & | p, |
NetworkAddress & | recv, | ||
bool | all = false , |
||
bool | broadcast = false |
||
) |
Send a packet over UDP.
p | the packet to send |
recv | the receiver (target) of the packet |
all | send the packet using all sockets that can send it |
broadcast | whether 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().