OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
14
15#include "../stdafx.h"
16
17#include "../debug.h"
18#include "../settings_type.h"
19#include "network_internal.h"
20#include "network_udp.h"
21
22#include "core/udp.h"
23
24#include "../safeguards.h"
25
27static uint16_t _network_udp_broadcast;
28
30struct UDPSocket {
31 const std::string name;
32 std::unique_ptr<NetworkUDPSocketHandler> socket = nullptr;
33
38 UDPSocket(const std::string &name) : name(name) {}
39
42 {
43 this->socket->CloseSocket();
44 this->socket = nullptr;
45 }
46
49 {
50 this->socket->ReceivePackets();
51 }
52};
53
54static UDPSocket _udp_client("Client");
55static UDPSocket _udp_server("Server");
56
57/* Communication with clients (we are server) */
58
61protected:
62 void ReceiveClientFindServer(Packet &p, NetworkAddress &client_addr) override;
63public:
69 ~ServerNetworkUDPSocketHandler() override = default;
70};
71
73{
74 Packet packet(this, PacketUDPType::ServerResponse);
75 this->SendPacket(packet, client_addr);
76
77 Debug(Facility::Net, Severity::Trace1, "Queried from {}", client_addr.GetHostname());
78}
79
80/* Communication with servers (we are client) */
81
84protected:
85 void ReceiveServerResponse(Packet &p, NetworkAddress &client_addr) override;
86public:
87 ~ClientNetworkUDPSocketHandler() override = default;
88};
89
91{
92 Debug(Facility::Net, Severity::Notice, "Server response from {}", client_addr.GetAddressAsString());
93
94 NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
95}
96
102{
103 for (NetworkAddress &addr : _broadcast_list) {
104 Debug(Facility::Net, Severity::Debug1, "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(Facility::Net, Severity::Notice, "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(Facility::Net, Severity::Notice, "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;
135 GetBindAddresses(&server, _settings_client.network.server_port);
136 _udp_server.socket = std::make_unique<ServerNetworkUDPSocketHandler>(&server);
137
138 _network_udp_server = false;
140}
141
144{
145 _network_udp_server = _udp_server.socket->Listen();
146}
147
150{
151 _udp_client.CloseSocket();
152 _udp_server.CloseSocket();
153
154 _network_udp_server = false;
156 Debug(Facility::Net, Severity::Debug1, "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:18
Helper class for handling all client side communication.
void ReceiveServerResponse(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:26
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:25
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:96
Base socket handler for all UDP sockets.
Definition udp.h:30
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition udp.cpp:70
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
Definition udp.cpp:20
Helper class for handling all server side communication.
void ReceiveClientFindServer(Packet &p, NetworkAddress &client_addr) override
Queries to the server for information about the game.
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Net
Net message facility.
Definition debug_type.h:33
@ Debug1
Debug #1 - High level debug messages.
Definition debug_type.h:20
@ Notice
Notice.
Definition debug_type.h:18
@ Trace1
Trace information #1.
Definition debug_type.h:22
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition network.cpp:78
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:707
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:733
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.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Internal entity of a packet.
Definition packet.h:55
Some information about a socket, which exists before the actual socket has been created to provide lo...
UDPSocket(const std::string &name)
Create this socket.
void CloseSocket()
Close the actual UDP socket.
void ReceivePackets()
Receive a packet at UDP level.
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.
@ 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