OpenTTD Source 20260421-master-gc2fbc6fdeb
tcp_game.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_CORE_TCP_GAME_H
11#define NETWORK_CORE_TCP_GAME_H
12
13#include "os_abstraction.h"
14#include "tcp.h"
15#include "../network_type.h"
16#include <chrono>
17
22enum class PacketGameType : uint8_t {
23 /*
24 * These first ten packets must remain in this order for backward and forward compatibility
25 * between clients that are trying to join directly. These packets can be received and/or sent
26 * by the server before the server has processed the 'join' packet from the client.
27 */
28
29 /* Packets sent by socket accepting code without ever constructing a client socket instance. */
32
33 /* Packets used by the client to join and an error message when the revision is wrong. */
36
37 /* Unused packet types, formerly used for the pre-game lobby. */
40
41 /* Packets used to get the game info. */
44
45 /* A server quitting this game. */
48
49 /*
50 * Packets after here assume that the client
51 * and server are running the same version. As
52 * such ordering is unimportant from here on.
53 *
54 * The following is the remainder of the packets
55 * sent as part of authenticating and getting
56 * the map and other important data.
57 */
58
59 /* After the join step, the first perform game authentication and enabling encryption. */
63
64 /* After the authentication is done, the next step is identification. */
66
67 /* After the identify step, the next is checking NewGRFs. */
70
71 /* The server welcomes the authenticated client and sends information of other clients. */
74
75 /* Getting the savegame/map. */
83
85
86 /*
87 * At this moment the client has the map and
88 * the client is fully authenticated. Now the
89 * normal communication starts.
90 */
91
92 /* Game progress monitoring. */
96
97 /* Sending commands around. */
100
101 /* Human communication! */
105
106 /* Remote console. */
109
110 /* Moving a client.*/
113
114 /* Configuration updates. */
117
118 /* A client quitting. */
123};
124
125template <> struct IsEnumPacketType<PacketGameType> {
126 static constexpr bool value = true;
127};
128
130struct CommandPacket;
131
137using CommandQueue = std::vector<CommandPacket>;
138
141/* TODO: rewrite into a proper class */
142private:
144 bool is_pending_deletion = false;
145
146protected:
148
154 virtual NetworkRecvStatus ReceiveServerFull(Packet &p);
155
161 virtual NetworkRecvStatus ReceiveServerBanned(Packet &p);
162
175 virtual NetworkRecvStatus ReceiveClientJoin(Packet &p);
176
183 virtual NetworkRecvStatus ReceiveServerError(Packet &p);
184
190 virtual NetworkRecvStatus ReceiveClientGameInfo(Packet &p);
191
198 virtual NetworkRecvStatus ReceiveServerGameInfo(Packet &p);
199
210
218 virtual NetworkRecvStatus ReceiveClientIdentify(Packet &p);
219
229
239
248
255 virtual NetworkRecvStatus ReceiveServerWelcome(Packet &p);
256
262 virtual NetworkRecvStatus ReceiveClientGetMap(Packet &p);
263
271
278 virtual NetworkRecvStatus ReceiveServerMapBegin(Packet &p);
279
286 virtual NetworkRecvStatus ReceiveServerMapSize(Packet &p);
287
294 virtual NetworkRecvStatus ReceiveServerMapData(Packet &p);
295
301 virtual NetworkRecvStatus ReceiveServerMapDone(Packet &p);
302
308 virtual NetworkRecvStatus ReceiveClientMapOk(Packet &p);
309
317
328 virtual NetworkRecvStatus ReceiveServerFrame(Packet &p);
329
338 virtual NetworkRecvStatus ReceiveServerSync(Packet &p);
339
347 virtual NetworkRecvStatus ReceiveClientAck(Packet &p);
348
359 virtual NetworkRecvStatus ReceiveClientCommand(Packet &p);
360
372 virtual NetworkRecvStatus ReceiveServerCommand(Packet &p);
373
384 virtual NetworkRecvStatus ReceiveClientChat(Packet &p);
385
395 virtual NetworkRecvStatus ReceiveServerChat(Packet &p);
396
407
414 virtual NetworkRecvStatus ReceiveClientSetName(Packet &p);
415
421 virtual NetworkRecvStatus ReceiveClientQuit(Packet &p);
422
429 virtual NetworkRecvStatus ReceiveClientError(Packet &p);
430
437 virtual NetworkRecvStatus ReceiveServerQuit(Packet &p);
438
446 virtual NetworkRecvStatus ReceiveServerErrorQuit(Packet &p);
447
453 virtual NetworkRecvStatus ReceiveServerShutdown(Packet &p);
454
460 virtual NetworkRecvStatus ReceiveServerNewGame(Packet &p);
461
470
479
489
496
504 virtual NetworkRecvStatus ReceiveServerMove(Packet &p);
505
512 virtual NetworkRecvStatus ReceiveClientMove(Packet &p);
513
522
524
525 NetworkGameSocketHandler(SOCKET s);
526public:
528 uint32_t last_frame = 0;
529 uint32_t last_frame_server = 0;
531 std::chrono::steady_clock::time_point last_packet{};
532
533 NetworkRecvStatus CloseConnection(bool error = true) override;
534
541 ~NetworkGameSocketHandler() override = default;
542
548 {
549 assert(info != nullptr && this->info == nullptr);
550 this->info = info;
551 }
552
558 {
559 return this->info;
560 }
561
563
564 std::optional<std::string_view> ReceiveCommand(Packet &p, CommandPacket &cp);
565 void SendCommand(Packet &p, const CommandPacket &cp);
566
571 bool IsPendingDeletion() const { return this->is_pending_deletion; }
572
573 void DeferDeletion();
574 static void ProcessDeferredDeletions();
575};
576
577#endif /* NETWORK_CORE_TCP_GAME_H */
virtual NetworkRecvStatus ReceiveClientAuthenticationResponse(Packet &p)
Send the response to the authentication request: 32 * uint8_t Public key of the client.
Definition tcp_game.cpp:154
virtual NetworkRecvStatus ReceiveServerChat(Packet &p)
Sends a chat-packet to the client: uint8_t ID of the action (see NetworkAction).
Definition tcp_game.cpp:171
virtual NetworkRecvStatus ReceiveServerRemoteConsoleCommand(Packet &p)
Send the result of an issues RCon command back to the client: uint16_t Colour code.
Definition tcp_game.cpp:180
NetworkRecvStatus ReceivePackets()
Do the actual receiving of packets.
Definition tcp_game.cpp:123
bool is_pending_deletion
Whether this socket is pending deletion.
Definition tcp_game.h:144
virtual NetworkRecvStatus ReceiveServerCommand(Packet &p)
Sends a DoCommand to the client: uint8_t ID of the company (0..MAX_COMPANIES-1).
Definition tcp_game.cpp:169
virtual NetworkRecvStatus ReceiveServerClientInfo(Packet &p)
Send information about a client: uint32_t ID of the client (always unique on a server.
Definition tcp_game.cpp:151
virtual NetworkRecvStatus ReceiveClientGameInfo(Packet &p)
Request game information.
Definition tcp_game.cpp:149
virtual NetworkRecvStatus ReceiveServerExternalChat(Packet &p)
Sends a chat-packet for external source to the client: string Name of the source this message came fr...
Definition tcp_game.cpp:172
virtual NetworkRecvStatus ReceiveServerCheckNewGRFs(Packet &p)
Sends information about all used GRFs to the client: uint8_t Amount of GRFs (the following data is re...
Definition tcp_game.cpp:182
virtual NetworkRecvStatus ReceiveClientCommand(Packet &p)
Send a DoCommand to the Server: uint8_t ID of the company (0..MAX_COMPANIES-1).
Definition tcp_game.cpp:168
bool IsPendingDeletion() const
Is this pending for deletion and as such should not be accessed anymore.
Definition tcp_game.h:571
NetworkGameSocketHandler(SOCKET s)
Create a new socket for the game connection.
Definition tcp_game.cpp:28
virtual NetworkRecvStatus ReceiveServerConfigurationUpdate(Packet &p)
Update the clients knowledge of the max settings: uint8_t Maximum number of companies allowed.
Definition tcp_game.cpp:186
uint32_t last_frame
Last frame we have executed.
Definition tcp_game.h:528
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
Definition tcp_game.h:557
virtual NetworkRecvStatus ReceiveServerBanned(Packet &p)
Notification that the client trying to join is banned.
Definition tcp_game.cpp:146
static void ProcessDeferredDeletions()
Actually delete the socket handlers that were marked for deletion.
Definition tcp_game.cpp:196
virtual NetworkRecvStatus ReceiveClientError(Packet &p)
The client made an error and is quitting the game.
Definition tcp_game.cpp:175
std::optional< std::string_view > ReceiveCommand(Packet &p, CommandPacket &cp)
Receives a command from the network.
virtual NetworkRecvStatus ReceiveServerMapDone(Packet &p)
Sends that all data of the map are sent to the client:
Definition tcp_game.cpp:162
virtual NetworkRecvStatus ReceiveClientGetMap(Packet &p)
Request the map from the server.
Definition tcp_game.cpp:157
virtual NetworkRecvStatus ReceiveServerAuthenticationRequest(Packet &p)
Indication to the client that it needs to authenticate: uint8_t The NetworkAuthenticationMethod to us...
Definition tcp_game.cpp:153
virtual NetworkRecvStatus ReceiveServerErrorQuit(Packet &p)
Inform all clients that one client made an error and thus has quit/been disconnected: uint32_t ID of ...
Definition tcp_game.cpp:177
virtual NetworkRecvStatus ReceiveServerQuit(Packet &p)
Notification that a client left the game: uint32_t ID of the client.
Definition tcp_game.cpp:176
NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type)
Helper for logging receiving invalid packets.
Definition tcp_game.cpp:139
ClientID client_id
Client identifier.
Definition tcp_game.h:527
virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status)=0
Close the network connection due to the given status.
virtual NetworkRecvStatus ReceiveClientRemoteConsoleCommand(Packet &p)
Send an RCon command to the server: string RCon password.
Definition tcp_game.cpp:181
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition tcp_game.h:530
std::chrono::steady_clock::time_point last_packet
Time we received the last frame.
Definition tcp_game.h:531
virtual NetworkRecvStatus ReceiveServerFull(Packet &p)
Notification that the server is full.
Definition tcp_game.cpp:145
virtual NetworkRecvStatus ReceiveServerGameInfo(Packet &p)
Sends information about the game.
Definition tcp_game.cpp:150
virtual NetworkRecvStatus ReceiveServerMove(Packet &p)
Move a client from one company into another: uint32_t ID of the client.
Definition tcp_game.cpp:184
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
Definition tcp_game.h:547
virtual NetworkRecvStatus ReceiveClientSetName(Packet &p)
Gives the client a new name: string New name of the client.
Definition tcp_game.cpp:173
virtual NetworkRecvStatus ReceiveClientJoin(Packet &p)
Try to join the server: string OpenTTD revision (norev0000 if no revision).
Definition tcp_game.cpp:147
virtual NetworkRecvStatus ReceiveServerEnableEncryption(Packet &p)
Indication to the client that authentication is complete and encryption has to be used from here on f...
Definition tcp_game.cpp:155
virtual NetworkRecvStatus ReceiveClientAck(Packet &p)
Tell the server we are done with this frame: uint32_t Current frame counter of the client.
Definition tcp_game.cpp:167
uint32_t last_frame_server
Last frame the server has executed.
Definition tcp_game.h:529
virtual NetworkRecvStatus ReceiveServerError(Packet &p)
The client made an error: uint8_t Error code caused (see NetworkErrorCode).
Definition tcp_game.cpp:148
virtual NetworkRecvStatus ReceiveServerSync(Packet &p)
Sends a sync-check to the client: uint32_t Frame counter.
Definition tcp_game.cpp:166
virtual NetworkRecvStatus ReceiveClientMapOk(Packet &p)
Tell the server that we are done receiving/loading the map.
Definition tcp_game.cpp:163
virtual NetworkRecvStatus ReceiveServerShutdown(Packet &p)
Let the clients know that the server is closing.
Definition tcp_game.cpp:178
NetworkClientInfo * info
Client info related to this socket.
Definition tcp_game.h:143
virtual NetworkRecvStatus ReceiveClientIdentify(Packet &p)
The client tells the server about the identity of the client: string Name of the client (max NETWORK_...
Definition tcp_game.cpp:152
NetworkRecvStatus HandlePacket(Packet &p)
Handle the given packet, i.e.
Definition tcp_game.cpp:59
virtual NetworkRecvStatus ReceiveClientNewGRFsChecked(Packet &p)
Tell the server that we have the required GRFs.
Definition tcp_game.cpp:183
virtual NetworkRecvStatus ReceiveServerMapData(Packet &p)
Sends the data of the map to the client: Contains a part of the map (until max size of packet).
Definition tcp_game.cpp:161
virtual NetworkRecvStatus ReceiveClientMove(Packet &p)
Request the server to move this client into another company: uint8_t ID of the company the client wan...
Definition tcp_game.cpp:185
void SendCommand(Packet &p, const CommandPacket &cp)
Sends a command over the network.
virtual NetworkRecvStatus ReceiveClientQuit(Packet &p)
The client is quitting the game.
Definition tcp_game.cpp:174
virtual NetworkRecvStatus ReceiveServerWelcome(Packet &p)
The client is joined and ready to receive their map: uint32_t Own client ID.
Definition tcp_game.cpp:156
void DeferDeletion()
Mark this socket handler for deletion, once iterating the socket handlers is done.
Definition tcp_game.cpp:189
virtual NetworkRecvStatus ReceiveServerMapBegin(Packet &p)
Sends that the server will begin with sending the map to the client: uint32_t Current frame.
Definition tcp_game.cpp:159
virtual NetworkRecvStatus ReceiveServerNewGame(Packet &p)
Let the clients know that the server is loading a new map.
Definition tcp_game.cpp:179
virtual NetworkRecvStatus ReceiveClientChat(Packet &p)
Sends a chat-packet to the server: uint8_t ID of the action (see NetworkAction).
Definition tcp_game.cpp:170
virtual NetworkRecvStatus ReceiveServerWaitForMap(Packet &p)
Notification that another client is currently receiving the map: uint8_t Number of clients waiting in...
Definition tcp_game.cpp:158
virtual NetworkRecvStatus ReceiveServerFrame(Packet &p)
Sends the current frame counter to the client: uint32_t Frame counter uint32_t Frame counter max (how...
Definition tcp_game.cpp:165
NetworkRecvStatus CloseConnection(bool error=true) override
Functions to help ReceivePacket/SendPacket a bit A socket can make errors.
Definition tcp_game.cpp:38
virtual NetworkRecvStatus ReceiveServerMapSize(Packet &p)
Sends the size of the map to the client.
Definition tcp_game.cpp:160
virtual NetworkRecvStatus ReceiveServerClientJoined(Packet &p)
A client joined (PacketGameType::ClientMapOk), what usually directly follows is a PacketGameType::Ser...
Definition tcp_game.cpp:164
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition tcp.h:64
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:21
Types used for networking.
ClientID
'Unique' identifier to be given to clients
@ INVALID_CLIENT_ID
Client is not part of anything.
Includes and/or implementations for the network stuff.
Everything we need to know about a command to be able to execute it.
static constexpr bool value
This is an enumeration of a PacketType.
Definition tcp_game.h:126
Trait to mark an enumeration as a PacketType.
Definition packet.h:33
Container for all information known about a client.
Internal entity of a packet.
Definition packet.h:56
Basic functions to receive and send TCP packets.
PacketGameType
Enum with all types of TCP packets.
Definition tcp_game.h:22
@ ClientUnused
Unused.
Definition tcp_game.h:38
@ ServerAuthenticationRequest
The server requests the client to authenticate using a number of methods.
Definition tcp_game.h:60
@ ClientCommand
Client executed a command and sends it to the server.
Definition tcp_game.h:98
@ ServerClientInfo
Server sends you information about a client.
Definition tcp_game.h:73
@ ServerShutdown
The server is shutting down.
Definition tcp_game.h:47
@ ServerMapDone
Server tells it has just sent the last bits of the map to the client.
Definition tcp_game.h:81
@ ServerMapBegin
Server tells the client that it is beginning to send the map.
Definition tcp_game.h:78
@ ServerFrame
Server tells the client what frame it is in, and thus to where the client may progress.
Definition tcp_game.h:93
@ ServerMapData
Server sends bits of the map to the client.
Definition tcp_game.h:80
@ ServerClientJoined
Tells clients that a new client has joined.
Definition tcp_game.h:84
@ ServerGameInfo
Information about the server.
Definition tcp_game.h:42
@ ClientQuit
A client tells the server it is going to quit.
Definition tcp_game.h:119
@ ServerMapSize
Server tells the client what the (compressed) size of the map is.
Definition tcp_game.h:79
@ ClientAck
The client tells the server which frame it has executed.
Definition tcp_game.h:94
@ ServerFull
The server is full and has no place for you.
Definition tcp_game.h:30
@ ServerMove
Server tells everyone that someone is moved to another company.
Definition tcp_game.h:112
@ ServerQuit
A server tells that a client has quit.
Definition tcp_game.h:120
@ ClientRemoteConsoleCommand
Client asks the server to execute some command.
Definition tcp_game.h:107
@ ServerBanned
The server has banned you.
Definition tcp_game.h:31
@ ClientJoin
The client telling the server it wants to join.
Definition tcp_game.h:34
@ ServerError
Server sending an error message to the client.
Definition tcp_game.h:35
@ ClientNewGRFsChecked
Client acknowledges that it has all required NewGRFs.
Definition tcp_game.h:69
@ ServerSync
Server tells the client what the random state should be.
Definition tcp_game.h:95
@ ClientSetName
A client changes its name.
Definition tcp_game.h:115
@ ClientError
A client reports an error to the server.
Definition tcp_game.h:121
@ ServerUnused
Unused.
Definition tcp_game.h:39
@ ServerConfigurationUpdate
Some network configuration important to the client changed.
Definition tcp_game.h:116
@ ServerRemoteConsoleCommand
Response of the executed command on the server.
Definition tcp_game.h:108
@ ClientMapOk
Client tells the server that it received the whole map.
Definition tcp_game.h:82
@ ServerExternalChat
Server distributing the message from external source.
Definition tcp_game.h:104
@ ServerErrorQuit
A server tells that a client has hit an error and did quit.
Definition tcp_game.h:122
@ ServerWelcome
Server welcomes you and gives you your ClientID.
Definition tcp_game.h:72
@ ServerCommand
Server distributes a command to (all) the clients.
Definition tcp_game.h:99
@ ServerChat
Server distributing the message of a client (or itself).
Definition tcp_game.h:103
@ ClientMove
A client would like to be moved to another company.
Definition tcp_game.h:111
@ ServerCheckNewGRFs
Server sends NewGRF IDs and MD5 checksums for the client to check.
Definition tcp_game.h:68
@ ServerWaitForMap
Server tells the client there are some people waiting for the map as well.
Definition tcp_game.h:77
@ ClientGameInfo
Request information about the server.
Definition tcp_game.h:43
@ ServerEnableEncryption
The server tells that authentication has completed and requests to enable encryption with the keys of...
Definition tcp_game.h:62
@ ClientGetMap
Client requests the actual map.
Definition tcp_game.h:76
@ ClientChat
Client said something that should be distributed.
Definition tcp_game.h:102
@ ServerNewGame
The server is preparing to start a new game.
Definition tcp_game.h:46
@ ClientAuthenticationResponse
The client responds to the authentication request.
Definition tcp_game.h:61
@ ClientIdentify
Client telling the server the client's name and requested company.
Definition tcp_game.h:65
std::vector< CommandPacket > CommandQueue
A "queue" of CommandPackets.
Definition tcp_game.h:137