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

OpenTTD Source 13.2.1

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 "../date_func.h"
17 #include "../map_func.h"
18 #include "../debug.h"
19 #include "core/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
35 static bool _network_udp_server;
36 static uint16 _network_udp_broadcast;
37
39 struct UDPSocket {
40 const std::string name;
41 NetworkUDPSocketHandler *socket;
42
43 UDPSocket(const std::string &name) : name(name), socket(nullptr) {}
44
45 void CloseSocket()
46 {
47 this->socket->CloseSocket();
48 delete this->socket;
49 this->socket = nullptr;
50 }
51
52 void ReceivePackets()
53 {
54 this->socket->ReceivePackets();
55 }
56 };
57
58 static UDPSocket _udp_client("Client");
59 static UDPSocket _udp_server("Server");
60
62
64 class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
65 protected:
66 void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override;
67 public:
72 ServerNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
73 virtual ~ServerNetworkUDPSocketHandler() {}
74 };
75
76 void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
77 {
78 Packet packet(PACKET_UDP_SERVER_RESPONSE);
79 this->SendPacket(&packet, client_addr);
80
81 Debug(net, 7, "Queried from {}", client_addr->GetHostname());
82 }
83
85
87 class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
88 protected:
89 void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override;
90 public:
91 virtual ~ClientNetworkUDPSocketHandler() {}
92 };
93
94 void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
95 {
96 Debug(net, 3, "Server response from {}", client_addr->GetAddressAsString());
97
98 NetworkAddServer(client_addr->GetAddressAsString(false), false, true);
99 }
100
102 static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
103 {
104 for (NetworkAddress &addr : _broadcast_list) {
105 Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
106
107 Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
108 socket->SendPacket(&p, &addr, true, true);
109 }
110 }
111
113 void NetworkUDPSearchGame()
114 {
115 /* We are still searching.. */
116 if (_network_udp_broadcast > 0) return;
117
118 Debug(net, 3, "Searching server");
119
120 NetworkUDPBroadCast(_udp_client.socket);
121 _network_udp_broadcast = 300; // Stay searching for 300 ticks
122 }
123
125 void NetworkUDPInitialize()
126 {
127 /* If not closed, then do it. */
128 if (_udp_server.socket != nullptr) NetworkUDPClose();
129
130 Debug(net, 3, "Initializing UDP listeners");
131 assert(_udp_client.socket == nullptr && _udp_server.socket == nullptr);
132
133 _udp_client.socket = new ClientNetworkUDPSocketHandler();
134
135 NetworkAddressList server;
136 GetBindAddresses(&server, _settings_client.network.server_port);
137 _udp_server.socket = new ServerNetworkUDPSocketHandler(&server);
138
139 _network_udp_server = false;
140 _network_udp_broadcast = 0;
141 }
142
144 void NetworkUDPServerListen()
145 {
146 _network_udp_server = _udp_server.socket->Listen();
147 }
148
150 void NetworkUDPClose()
151 {
152 _udp_client.CloseSocket();
153 _udp_server.CloseSocket();
154
155 _network_udp_server = false;
156 _network_udp_broadcast = 0;
157 Debug(net, 5, "Closed UDP listeners");
158 }
159
161 void NetworkBackgroundUDPLoop()
162 {
163 if (_network_udp_server) {
164 _udp_server.ReceivePackets();
165 } else {
166 _udp_client.ReceivePackets();
167 if (_network_udp_broadcast > 0) _network_udp_broadcast--;
168 }
169 }
NetworkUDPSocketHandler
Base socket handler for all UDP sockets.
Definition: udp.h:26
NetworkAddServer
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:675
UDPSocket::name
const std::string name
The name of the socket.
Definition: network_udp.cpp:40
NetworkUDPServerListen
void NetworkUDPServerListen()
Start the listening of the UDP server component.
Definition: network_udp.cpp:144
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
Definition: network.cpp:701
NetworkSettings::server_port
uint16 server_port
port the server listens on
Definition: settings_type.h:282
PACKET_UDP_CLIENT_FIND_SERVER
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:20
_udp_client
static UDPSocket _udp_client("Client")
udp client socket
_udp_server
static UDPSocket _udp_server("Server")
udp server socket
NetworkUDPBroadCast
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
Broadcast to all ips.
Definition: network_udp.cpp:102
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:53
NetworkUDPSocketHandler::CloseSocket
void CloseSocket()
Close the actual UDP socket.
Definition: udp.cpp:59
NetworkUDPSearchGame
void NetworkUDPSearchGame()
Find all servers.
Definition: network_udp.cpp:113
UDPSocket
Some information about a socket, which exists before the actual socket has been created to provide lo...
Definition: network_udp.cpp:39
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:21
UDPSocket::socket
NetworkUDPSocketHandler * socket
The actual socket, which may be nullptr when not initialized yet.
Definition: network_udp.cpp:41
_network_udp_broadcast
static uint16 _network_udp_broadcast
Timeout for the UDP broadcasts.
Definition: network_udp.cpp:36
NetworkAddress::GetAddressAsString
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:94
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkUDPSocketHandler
‍*** Communication with servers (we are client) ***‍/
Definition: network_udp.cpp:87
PACKET_UDP_SERVER_RESPONSE
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:21
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:76
NetworkBackgroundUDPLoop
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
Definition: network_udp.cpp:161
ServerNetworkUDPSocketHandler
‍*** Communication with clients (we are server) ***‍/
Definition: network_udp.cpp:64
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
_network_udp_server
static bool _network_udp_server
Is the UDP server started?
Definition: network_udp.cpp:35
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:94
network_udp.h
NetworkUDPInitialize
void NetworkUDPInitialize()
Initialize the whole UDP bit.
Definition: network_udp.cpp:125
network_internal.h
NetworkAddress::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:23
NetworkUDPSocketHandler::Listen
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:44
game_info.h
udp.h
network.h
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
NetworkUDPClose
void NetworkUDPClose()
Close all UDP related stuff.
Definition: network_udp.cpp:150
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:605
NetworkUDPSocketHandler::ReceivePackets
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:110
network_gamelist.h
ServerNetworkUDPSocketHandler::ServerNetworkUDPSocketHandler
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Definition: network_udp.cpp:72
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition: network.cpp:74
NetworkUDPSocketHandler::SendPacket
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:74