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

OpenTTD Source 13.2.1

tcp_coordinator.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
12 #include "../../stdafx.h"
13 #include "../../date_func.h"
14 #include "../../debug.h"
15 #include "tcp_coordinator.h"
16
17 #include "../../safeguards.h"
18
25 bool NetworkCoordinatorSocketHandler::HandlePacket(Packet *p)
26 {
27 PacketCoordinatorType type = (PacketCoordinatorType)p->Recv_uint8();
28
29 switch (type) {
30 case PACKET_COORDINATOR_GC_ERROR: return this->Receive_GC_ERROR(p);
31 case PACKET_COORDINATOR_SERVER_REGISTER: return this->Receive_SERVER_REGISTER(p);
32 case PACKET_COORDINATOR_GC_REGISTER_ACK: return this->Receive_GC_REGISTER_ACK(p);
33 case PACKET_COORDINATOR_SERVER_UPDATE: return this->Receive_SERVER_UPDATE(p);
34 case PACKET_COORDINATOR_CLIENT_LISTING: return this->Receive_CLIENT_LISTING(p);
35 case PACKET_COORDINATOR_GC_LISTING: return this->Receive_GC_LISTING(p);
36 case PACKET_COORDINATOR_CLIENT_CONNECT: return this->Receive_CLIENT_CONNECT(p);
37 case PACKET_COORDINATOR_GC_CONNECTING: return this->Receive_GC_CONNECTING(p);
38 case PACKET_COORDINATOR_SERCLI_CONNECT_FAILED: return this->Receive_SERCLI_CONNECT_FAILED(p);
39 case PACKET_COORDINATOR_GC_CONNECT_FAILED: return this->Receive_GC_CONNECT_FAILED(p);
40 case PACKET_COORDINATOR_CLIENT_CONNECTED: return this->Receive_CLIENT_CONNECTED(p);
41 case PACKET_COORDINATOR_GC_DIRECT_CONNECT: return this->Receive_GC_DIRECT_CONNECT(p);
42 case PACKET_COORDINATOR_GC_STUN_REQUEST: return this->Receive_GC_STUN_REQUEST(p);
43 case PACKET_COORDINATOR_SERCLI_STUN_RESULT: return this->Receive_SERCLI_STUN_RESULT(p);
44 case PACKET_COORDINATOR_GC_STUN_CONNECT: return this->Receive_GC_STUN_CONNECT(p);
45 case PACKET_COORDINATOR_GC_NEWGRF_LOOKUP: return this->Receive_GC_NEWGRF_LOOKUP(p);
46 case PACKET_COORDINATOR_GC_TURN_CONNECT: return this->Receive_GC_TURN_CONNECT(p);
47
48 default:
49 Debug(net, 0, "[tcp/coordinator] Received invalid packet type {}", type);
50 return false;
51 }
52 }
53
58 bool NetworkCoordinatorSocketHandler::ReceivePackets()
59 {
60 /*
61 * We read only a few of the packets. This allows the GUI to update when
62 * a large set of servers is being received. Otherwise the interface
63 * "hangs" while the game is updating the server-list.
64 *
65 * What arbitrary number to choose is the ultimate question though.
66 */
67 Packet *p;
68 static const int MAX_PACKETS_TO_RECEIVE = 42;
69 int i = MAX_PACKETS_TO_RECEIVE;
70 while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
71 bool cont = this->HandlePacket(p);
72 delete p;
73 if (!cont) return true;
74 }
75
76 return i != MAX_PACKETS_TO_RECEIVE - 1;
77 }
78
84 bool NetworkCoordinatorSocketHandler::ReceiveInvalidPacket(PacketCoordinatorType type)
85 {
86 Debug(net, 0, "[tcp/coordinator] Received illegal packet type {}", type);
87 return false;
88 }
89
90 bool NetworkCoordinatorSocketHandler::Receive_GC_ERROR(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_ERROR); }
91 bool NetworkCoordinatorSocketHandler::Receive_SERVER_REGISTER(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_SERVER_REGISTER); }
92 bool NetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_REGISTER_ACK); }
93 bool NetworkCoordinatorSocketHandler::Receive_SERVER_UPDATE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_SERVER_UPDATE); }
94 bool NetworkCoordinatorSocketHandler::Receive_CLIENT_LISTING(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_CLIENT_LISTING); }
95 bool NetworkCoordinatorSocketHandler::Receive_GC_LISTING(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_LISTING); }
96 bool NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_CLIENT_CONNECT); }
97 bool NetworkCoordinatorSocketHandler::Receive_GC_CONNECTING(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_CONNECTING); }
98 bool NetworkCoordinatorSocketHandler::Receive_SERCLI_CONNECT_FAILED(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_SERCLI_CONNECT_FAILED); }
99 bool NetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_CONNECT_FAILED); }
100 bool NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECTED(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_CLIENT_CONNECTED); }
101 bool NetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_DIRECT_CONNECT); }
102 bool NetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_STUN_REQUEST); }
103 bool NetworkCoordinatorSocketHandler::Receive_SERCLI_STUN_RESULT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_SERCLI_STUN_RESULT); }
104 bool NetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_STUN_CONNECT); }
105 bool NetworkCoordinatorSocketHandler::Receive_GC_NEWGRF_LOOKUP(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_NEWGRF_LOOKUP); }
106 bool NetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_COORDINATOR_GC_TURN_CONNECT); }
NetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK
virtual bool Receive_GC_REGISTER_ACK(Packet *p)
Game Coordinator acknowledges the registration.
Definition: tcp_coordinator.cpp:92
NetworkCoordinatorSocketHandler::Receive_GC_LISTING
virtual bool Receive_GC_LISTING(Packet *p)
Game Coordinator replies with a list of all public servers.
Definition: tcp_coordinator.cpp:95
NetworkCoordinatorSocketHandler::Receive_GC_NEWGRF_LOOKUP
virtual bool Receive_GC_NEWGRF_LOOKUP(Packet *p)
Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF des...
Definition: tcp_coordinator.cpp:105
NetworkCoordinatorSocketHandler::Receive_SERVER_REGISTER
virtual bool Receive_SERVER_REGISTER(Packet *p)
Server is starting a multiplayer game and wants to let the Game Coordinator know.
Definition: tcp_coordinator.cpp:91
NetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT
virtual bool Receive_GC_STUN_CONNECT(Packet *p)
Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side).
Definition: tcp_coordinator.cpp:104
PACKET_COORDINATOR_CLIENT_CONNECTED
@ PACKET_COORDINATOR_CLIENT_CONNECTED
Client informs the Game Coordinator the connection with the server is established.
Definition: tcp_coordinator.h:39
NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECTED
virtual bool Receive_CLIENT_CONNECTED(Packet *p)
Client informs the Game Coordinator the connection with the Server is established.
Definition: tcp_coordinator.cpp:100
NetworkCoordinatorSocketHandler::Receive_SERCLI_STUN_RESULT
virtual bool Receive_SERCLI_STUN_RESULT(Packet *p)
Client/server informs the Game Coordinator the result of a STUN request.
Definition: tcp_coordinator.cpp:103
PACKET_COORDINATOR_GC_CONNECT_FAILED
@ PACKET_COORDINATOR_GC_CONNECT_FAILED
Game Coordinator informs client/server it has given up on the connection attempt.
Definition: tcp_coordinator.h:38
NetworkCoordinatorSocketHandler::Receive_SERCLI_CONNECT_FAILED
virtual bool Receive_SERCLI_CONNECT_FAILED(Packet *p)
Client or Server failed to connect to the remote side.
Definition: tcp_coordinator.cpp:98
PACKET_COORDINATOR_CLIENT_LISTING
@ PACKET_COORDINATOR_CLIENT_LISTING
Client is requesting a listing of all public servers.
Definition: tcp_coordinator.h:33
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:144
NetworkCoordinatorSocketHandler::Receive_CLIENT_LISTING
virtual bool Receive_CLIENT_LISTING(Packet *p)
Client requests a list of all public servers.
Definition: tcp_coordinator.cpp:94
NetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT
virtual bool Receive_GC_TURN_CONNECT(Packet *p)
Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server.
Definition: tcp_coordinator.cpp:106
NetworkCoordinatorSocketHandler::HandlePacket
bool HandlePacket(Packet *p)
Handle the given packet, i.e.
Definition: tcp_coordinator.cpp:25
NetworkCoordinatorSocketHandler::Receive_SERVER_UPDATE
virtual bool Receive_SERVER_UPDATE(Packet *p)
Send an update of the current state of the server to the Game Coordinator.
Definition: tcp_coordinator.cpp:93
PACKET_COORDINATOR_SERCLI_STUN_RESULT
@ PACKET_COORDINATOR_SERCLI_STUN_RESULT
Client/server informs the Game Coordinator of the result of the STUN request.
Definition: tcp_coordinator.h:42
NetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT
virtual bool Receive_GC_DIRECT_CONNECT(Packet *p)
Game Coordinator requests that the Client makes a direct connection to the indicated peer,...
Definition: tcp_coordinator.cpp:101
PACKET_COORDINATOR_GC_STUN_CONNECT
@ PACKET_COORDINATOR_GC_STUN_CONNECT
Game Coordinator tells client/server to connect() reusing the STUN local address.
Definition: tcp_coordinator.h:43
PACKET_COORDINATOR_CLIENT_CONNECT
@ PACKET_COORDINATOR_CLIENT_CONNECT
Client wants to connect to a server based on an invite code.
Definition: tcp_coordinator.h:35
NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECT
virtual bool Receive_CLIENT_CONNECT(Packet *p)
Client wants to connect to a Server.
Definition: tcp_coordinator.cpp:96
PACKET_COORDINATOR_SERVER_REGISTER
@ PACKET_COORDINATOR_SERVER_REGISTER
Server registration.
Definition: tcp_coordinator.h:30
Packet
Internal entity of a packet.
Definition: packet.h:44
PACKET_COORDINATOR_GC_REGISTER_ACK
@ PACKET_COORDINATOR_GC_REGISTER_ACK
Game Coordinator accepts the registration.
Definition: tcp_coordinator.h:31
PACKET_COORDINATOR_GC_NEWGRF_LOOKUP
@ PACKET_COORDINATOR_GC_NEWGRF_LOOKUP
Game Coordinator informs client about NewGRF lookup table updates needed for GC_LISTING.
Definition: tcp_coordinator.h:44
PACKET_COORDINATOR_GC_CONNECTING
@ PACKET_COORDINATOR_GC_CONNECTING
Game Coordinator informs the client of the token assigned to the connection attempt.
Definition: tcp_coordinator.h:36
PACKET_COORDINATOR_GC_ERROR
@ PACKET_COORDINATOR_GC_ERROR
Game Coordinator indicates there was an error.
Definition: tcp_coordinator.h:29
NetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST
virtual bool Receive_GC_STUN_REQUEST(Packet *p)
Game Coordinator requests the client/server to do a STUN request to the STUN server.
Definition: tcp_coordinator.cpp:102
NetworkCoordinatorSocketHandler::ReceiveInvalidPacket
bool ReceiveInvalidPacket(PacketCoordinatorType type)
Helper for logging receiving invalid packets.
Definition: tcp_coordinator.cpp:84
NetworkCoordinatorSocketHandler::Receive_GC_ERROR
virtual bool Receive_GC_ERROR(Packet *p)
Game Coordinator indicates there was an error.
Definition: tcp_coordinator.cpp:90
tcp_coordinator.h
PACKET_COORDINATOR_GC_LISTING
@ PACKET_COORDINATOR_GC_LISTING
Game Coordinator returns a listing of all public servers.
Definition: tcp_coordinator.h:34
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:317
NetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED
virtual bool Receive_GC_CONNECT_FAILED(Packet *p)
Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server.
Definition: tcp_coordinator.cpp:99
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
PACKET_COORDINATOR_GC_STUN_REQUEST
@ PACKET_COORDINATOR_GC_STUN_REQUEST
Game Coordinator tells client/server to initiate a STUN request.
Definition: tcp_coordinator.h:41
NetworkCoordinatorSocketHandler::Receive_GC_CONNECTING
virtual bool Receive_GC_CONNECTING(Packet *p)
Game Coordinator informs the Client under what token it will start the attempt to connect the Server ...
Definition: tcp_coordinator.cpp:97
NetworkCoordinatorSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_coordinator.cpp:58
PACKET_COORDINATOR_GC_DIRECT_CONNECT
@ PACKET_COORDINATOR_GC_DIRECT_CONNECT
Game Coordinator tells client to directly connect to the hostname:port of the server.
Definition: tcp_coordinator.h:40
PACKET_COORDINATOR_SERCLI_CONNECT_FAILED
@ PACKET_COORDINATOR_SERCLI_CONNECT_FAILED
Client/server tells the Game Coordinator the current connection attempt failed.
Definition: tcp_coordinator.h:37
PACKET_COORDINATOR_SERVER_UPDATE
@ PACKET_COORDINATOR_SERVER_UPDATE
Server sends an set intervals an update of the server.
Definition: tcp_coordinator.h:32
PACKET_COORDINATOR_GC_TURN_CONNECT
@ PACKET_COORDINATOR_GC_TURN_CONNECT
Game Coordinator tells client/server to connect to a specific TURN server.
Definition: tcp_coordinator.h:45
PacketCoordinatorType
PacketCoordinatorType
Enum with all types of TCP Game Coordinator packets.
Definition: tcp_coordinator.h:28