OpenTTD Source 20250524-master-gc366e6a48e
network_udp.cpp
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
15#include "../stdafx.h"
16
17#include "../debug.h"
18#include "network_internal.h"
19#include "network_udp.h"
20
21#include "core/udp.h"
22
23#include "../safeguards.h"
24
26static uint16_t _network_udp_broadcast;
27
29struct UDPSocket {
30 const std::string name;
31 std::unique_ptr<NetworkUDPSocketHandler> socket = nullptr;
32
33 UDPSocket(const std::string &name) : name(name) {}
34
35 void CloseSocket()
36 {
37 this->socket->CloseSocket();
38 this->socket = nullptr;
39 }
40
41 void ReceivePackets()
42 {
43 this->socket->ReceivePackets();
44 }
45};
46
47static UDPSocket _udp_client("Client");
48static UDPSocket _udp_server("Server");
49
50/* Communication with clients (we are server) */
51
54protected:
55 void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
56public:
62 virtual ~ServerNetworkUDPSocketHandler() = default;
63};
64
66{
68 this->SendPacket(packet, client_addr);
69
70 Debug(net, 7, "Queried from {}", client_addr.GetHostname());
71}
72
73/* Communication with servers (we are client) */
74
77protected:
78 void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
79public:
80 virtual ~ClientNetworkUDPSocketHandler() = default;
81};
82
84{
85 Debug(net, 3, "Server response from {}", client_addr.GetAddressAsString());
86
87 NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
88}
89
92{
93 for (NetworkAddress &addr : _broadcast_list) {
94 Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
95
97 socket.SendPacket(p, addr, true, true);
98 }
99}
100
103{
104 /* We are still searching.. */
105 if (_network_udp_broadcast > 0) return;
106
107 Debug(net, 3, "Searching server");
108
110 _network_udp_broadcast = 300; // Stay searching for 300 ticks
111}
112
115{
116 /* If not closed, then do it. */
117 if (_udp_server.socket != nullptr) NetworkUDPClose();
118
119 Debug(net, 3, "Initializing UDP listeners");
120 assert(_udp_client.socket == nullptr && _udp_server.socket == nullptr);
121
122 _udp_client.socket = std::make_unique<ClientNetworkUDPSocketHandler>();
123
124 NetworkAddressList server;
126 _udp_server.socket = std::make_unique<ServerNetworkUDPSocketHandler>(&server);
127
128 _network_udp_server = false;
130}
131
137
140{
141 _udp_client.CloseSocket();
142 _udp_server.CloseSocket();
143
144 _network_udp_server = false;
146 Debug(net, 5, "Closed UDP listeners");
147}
148
151{
153 _udp_server.ReceivePackets();
154 } else {
155 _udp_client.ReceivePackets();
157 }
158}
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
Helper class for handling all client side communication.
void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override
Response to a query letting the client know we are here.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:24
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:95
Base socket handler for all UDP sockets.
Definition udp.h:26
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition udp.cpp:74
Helper class for handling all server side communication.
void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override
Queries to the server for information about the game.
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition network.cpp:82
NetworkGame * NetworkAddServer(std::string_view connection_string, bool manually, bool never_expire)
Validates an address entered as a string and adds the server to the list.
Definition network.cpp:693
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:719
Variables and function used internally.
static void NetworkUDPBroadCast(NetworkUDPSocketHandler &socket)
Broadcast to all ips.
void NetworkUDPClose()
Close all UDP related stuff.
static UDPSocket _udp_server("Server")
udp server socket
void NetworkUDPInitialize()
Initialize the whole UDP bit.
void NetworkUDPSearchGame()
Find all servers.
void NetworkUDPServerListen()
Start the listening of the UDP server component.
static UDPSocket _udp_client("Client")
udp client socket
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
static uint16_t _network_udp_broadcast
Timeout for the UDP broadcasts.
static bool _network_udp_server
Is the UDP server started?
Sending and receiving UDP messages.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:59
NetworkSettings network
settings related to the network
uint16_t server_port
port the server listens on
Internal entity of a packet.
Definition packet.h:43
Some information about a socket, which exists before the actual socket has been created to provide lo...
std::unique_ptr< NetworkUDPSocketHandler > socket
The actual socket, which may be nullptr when not initialized yet.
const std::string name
The name of the socket.
Basic functions to receive and send UDP packets.
@ 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