OpenTTD Source  20240919-master-gdf0233f4c2
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 */
NetworkUDPSocketHandler
Base socket handler for all UDP sockets.
Definition: udp.h:26
NetworkUDPSocketHandler::SendPacket
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:74
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:43
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:189
NetworkUDPSocketHandler::CloseSocket
void CloseSocket()
Close the actual UDP socket.
Definition: udp.cpp:59
NetworkUDPSocketHandler::NetworkUDPSocketHandler
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
Definition: udp.cpp:24
SocketList
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition: address.h:21
NetworkUDPSocketHandler::Receive_SERVER_RESPONSE
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
PACKET_UDP_SERVER_RESPONSE
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:21
NetworkUDPSocketHandler::~NetworkUDPSocketHandler
virtual ~NetworkUDPSocketHandler()
On destructing of this class, the socket needs to be closed.
Definition: udp.h:54
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:19
address.h
NetworkUDPSocketHandler::sockets
SocketList sockets
The opened sockets.
Definition: udp.h:31
NetworkUDPSocketHandler::bind
NetworkAddressList bind
The address to bind to.
Definition: udp.h:29
NetworkUDPSocketHandler::HandleUDPPacket
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
Handle an incoming packets by sending it to the correct function.
Definition: udp.cpp:156
Packet
Internal entity of a packet.
Definition: packet.h:42
PACKET_UDP_CLIENT_FIND_SERVER
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:20
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:28
packet.h
PacketUDPType
PacketUDPType
Enum with all types of UDP packets.
Definition: udp.h:19
NetworkUDPSocketHandler::Listen
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:44
PACKET_UDP_END
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
Definition: udp.h:22
NetworkUDPSocketHandler::ReceivePackets
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:110
NetworkUDPSocketHandler::ReceiveInvalidPacket
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
Definition: udp.cpp:184