OpenTTD Source  1.11.2
udp.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
12 #ifndef NETWORK_CORE_UDP_H
13 #define NETWORK_CORE_UDP_H
14 
15 #include "address.h"
16 #include "packet.h"
17 
33 };
34 
37  SLT_IPv4 = 0,
38  SLT_IPv6 = 1,
40 
42 };
43 
46 protected:
51 
52  NetworkRecvStatus CloseConnection(bool error = true) override;
53 
55 
61  virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr);
62 
69  virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr);
70 
76  virtual void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr);
77 
105  virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr);
106 
116  virtual void Receive_SERVER_REGISTER(Packet *p, NetworkAddress *client_addr);
117 
123  virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr);
124 
132  virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr);
133 
143  virtual void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr);
144 
152  virtual void Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr);
153 
163  virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr);
164 
175  virtual void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr);
176 
183  virtual void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr);
184 
185  void HandleUDPPacket(Packet *p, NetworkAddress *client_addr);
186 public:
188 
190  virtual ~NetworkUDPSocketHandler() { this->Close(); }
191 
192  bool Listen();
193  void Close() override;
194 
195  void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false);
196  void ReceivePackets();
197 };
198 
199 #endif /* NETWORK_CORE_UDP_H */
PACKET_UDP_CLIENT_GET_LIST
@ PACKET_UDP_CLIENT_GET_LIST
Request for serverlist from master server.
Definition: udp.h:26
NetworkUDPSocketHandler::Receive_SERVER_UNREGISTER
virtual void Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr)
A server unregisters itself at the master server.
Definition: udp.cpp:209
PACKET_UDP_CLIENT_DETAIL_INFO
@ PACKET_UDP_CLIENT_DETAIL_INFO
Queries a game server about details of the game, such as companies.
Definition: udp.h:22
NetworkUDPSocketHandler
Base socket handler for all UDP sockets.
Definition: udp.h:45
NetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY
virtual void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr)
The master server sends us a session key.
Definition: udp.cpp:212
PACKET_UDP_CLIENT_GET_NEWGRFS
@ PACKET_UDP_CLIENT_GET_NEWGRFS
Requests the name for a list of GRFs (GRF_ID and MD5)
Definition: udp.h:29
NetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST
virtual void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr)
The server sends a list of servers.
Definition: udp.cpp:208
ServerListType
ServerListType
The types of server lists we can get.
Definition: udp.h:36
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41
PACKET_UDP_CLIENT_FIND_SERVER
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:20
PACKET_UDP_SERVER_UNREGISTER
@ PACKET_UDP_SERVER_UNREGISTER
Request to be removed from the server-list.
Definition: udp.h:28
PACKET_UDP_SERVER_REGISTER
@ PACKET_UDP_SERVER_REGISTER
Packet to register itself to the master server.
Definition: udp.h:24
NetworkUDPSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: udp.cpp:67
NetworkUDPSocketHandler::Close
void Close() override
Close the given UDP socket.
Definition: udp.cpp:59
NetworkUDPSocketHandler::Receive_SERVER_NEWGRFS
virtual void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The server returns information about some NewGRFs.
Definition: udp.cpp:211
NetworkUDPSocketHandler::NetworkUDPSocketHandler
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
Definition: udp.cpp:24
NetworkUDPSocketHandler::Receive_SERVER_REGISTER
virtual void Receive_SERVER_REGISTER(Packet *p, NetworkAddress *client_addr)
Registers the server to the master server.
Definition: udp.cpp:205
SLT_IPv6
@ SLT_IPv6
Get the IPv6 addresses.
Definition: udp.h:38
NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
Queries to the server for information about the game.
Definition: udp.cpp:201
SmallMap< NetworkAddress, SOCKET >
PACKET_UDP_MASTER_SESSION_KEY
@ PACKET_UDP_MASTER_SESSION_KEY
Sends a fresh session key to the client.
Definition: udp.h:31
NetworkUDPSocketHandler::~NetworkUDPSocketHandler
virtual ~NetworkUDPSocketHandler()
On destructing of this class, the socket needs to be closed.
Definition: udp.h:190
NetworkUDPSocketHandler::Receive_SERVER_DETAIL_INFO
virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Reply with detailed company information.
Definition: udp.cpp:204
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:20
address.h
NetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS
virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The client requests information about some NewGRFs.
Definition: udp.cpp:210
NetworkUDPSocketHandler::sockets
SocketList sockets
The opened sockets.
Definition: udp.h:50
PACKET_UDP_END
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
Definition: udp.h:32
NetworkUDPSocketHandler::bind
NetworkAddressList bind
The address to bind to.
Definition: udp.h:48
PACKET_UDP_MASTER_ACK_REGISTER
@ PACKET_UDP_MASTER_ACK_REGISTER
Packet indicating registration has succeeded.
Definition: udp.h:25
PACKET_UDP_SERVER_DETAIL_INFO
@ PACKET_UDP_SERVER_DETAIL_INFO
Reply of the game server about details of the game, such as companies.
Definition: udp.h:23
SLT_END
@ SLT_END
End of 'arrays' marker.
Definition: udp.h:41
SLT_IPv4
@ SLT_IPv4
Get the IPv4 addresses.
Definition: udp.h:37
Packet
Internal entity of a packet.
Definition: packet.h:40
NetworkUDPSocketHandler::ReceiveInvalidPacket
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr)
Helper for logging receiving invalid packets.
Definition: udp.cpp:196
PACKET_UDP_SERVER_NEWGRFS
@ PACKET_UDP_SERVER_NEWGRFS
Sends the list of NewGRF's requested.
Definition: udp.h:30
PACKET_UDP_SERVER_RESPONSE
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:21
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
NetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER
virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
The master server acknowledges the registration.
Definition: udp.cpp:206
packet.h
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
NetworkUDPSocketHandler::HandleUDPPacket
void HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
Handle an incoming packets by sending it to the correct function.
Definition: udp.cpp:158
NetworkUDPSocketHandler::Listen
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:44
SLT_AUTODETECT
@ SLT_AUTODETECT
Autodetect the type based on the connection.
Definition: udp.h:39
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
NetworkUDPSocketHandler::Receive_CLIENT_GET_LIST
virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr)
The client requests a list of servers.
Definition: udp.cpp:207
PACKET_UDP_MASTER_RESPONSE_LIST
@ PACKET_UDP_MASTER_RESPONSE_LIST
Response from master server with server ip's + port's.
Definition: udp.h:27
NetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO
virtual void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Query for detailed information about companies.
Definition: udp.cpp:203
NetworkUDPSocketHandler::ReceivePackets
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:116
NetworkUDPSocketHandler::Receive_SERVER_RESPONSE
virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
Return of server information to the client.
Definition: udp.cpp:202
PacketUDPType
PacketUDPType
Enum with all types of UDP packets.
Definition: udp.h:19
NetworkUDPSocketHandler::SendPacket
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:80