OpenTTD Source: NetworkUDPSocketHandler Class Reference

OpenTTD Source 13.2.1

Public Member Functions | Protected Member Functions | Protected Attributes
NetworkUDPSocketHandler Class Reference

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

`#include <udp.h>`

Inheritance diagram for NetworkUDPSocketHandler:

## 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 ()
| 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.

## 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

  bind | the addresses to bind to.

Definition at line 24 of file udp.cpp.

References bind.

## Member Function Documentation

## ◆ HandleUDPPacket()

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

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

Parameters

  p | the received packet
  client_addr | the sender of the packet

Definition at line 153 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

  p | The received packet.
  client_addr | The origin of the packet.

Reimplemented in ServerNetworkUDPSocketHandler.

Definition at line 186 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

  p | The received packet.
  client_addr | The origin of the packet.

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 187 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

  type | The received packet type.
  client_addr | The address we received the packet from.

Definition at line 181 of file udp.cpp.

References Debug, and NetworkAddress::GetAddressAsString().

Referenced by Receive_CLIENT_FIND_SERVER(), and Receive_SERVER_RESPONSE().

## ◆ SendPacket()

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

Send a packet over UDP.

Parameters

  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().

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

- src/network/core/udp.h
- src/network/core/udp.cpp