OpenTTD Source 20260109-master-g241b5fcdfe
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
10#ifndef NETWORK_CORE_UDP_H
11#define NETWORK_CORE_UDP_H
12
13#include "address.h"
14#include "packet.h"
15
22
25protected:
30
32
38 virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr);
39
45 virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr);
46
47 void HandleUDPPacket(Packet &p, NetworkAddress &client_addr);
48public:
50
52 ~NetworkUDPSocketHandler() override { this->CloseSocket(); }
53
54 bool Listen();
55 void CloseSocket();
56
57 void SendPacket(Packet &p, NetworkAddress &recv, bool all = false, bool broadcast = false);
58 void ReceivePackets();
59};
60
61#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
SocketHandler for all network sockets in OpenTTD.
Definition core.h:41
Base socket handler for all UDP sockets.
Definition udp.h:24
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:29
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:52
void ReceivePackets()
Receive a packet at UDP level.
Definition udp.cpp:110
virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr)
Queries to the server for information about the game.
Definition udp.cpp:190
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
Definition udp.cpp:185
NetworkAddressList bind
The address to bind to.
Definition udp.h:27
virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr)
Response to a query letting the client know we are here.
Definition udp.cpp:191
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.
Internal entity of a packet.
Definition packet.h:41
PacketUDPType
Enum with all types of UDP packets.
Definition udp.h:17
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition udp.h:18
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition udp.h:19
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
Definition udp.h:20