# network_udp.cpp File Reference

This file handles the UDP related communication. More...

`#include "../stdafx.h"`
`#include "../date_func.h"`
`#include "../map_func.h"`
`#include "../debug.h"`
`#include "network_gamelist.h"`
`#include "network_internal.h"`
`#include "network_udp.h"`
`#include "network.h"`
`#include "../core/endian_func.hpp"`
`#include "../company_base.h"`
`#include "../thread/thread.h"`
`#include "../rev.h"`
`#include "core/udp.h"`

Go to the source code of this file.

## Data Structures

class | MasterNetworkUDPSocketHandler
| *** Communication with the masterserver ***/ More...
class | ServerNetworkUDPSocketHandler
| *** Communication with clients (we are server) ***/ More...
class | ClientNetworkUDPSocketHandler
| *** Communication with servers (we are client) ***/ More...
struct | NetworkUDPQueryServerInfo
| Simpler wrapper struct for NetworkUDPQueryServerThread. More...

## Enumerations

enum | { ADVERTISE_NORMAL_INTERVAL = 30000, ADVERTISE_RETRY_INTERVAL = 300, ADVERTISE_RETRY_TIMES = 3 }

## Functions

| DEF_UDP_RECEIVE_COMMAND (Master, PACKET_UDP_MASTER_ACK_REGISTER)
| DEF_UDP_RECEIVE_COMMAND (Master, PACKET_UDP_MASTER_SESSION_KEY)
| DEF_UDP_RECEIVE_COMMAND (Server, PACKET_UDP_CLIENT_FIND_SERVER)
| DEF_UDP_RECEIVE_COMMAND (Server, PACKET_UDP_CLIENT_DETAIL_INFO)
| DEF_UDP_RECEIVE_COMMAND (Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
| A client has requested the names of some NewGRFs.
| DEF_UDP_RECEIVE_COMMAND (Client, PACKET_UDP_SERVER_RESPONSE)
| DEF_UDP_RECEIVE_COMMAND (Client, PACKET_UDP_MASTER_RESPONSE_LIST)
| DEF_UDP_RECEIVE_COMMAND (Client, PACKET_UDP_SERVER_NEWGRFS)
| The return of the client's request of the names of some NewGRFs.
static void | NetworkUDPBroadCast (NetworkUDPSocketHandler *socket)
void | NetworkUDPQueryMasterServer ()
void | NetworkUDPSearchGame ()
static void | NetworkUDPQueryServerThread (void *pntr)
| Threaded part for resolving the IP of a server and querying it.
void | NetworkUDPQueryServer (NetworkAddress address, bool manually)
static void | NetworkUDPRemoveAdvertiseThread (void *pntr)
void | NetworkUDPRemoveAdvertise (bool blocking)
| Remove our advertise from the master-server.
static void | NetworkUDPAdvertiseThread (void *pntr)
void | NetworkUDPAdvertise ()
void | NetworkUDPInitialize ()
void | NetworkUDPClose ()

## Variables

static ThreadMutex * | _network_udp_mutex = ThreadMutex::New()
static uint64 | _session_key = 0
| Session key to register ourselves to the master server.
NetworkUDPSocketHandler * | _udp_client_socket = NULL
| udp client socket
NetworkUDPSocketHandler * | _udp_server_socket = NULL
| udp server socket
NetworkUDPSocketHandler * | _udp_master_socket = NULL
| udp master socket

## Detailed Description

This file handles the UDP related communication.

This is the GameServer <-> MasterServer and GameServer <-> GameClient communication before the game is being joined.

Definition in file network_udp.cpp.

## Function Documentation

DEF_UDP_RECEIVE_COMMAND | ( | Server | ,
|  | PACKET_UDP_CLIENT_GET_NEWGRFS |  |
| ) |  |  |

A client has requested the names of some NewGRFs.

Replying this can be tricky as we have a limit of SEND_MTU bytes in the reply packet and we can send up to 100 bytes per NewGRF (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name). As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it could be that a packet overflows. To stop this we only reply with the first N NewGRFs so that if the first N + 1 NewGRFs would be sent, the packet overflows. in_reply and in_reply_count are used to keep a list of GRFs to send in the reply.

Definition at line 165 of file network_udp.cpp.

References GRFConfig::filename, GRFIdentifier::grfid, lastof, GRFIdentifier::md5sum, min(), GRFConfig::name, NETWORK_GRF_NAME_LENGTH, NETWORK_MAX_GRF_COUNT, PACKET_UDP_SERVER_NEWGRFS, SEND_MTU, Packet::Send_string(), strecpy(), and StrEmpty().

static void NetworkUDPQueryServerThread | ( | void * | pntr | ) | `[static]`

Threaded part for resolving the IP of a server and querying it.

Parameters:

  | pntr | the NetworkUDPQueryServerInfo.

Definition at line 435 of file network_udp.cpp.

References NetworkGameList::address, ThreadMutex::BeginCritical(), ThreadMutex::EndCritical(), NetworkAddress::GetAddressAsString(), NetworkAddress::GetHostname(), NetworkGameInfo::hostname, NetworkGameList::info, lastof, NetworkUDPQueryServerInfo::manually, NetworkGameList::manually, NetworkGameListAddItemDelayed(), PACKET_UDP_CLIENT_FIND_SERVER, NetworkUDPSocketHandler::SendPacket(), NetworkGameInfo::server_name, and strecpy().

void NetworkUDPRemoveAdvertise | ( | bool | blocking | ) |

Remove our advertise from the master-server.

Parameters:

  | blocking | whether to wait until the removal has finished.

Definition at line 486 of file network_udp.cpp.

References _network_server, _networking, and ThreadObject::New().

Referenced by NetworkDisconnect().