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

OpenTTD Source 15.0-RC2

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