OpenTTD Source 20250524-master-gc366e6a48e
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
12#include "../debug.h"
13#include "network_coordinator.h"
14#include "network_turn.h"
15
16#include "../safeguards.h"
17
20private:
22
23public:
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
50{
51 Debug(net, 9, "Receive_TURN_ERROR()");
52
53 this->ConnectFailure();
54
55 return false;
56}
57
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
70 _network_coordinator_client.ConnectSuccess(this->token, game_sock, address);
71
72 return false;
73}
74
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{
112}
113
115{
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
125}
126
127ClientNetworkTurnSocketHandler::~ClientNetworkTurnSocketHandler()
128{
129 if (this->connecter != nullptr) {
130 this->connecter->Kill();
131 this->connecter = nullptr;
132 }
133}
134
140{
141 if (this->sock == INVALID_SOCKET) return;
142
143 if (this->CanSendReceive()) {
144 this->ReceivePackets();
145 }
146
147 this->SendPackets();
148}
void ConnectFailure(std::string_view token, uint8_t tracking_number)
Callback from a Connecter to let the Game Coordinator know the connection failed.
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...
Class for handling the client side of the TURN connection.
bool Receive_TURN_ERROR(Packet &p) override
TURN server was unable to connect the client or server based on the token.
bool connect_started
Whether we started the connection.
uint8_t tracking_number
Tracking number of this connection.
std::string token
Token of this connection.
void Connect()
Connect to the TURN server.
bool Receive_TURN_CONNECTED(Packet &p) override
TURN server has connected client and server together and will now relay all packets to each other.
void SendReceive()
Check whether we received/can send some data from/to the TURN server and when that's the case handle ...
std::string connection_string
The connection string of the TURN server we are connecting to.
std::shared_ptr< TCPConnecter > connecter
Connecter instance.
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
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.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition tcp.cpp:41
SOCKET sock
The socket currently connected to.
Definition tcp.h:38
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition tcp.cpp:76
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition tcp.cpp:194
Connect to the TURN server.
NetworkTurnConnecter(ClientNetworkTurnSocketHandler *handler, std::string_view connection_string)
Initiate the connecting.
void OnFailure() override
Callback for when the connection attempt failed.
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
bool ReceivePackets()
Receive a packet at TCP level.
Definition tcp_turn.cpp:44
"Helper" class for creating TCP connections in a non-blocking manner
Definition tcp.h:74
std::string connection_string
Current address we are connecting to (before resolving).
Definition tcp.h:103
static const uint16_t NETWORK_TURN_SERVER_PORT
The default port of the TURN server (TCP)
Definition config.h:23
static const uint8_t NETWORK_COORDINATOR_VERSION
What version of game-coordinator-protocol do we use?
Definition config.h:50
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition config.h:54
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition config.h:25
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition core.h:24
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
ClientNetworkCoordinatorSocketHandler _network_coordinator_client
The connection to the Game Coordinator.
Part of the network protocol handling Game Coordinator requests.
Part of the network protocol handling TURN requests.
Internal entity of a packet.
Definition packet.h:43
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
Client or server is connecting to the TURN server.
Definition tcp_turn.h:23