OpenTTD Source: src/network/core/tcp_turn.cpp Source File

OpenTTD Source 15.0-RC2

tcp_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
12#include "../../stdafx.h"
13#include "../../timer/timer_game_calendar.h"
14#include "../../debug.h"
15#include "tcp_turn.h"
16
17#include "../../safeguards.h"
18
25 bool NetworkTurnSocketHandler::HandlePacket(Packet &p)
26{
27 PacketTurnType type = (PacketTurnType)p.Recv_uint8();
28
29 switch (type) {
30 case PACKET_TURN_TURN_ERROR: return this->Receive_TURN_ERROR(p);
31 case PACKET_TURN_SERCLI_CONNECT: return this->Receive_SERCLI_CONNECT(p);
32 case PACKET_TURN_TURN_CONNECTED: return this->Receive_TURN_CONNECTED(p);
33
34 default:
35 Debug(net, 0, "[tcp/turn] Received invalid packet type {}", type);
36 return false;
37 }
38}
39
44 bool NetworkTurnSocketHandler::ReceivePackets()
45{
46 std::unique_ptr<Packet> p;
47 static const int MAX_PACKETS_TO_RECEIVE = 4;
48 int i = MAX_PACKETS_TO_RECEIVE;
49 while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
50 bool cont = this->HandlePacket(*p);
51 if (!cont) return true;
52 }
53
54 return i != MAX_PACKETS_TO_RECEIVE - 1;
55}
56
62 bool NetworkTurnSocketHandler::ReceiveInvalidPacket(PacketTurnType type)
63{
64 Debug(net, 0, "[tcp/turn] Received illegal packet type {}", type);
65 return false;
66}
67
68 bool NetworkTurnSocketHandler::Receive_TURN_ERROR(Packet &) { return this->ReceiveInvalidPacket(PACKET_TURN_TURN_ERROR); }
69 bool NetworkTurnSocketHandler::Receive_SERCLI_CONNECT(Packet &) { return this->ReceiveInvalidPacket(PACKET_TURN_SERCLI_CONNECT); }
70 bool NetworkTurnSocketHandler::Receive_TURN_CONNECTED(Packet &) { return this->ReceiveInvalidPacket(PACKET_TURN_TURN_CONNECTED); }
NetworkTCPSocketHandler::ReceivePacket
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:119
NetworkTurnSocketHandler::Receive_SERCLI_CONNECT
virtual bool Receive_SERCLI_CONNECT(Packet &p)
Client or servers wants to connect to the TURN server (on request by the Game Coordinator).
Definition tcp_turn.cpp:69
NetworkTurnSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition tcp_turn.cpp:44
NetworkTurnSocketHandler::Receive_TURN_CONNECTED
virtual bool Receive_TURN_CONNECTED(Packet &p)
TURN server has connected client and server together and will now relay all packets to each other.
Definition tcp_turn.cpp:70
NetworkTurnSocketHandler::Receive_TURN_ERROR
virtual bool Receive_TURN_ERROR(Packet &p)
TURN server was unable to connect the client or server based on the token.
Definition tcp_turn.cpp:68
NetworkTurnSocketHandler::ReceiveInvalidPacket
bool ReceiveInvalidPacket(PacketTurnType type)
Helper for logging receiving invalid packets.
Definition tcp_turn.cpp:62
NetworkTurnSocketHandler::HandlePacket
bool HandlePacket(Packet &p)
Handle the given packet, i.e.
Definition tcp_turn.cpp:25
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
Packet
Internal entity of a packet.
Definition packet.h:43
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:318
tcp_turn.h
Basic functions to receive and send TCP packets to/from the TURN server.
PacketTurnType
PacketTurnType
Enum with all types of TCP TURN packets.
Definition tcp_turn.h:21
PACKET_TURN_TURN_CONNECTED
@ PACKET_TURN_TURN_CONNECTED
TURN server indicates the socket is now being relayed.
Definition tcp_turn.h:24
PACKET_TURN_SERCLI_CONNECT
@ PACKET_TURN_SERCLI_CONNECT
Client or server is connecting to the TURN server.
Definition tcp_turn.h:23
PACKET_TURN_TURN_ERROR
@ PACKET_TURN_TURN_ERROR
TURN server is unable to relay.
Definition tcp_turn.h:22