Merge branch 'master' into jgrpp
[openttd-jgr.git] / src / network / core / tcp_coordinator.h
blob4635847df2714755a9a07838e8d0f4f626e960ed
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 */
8 /**
9 * @file tcp_coordinator.h Basic functions to receive and send TCP packets to/from the Game Coordinator server.
12 #ifndef NETWORK_CORE_TCP_COORDINATOR_H
13 #define NETWORK_CORE_TCP_COORDINATOR_H
15 #include "os_abstraction.h"
16 #include "tcp.h"
17 #include "packet.h"
18 #include "network_game_info.h"
20 /**
21 * Enum with all types of TCP Game Coordinator packets. The order MUST not be changed.
23 * GC -> packets from Game Coordinator to either Client or Server.
24 * SERVER -> packets from Server to Game Coordinator.
25 * CLIENT -> packets from Client to Game Coordinator.
26 * SERCLI -> packets from either the Server or Client to Game Coordinator.
27 **/
28 enum PacketCoordinatorType : uint8_t {
29 PACKET_COORDINATOR_GC_ERROR, ///< Game Coordinator indicates there was an error.
30 PACKET_COORDINATOR_SERVER_REGISTER, ///< Server registration.
31 PACKET_COORDINATOR_GC_REGISTER_ACK, ///< Game Coordinator accepts the registration.
32 PACKET_COORDINATOR_SERVER_UPDATE, ///< Server sends an set intervals an update of the server.
33 PACKET_COORDINATOR_CLIENT_LISTING, ///< Client is requesting a listing of all public servers.
34 PACKET_COORDINATOR_GC_LISTING, ///< Game Coordinator returns a listing of all public servers.
35 PACKET_COORDINATOR_CLIENT_CONNECT, ///< Client wants to connect to a server based on an invite code.
36 PACKET_COORDINATOR_GC_CONNECTING, ///< Game Coordinator informs the client of the token assigned to the connection attempt.
37 PACKET_COORDINATOR_SERCLI_CONNECT_FAILED, ///< Client/server tells the Game Coordinator the current connection attempt failed.
38 PACKET_COORDINATOR_GC_CONNECT_FAILED, ///< Game Coordinator informs client/server it has given up on the connection attempt.
39 PACKET_COORDINATOR_CLIENT_CONNECTED, ///< Client informs the Game Coordinator the connection with the server is established.
40 PACKET_COORDINATOR_GC_DIRECT_CONNECT, ///< Game Coordinator tells client to directly connect to the hostname:port of the server.
41 PACKET_COORDINATOR_GC_STUN_REQUEST, ///< Game Coordinator tells client/server to initiate a STUN request.
42 PACKET_COORDINATOR_SERCLI_STUN_RESULT, ///< Client/server informs the Game Coordinator of the result of the STUN request.
43 PACKET_COORDINATOR_GC_STUN_CONNECT, ///< Game Coordinator tells client/server to connect() reusing the STUN local address.
44 PACKET_COORDINATOR_GC_NEWGRF_LOOKUP, ///< Game Coordinator informs client about NewGRF lookup table updates needed for GC_LISTING.
45 PACKET_COORDINATOR_GC_TURN_CONNECT, ///< Game Coordinator tells client/server to connect to a specific TURN server.
46 PACKET_COORDINATOR_END, ///< Must ALWAYS be on the end of this list!! (period)
49 /**
50 * The type of connection the Game Coordinator can detect we have.
52 enum ConnectionType {
53 CONNECTION_TYPE_UNKNOWN, ///< The Game Coordinator hasn't informed us yet what type of connection we have.
54 CONNECTION_TYPE_ISOLATED, ///< The Game Coordinator failed to find a way to connect to your server. Nobody will be able to join.
55 CONNECTION_TYPE_DIRECT, ///< The Game Coordinator can directly connect to your server.
56 CONNECTION_TYPE_STUN, ///< The Game Coordinator can connect to your server via a STUN request.
57 CONNECTION_TYPE_TURN, ///< The Game Coordinator needs you to connect to a relay.
60 /**
61 * The type of error from the Game Coordinator.
63 enum NetworkCoordinatorErrorType {
64 NETWORK_COORDINATOR_ERROR_UNKNOWN, ///< There was an unknown error.
65 NETWORK_COORDINATOR_ERROR_REGISTRATION_FAILED, ///< Your request for registration failed.
66 NETWORK_COORDINATOR_ERROR_INVALID_INVITE_CODE, ///< The invite code given is invalid.
67 NETWORK_COORDINATOR_ERROR_REUSE_OF_INVITE_CODE, ///< The invite code is used by another (newer) server.
70 /** Base socket handler for all Game Coordinator TCP sockets. */
71 class NetworkCoordinatorSocketHandler : public NetworkTCPSocketHandler {
72 protected:
73 bool ReceiveInvalidPacket(PacketCoordinatorType type);
75 /**
76 * Game Coordinator indicates there was an error. This can either be a
77 * permanent error causing the connection to be dropped, or in response
78 * to a request that is invalid.
80 * uint8_t Type of error (see NetworkCoordinatorErrorType).
81 * string Details of the error.
83 * @param p The packet that was just received.
84 * @return True upon success, otherwise false.
86 virtual bool Receive_GC_ERROR(Packet &p);
88 /**
89 * Server is starting a multiplayer game and wants to let the
90 * Game Coordinator know.
92 * uint8_t Game Coordinator protocol version.
93 * uint8_t Type of game (see ServerGameType).
94 * uint16_t Local port of the server.
95 * string Invite code the server wants to use (can be empty; coordinator will assign a new invite code).
96 * string Secret that belongs to the invite code (empty if invite code is empty).
98 * @param p The packet that was just received.
99 * @return True upon success, otherwise false.
101 virtual bool Receive_SERVER_REGISTER(Packet &p);
104 * Game Coordinator acknowledges the registration.
106 * string Invite code that can be used to join this server.
107 * string Secret that belongs to the invite code (only needed if reusing the invite code on next SERVER_REGISTER).
108 * uint8_t Type of connection was detected (see ConnectionType).
110 * @param p The packet that was just received.
111 * @return True upon success, otherwise false.
113 virtual bool Receive_GC_REGISTER_ACK(Packet &p);
116 * Send an update of the current state of the server to the Game Coordinator.
118 * uint8_t Game Coordinator protocol version.
119 * Serialized NetworkGameInfo. See game_info.hpp for details.
121 * @param p The packet that was just received.
122 * @return True upon success, otherwise false.
124 virtual bool Receive_SERVER_UPDATE(Packet &p);
127 * Client requests a list of all public servers.
129 * uint8_t Game Coordinator protocol version.
130 * uint8_t Game-info version used by this client.
131 * string Revision of the client.
132 * uint32_t (Game Coordinator protocol >= 4) Cursor as received from GC_NEWGRF_LOOKUP, or zero.
134 * @param p The packet that was just received.
135 * @return True upon success, otherwise false.
137 virtual bool Receive_CLIENT_LISTING(Packet &p);
140 * Game Coordinator replies with a list of all public servers. Multiple
141 * of these packets are received after a request till all servers are
142 * sent over. Last packet will have server count of 0.
144 * uint16_t Amount of public servers in this packet.
145 * For each server:
146 * string Connection string for this server.
147 * Serialized NetworkGameInfo. See game_info.hpp for details.
149 * @param p The packet that was just received.
150 * @return True upon success, otherwise false.
152 virtual bool Receive_GC_LISTING(Packet &p);
155 * Client wants to connect to a Server.
157 * uint8_t Game Coordinator protocol version.
158 * string Invite code of the Server to join.
160 * @param p The packet that was just received.
161 * @return True upon success, otherwise false.
163 virtual bool Receive_CLIENT_CONNECT(Packet &p);
166 * Game Coordinator informs the Client under what token it will start the
167 * attempt to connect the Server and Client together.
169 * string Token to track the current connect request.
170 * string Invite code of the Server to join.
172 * @param p The packet that was just received.
173 * @return True upon success, otherwise false.
175 virtual bool Receive_GC_CONNECTING(Packet &p);
178 * Client or Server failed to connect to the remote side.
180 * uint8_t Game Coordinator protocol version.
181 * string Token to track the current connect request.
182 * uint8_t Tracking number to track current connect request.
184 * @param p The packet that was just received.
185 * @return True upon success, otherwise false.
187 virtual bool Receive_SERCLI_CONNECT_FAILED(Packet &p);
190 * Game Coordinator informs the Client that it failed to find a way to
191 * connect the Client to the Server. Any open connections for this token
192 * should be closed now.
194 * string Token to track the current connect request.
196 * @param p The packet that was just received.
197 * @return True upon success, otherwise false.
199 virtual bool Receive_GC_CONNECT_FAILED(Packet &p);
202 * Client informs the Game Coordinator the connection with the Server is
203 * established. The Client will disconnect from the Game Coordinator next.
205 * uint8_t Game Coordinator protocol version.
206 * string Token to track the current connect request.
208 * @param p The packet that was just received.
209 * @return True upon success, otherwise false.
211 virtual bool Receive_CLIENT_CONNECTED(Packet &p);
214 * Game Coordinator requests that the Client makes a direct connection to
215 * the indicated peer, which is a Server.
217 * string Token to track the current connect request.
218 * uint8_t Tracking number to track current connect request.
219 * string Hostname of the peer.
220 * uint16_t Port of the peer.
222 * @param p The packet that was just received.
223 * @return True upon success, otherwise false.
225 virtual bool Receive_GC_DIRECT_CONNECT(Packet &p);
228 * Game Coordinator requests the client/server to do a STUN request to the
229 * STUN server. Important is to remember the local port these STUN requests
230 * are sent from, as this will be needed for later conenctions too.
231 * The client/server should do multiple STUN requests for every available
232 * interface that connects to the Internet (e.g., once for IPv4 and once
233 * for IPv6).
235 * string Token to track the current connect request.
237 * @param p The packet that was just received.
238 * @return True upon success, otherwise false.
240 virtual bool Receive_GC_STUN_REQUEST(Packet &p);
243 * Client/server informs the Game Coordinator the result of a STUN request.
245 * uint8_t Game Coordinator protocol version.
246 * string Token to track the current connect request.
247 * uint8_t Interface number, as given during STUN request.
248 * bool Whether the STUN connection was successful.
250 * @param p The packet that was just received.
251 * @return True upon success, otherwise false.
253 virtual bool Receive_SERCLI_STUN_RESULT(Packet &p);
256 * Game Coordinator informs the client/server of its STUN peer (the host:ip
257 * of the other side). It should start a connect() to this peer ASAP with
258 * the local address as used with the STUN request.
260 * string Token to track the current connect request.
261 * uint8_t Tracking number to track current connect request.
262 * uint8_t Interface number, as given during STUN request.
263 * string Host of the peer.
264 * uint16_t Port of the peer.
266 * @param p The packet that was just received.
267 * @return True upon success, otherwise false.
269 virtual bool Receive_GC_STUN_CONNECT(Packet &p);
272 * Game Coordinator informs the client of updates for the NewGRFs lookup table
273 * as used by the NewGRF deserialization in GC_LISTING.
274 * This packet is sent after a CLIENT_LISTING request, but before GC_LISTING.
276 * uint32_t Lookup table cursor.
277 * uint16_t Number of NewGRFs in the packet, with for each of the NewGRFs:
278 * uint32_t Lookup table index for the NewGRF.
279 * uint32_t Unique NewGRF ID.
280 * uint8_t[16] MD5 checksum of the NewGRF
281 * string Name of the NewGRF.
283 * The lookup table built using these packets are used by the deserialisation
284 * of the NewGRFs for servers in the GC_LISTING. These updates are additive,
285 * i.e. each update will add NewGRFs but never remove them. However, this
286 * lookup table is specific to the connection with the Game Coordinator, and
287 * should be considered invalid after disconnecting from the Game Coordinator.
289 * @param p The packet that was just received.
290 * @return True upon success, otherwise false.
292 virtual bool Receive_GC_NEWGRF_LOOKUP(Packet &p);
295 * Game Coordinator requests that we make a connection to the indicated
296 * peer, which is a TURN server.
298 * string Token to track the current connect request.
299 * uint8_t Tracking number to track current connect request.
300 * string Ticket to hand over to the TURN server.
301 * string Connection string of the TURN server.
303 * @param p The packet that was just received.
304 * @return True upon success, otherwise false.
306 virtual bool Receive_GC_TURN_CONNECT(Packet &p);
308 bool HandlePacket(Packet &p);
309 public:
311 * Create a new cs socket handler for a given cs.
312 * @param s The socket we are connected with.
314 NetworkCoordinatorSocketHandler(SOCKET s = INVALID_SOCKET) : NetworkTCPSocketHandler(s) {}
316 bool ReceivePackets();
319 #endif /* NETWORK_CORE_TCP_COORDINATOR_H */