OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NETWORK_CORE_UDP_H
11#define NETWORK_CORE_UDP_H
12
13#include "address.h"
14#include "packet.h"
15
24
25template <> struct IsEnumPacketType<PacketUDPType> {
26 static constexpr bool value = true;
27};
28
31protected:
36
38
44 virtual void ReceiveClientFindServer(Packet &p, NetworkAddress &client_addr);
45
51 virtual void ReceiveServerResponse(Packet &p, NetworkAddress &client_addr);
52
53 void HandleUDPPacket(Packet &p, NetworkAddress &client_addr);
54public:
56
58 ~NetworkUDPSocketHandler() override { this->CloseSocket(); }
59
60 bool Listen();
61 void CloseSocket();
62
63 void SendPacket(Packet &p, NetworkAddress &recv, bool all = false, bool broadcast = false);
64 void ReceivePackets();
65};
66
67#endif /* NETWORK_CORE_UDP_H */
Wrapper for network addresses.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
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
NetworkSocketHandler()=default
Create a new unbound socket.
void CloseSocket()
Close the actual UDP socket.
Definition udp.cpp:57
bool Listen()
Start listening on the given host and port.
Definition udp.cpp:42
SocketList sockets
The opened sockets.
Definition udp.h:35
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition udp.cpp:72
~NetworkUDPSocketHandler() override
On destructing of this class, the socket needs to be closed.
Definition udp.h:58
virtual void ReceiveServerResponse(Packet &p, NetworkAddress &client_addr)
Response to a query letting the client know we are here.
Definition udp.cpp:185
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:22
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
Definition udp.cpp:179
NetworkAddressList bind
The address to bind to.
Definition udp.h:33
virtual void ReceiveClientFindServer(Packet &p, NetworkAddress &client_addr)
Queries to the server for information about the game.
Definition udp.cpp:184
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
Handle an incoming packets by sending it to the correct function.
Definition udp.cpp:157
Basic functions to create, fill and read packets.
static constexpr bool value
This is an enumeration of a PacketType.
Definition udp.h:26
Trait to mark an enumeration as a PacketType.
Definition packet.h:24
Internal entity of a packet.
Definition packet.h:47
PacketUDPType
Enum with all types of UDP packets.
Definition udp.h:20
@ ClientFindServer
Queries a game server for game information.
Definition udp.h:21
@ ServerResponse
Reply of the game server with game information.
Definition udp.h:22