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

OpenTTD Source 15.3

network_server.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
10#ifndef NETWORK_SERVER_H
11#define NETWORK_SERVER_H
12
13#include "network_internal.h"
14#include "core/tcp_listen.h"
15
16class ServerNetworkGameSocketHandler;
18 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
20 using NetworkClientSocketPool = Pool<NetworkClientSocket, ClientPoolID, 8, PoolType::NetworkClient>;
21extern NetworkClientSocketPool _networkclientsocket_pool;
22
24 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
25protected:
26 std::unique_ptr<class NetworkAuthenticationServerHandler> authentication_handler = nullptr;
27 std::string peer_public_key{};
28
29 NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override;
30 NetworkRecvStatus Receive_CLIENT_IDENTIFY(Packet &p) override;
31 NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet &p) override;
32 NetworkRecvStatus Receive_CLIENT_AUTH_RESPONSE(Packet &p) override;
33 NetworkRecvStatus Receive_CLIENT_GETMAP(Packet &p) override;
34 NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet &p) override;
35 NetworkRecvStatus Receive_CLIENT_ACK(Packet &p) override;
36 NetworkRecvStatus Receive_CLIENT_COMMAND(Packet &p) override;
37 NetworkRecvStatus Receive_CLIENT_CHAT(Packet &p) override;
38 NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet &p) override;
39 NetworkRecvStatus Receive_CLIENT_QUIT(Packet &p) override;
40 NetworkRecvStatus Receive_CLIENT_ERROR(Packet &p) override;
41 NetworkRecvStatus Receive_CLIENT_RCON(Packet &p) override;
42 NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet &p) override;
43 NetworkRecvStatus Receive_CLIENT_MOVE(Packet &p) override;
44
45 NetworkRecvStatus SendGameInfo();
46 NetworkRecvStatus SendNewGRFCheck();
47 NetworkRecvStatus SendWelcome();
48 NetworkRecvStatus SendAuthRequest();
49 NetworkRecvStatus SendEnableEncryption();
50
51public:
53 enum ClientStatus : uint8_t {
54 STATUS_INACTIVE,
55 STATUS_AUTH_GAME,
56 STATUS_IDENTIFY,
57 STATUS_NEWGRFS_CHECK,
58 STATUS_AUTHORIZED,
59 STATUS_MAP_WAIT,
60 STATUS_MAP,
61 STATUS_DONE_MAP,
62 STATUS_PRE_ACTIVE,
63 STATUS_ACTIVE,
64 STATUS_END,
65 };
66
67 uint8_t lag_test = 0;
68 uint8_t last_token = 0;
69 uint32_t last_token_frame = 0;
70 ClientStatus status = STATUS_INACTIVE;
71 CommandQueue outgoing_queue{};
72 size_t receive_limit = 0;
73
74 std::shared_ptr<struct PacketWriter> savegame = nullptr;
75 NetworkAddress client_address{};
76
77 ServerNetworkGameSocketHandler(SOCKET s);
78 ~ServerNetworkGameSocketHandler();
79
80 std::unique_ptr<Packet> ReceivePacket() override;
81 NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
82 std::string GetClientName() const;
83
84 void CheckNextClientToSendMap(NetworkClientSocket *ignore_cs = nullptr);
85
86 NetworkRecvStatus SendWait();
87 NetworkRecvStatus SendMap();
88 NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
89 NetworkRecvStatus SendQuit(ClientID client_id);
90 NetworkRecvStatus SendShutdown();
91 NetworkRecvStatus SendNewGame();
92 NetworkRecvStatus SendRConResult(uint16_t colour, std::string_view command);
93 NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
94
95 NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
96 NetworkRecvStatus SendError(NetworkErrorCode error, std::string_view reason = {});
97 NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, std::string_view msg, int64_t data);
98 NetworkRecvStatus SendExternalChat(std::string_view source, TextColour colour, std::string_view user, std::string_view msg);
99 NetworkRecvStatus SendJoin(ClientID client_id);
100 NetworkRecvStatus SendFrame();
101 NetworkRecvStatus SendSync();
102 NetworkRecvStatus SendCommand(const CommandPacket &cp);
103 NetworkRecvStatus SendConfigUpdate();
104
105 static void Send();
106 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
107 static bool AllowConnection();
108
113 static std::string_view GetName()
114 {
115 return "server";
116 }
117
118 std::string_view GetClientIP();
119 std::string_view GetPeerPublicKey() const { return this->peer_public_key; }
120
121 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
122};
123
124void NetworkServer_Tick(bool send_frame);
125void ChangeNetworkRestartTime(bool reset);
126
127#endif /* NETWORK_SERVER_H */
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition tcp_game.h:140
NetworkGameSocketHandler::client_id
ClientID client_id
Client identifier.
Definition tcp_game.h:485
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition network_server.h:24
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME
NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet &p) override
Gives the client a new name: string New name of the client.
Definition network_server.cpp:1407
ServerNetworkGameSocketHandler::GetName
static std::string_view GetName()
Get the name used by the listener.
Definition network_server.h:113
ServerNetworkGameSocketHandler::receive_limit
size_t receive_limit
Amount of bytes that we can receive at this moment.
Definition network_server.h:72
ServerNetworkGameSocketHandler::GetByClientID
static ServerNetworkGameSocketHandler * GetByClientID(ClientID client_id)
Return the client state given it's client-identifier.
Definition network.cpp:154
ServerNetworkGameSocketHandler::savegame
std::shared_ptr< struct PacketWriter > savegame
Writer used to write the savegame.
Definition network_server.h:74
ServerNetworkGameSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition network_server.cpp:303
ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE
NetworkRecvStatus Receive_CLIENT_MOVE(Packet &p) override
Request the server to move this client into another company: uint8_t ID of the company the client wan...
Definition network_server.cpp:1467
ServerNetworkGameSocketHandler::SendQuit
NetworkRecvStatus SendQuit(ClientID client_id)
Tell the client another client quit.
Definition network_server.cpp:744
ServerNetworkGameSocketHandler::Receive_CLIENT_ACK
NetworkRecvStatus Receive_CLIENT_ACK(Packet &p) override
Tell the server we are done with this frame: uint32_t Current frame counter of the client.
Definition network_server.cpp:1193
ServerNetworkGameSocketHandler::lag_test
uint8_t lag_test
Byte used for lag-testing the client.
Definition network_server.h:67
ServerNetworkGameSocketHandler::Receive_CLIENT_AUTH_RESPONSE
NetworkRecvStatus Receive_CLIENT_AUTH_RESPONSE(Packet &p) override
Send the response to the authentication request: 32 * uint8_t Public key of the client.
Definition network_server.cpp:945
ServerNetworkGameSocketHandler::AcceptConnection
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the accepting of a connection to the server.
Definition network.cpp:567
ServerNetworkGameSocketHandler::ReceivePacket
std::unique_ptr< Packet > ReceivePacket() override
Receives a packet for the given client.
Definition network_server.cpp:222
ServerNetworkGameSocketHandler::ClientStatus
ClientStatus
Status of a client.
Definition network_server.h:53
ServerNetworkGameSocketHandler::STATUS_END
@ STATUS_END
Must ALWAYS be on the end of this list!! (period).
Definition network_server.h:64
ServerNetworkGameSocketHandler::STATUS_PRE_ACTIVE
@ STATUS_PRE_ACTIVE
The client is catching up the delayed frames.
Definition network_server.h:62
ServerNetworkGameSocketHandler::STATUS_IDENTIFY
@ STATUS_IDENTIFY
The client is identifying itself.
Definition network_server.h:56
ServerNetworkGameSocketHandler::STATUS_AUTH_GAME
@ STATUS_AUTH_GAME
The client is authorizing with game (server) password.
Definition network_server.h:55
ServerNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
@ STATUS_NEWGRFS_CHECK
The client is checking NewGRFs.
Definition network_server.h:57
ServerNetworkGameSocketHandler::STATUS_AUTHORIZED
@ STATUS_AUTHORIZED
The client is authorized.
Definition network_server.h:58
ServerNetworkGameSocketHandler::STATUS_INACTIVE
@ STATUS_INACTIVE
The client is not connected nor active.
Definition network_server.h:54
ServerNetworkGameSocketHandler::STATUS_MAP
@ STATUS_MAP
The client is downloading the map.
Definition network_server.h:60
ServerNetworkGameSocketHandler::STATUS_DONE_MAP
@ STATUS_DONE_MAP
The client has downloaded the map.
Definition network_server.h:61
ServerNetworkGameSocketHandler::STATUS_ACTIVE
@ STATUS_ACTIVE
The client is active within in the game.
Definition network_server.h:63
ServerNetworkGameSocketHandler::STATUS_MAP_WAIT
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition network_server.h:59
ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND
NetworkRecvStatus Receive_CLIENT_COMMAND(Packet &p) override
The client has done a command and wants us to handle it.
Definition network_server.cpp:1054
ServerNetworkGameSocketHandler::SendRConResult
NetworkRecvStatus SendRConResult(uint16_t colour, std::string_view command)
Send the result of a console action.
Definition network_server.cpp:781
ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT
NetworkRecvStatus Receive_CLIENT_QUIT(Packet &p) override
The client is quitting the game.
Definition network_server.cpp:1169
ServerNetworkGameSocketHandler::outgoing_queue
CommandQueue outgoing_queue
The command-queue awaiting delivery; conceptually more a bucket to gather commands in,...
Definition network_server.h:71
ServerNetworkGameSocketHandler::SendChat
NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, std::string_view msg, int64_t data)
Send a chat message.
Definition network_server.cpp:680
ServerNetworkGameSocketHandler::SendWelcome
NetworkRecvStatus SendWelcome()
Send the client a welcome message with some basic information.
Definition network_server.cpp:472
ServerNetworkGameSocketHandler::SendAuthRequest
NetworkRecvStatus SendAuthRequest()
Request the game password.
Definition network_server.cpp:433
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler
~ServerNetworkGameSocketHandler()
Clear everything related to this client.
Definition network_server.cpp:207
ServerNetworkGameSocketHandler::GetClientName
std::string GetClientName() const
Get the name of the client, if the user did not send it yet, Client ID is used.
Definition network_server.cpp:2104
ServerNetworkGameSocketHandler::last_token
uint8_t last_token
The last random token we did send to verify the client is listening.
Definition network_server.h:68
ServerNetworkGameSocketHandler::SendFrame
NetworkRecvStatus SendFrame()
Tell the client that they may run to a particular frame.
Definition network_server.cpp:616
ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR
NetworkRecvStatus Receive_CLIENT_ERROR(Packet &p) override
The client made an error and is quitting the game.
Definition network_server.cpp:1138
ServerNetworkGameSocketHandler::SendGameInfo
NetworkRecvStatus SendGameInfo()
Send the client information about the server.
Definition network_server.cpp:342
ServerNetworkGameSocketHandler::SendJoin
NetworkRecvStatus SendJoin(ClientID client_id)
Tell that a client joined.
Definition network_server.cpp:603
ServerNetworkGameSocketHandler::peer_public_key
std::string peer_public_key
The public key of our client.
Definition network_server.h:27
ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT
NetworkRecvStatus Receive_CLIENT_CHAT(Packet &p) override
Sends a chat-packet to the server: uint8_t ID of the action (see NetworkAction).
Definition network_server.cpp:1377
ServerNetworkGameSocketHandler::SendClientInfo
NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci)
Send the client information about a client.
Definition network_server.cpp:325
ServerNetworkGameSocketHandler::AllowConnection
static bool AllowConnection()
Whether an connection is allowed or not at this moment.
Definition network_server.cpp:290
ServerNetworkGameSocketHandler::SendEnableEncryption
NetworkRecvStatus SendEnableEncryption()
Notify the client that the authentication has completed and tell that for the remainder of this socke...
Definition network_server.cpp:458
ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN
NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override
Try to join the server: string OpenTTD revision (norev0000 if no revision).
Definition network_server.cpp:846
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO
NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet &p) override
Request game information.
Definition network_server.cpp:827
ServerNetworkGameSocketHandler::SendExternalChat
NetworkRecvStatus SendExternalChat(std::string_view source, TextColour colour, std::string_view user, std::string_view msg)
Send a chat message from external source.
Definition network_server.cpp:705
ServerNetworkGameSocketHandler::SendErrorQuit
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
Tell the client another client quit with an error.
Definition network_server.cpp:727
ServerNetworkGameSocketHandler::client_address
NetworkAddress client_address
IP-address of the client (so they can be banned)
Definition network_server.h:75
ServerNetworkGameSocketHandler::SendNewGame
NetworkRecvStatus SendNewGame()
Tell the client we're starting a new game.
Definition network_server.cpp:767
ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP
NetworkRecvStatus Receive_CLIENT_GETMAP(Packet &p) override
Request the map from the server.
Definition network_server.cpp:983
ServerNetworkGameSocketHandler::SendSync
NetworkRecvStatus SendSync()
Request the client to sync.
Definition network_server.cpp:639
ServerNetworkGameSocketHandler::authentication_handler
std::unique_ptr< class NetworkAuthenticationServerHandler > authentication_handler
The handler for the authentication.
Definition network_server.h:26
ServerNetworkGameSocketHandler::SendMove
NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id)
Tell that a client moved to another company.
Definition network_server.cpp:798
ServerNetworkGameSocketHandler::Receive_CLIENT_RCON
NetworkRecvStatus Receive_CLIENT_RCON(Packet &p) override
Send an RCon command to the server: string RCon password.
Definition network_server.cpp:1441
ServerNetworkGameSocketHandler::SendError
NetworkRecvStatus SendError(NetworkErrorCode error, std::string_view reason={})
Send an error to the client, and close its connection.
Definition network_server.cpp:359
ServerNetworkGameSocketHandler::Receive_CLIENT_IDENTIFY
NetworkRecvStatus Receive_CLIENT_IDENTIFY(Packet &p) override
The client tells the server about the identity of the client: string Name of the client (max NETWORK_...
Definition network_server.cpp:872
ServerNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition network_server.cpp:235
ServerNetworkGameSocketHandler::SendMap
NetworkRecvStatus SendMap()
This sends the map to the client.
Definition network_server.cpp:550
ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED
NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet &p) override
Tell the server that we have the required GRFs.
Definition network_server.cpp:834
ServerNetworkGameSocketHandler::last_token_frame
uint32_t last_token_frame
The last frame we received the right token.
Definition network_server.h:69
ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK
NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet &p) override
Tell the server that we are done receiving/loading the map.
Definition network_server.cpp:1007
ServerNetworkGameSocketHandler::SendShutdown
NetworkRecvStatus SendShutdown()
Tell the client we're shutting down.
Definition network_server.cpp:757
ServerNetworkGameSocketHandler::status
ClientStatus status
Status of this client.
Definition network_server.h:70
ServerNetworkGameSocketHandler::SendNewGRFCheck
NetworkRecvStatus SendNewGRFCheck()
Send the check for the NewGRFs.
Definition network_server.cpp:404
ServerNetworkGameSocketHandler::GetClientIP
std::string_view GetClientIP()
Get the IP address/hostname of the connected client.
Definition network_server.cpp:1919
ServerNetworkGameSocketHandler::SendConfigUpdate
NetworkRecvStatus SendConfigUpdate()
Send an update about the max company/spectator counts.
Definition network_server.cpp:811
ServerNetworkGameSocketHandler::SendWait
NetworkRecvStatus SendWait()
Tell the client that its put in a waiting queue.
Definition network_server.cpp:502
ServerNetworkGameSocketHandler::SendCommand
NetworkRecvStatus SendCommand(const CommandPacket &cp)
Send a command to the client to execute.
Definition network_server.cpp:658
TCPListenHandler
Template for TCP listeners.
Definition tcp_listen.h:30
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
network_internal.h
Variables and function used internally.
_networkclientsocket_pool
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket")
Make very sure the preconditions given in network_type.h are actually followed.
NetworkClientSocket
ServerNetworkGameSocketHandler NetworkClientSocket
Make the code look slightly nicer/simpler.
Definition network_server.h:18
ChangeNetworkRestartTime
void ChangeNetworkRestartTime(bool reset)
Reset the automatic network restart time interval.
Definition network_server.cpp:1847
NetworkServer_Tick
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
Definition network_server.cpp:1679
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition network_type.h:99
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition network_type.h:79
ClientID
ClientID
'Unique' identifier to be given to clients
Definition network_type.h:43
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition network_internal.h:95
NetworkClientInfo
Container for all information known about a client.
Definition network_base.h:24
Packet
Internal entity of a packet.
Definition packet.h:43
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool
Base class for all pools.
Definition pool_type.hpp:133
CommandQueue
std::vector< CommandPacket > CommandQueue
A "queue" of CommandPackets.
Definition tcp_game.h:137
tcp_listen.h
Basic functions to listen for TCP connections.