OpenTTD Source: src/network/network_coordinator.h Source File

OpenTTD Source 13.1

network_coordinator.h
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
10 #ifndef NETWORK_COORDINATOR_H
11 #define NETWORK_COORDINATOR_H
12
13 #include "core/tcp_coordinator.h"
14 #include "network_stun.h"
15 #include "network_turn.h"
16 #include <map>
17
54 class ClientNetworkCoordinatorSocketHandler : public NetworkCoordinatorSocketHandler {
55 private:
56 std::chrono::steady_clock::time_point next_update;
57 std::map<std::string, std::pair<std::string, TCPServerConnecter *>> connecter;
58 std::map<std::string, TCPServerConnecter *> connecter_pre;
59 std::map<std::string, std::map<int, std::unique_ptr<ClientNetworkStunSocketHandler>>> stun_handlers;
60 std::map<std::string, std::unique_ptr<ClientNetworkTurnSocketHandler>> turn_handlers;
61 TCPConnecter *game_connecter = nullptr;
62
63 uint32 newgrf_lookup_table_cursor = 0;
64 GameInfoNewGRFLookupTable newgrf_lookup_table;
65
66 protected:
67 bool Receive_GC_ERROR(Packet *p) override;
68 bool Receive_GC_REGISTER_ACK(Packet *p) override;
69 bool Receive_GC_LISTING(Packet *p) override;
70 bool Receive_GC_CONNECTING(Packet *p) override;
71 bool Receive_GC_CONNECT_FAILED(Packet *p) override;
72 bool Receive_GC_DIRECT_CONNECT(Packet *p) override;
73 bool Receive_GC_STUN_REQUEST(Packet *p) override;
74 bool Receive_GC_STUN_CONNECT(Packet *p) override;
75 bool Receive_GC_NEWGRF_LOOKUP(Packet *p) override;
76 bool Receive_GC_TURN_CONNECT(Packet *p) override;
77
78 public:
80 static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
81
82 std::chrono::steady_clock::time_point last_activity;
83 bool connecting;
84
85 ClientNetworkCoordinatorSocketHandler() : connecting(false) {}
86
87 NetworkRecvStatus CloseConnection(bool error = true) override;
88 void SendReceive();
89
90 void ConnectFailure(const std::string &token, uint8 tracking_number);
91 void ConnectSuccess(const std::string &token, SOCKET sock, NetworkAddress &address);
92 void StunResult(const std::string &token, uint8 family, bool result);
93
94 void Connect();
95 void CloseToken(const std::string &token);
96 void CloseAllConnections();
97 void CloseStunHandler(const std::string &token, uint8 family = AF_UNSPEC);
98 void CloseTurnHandler(const std::string &token);
99
100 void Register();
101 void SendServerUpdate();
102 void GetListing();
103
104 void ConnectToServer(const std::string &invite_code, TCPServerConnecter *connecter);
105 void StartTurnConnection(std::string &token);
106 };
107
108 extern ClientNetworkCoordinatorSocketHandler _network_coordinator_client;
109
110 #endif /* NETWORK_COORDINATOR_H */
ClientNetworkCoordinatorSocketHandler::ConnectFailure
void ConnectFailure(const std::string &token, uint8 tracking_number)
Callback from a Connecter to let the Game Coordinator know the connection failed.
Definition: network_coordinator.cpp:545
network_turn.h
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:71
ClientNetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED
bool Receive_GC_CONNECT_FAILED(Packet *p) override
Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server.
Definition: network_coordinator.cpp:288
ClientNetworkCoordinatorSocketHandler::ConnectSuccess
void ConnectSuccess(const std::string &token, SOCKET sock, NetworkAddress &address)
Callback from a Connecter to let the Game Coordinator know the connection to the game server is estab...
Definition: network_coordinator.cpp:567
network_stun.h
ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT
bool Receive_GC_STUN_CONNECT(Packet *p) override
Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side).
Definition: network_coordinator.cpp:322
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
ClientNetworkCoordinatorSocketHandler::Receive_GC_ERROR
bool Receive_GC_ERROR(Packet *p) override
Game Coordinator indicates there was an error.
Definition: network_coordinator.cpp:127
ClientNetworkCoordinatorSocketHandler::CloseAllConnections
void CloseAllConnections()
Close all pending connection tokens.
Definition: network_coordinator.cpp:685
ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST
bool Receive_GC_STUN_REQUEST(Packet *p) override
Game Coordinator requests the client/server to do a STUN request to the STUN server.
Definition: network_coordinator.cpp:313
ClientNetworkCoordinatorSocketHandler::ConnectToServer
void ConnectToServer(const std::string &invite_code, TCPServerConnecter *connecter)
Join a server based on an invite code.
Definition: network_coordinator.cpp:515
ClientNetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT
bool Receive_GC_TURN_CONNECT(Packet *p) override
Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server.
Definition: network_coordinator.cpp:368
ClientNetworkCoordinatorSocketHandler::stun_handlers
std::map< std::string, std::map< int, std::unique_ptr< ClientNetworkStunSocketHandler > > > stun_handlers
All pending STUN handlers, stored by token:family.
Definition: network_coordinator.h:59
ClientNetworkCoordinatorSocketHandler::game_connecter
TCPConnecter * game_connecter
Pending connecter to the game server.
Definition: network_coordinator.h:61
ClientNetworkCoordinatorSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
Definition: network_coordinator.cpp:432
ClientNetworkCoordinatorSocketHandler::StunResult
void StunResult(const std::string &token, uint8 family, bool result)
Callback from the STUN connecter to inform the Game Coordinator about the result of the STUN.
Definition: network_coordinator.cpp:607
ClientNetworkCoordinatorSocketHandler::GetListing
void GetListing()
Request a listing of all public servers.
Definition: network_coordinator.cpp:495
ClientNetworkCoordinatorSocketHandler::Register
void Register()
Register our server to receive our invite code.
Definition: network_coordinator.cpp:452
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkCoordinatorSocketHandler::newgrf_lookup_table_cursor
uint32 newgrf_lookup_table_cursor
Last received cursor for the GameInfoNewGRFLookupTable updates.
Definition: network_coordinator.h:63
ClientNetworkCoordinatorSocketHandler::newgrf_lookup_table
GameInfoNewGRFLookupTable newgrf_lookup_table
Table to look up NewGRFs in the GC_LISTING packets.
Definition: network_coordinator.h:64
ClientNetworkCoordinatorSocketHandler::Receive_GC_LISTING
bool Receive_GC_LISTING(Packet *p) override
Game Coordinator replies with a list of all public servers.
Definition: network_coordinator.cpp:233
ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT
bool Receive_GC_DIRECT_CONNECT(Packet *p) override
Game Coordinator requests that the Client makes a direct connection to the indicated peer,...
Definition: network_coordinator.cpp:296
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
ClientNetworkCoordinatorSocketHandler::connecter_pre
std::map< std::string, TCPServerConnecter * > connecter_pre
Based on invite codes, the current connecters that are pending.
Definition: network_coordinator.h:58
ClientNetworkCoordinatorSocketHandler::Receive_GC_NEWGRF_LOOKUP
bool Receive_GC_NEWGRF_LOOKUP(Packet *p) override
Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF des...
Definition: network_coordinator.cpp:356
ClientNetworkCoordinatorSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the Game Coordinator server and when that's the ...
Definition: network_coordinator.cpp:717
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
ClientNetworkCoordinatorSocketHandler::last_activity
std::chrono::steady_clock::time_point last_activity
The last time there was network activity.
Definition: network_coordinator.h:82
ClientNetworkCoordinatorSocketHandler
Game Coordinator communication.
Definition: network_coordinator.h:54
tcp_coordinator.h
_network_coordinator_client
ClientNetworkCoordinatorSocketHandler _network_coordinator_client
The connection to the Game Coordinator.
Definition: network_coordinator.cpp:30
ClientNetworkCoordinatorSocketHandler::SendServerUpdate
void SendServerUpdate()
Send an update of our server status to the Game Coordinator.
Definition: network_coordinator.cpp:479
ClientNetworkCoordinatorSocketHandler::next_update
std::chrono::steady_clock::time_point next_update
When to send the next update (if server and public).
Definition: network_coordinator.h:56
TCPServerConnecter
Definition: tcp.h:138
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
GameInfoNewGRFLookupTable
std::unordered_map< uint32, NamedGRFIdentifier > GameInfoNewGRFLookupTable
Lookup table for the GameInfo in case of NST_LOOKUP_ID.
Definition: game_info.h:131
ClientNetworkCoordinatorSocketHandler::turn_handlers
std::map< std::string, std::unique_ptr< ClientNetworkTurnSocketHandler > > turn_handlers
Pending TURN handler (if any), stored by token.
Definition: network_coordinator.h:60
ClientNetworkCoordinatorSocketHandler::CloseToken
void CloseToken(const std::string &token)
Close everything related to this connection token.
Definition: network_coordinator.cpp:668
ClientNetworkCoordinatorSocketHandler::CloseTurnHandler
void CloseTurnHandler(const std::string &token)
Close the TURN handler.
Definition: network_coordinator.cpp:649
ClientNetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK
bool Receive_GC_REGISTER_ACK(Packet *p) override
Game Coordinator acknowledges the registration.
Definition: network_coordinator.cpp:177
NetworkCoordinatorSocketHandler
Base socket handler for all Game Coordinator TCP sockets.
Definition: tcp_coordinator.h:71
ClientNetworkCoordinatorSocketHandler::connecter
std::map< std::string, std::pair< std::string, TCPServerConnecter * > > connecter
Based on tokens, the current (invite-code, connecter) that are pending.
Definition: network_coordinator.h:57
ClientNetworkCoordinatorSocketHandler::connecting
bool connecting
Are we connecting to the Game Coordinator?
Definition: network_coordinator.h:83
ClientNetworkCoordinatorSocketHandler::IDLE_TIMEOUT
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
Definition: network_coordinator.h:80
ClientNetworkCoordinatorSocketHandler::CloseStunHandler
void CloseStunHandler(const std::string &token, uint8 family=AF_UNSPEC)
Close the STUN handler.
Definition: network_coordinator.cpp:622
ClientNetworkCoordinatorSocketHandler::Receive_GC_CONNECTING
bool Receive_GC_CONNECTING(Packet *p) override
Game Coordinator informs the Client under what token it will start the attempt to connect the Server ...
Definition: network_coordinator.cpp:269