OpenTTD Source 20260421-master-gc2fbc6fdeb
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NETWORK_COORDINATOR_H
11#define NETWORK_COORDINATOR_H
12
14#include "network_stun.h"
15#include "network_turn.h"
16
51
53class ClientNetworkCoordinatorSocketHandler : public NetworkCoordinatorSocketHandler {
54private:
55 std::chrono::steady_clock::time_point next_update;
56 std::map<std::string, std::pair<std::string, TCPServerConnecter *>, std::less<>> connecter;
57 std::map<std::string, TCPServerConnecter *, std::less<>> connecter_pre;
58 std::map<std::string, std::map<int, std::unique_ptr<ClientNetworkStunSocketHandler>>, std::less<>> stun_handlers;
59 std::map<std::string, std::unique_ptr<ClientNetworkTurnSocketHandler>, std::less<>> turn_handlers;
60 std::shared_ptr<TCPConnecter> game_connecter{};
61
64
65protected:
66 bool ReceiveGameCoordinatorError(Packet &p) override;
67 bool ReceiveGameCoordinatorRegisterAck(Packet &p) override;
68 bool ReceiveGameCoordinatorListing(Packet &p) override;
69 bool ReceiveGameCoordinatorConnecting(Packet &p) override;
70 bool ReceiveGameCoordinatorConnectFailed(Packet &p) override;
71 bool ReceiveGameCoordinatorDirectConnect(Packet &p) override;
72 bool ReceiveGameCoordinatorStunRequest(Packet &p) override;
73 bool ReceiveGameCoordinatorStunConnect(Packet &p) override;
74 bool ReceiveGameCoordinatorNewGRFLookup(Packet &p) override;
75 bool ReceiveGameCoordinatorTurnConnect(Packet &p) override;
76
77public:
79 static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
80
81 std::chrono::steady_clock::time_point last_activity;
83
84 ClientNetworkCoordinatorSocketHandler() : connecting(false) {}
85
86 NetworkRecvStatus CloseConnection(bool error = true) override;
87 void SendReceive();
88
89 void ConnectFailure(std::string_view token, uint8_t tracking_number);
90 void ConnectSuccess(std::string_view token, SOCKET sock, NetworkAddress &address);
91 void StunResult(std::string_view token, uint8_t family, bool result);
92
93 void Connect();
94 void CloseToken(std::string_view token);
96 void CloseStunHandler(std::string_view token, uint8_t family = AF_UNSPEC);
97 void CloseTurnHandler(std::string_view token);
98
99 void Register();
100 void SendServerUpdate();
101 void GetListing();
102
103 void ConnectToServer(std::string_view invite_code, TCPServerConnecter *connecter);
104 void StartTurnConnection(std::string_view token);
105};
106
108
109#endif /* NETWORK_COORDINATOR_H */
Game Coordinator communication.
GameInfoNewGRFLookupTable newgrf_lookup_table
Table to look up NewGRFs in the GC_LISTING packets.
bool ReceiveGameCoordinatorDirectConnect(Packet &p) override
Game Coordinator requests that the Client makes a direct connection to the indicated peer,...
void StartTurnConnection(std::string_view token)
Perform the TURN connection with the given token.
void CloseTurnHandler(std::string_view token)
Close the TURN handler.
std::map< std::string, std::unique_ptr< ClientNetworkTurnSocketHandler >, std::less<> > turn_handlers
Pending TURN handler (if any), stored by token.
void Register()
Register our server to receive our invite code.
void ConnectToServer(std::string_view invite_code, TCPServerConnecter *connecter)
Join a server based on an invite code.
void CloseStunHandler(std::string_view token, uint8_t family=AF_UNSPEC)
Close the STUN handler.
std::map< std::string, std::pair< std::string, TCPServerConnecter * >, std::less<> > connecter
Based on tokens, the current (invite-code, connecter) that are pending.
bool ReceiveGameCoordinatorStunConnect(Packet &p) override
Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side).
uint32_t newgrf_lookup_table_cursor
Last received cursor for the GameInfoNewGRFLookupTable updates.
void SendReceive()
Check whether we received/can send some data from/to the Game Coordinator server and when that's the ...
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
std::chrono::steady_clock::time_point next_update
When to send the next update (if server and public).
bool connecting
Are we connecting to the Game Coordinator?
bool ReceiveGameCoordinatorError(Packet &p) override
Game Coordinator indicates there was an error.
void CloseAllConnections()
Close all pending connection tokens.
void SendServerUpdate()
Send an update of our server status to the Game Coordinator.
bool ReceiveGameCoordinatorStunRequest(Packet &p) override
Game Coordinator requests the client/server to do a STUN request to the STUN server.
void Connect()
Connect to the coordinator.
std::map< std::string, TCPServerConnecter *, std::less<> > connecter_pre
Based on invite codes, the current connecters that are pending.
bool ReceiveGameCoordinatorConnectFailed(Packet &p) override
Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server.
void ConnectFailure(std::string_view token, uint8_t tracking_number)
Callback from a Connecter to let the Game Coordinator know the connection failed.
bool ReceiveGameCoordinatorRegisterAck(Packet &p) override
Game Coordinator acknowledges the registration.
bool ReceiveGameCoordinatorConnecting(Packet &p) override
Game Coordinator informs the Client under what token it will start the attempt to connect the Server ...
void CloseToken(std::string_view token)
Close everything related to this connection token.
std::chrono::steady_clock::time_point last_activity
The last time there was network activity.
void StunResult(std::string_view token, uint8_t family, bool result)
Callback from the STUN connecter to inform the Game Coordinator about the result of the STUN.
bool ReceiveGameCoordinatorListing(Packet &p) override
Game Coordinator replies with a list of all public servers.
bool ReceiveGameCoordinatorTurnConnect(Packet &p) override
Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server.
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
void ConnectSuccess(std::string_view token, SOCKET sock, NetworkAddress &address)
Callback from a Connecter to let the Game Coordinator know the connection to the game server is estab...
bool ReceiveGameCoordinatorNewGRFLookup(Packet &p) override
Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF des...
std::map< std::string, std::map< int, std::unique_ptr< ClientNetworkStunSocketHandler > >, std::less<> > stun_handlers
All pending STUN handlers, stored by token:family.
std::shared_ptr< TCPConnecter > game_connecter
Pending connecter to the game server.
void GetListing()
Request a listing of all public servers.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkCoordinatorSocketHandler(SOCKET s=INVALID_SOCKET)
Create a new cs socket handler for a given cs.
SOCKET sock
The socket currently connected to.
Definition tcp.h:35
TCPConnecter that resolves the server invite code if needed before connecting.
Definition tcp.h:153
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:21
ClientNetworkCoordinatorSocketHandler _network_coordinator_client
The connection to the Game Coordinator.
std::unordered_map< uint32_t, NamedGRFIdentifier > GameInfoNewGRFLookupTable
Lookup table for the GameInfo in case of NST_LOOKUP_ID.
Part of the network protocol handling STUN requests.
Part of the network protocol handling TURN requests.
Basic functions to receive and send TCP packets to/from the Game Coordinator server.