OpenTTD Source 20250312-master-gcdcc6b491d
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#include "../timer/timer_game_calendar.h"
17#include "../map_func.h"
18#include "../debug.h"
19#include "core/network_game_info.h"
20#include "network_gamelist.h"
21#include "network_internal.h"
22#include "network_udp.h"
23#include "network.h"
24#include "../core/endian_func.hpp"
25#include "../company_base.h"
26#include "../rev.h"
27#include "../newgrf_text.h"
28#include "../strings_func.h"
29#include "table/strings.h"
30
31#include "core/udp.h"
32
33#include "../safeguards.h"
34
36static uint16_t _network_udp_broadcast;
37
39struct UDPSocket {
40 const std::string name;
41 std::unique_ptr<NetworkUDPSocketHandler> socket = nullptr;
42
43 UDPSocket(const std::string &name) : name(name) {}
44
45 void CloseSocket()
46 {
47 this->socket->CloseSocket();
48 this->socket = nullptr;
49 }
50
51 void ReceivePackets()
52 {
53 this->socket->ReceivePackets();
54 }
55};
56
57static UDPSocket _udp_client("Client");
58static UDPSocket _udp_server("Server");
59
61
64protected:
65 void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
66public:
72 virtual ~ServerNetworkUDPSocketHandler() = default;
73};
74
76{
78 this->SendPacket(packet, client_addr);
79
80 Debug(net, 7, "Queried from {}", client_addr.GetHostname());
81}
82
84
87protected:
88 void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
89public:
90 virtual ~ClientNetworkUDPSocketHandler() = default;
91};
92
94{
95 Debug(net, 3, "Server response from {}", client_addr.GetAddressAsString());
96
97 NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
98}
99
102{
103 for (NetworkAddress &addr : _broadcast_list) {
104 Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
105
107 socket.SendPacket(p, addr, true, true);
108 }
109}
110
113{
114 /* We are still searching.. */
115 if (_network_udp_broadcast > 0) return;
116
117 Debug(net, 3, "Searching server");
118
120 _network_udp_broadcast = 300; // Stay searching for 300 ticks
121}
122
125{
126 /* If not closed, then do it. */
127 if (_udp_server.socket != nullptr) NetworkUDPClose();
128
129 Debug(net, 3, "Initializing UDP listeners");
130 assert(_udp_client.socket == nullptr && _udp_server.socket == nullptr);
131
132 _udp_client.socket = std::make_unique<ClientNetworkUDPSocketHandler>();
133
134 NetworkAddressList server;
136 _udp_server.socket = std::make_unique<ServerNetworkUDPSocketHandler>(&server);
137
138 _network_udp_server = false;
140}
141
147
150{
151 _udp_client.CloseSocket();
152 _udp_server.CloseSocket();
153
154 _network_udp_server = false;
156 Debug(net, 5, "Closed UDP listeners");
157}
158
161{
163 _udp_server.ReceivePackets();
164 } else {
165 _udp_client.ReceivePackets();
167 }
168}
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
‍*** Communication with servers (we are client) ***‍/
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:23
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:94
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
‍*** Communication with clients (we are server) ***‍/
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:80
NetworkGameList * NetworkAddServer(const std::string &connection_string, bool manually, bool never_expire)
Validates an address entered as a string and adds the server to the list.
Definition network.cpp:692
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:718
Basic functions/variables used all over the place.
Handling of the list of games.
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:57
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