OpenTTD Source 20260421-master-gc2fbc6fdeb
tcp_game.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../../stdafx.h"
11
12#include "../network.h"
13#include "../network_internal.h"
14#include "../../debug.h"
15#include "../../error.h"
16#include "../../strings_func.h"
17
18#include "table/strings.h"
19
20#include "../../safeguards.h"
21
22static std::vector<std::unique_ptr<NetworkGameSocketHandler>> _deferred_deletions;
23
30
39{
40 /* Clients drop back to the main menu */
44 _networking = false;
45 ShowErrorMessage(GetEncodedString(STR_NETWORK_ERROR_LOSTCONNECTION), {}, WL_CRITICAL);
46
48 }
49
51}
52
53
60{
61 PacketGameType type = static_cast<PacketGameType>(p.Recv_uint8());
62
63 this->last_packet = std::chrono::steady_clock::now();
64
65 switch (type) {
88 case PacketGameType::ClientAck: return this->ReceiveClientAck(p);
105 case PacketGameType::ServerMove: return this->ReceiveServerMove(p);
106 case PacketGameType::ClientMove: return this->ReceiveClientMove(p);
108
109 default:
110 Debug(net, 0, "[tcp/game] Received invalid packet type {} from client {}", type, this->client_id);
111 this->CloseConnection();
113 }
114}
115
124{
125 std::unique_ptr<Packet> p;
126 while ((p = this->ReceivePacket()) != nullptr) {
128 if (res != NETWORK_RECV_STATUS_OKAY) return res;
129 }
130
132}
133
140{
141 Debug(net, 0, "[tcp/game] Received illegal packet type {} from client {}", type, this->client_id);
143}
144
187
190{
191 _deferred_deletions.emplace_back(this);
192 this->is_pending_deletion = true;
193}
194
197{
198 /* Calls deleter on all items */
199 _deferred_deletions.clear();
200}
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
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
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
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 ReceiveClientRemoteConsoleCommand(Packet &p)
Send an RCon command to the server: string RCon password.
Definition tcp_game.cpp:181
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
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
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
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
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:118
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
@ NETWORK_RECV_STATUS_CLIENT_QUIT
The connection is lost gracefully. Other clients are already informed of this leaving client.
Definition core.h:26
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition core.h:22
@ NETWORK_RECV_STATUS_CONNECTION_LOST
The connection is lost unexpectedly.
Definition core.h:32
@ NETWORK_RECV_STATUS_MALFORMED_PACKET
We apparently send a malformed packet.
Definition core.h:27
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
Functions related to errors.
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition error.h:27
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:50
uint32_t _frame_counter
The current frame.
Definition network.cpp:80
bool _networking
are we in networking mode?
Definition network.cpp:67
bool _network_server
network-server is active
Definition network.cpp:68
Basic functions/variables used all over the place.
void ClientNetworkEmergencySave()
Create an emergency savegame when the network connection is lost.
Variables and function used internally.
@ SM_MENU
Switch to game intro menu.
Definition openttd.h:33
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
Functions related to OTTD's strings.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:316
static std::vector< std::unique_ptr< NetworkGameSocketHandler > > _deferred_deletions
NetworkGameSocketHandler that still need to be deleted.
Definition tcp_game.cpp:22
PacketGameType
Enum with all types of TCP packets.
Definition tcp_game.h:22
@ 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
@ 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