OpenTTD Source  12.0-beta2
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  this->handler->connecter = nullptr;
36 
37  this->handler->ConnectFailure();
38  }
39 
40  void OnConnect(SOCKET s) override
41  {
42  this->handler->connecter = nullptr;
43 
44  handler->sock = s;
45  }
46 };
47 
49 {
50  this->ConnectFailure();
51 
52  return false;
53 }
54 
56 {
57  std::string hostname = p->Recv_string(NETWORK_HOSTNAME_LENGTH);
58 
59  /* Act like we no longer have a socket, as we are handing it over to the
60  * game handler. */
61  SOCKET game_sock = this->sock;
62  this->sock = INVALID_SOCKET;
63 
65  _network_coordinator_client.ConnectSuccess(this->token, game_sock, address);
66 
67  return false;
68 }
69 
74 {
75  this->connect_started = true;
76  this->connecter = new NetworkTurnConnecter(this, this->connection_string);
77 }
78 
89 /* static */ std::unique_ptr<ClientNetworkTurnSocketHandler> ClientNetworkTurnSocketHandler::Turn(const std::string &token, uint8 tracking_number, const std::string &ticket, const std::string &connection_string)
90 {
91  auto turn_handler = std::make_unique<ClientNetworkTurnSocketHandler>(token, tracking_number, connection_string);
92 
95  p->Send_string(ticket);
96 
97  turn_handler->SendPacket(p);
98 
99  return turn_handler;
100 }
101 
102 void ClientNetworkTurnSocketHandler::ConnectFailure()
103 {
105 }
106 
108 {
110 
111  /* If our connecter is still pending, shut it down too. Otherwise the
112  * callback of the connecter can call into us, and our object is most
113  * likely about to be destroyed. */
114  if (this->connecter != nullptr) {
115  this->connecter->Kill();
116  this->connecter = nullptr;
117  }
118 
120 }
121 
127 {
128  if (this->sock == INVALID_SOCKET) return;
129 
130  if (this->CanSendReceive()) {
131  this->ReceivePackets();
132  }
133 
134  this->SendPackets();
135 }
ClientNetworkTurnSocketHandler::connection_string
std::string connection_string
The connection string of the TURN server we are connecting to.
Definition: network_turn.h:20
NETWORK_DEFAULT_PORT
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:28
ClientNetworkCoordinatorSocketHandler::ConnectFailure
void ConnectFailure(const std::string &token, uint8 tracking_number)
Callback from a Connecter to let the Game Coordinator know the connection failed.
Definition: network_coordinator.cpp:546
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition: tcp.h:100
network_turn.h
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:55
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:71
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:568
TCPConnecter::Kill
void Kill()
Kill this connecter.
Definition: tcp_connect.cpp:85
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
NetworkTurnConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network_turn.cpp:40
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:380
network_coordinator.h
ClientNetworkTurnSocketHandler::connecter
TCPConnecter * connecter
Connecter instance.
Definition: network_turn.h:27
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:65
ClientNetworkTurnSocketHandler::token
std::string token
Token of this connection.
Definition: network_turn.h:18
Packet
Internal entity of a packet.
Definition: packet.h:44
NetworkTurnSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_turn.cpp:44
ClientNetworkTurnSocketHandler::Connect
void Connect()
Connect to the TURN server.
Definition: network_turn.cpp:73
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:99
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
ClientNetworkTurnSocketHandler::tracking_number
uint8 tracking_number
Tracking number of this connection.
Definition: network_turn.h:19
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
_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
PACKET_TURN_SERCLI_CONNECT
@ PACKET_TURN_SERCLI_CONNECT
Client or server is connecting to the TURN server.
Definition: tcp_turn.h:23
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:48
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:181
NETWORK_HOSTNAME_LENGTH
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition: config.h:57
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
NETWORK_COORDINATOR_VERSION
static const byte NETWORK_COORDINATOR_VERSION
What version of game-coordinator-protocol do we use?
Definition: config.h:53
ClientNetworkTurnSocketHandler::Turn
static std::unique_ptr< ClientNetworkTurnSocketHandler > Turn(const std::string &token, uint8 tracking_number, const std::string &ticket, const std::string &connection_string)
Prepare a TURN connection.
Definition: network_turn.cpp:89
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:126
ClientNetworkTurnSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
Definition: network_turn.cpp:107
NETWORK_TURN_SERVER_PORT
static const uint16 NETWORK_TURN_SERVER_PORT
The default port of the TURN server (TCP)
Definition: config.h:25