OpenTTD Source: src/network/network_udp.cpp Source File

OpenTTD Source 15.0-RC2

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
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
25 static bool _network_udp_server;
26 static uint16_t _network_udp_broadcast;
27
29 struct 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
47 static UDPSocket _udp_client("Client");
48 static UDPSocket _udp_server("Server");
49
50/* Communication with clients (we are server) */
51
53 class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
54protected:
55 void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
56public:
61 ServerNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
62 virtual ~ServerNetworkUDPSocketHandler() = default;
63};
64
65 void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet &, NetworkAddress &client_addr)
66{
67 Packet packet(this, PACKET_UDP_SERVER_RESPONSE);
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
76 class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
77protected:
78 void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
79public:
80 virtual ~ClientNetworkUDPSocketHandler() = default;
81};
82
83 void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet &, NetworkAddress &client_addr)
84{
85 Debug(net, 3, "Server response from {}", client_addr.GetAddressAsString());
86
87 NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
88}
89
91 static void NetworkUDPBroadCast(NetworkUDPSocketHandler &socket)
92{
93 for (NetworkAddress &addr : _broadcast_list) {
94 Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
95
96 Packet p(&socket, PACKET_UDP_CLIENT_FIND_SERVER);
97 socket.SendPacket(p, addr, true, true);
98 }
99}
100
102 void NetworkUDPSearchGame()
103{
104 /* We are still searching.. */
105 if (_network_udp_broadcast > 0) return;
106
107 Debug(net, 3, "Searching server");
108
109 NetworkUDPBroadCast(*_udp_client.socket);
110 _network_udp_broadcast = 300; // Stay searching for 300 ticks
111}
112
114 void NetworkUDPInitialize()
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;
125 GetBindAddresses(&server, _settings_client.network.server_port);
126 _udp_server.socket = std::make_unique<ServerNetworkUDPSocketHandler>(&server);
127
128 _network_udp_server = false;
129 _network_udp_broadcast = 0;
130}
131
133 void NetworkUDPServerListen()
134{
135 _network_udp_server = _udp_server.socket->Listen();
136}
137
139 void NetworkUDPClose()
140{
141 _udp_client.CloseSocket();
142 _udp_server.CloseSocket();
143
144 _network_udp_server = false;
145 _network_udp_broadcast = 0;
146 Debug(net, 5, "Closed UDP listeners");
147}
148
150 void NetworkBackgroundUDPLoop()
151{
152 if (_network_udp_server) {
153 _udp_server.ReceivePackets();
154 } else {
155 _udp_client.ReceivePackets();
156 if (_network_udp_broadcast > 0) _network_udp_broadcast--;
157 }
158}
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
ClientNetworkUDPSocketHandler
Helper class for handling all client side communication.
Definition network_udp.cpp:76
ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE
void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override
Response to a query letting the client know we are here.
Definition network_udp.cpp:83
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkAddress::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:24
NetworkAddress::GetAddressAsString
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:95
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
ServerNetworkUDPSocketHandler
Helper class for handling all server side communication.
Definition network_udp.cpp:53
ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER
void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override
Queries to the server for information about the game.
Definition network_udp.cpp:65
ServerNetworkUDPSocketHandler::ServerNetworkUDPSocketHandler
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Definition network_udp.cpp:61
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition network.cpp:81
NetworkAddServer
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:692
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:718
network_internal.h
Variables and function used internally.
NetworkUDPBroadCast
static void NetworkUDPBroadCast(NetworkUDPSocketHandler &socket)
Broadcast to all ips.
Definition network_udp.cpp:91
NetworkUDPClose
void NetworkUDPClose()
Close all UDP related stuff.
Definition network_udp.cpp:139
_udp_server
static UDPSocket _udp_server("Server")
udp server socket
NetworkUDPInitialize
void NetworkUDPInitialize()
Initialize the whole UDP bit.
Definition network_udp.cpp:114
NetworkUDPSearchGame
void NetworkUDPSearchGame()
Find all servers.
Definition network_udp.cpp:102
NetworkUDPServerListen
void NetworkUDPServerListen()
Start the listening of the UDP server component.
Definition network_udp.cpp:133
_udp_client
static UDPSocket _udp_client("Client")
udp client socket
NetworkBackgroundUDPLoop
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
Definition network_udp.cpp:150
_network_udp_broadcast
static uint16_t _network_udp_broadcast
Timeout for the UDP broadcasts.
Definition network_udp.cpp:26
_network_udp_server
static bool _network_udp_server
Is the UDP server started?
Definition network_udp.cpp:25
network_udp.h
Sending and receiving UDP messages.
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
ClientSettings::network
NetworkSettings network
settings related to the network
Definition settings_type.h:651
NetworkSettings::server_port
uint16_t server_port
port the server listens on
Definition settings_type.h:343
Packet
Internal entity of a packet.
Definition packet.h:43
UDPSocket
Some information about a socket, which exists before the actual socket has been created to provide lo...
Definition network_udp.cpp:29
UDPSocket::socket
std::unique_ptr< NetworkUDPSocketHandler > socket
The actual socket, which may be nullptr when not initialized yet.
Definition network_udp.cpp:31
UDPSocket::name
const std::string name
The name of the socket.
Definition network_udp.cpp:30
udp.h
Basic functions to receive and send UDP packets.
PACKET_UDP_CLIENT_FIND_SERVER
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition udp.h:20
PACKET_UDP_SERVER_RESPONSE
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition udp.h:21