OpenTTD Source  20240919-master-gdf0233f4c2
network_turn.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 
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../error.h"
13 #include "../strings_func.h"
14 #include "network_coordinator.h"
15 #include "network_turn.h"
16 
17 #include "table/strings.h"
18 
19 #include "../safeguards.h"
20 
23 private:
25 
26 public:
32 
33  void OnFailure() override
34  {
35  Debug(net, 9, "Turn::OnFailure()");
36 
37  this->handler->connecter = nullptr;
38 
39  this->handler->ConnectFailure();
40  }
41 
42  void OnConnect(SOCKET s) override
43  {
44  Debug(net, 9, "Turn::OnConnect()");
45 
46  this->handler->connecter = nullptr;
47 
48  this->handler->sock = s;
49  }
50 };
51 
53 {
54  Debug(net, 9, "Receive_TURN_ERROR()");
55 
56  this->ConnectFailure();
57 
58  return false;
59 }
60 
62 {
63  Debug(net, 9, "Receive_TURN_CONNECTED()");
64 
65  std::string hostname = p.Recv_string(NETWORK_HOSTNAME_LENGTH);
66 
67  /* Act like we no longer have a socket, as we are handing it over to the
68  * game handler. */
69  SOCKET game_sock = this->sock;
70  this->sock = INVALID_SOCKET;
71 
73  _network_coordinator_client.ConnectSuccess(this->token, game_sock, address);
74 
75  return false;
76 }
77 
82 {
83  Debug(net, 9, "Turn::Connect()");
84 
85  this->connect_started = true;
86  this->connecter = TCPConnecter::Create<NetworkTurnConnecter>(this, this->connection_string);
87 }
88 
99 /* static */ std::unique_ptr<ClientNetworkTurnSocketHandler> ClientNetworkTurnSocketHandler::Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, const std::string &connection_string)
100 {
101  auto turn_handler = std::make_unique<ClientNetworkTurnSocketHandler>(token, tracking_number, connection_string);
102 
103  auto p = std::make_unique<Packet>(turn_handler.get(), PACKET_TURN_SERCLI_CONNECT);
104  p->Send_uint8(NETWORK_COORDINATOR_VERSION);
105  p->Send_string(ticket);
106 
107  turn_handler->SendPacket(std::move(p));
108 
109  return turn_handler;
110 }
111 
112 void ClientNetworkTurnSocketHandler::ConnectFailure()
113 {
115 }
116 
118 {
120 
121  /* Also make sure any pending connecter is killed ASAP. */
122  if (this->connecter != nullptr) {
123  this->connecter->Kill();
124  this->connecter = nullptr;
125  }
126 
128 }
129 
130 ClientNetworkTurnSocketHandler::~ClientNetworkTurnSocketHandler()
131 {
132  if (this->connecter != nullptr) {
133  this->connecter->Kill();
134  this->connecter = nullptr;
135  }
136 }
137 
143 {
144  if (this->sock == INVALID_SOCKET) return;
145 
146  if (this->CanSendReceive()) {
147  this->ReceivePackets();
148  }
149 
150  this->SendPackets();
151 }
ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED
bool Receive_TURN_CONNECTED(Packet &p) override
TURN server has connected client and server together and will now relay all packets to each other.
Definition: network_turn.cpp:61
ClientNetworkTurnSocketHandler::connection_string
std::string connection_string
The connection string of the TURN server we are connecting to.
Definition: network_turn.h:20
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition: tcp.h:99
ClientNetworkTurnSocketHandler::Turn
static std::unique_ptr< ClientNetworkTurnSocketHandler > Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, const std::string &connection_string)
Prepare a TURN connection.
Definition: network_turn.cpp:99
network_turn.h
NetworkTurnConnecter
Connect to the TURN server.
Definition: network_turn.cpp:22
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:70
ClientNetworkCoordinatorSocketHandler::ConnectFailure
void ConnectFailure(const std::string &token, uint8_t tracking_number)
Callback from a Connecter to let the Game Coordinator know the connection failed.
Definition: network_coordinator.cpp:545
NETWORK_COORDINATOR_VERSION
static const uint8_t NETWORK_COORDINATOR_VERSION
What version of game-coordinator-protocol do we use?
Definition: config.h:50
ClientNetworkCoordinatorSocketHandler::ConnectSuccess
void ConnectSuccess(const std::string &token, SOCKET sock, NetworkAddress &address)
Callback from a Connecter to let the Game Coordinator know the connection to the game server is estab...
Definition: network_coordinator.cpp:567
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:38
ClientNetworkTurnSocketHandler::connecter
std::shared_ptr< TCPConnecter > connecter
Connecter instance.
Definition: network_turn.h:27
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition: packet.cpp:425
network_coordinator.h
ClientNetworkTurnSocketHandler::Receive_TURN_ERROR
bool Receive_TURN_ERROR(Packet &p) override
TURN server was unable to connect the client or server based on the token.
Definition: network_turn.cpp:52
NETWORK_TURN_SERVER_PORT
static const uint16_t NETWORK_TURN_SERVER_PORT
The default port of the TURN server (TCP)
Definition: config.h:23
ClientNetworkTurnSocketHandler
Class for handling the client side of the TURN connection.
Definition: network_turn.h:16
NetworkTurnConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network_turn.cpp:33
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition: tcp.cpp:51
ClientNetworkTurnSocketHandler::token
std::string token
Token of this connection.
Definition: network_turn.h:18
Packet
Internal entity of a packet.
Definition: packet.h:42
NetworkTurnSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_turn.cpp:44
PACKET_TURN_SERCLI_CONNECT
@ PACKET_TURN_SERCLI_CONNECT
Client or server is connecting to the TURN server.
Definition: tcp_turn.h:23
NETWORK_DEFAULT_PORT
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:25
ClientNetworkTurnSocketHandler::Connect
void Connect()
Connect to the TURN server.
Definition: network_turn.cpp:81
NetworkTurnConnecter::NetworkTurnConnecter
NetworkTurnConnecter(ClientNetworkTurnSocketHandler *handler, const std::string &connection_string)
Initiate the connecting.
Definition: network_turn.cpp:31
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:86
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:28
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:204
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:23
_network_coordinator_client
ClientNetworkCoordinatorSocketHandler _network_coordinator_client
The connection to the Game Coordinator.
Definition: network_coordinator.cpp:30
ClientNetworkTurnSocketHandler::connect_started
bool connect_started
Whether we started the connection.
Definition: network_turn.h:28
NETWORK_HOSTNAME_LENGTH
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition: config.h:55
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:24
ClientNetworkTurnSocketHandler::tracking_number
uint8_t tracking_number
Tracking number of this connection.
Definition: network_turn.h:19
ClientNetworkTurnSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the TURN server and when that's the case handle ...
Definition: network_turn.cpp:142
ClientNetworkTurnSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
Definition: network_turn.cpp:117