# NetworkUDPSocketHandler Class Reference

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

`#include <udp.h>`

Inheritance diagram for NetworkUDPSocketHandler:

## Public Member Functions

| NetworkUDPSocketHandler (NetworkAddressList *bind=NULL)
| 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 | Close ()
| Close the given 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.
void | Send_NetworkGameInfo (Packet *p, const NetworkGameInfo *info)
| Serializes the NetworkGameInfo struct to the packet.
void | Recv_NetworkGameInfo (Packet *p, NetworkGameInfo *info)
| Deserializes the NetworkGameInfo struct from the packet.

## Protected Member Functions

NetworkRecvStatus | CloseConnection (bool error=true)
| 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.
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_CLIENT_FIND_SERVER)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_SERVER_RESPONSE)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_CLIENT_DETAIL_INFO)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_SERVER_DETAIL_INFO)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_SERVER_REGISTER)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_MASTER_ACK_REGISTER)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_CLIENT_GET_LIST)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_MASTER_RESPONSE_LIST)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_SERVER_UNREGISTER)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_CLIENT_GET_NEWGRFS)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_SERVER_NEWGRFS)
| DECLARE_UDP_RECEIVE_COMMAND (PACKET_UDP_MASTER_SESSION_KEY)
void | HandleUDPPacket (Packet *p, NetworkAddress *client_addr)
| Handle an incoming packets by sending it to the correct function.
virtual void | HandleIncomingNetworkGameInfoGRFConfig (GRFConfig *config)
| Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.

## 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 110 of file udp.h.

## Constructor & Destructor Documentation

NetworkUDPSocketHandler::NetworkUDPSocketHandler | ( | NetworkAddressList * | bind = `NULL` | ) |

Create an UDP socket but don't listen yet.

Parameters:

  | bind | the addresses to bind to.

Definition at line 25 of file udp.cpp.

References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), and SmallVector< T, S >::End().

## Member Function Documentation

NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection | ( | bool | error = `true` | ) | `[protected, virtual]`

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:

  | error | Whether we quit under an error condition or not.

Returns:
  new status of the connection.

Reimplemented from NetworkSocketHandler.

Definition at line 68 of file udp.cpp.

References NETWORK_RECV_STATUS_OKAY.

virtual void NetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig | ( | GRFConfig * | config | ) | `[inline, protected, virtual]`

Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.

Only grfid and md5sum are set, the rest is zero. This function must set all appropriate fields. This GRF is later appended to the grfconfig list of the NetworkGameInfo.

Parameters:

  | config | the GRF to handle

Reimplemented in ClientNetworkUDPSocketHandler.

Definition at line 143 of file udp.h.

Referenced by Recv_NetworkGameInfo().

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

References NetworkAddress::GetAddressAsString(), NetworkSocketHandler::HasClientQuit(), PACKET_UDP_CLIENT_DETAIL_INFO, PACKET_UDP_CLIENT_FIND_SERVER, PACKET_UDP_CLIENT_GET_LIST, PACKET_UDP_CLIENT_GET_NEWGRFS, PACKET_UDP_END, PACKET_UDP_MASTER_ACK_REGISTER, PACKET_UDP_MASTER_RESPONSE_LIST, PACKET_UDP_MASTER_SESSION_KEY, PACKET_UDP_SERVER_DETAIL_INFO, PACKET_UDP_SERVER_NEWGRFS, PACKET_UDP_SERVER_REGISTER, PACKET_UDP_SERVER_RESPONSE, PACKET_UDP_SERVER_UNREGISTER, NetworkSocketHandler::Reopen(), and UDP_COMMAND.

Referenced by ReceivePackets().

bool NetworkUDPSocketHandler::Listen | ( |  | ) |

Start listening on the given host and port.

Returns:
  true if at least one port is listening

Definition at line 45 of file udp.cpp.

References SmallVector< T, S >::Begin(), bind, Close(), SmallVector< T, S >::End(), SmallVector< T, S >::Length(), and sockets.

Referenced by SendPacket().

void NetworkUDPSocketHandler::Recv_NetworkGameInfo | ( | Packet * | p,
|  | NetworkGameInfo * | info |
| ) |  |  |

Deserializes the NetworkGameInfo struct from the packet.

Parameters:

  | p | the packet to read the data from
  | info | the NetworkGameInfo to deserialize into

Definition at line 217 of file udp.cpp.

References Clamp(), NetworkGameInfo::clients_max, NetworkServerGameInfo::clients_on, NetworkGameInfo::companies_max, NetworkGameInfo::companies_on, ConvertYMDToDate(), DAYS_TILL_ORIGINAL_BASE_YEAR, NetworkGameInfo::dedicated, NetworkGameInfo::game_date, NetworkGameInfo::game_info_version, NetworkGameInfo::grfconfig, HandleIncomingNetworkGameInfoGRFConfig(), NetworkGameInfo::map_height, NetworkServerGameInfo::map_name, NetworkGameInfo::map_set, NetworkGameInfo::map_width, MAX_YEAR, NETWORK_MAX_GRF_COUNT, NETWORK_NUM_LANDSCAPES, NETWORK_NUM_LANGUAGES, GRFConfig::next, NetworkSocketHandler::Recv_GRFIdentifier(), Packet::Recv_string(), NetworkGameInfo::server_lang, NetworkGameInfo::server_name, NetworkGameInfo::server_revision, NetworkGameInfo::spectators_max, NetworkGameInfo::spectators_on, NetworkGameInfo::start_date, and NetworkGameInfo::use_password.

void NetworkUDPSocketHandler::Send_NetworkGameInfo | ( | Packet * | p,
|  | const NetworkGameInfo * | info |
| ) |  |  |

Serializes the NetworkGameInfo struct to the packet.

Parameters:

  | p | the packet to write the data to
  | info | the NetworkGameInfo struct to serialize

Definition at line 155 of file udp.cpp.

References NetworkGameInfo::clients_max, NetworkServerGameInfo::clients_on, NetworkGameInfo::companies_max, NetworkGameInfo::companies_on, NetworkGameInfo::dedicated, GRFConfig::flags, NetworkGameInfo::game_date, GCF_STATIC, NetworkGameInfo::grfconfig, HasBit(), NetworkGameInfo::map_height, NetworkServerGameInfo::map_name, NetworkGameInfo::map_set, NetworkGameInfo::map_width, NETWORK_GAME_INFO_VERSION, GRFConfig::next, Packet::Send_bool(), NetworkSocketHandler::Send_GRFIdentifier(), Packet::Send_string(), NetworkGameInfo::server_lang, NetworkGameInfo::server_name, NetworkGameInfo::server_revision, NetworkGameInfo::spectators_max, NetworkGameInfo::spectators_on, NetworkGameInfo::start_date, and NetworkGameInfo::use_password.

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

References SmallVector< T, S >::Begin(), Packet::buffer, SmallVector< T, S >::End(), NetworkAddress::GetAddress(), NetworkAddress::GetAddressAsString(), NetworkAddress::GetAddressLength(), NetworkAddress::IsFamily(), SmallVector< T, S >::Length(), Listen(), Packet::PrepareToSend(), Packet::size, and sockets.

Referenced by NetworkUDPQueryServerThread().

## Field Documentation

NetworkAddressList NetworkUDPSocketHandler::bind`[protected]`

The address to bind to.

Definition at line 113 of file udp.h.

Referenced by Listen().

SocketList NetworkUDPSocketHandler::sockets`[protected]`

The opened sockets.

Definition at line 115 of file udp.h.

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

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

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