OpenTTD Source 20241224-master-gee860a5c8e
|
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. | |
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 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 | ) |
|
inlinevirtual |
On destructing of this class, the socket needs to be closed.
Definition at line 54 of file udp.h.
References CloseSocket().
void NetworkUDPSocketHandler::CloseSocket | ( | ) |
Close the actual UDP socket.
Definition at line 59 of file udp.cpp.
References sockets.
Referenced by Listen(), and ~NetworkUDPSocketHandler().
|
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::ReceivePackets | ( | ) |
Receive a packet at UDP level.
Definition at line 110 of file udp.cpp.
References Debug, NetworkAddress::GetAddressAsString(), HandleUDPPacket(), Packet::ParsePacketSize(), Packet::PrepareToRead(), SetNonBlocking(), Packet::Size(), sockets, Packet::TransferIn(), and UDP_MTU.
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().
|
protected |
The address to bind to.
Definition at line 29 of file udp.h.
Referenced by Listen(), and NetworkUDPSocketHandler().
|
protected |
The opened sockets.
Definition at line 31 of file udp.h.
Referenced by CloseSocket(), Listen(), ReceivePackets(), and SendPacket().