Files
@ r28800:381a234fb097
Branch filter:
Location: cpp/openttd-patchpack/source/src/network/network_turn.cpp
r28800:381a234fb097
4.4 KiB
text/x-c
Update: Translations from eints
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file network_turn.cpp TURN sending/receiving part of the network protocol. */
#include "../stdafx.h"
#include "../debug.h"
#include "../error.h"
#include "../strings_func.h"
#include "network_coordinator.h"
#include "network_turn.h"
#include "table/strings.h"
#include "../safeguards.h"
/** Connect to the TURN server. */
class NetworkTurnConnecter : public TCPConnecter {
private:
ClientNetworkTurnSocketHandler *handler;
public:
/**
* Initiate the connecting.
* @param connection_string The address of the TURN server.
*/
NetworkTurnConnecter(ClientNetworkTurnSocketHandler *handler, const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_TURN_SERVER_PORT), handler(handler) {}
void OnFailure() override
{
Debug(net, 9, "Turn::OnFailure()");
this->handler->connecter = nullptr;
this->handler->ConnectFailure();
}
void OnConnect(SOCKET s) override
{
Debug(net, 9, "Turn::OnConnect()");
this->handler->connecter = nullptr;
this->handler->sock = s;
}
};
bool ClientNetworkTurnSocketHandler::Receive_TURN_ERROR(Packet &)
{
Debug(net, 9, "Receive_TURN_ERROR()");
this->ConnectFailure();
return false;
}
bool ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED(Packet &p)
{
Debug(net, 9, "Receive_TURN_CONNECTED()");
std::string hostname = p.Recv_string(NETWORK_HOSTNAME_LENGTH);
/* Act like we no longer have a socket, as we are handing it over to the
* game handler. */
SOCKET game_sock = this->sock;
this->sock = INVALID_SOCKET;
NetworkAddress address = NetworkAddress(hostname, NETWORK_DEFAULT_PORT);
_network_coordinator_client.ConnectSuccess(this->token, game_sock, address);
return false;
}
/**
* Connect to the TURN server.
*/
void ClientNetworkTurnSocketHandler::Connect()
{
Debug(net, 9, "Turn::Connect()");
this->connect_started = true;
this->connecter = TCPConnecter::Create<NetworkTurnConnecter>(this, this->connection_string);
}
/**
* Prepare a TURN connection.
* Not until you run Connect() on the resulting instance will it start setting
* up the TURN connection.
* @param token The token as received from the Game Coordinator.
* @param tracking_number The tracking number as recieved from the Game Coordinator.
* @param ticket The ticket as received from the Game Coordinator.
* @param connection_string Connection string of the TURN server.
* @return The handler for this TURN connection.
*/
/* static */ std::unique_ptr<ClientNetworkTurnSocketHandler> ClientNetworkTurnSocketHandler::Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, const std::string &connection_string)
{
auto turn_handler = std::make_unique<ClientNetworkTurnSocketHandler>(token, tracking_number, connection_string);
auto p = std::make_unique<Packet>(PACKET_TURN_SERCLI_CONNECT);
p->Send_uint8(NETWORK_COORDINATOR_VERSION);
p->Send_string(ticket);
turn_handler->SendPacket(std::move(p));
return turn_handler;
}
void ClientNetworkTurnSocketHandler::ConnectFailure()
{
_network_coordinator_client.ConnectFailure(this->token, this->tracking_number);
}
NetworkRecvStatus ClientNetworkTurnSocketHandler::CloseConnection(bool error)
{
NetworkTurnSocketHandler::CloseConnection(error);
/* Also make sure any pending connecter is killed ASAP. */
if (this->connecter != nullptr) {
this->connecter->Kill();
this->connecter = nullptr;
}
return NETWORK_RECV_STATUS_OKAY;
}
ClientNetworkTurnSocketHandler::~ClientNetworkTurnSocketHandler()
{
if (this->connecter != nullptr) {
this->connecter->Kill();
this->connecter = nullptr;
}
}
/**
* Check whether we received/can send some data from/to the TURN server and
* when that's the case handle it appropriately
*/
void ClientNetworkTurnSocketHandler::SendReceive()
{
if (this->sock == INVALID_SOCKET) return;
if (this->CanSendReceive()) {
this->ReceivePackets();
}
this->SendPackets();
}
|