OpenTTD
|
Base socket handler for all UDP sockets. More...
#include <udp.h>
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 | SendNetworkGameInfo (Packet *p, const NetworkGameInfo *info) |
Serializes the NetworkGameInfo struct to the packet. | |
void | ReceiveNetworkGameInfo (Packet *p, NetworkGameInfo *info) |
Deserializes the NetworkGameInfo struct from the packet. | |
![]() | |
NetworkSocketHandler () | |
Create a new unbound socket. | |
virtual | ~NetworkSocketHandler () |
Close the socket when destructing the socket handler. | |
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. | |
void | SendGRFIdentifier (Packet *p, const GRFIdentifier *grf) |
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet. | |
void | ReceiveGRFIdentifier (Packet *p, GRFIdentifier *grf) |
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet. | |
void | SendCompanyInformation (Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats, uint max_len=NETWORK_COMPANY_NAME_LENGTH) |
Package some generic company information into a 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. | |
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) |
Return of server information to the client. | |
virtual void | Receive_CLIENT_DETAIL_INFO (Packet *p, NetworkAddress *client_addr) |
Query for detailed information about companies. | |
virtual void | Receive_SERVER_DETAIL_INFO (Packet *p, NetworkAddress *client_addr) |
Reply with detailed company information. | |
virtual void | Receive_SERVER_REGISTER (Packet *p, NetworkAddress *client_addr) |
Registers the server to the master server. | |
virtual void | Receive_MASTER_ACK_REGISTER (Packet *p, NetworkAddress *client_addr) |
The master server acknowledges the registration. | |
virtual void | Receive_CLIENT_GET_LIST (Packet *p, NetworkAddress *client_addr) |
The client requests a list of servers. | |
virtual void | Receive_MASTER_RESPONSE_LIST (Packet *p, NetworkAddress *client_addr) |
The server sends a list of servers. | |
virtual void | Receive_SERVER_UNREGISTER (Packet *p, NetworkAddress *client_addr) |
A server unregisters itself at the master server. | |
virtual void | Receive_CLIENT_GET_NEWGRFS (Packet *p, NetworkAddress *client_addr) |
The client requests information about some NewGRFs. | |
virtual void | Receive_SERVER_NEWGRFS (Packet *p, NetworkAddress *client_addr) |
The server returns information about some NewGRFs. | |
virtual void | Receive_MASTER_SESSION_KEY (Packet *p, NetworkAddress *client_addr) |
The master server sends us a 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. |
NetworkUDPSocketHandler::NetworkUDPSocketHandler | ( | NetworkAddressList * | bind = NULL | ) |
Create an UDP socket but don't listen yet.
bind | the addresses to bind to. |
Definition at line 27 of file udp.cpp.
References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), and SmallVector< T, S >::End().
|
protectedvirtual |
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.
error | Whether we quit under an error condition or not. |
Reimplemented from NetworkSocketHandler.
Definition at line 70 of file udp.cpp.
References NETWORK_RECV_STATUS_OKAY.
|
inlineprotectedvirtual |
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.
config | the GRF to handle |
Reimplemented in ClientNetworkUDPSocketHandler.
Definition at line 232 of file udp.h.
Referenced by ReceiveNetworkGameInfo().
|
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 294 of file udp.cpp.
References DEBUG, 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, Receive_CLIENT_DETAIL_INFO(), Receive_CLIENT_FIND_SERVER(), Receive_CLIENT_GET_LIST(), Receive_CLIENT_GET_NEWGRFS(), Receive_MASTER_ACK_REGISTER(), Receive_MASTER_RESPONSE_LIST(), Receive_MASTER_SESSION_KEY(), Receive_SERVER_DETAIL_INFO(), Receive_SERVER_NEWGRFS(), Receive_SERVER_REGISTER(), Receive_SERVER_RESPONSE(), Receive_SERVER_UNREGISTER(), Packet::Recv_uint8(), and NetworkSocketHandler::Reopen().
Referenced by ReceivePackets().
bool NetworkUDPSocketHandler::Listen | ( | ) |
Start listening on the given host and port.
Definition at line 47 of file udp.cpp.
References SmallVector< T, S >::Begin(), bind, Close(), SmallVector< T, S >::End(), SmallVector< T, S >::Length(), and sockets.
Referenced by SendPacket().
|
protectedvirtual |
Query for detailed information about companies.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in ServerNetworkUDPSocketHandler.
Definition at line 339 of file udp.cpp.
References PACKET_UDP_CLIENT_DETAIL_INFO, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
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 337 of file udp.cpp.
References PACKET_UDP_CLIENT_FIND_SERVER, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The client requests a list of servers.
uint8 The protocol version. uint8 The type of server to look for: IPv4, IPv6 or based on the received packet.
p | The received packet. |
client_addr | The origin of the packet. |
Definition at line 343 of file udp.cpp.
References PACKET_UDP_CLIENT_GET_LIST, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The client requests information about some NewGRFs.
uint8 The number of NewGRFs information is requested about. For each NewGRF: uint32 The GRFID. 16 * uint8 MD5 checksum of the GRF.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in ServerNetworkUDPSocketHandler.
Definition at line 346 of file udp.cpp.
References PACKET_UDP_CLIENT_GET_NEWGRFS, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The master server acknowledges the registration.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in MasterNetworkUDPSocketHandler.
Definition at line 342 of file udp.cpp.
References PACKET_UDP_MASTER_ACK_REGISTER, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The server sends a list of servers.
uint8 The protocol version. For each server: 4 or 16 bytes of IPv4 or IPv6 address. uint8 The port.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in ClientNetworkUDPSocketHandler.
Definition at line 344 of file udp.cpp.
References PACKET_UDP_MASTER_RESPONSE_LIST, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The master server sends us a session key.
uint64 The session key.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in MasterNetworkUDPSocketHandler.
Definition at line 348 of file udp.cpp.
References PACKET_UDP_MASTER_SESSION_KEY, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
Reply with detailed company information.
uint8 Version of the packet. uint8 Number of companies. For each company: uint8 ID of the company. string Name of the company. uint32 Year the company was inaugurated. uint64 Value. uint64 Money. uint64 Income. uint16 Performance (last quarter). bool Company is password protected. uint16 Number of trains. uint16 Number of lorries. uint16 Number of busses. uint16 Number of planes. uint16 Number of ships. uint16 Number of train stations. uint16 Number of lorry stations. uint16 Number of bus stops. uint16 Number of airports and heliports. uint16 Number of harbours. bool Company is an AI.
p | The received packet. |
client_addr | The origin of the packet. |
Definition at line 340 of file udp.cpp.
References PACKET_UDP_SERVER_DETAIL_INFO, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
The server returns information about some NewGRFs.
uint8 The number of NewGRFs information is requested about. For each NewGRF: uint32 The GRFID. 16 * uint8 MD5 checksum of the GRF. string The name of the NewGRF.
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in ClientNetworkUDPSocketHandler.
Definition at line 347 of file udp.cpp.
References PACKET_UDP_SERVER_NEWGRFS, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
Registers the server to the master server.
string The "welcome" message to root out other binary packets. uint8 Version of the protocol. uint16 The port to unregister. uint64 The session key.
p | The received packet. |
client_addr | The origin of the packet. |
Definition at line 341 of file udp.cpp.
References PACKET_UDP_SERVER_REGISTER, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
Return of server information to the client.
This packet has several legacy versions, so we list the version and size of each "field":
Version: Bytes: Description: all 1 the version of this packet's structure
4+ 1 number of GRFs attached (n) 4+ n * 20 unique identifier for GRF files. Consists of:
3+ 4 current game date in days since 1-1-0 (DMY) 3+ 4 game introduction date in days since 1-1-0 (DMY)
2+ 1 maximum number of companies allowed on the server 2+ 1 number of companies on the server 2+ 1 maximum number of spectators allowed on the server
1+ var string with the name of the server 1+ var string with the revision of the server 1+ 1 the language run on the server (0 = any, 1 = English, 2 = German, 3 = French) 1+ 1 whether the server uses a password (0 = no, 1 = yes) 1+ 1 maximum number of clients allowed on the server 1+ 1 number of clients on the server 1+ 1 number of spectators on the server 1 & 2 2 current game date in days since 1-1-1920 (DMY) 1 & 2 2 game introduction date in days since 1-1-1920 (DMY) 1+ var string with the name of the map 1+ 2 width of the map in tiles 1+ 2 height of the map in tiles 1+ 1 type of map: (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland) 1+ 1 whether the server is dedicated (0 = no, 1 = yes)
p | The received packet. |
client_addr | The origin of the packet. |
Reimplemented in ClientNetworkUDPSocketHandler.
Definition at line 338 of file udp.cpp.
References PACKET_UDP_SERVER_RESPONSE, and ReceiveInvalidPacket().
Referenced by HandleUDPPacket().
|
protectedvirtual |
A server unregisters itself at the master server.
uint8 Version of the protocol. uint16 The port to unregister.
p | The received packet. |
client_addr | The origin of the packet. |
Definition at line 345 of file udp.cpp.
References PACKET_UDP_SERVER_UNREGISTER, 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 332 of file udp.cpp.
References DEBUG, and NetworkAddress::GetAddressAsString().
Referenced by Receive_CLIENT_DETAIL_INFO(), Receive_CLIENT_FIND_SERVER(), Receive_CLIENT_GET_LIST(), Receive_CLIENT_GET_NEWGRFS(), Receive_MASTER_ACK_REGISTER(), Receive_MASTER_RESPONSE_LIST(), Receive_MASTER_SESSION_KEY(), Receive_SERVER_DETAIL_INFO(), Receive_SERVER_NEWGRFS(), Receive_SERVER_REGISTER(), Receive_SERVER_RESPONSE(), and Receive_SERVER_UNREGISTER().
void NetworkUDPSocketHandler::ReceiveNetworkGameInfo | ( | Packet * | p, |
NetworkGameInfo * | info | ||
) |
Deserializes the NetworkGameInfo struct from the packet.
p | the packet to read the data from |
info | the NetworkGameInfo to deserialize into |
Definition at line 223 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(), GRFConfig::ident, 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::ReceiveGRFIdentifier(), Packet::Recv_bool(), Packet::Recv_string(), Packet::Recv_uint16(), Packet::Recv_uint32(), Packet::Recv_uint8(), NetworkGameInfo::server_lang, NetworkGameInfo::server_name, NetworkGameInfo::server_revision, NetworkGameInfo::spectators_max, NetworkGameInfo::spectators_on, NetworkGameInfo::start_date, and NetworkGameInfo::use_password.
Referenced by ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE().
void NetworkUDPSocketHandler::SendNetworkGameInfo | ( | Packet * | p, |
const NetworkGameInfo * | info | ||
) |
Serializes the NetworkGameInfo struct to the packet.
p | the packet to write the data to |
info | the NetworkGameInfo struct to serialize |
Definition at line 161 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(), GRFConfig::ident, NetworkGameInfo::map_height, NetworkServerGameInfo::map_name, NetworkGameInfo::map_set, NetworkGameInfo::map_width, NETWORK_GAME_INFO_VERSION, GRFConfig::next, Packet::Send_bool(), Packet::Send_string(), Packet::Send_uint16(), Packet::Send_uint32(), Packet::Send_uint8(), NetworkSocketHandler::SendGRFIdentifier(), NetworkGameInfo::server_lang, NetworkGameInfo::server_name, NetworkGameInfo::server_revision, NetworkGameInfo::spectators_max, NetworkGameInfo::spectators_on, NetworkGameInfo::start_date, and NetworkGameInfo::use_password.
Referenced by ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER().
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 83 of file udp.cpp.
References SmallVector< T, S >::Begin(), Packet::buffer, DEBUG, 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 NetworkUDPAdvertiseThread(), NetworkUDPBroadCast(), NetworkUDPQueryMasterServer(), NetworkUDPQueryServer(), NetworkUDPRemoveAdvertiseThread(), ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(), ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), and ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE().
|
protected |
|
protected |
The opened sockets.
Definition at line 55 of file udp.h.
Referenced by Close(), Listen(), ReceivePackets(), and SendPacket().