OpenTTD Source 20260206-master-g4d4e37dbf1
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
9
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
19class NetworkTurnConnecter : public TCPConnecter {
20private:
22
23public:
30
31 void OnFailure() override
32 {
33 Debug(net, 9, "Turn::OnFailure()");
34
35 this->handler->connecter = nullptr;
36
37 this->handler->ConnectFailure();
38 }
39
40 void OnConnect(SOCKET s) override
41 {
42 Debug(net, 9, "Turn::OnConnect()");
43
44 this->handler->connecter = nullptr;
45
46 this->handler->sock = s;
47 }
48};
49
51{
52 Debug(net, 9, "Receive_TURN_ERROR()");
53
54 this->ConnectFailure();
55
56 return false;
57}
58
60{
61 std::string hostname = p.Recv_string(NETWORK_HOSTNAME_LENGTH);
62 Debug(net, 9, "Turn::Receive_TURN_CONNECTED({})", hostname);
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;
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 Debug(net, 9, "Turn::SendTurn({})", ticket);
105 turn_handler->SendPacket(std::move(p));
106
107 return turn_handler;
108}
109
110void ClientNetworkTurnSocketHandler::ConnectFailure()
111{
112 _network_coordinator_client.ConnectFailure(this->token, this->tracking_number);
113}
114
116{
118
119 /* Also make sure any pending connecter is killed ASAP. */
120 if (this->connecter != nullptr) {
121 this->connecter->Kill();
122 this->connecter = nullptr;
123 }
124
126}
127
128ClientNetworkTurnSocketHandler::~ClientNetworkTurnSocketHandler()
129{
130 if (this->connecter != nullptr) {
131 this->connecter->Kill();
132 this->connecter = nullptr;
133 }
134}
135
141{
142 if (this->sock == INVALID_SOCKET) return;
143
144 if (this->CanSendReceive()) {
145 this->ReceivePackets();
146 }
147
148 this->SendPackets();
149}
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:39
SOCKET sock
The socket currently connected to.
Definition tcp.h:36
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition tcp.cpp:74
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition tcp.cpp:192
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:42
std::string connection_string
Current address we are connecting to (before resolving).
Definition tcp.h:101
static std::shared_ptr< TCPConnecter > Create(Args &&... args)
Create the connecter, and initiate connecting by putting it in the collection of TCP connections to m...
Definition tcp.h:147
static const uint16_t NETWORK_TURN_SERVER_PORT
The default port of the TURN server (TCP).
Definition config.h:21
static const uint8_t NETWORK_COORDINATOR_VERSION
What version of game-coordinator-protocol do we use?
Definition config.h:48
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition config.h:52
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP).
Definition config.h:23
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:21
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition core.h:22
Functions related to debugging.
#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.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
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:423
@ PACKET_TURN_SERCLI_CONNECT
Client or server is connecting to the TURN server.
Definition tcp_turn.h:21