OpenTTD Source 20261112-master-gbcbec5e63f
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
9
10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "tcp_turn.h"
13
14#include "../../safeguards.h"
15
23{
24 PacketTurnType type = static_cast<PacketTurnType>(p.Recv_uint8());
25
26 switch (type) {
30
31 default:
32 Debug(Facility::Net, Severity::Critical, "[tcp/turn] Received invalid packet type {}", type);
33 return false;
34 }
35}
36
42{
43 std::unique_ptr<Packet> p;
44 static const int MAX_PACKETS_TO_RECEIVE = 4;
45 int i = MAX_PACKETS_TO_RECEIVE;
46 while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
47 bool cont = this->HandlePacket(*p);
48 if (!cont) return true;
49 }
50
51 return i != MAX_PACKETS_TO_RECEIVE - 1;
52}
53
60{
61 Debug(Facility::Net, Severity::Critical, "[tcp/turn] Received illegal packet type {}", type);
62 return false;
63}
64
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:118
virtual bool ReceiveClientConnect(Packet &p)
Client (or OpenTTD server) wants to connect to the TURN server (on request by the Game Coordinator).
Definition tcp_turn.cpp:66
virtual bool ReceiveServerError(Packet &p)
TURN server was unable to connect the client or server based on the token.
Definition tcp_turn.cpp:65
virtual bool ReceiveServerConnected(Packet &p)
TURN server has connected client and OpenTTD server together and will now relay all packets to each o...
Definition tcp_turn.cpp:67
bool ReceivePackets()
Receive a packet at TCP level.
Definition tcp_turn.cpp:41
bool ReceiveInvalidPacket(PacketTurnType type)
Helper for logging receiving invalid packets.
Definition tcp_turn.cpp:59
bool HandlePacket(Packet &p)
Handle the given packet, i.e.
Definition tcp_turn.cpp:22
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Net
Net message facility.
Definition debug_type.h:33
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:316
Basic functions to receive and send TCP packets to/from the TURN server.
PacketTurnType
Enum with all types of TCP TURN packets.
Definition tcp_turn.h:21
@ ServerError
TURN server is unable to relay.
Definition tcp_turn.h:22
@ ServerConnected
TURN server indicates the socket is now being relayed.
Definition tcp_turn.h:24
@ ClientConnect
Client (or OpenTTD server) is connecting to the TURN server.
Definition tcp_turn.h:23