OpenTTD Source  20241120-master-g6d3adc6169
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 
35 static bool _network_udp_server;
36 static uint16_t _network_udp_broadcast;
37 
39 struct UDPSocket {
40  const std::string name;
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 
65 protected:
66  void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
67 public:
73  virtual ~ServerNetworkUDPSocketHandler() = default;
74 };
75 
77 {
78  Packet packet(this, PACKET_UDP_SERVER_RESPONSE);
79  this->SendPacket(packet, client_addr);
80 
81  Debug(net, 7, "Queried from {}", client_addr.GetHostname());
82 }
83 
85 
88 protected:
89  void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
90 public:
91  virtual ~ClientNetworkUDPSocketHandler() = default;
92 };
93 
95 {
96  Debug(net, 3, "Server response from {}", client_addr.GetAddressAsString());
97 
98  NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
99 }
100 
103 {
104  for (NetworkAddress &addr : _broadcast_list) {
105  Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
106 
108  socket->SendPacket(p, addr, true, true);
109  }
110 }
111 
114 {
115  /* We are still searching.. */
116  if (_network_udp_broadcast > 0) return;
117 
118  Debug(net, 3, "Searching server");
119 
121  _network_udp_broadcast = 300; // Stay searching for 300 ticks
122 }
123 
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 
134 
135  NetworkAddressList server;
138 
139  _network_udp_server = false;
141 }
142 
145 {
147 }
148 
151 {
152  _udp_client.CloseSocket();
153  _udp_server.CloseSocket();
154 
155  _network_udp_server = false;
157  Debug(net, 5, "Closed UDP listeners");
158 }
159 
162 {
163  if (_network_udp_server) {
164  _udp_server.ReceivePackets();
165  } else {
166  _udp_client.ReceivePackets();
168  }
169 }
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:19
‍*** Communication with servers (we are client) ***‍/
Definition: network_udp.cpp:87
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
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 CloseSocket()
Close the actual UDP socket.
Definition: udp.cpp:59
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:44
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:74
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:110
‍*** Communication with clients (we are server) ***‍/
Definition: network_udp.cpp:64
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
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Definition: network_udp.cpp:72
#define Debug(category, level, format_string,...)
Ouptut 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.
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.
Definition: network_udp.cpp:36
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
Broadcast to all ips.
static bool _network_udp_server
Is the UDP server started?
Definition: network_udp.cpp:35
Sending and receiving UDP messages.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
NetworkSettings network
settings related to the network
uint16_t server_port
port the server listens on
Internal entity of a packet.
Definition: packet.h:42
Some information about a socket, which exists before the actual socket has been created to provide lo...
Definition: network_udp.cpp:39
NetworkUDPSocketHandler * socket
The actual socket, which may be nullptr when not initialized yet.
Definition: network_udp.cpp:41
const std::string name
The name of the socket.
Definition: network_udp.cpp:40
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