OpenTTD Source  20241121-master-g67a0fccfad
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 
19 enum PacketUDPType : uint8_t {
23 };
24 
27 protected:
32 
34 
40  virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr);
41 
47  virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr);
48 
49  void HandleUDPPacket(Packet &p, NetworkAddress &client_addr);
50 public:
52 
54  virtual ~NetworkUDPSocketHandler() { this->CloseSocket(); }
55 
56  bool Listen();
57  void CloseSocket();
58 
59  void SendPacket(Packet &p, NetworkAddress &recv, bool all = false, bool broadcast = false);
60  void ReceivePackets();
61 };
62 
63 #endif /* NETWORK_CORE_UDP_H */
Wrapper for network addresses.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:19
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition: address.h:21
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:28
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:43
Base socket handler for all UDP sockets.
Definition: udp.h:26
void CloseSocket()
Close the actual UDP socket.
Definition: udp.cpp:59
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:44
SocketList sockets
The opened sockets.
Definition: udp.h:31
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:74
virtual ~NetworkUDPSocketHandler()
On destructing of this class, the socket needs to be closed.
Definition: udp.h:54
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:110
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
Definition: udp.cpp:24
virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr)
Queries to the server for information about the game.
Definition: udp.cpp:189
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
Definition: udp.cpp:184
NetworkAddressList bind
The address to bind to.
Definition: udp.h:29
virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr)
Response to a query letting the client know we are here.
Definition: udp.cpp:190
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
Handle an incoming packets by sending it to the correct function.
Definition: udp.cpp:156
Basic functions to create, fill and read packets.
Internal entity of a packet.
Definition: packet.h:42
PacketUDPType
Enum with all types of UDP packets.
Definition: udp.h:19
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:20
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:21
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
Definition: udp.h:22