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/>.
8 /** @file network_server.h Server part of the network protocol. */
10 #ifndef NETWORK_SERVER_H
11 #define NETWORK_SERVER_H
13 #include "network_internal.h"
14 #include "core/tcp_listen.h"
16 class ServerNetworkGameSocketHandler
;
17 /** Make the code look slightly nicer/simpler. */
18 typedef ServerNetworkGameSocketHandler NetworkClientSocket
;
19 /** Pool with all client sockets. */
20 typedef Pool
<NetworkClientSocket
, ClientIndex
, 8, MAX_CLIENT_SLOTS
, PT_NCLIENT
> NetworkClientSocketPool
;
21 extern NetworkClientSocketPool _networkclientsocket_pool
;
23 /** Class for handling the server side of the game connection. */
24 class ServerNetworkGameSocketHandler
: public NetworkClientSocketPool::PoolItem
<&_networkclientsocket_pool
>, public NetworkGameSocketHandler
, public TCPListenHandler
<ServerNetworkGameSocketHandler
, PACKET_SERVER_FULL
, PACKET_SERVER_BANNED
> {
26 std::unique_ptr
<class NetworkAuthenticationServerHandler
> authentication_handler
; ///< The handler for the authentication.
27 std::string peer_public_key
; ///< The public key of our client.
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
;
45 NetworkRecvStatus
SendGameInfo();
46 NetworkRecvStatus
SendNewGRFCheck();
47 NetworkRecvStatus
SendWelcome();
48 NetworkRecvStatus
SendAuthRequest();
49 NetworkRecvStatus
SendEnableEncryption();
52 /** Status of a client */
54 STATUS_INACTIVE
, ///< The client is not connected nor active.
55 STATUS_AUTH_GAME
, ///< The client is authorizing with game (server) password.
56 STATUS_IDENTIFY
, ///< The client is identifying itself.
57 STATUS_NEWGRFS_CHECK
, ///< The client is checking NewGRFs.
58 STATUS_AUTHORIZED
, ///< The client is authorized.
59 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
60 STATUS_MAP
, ///< The client is downloading the map.
61 STATUS_DONE_MAP
, ///< The client has downloaded the map.
62 STATUS_PRE_ACTIVE
, ///< The client is catching up the delayed frames.
63 STATUS_ACTIVE
, ///< The client is active within in the game.
64 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period).
67 uint8_t lag_test
; ///< Byte used for lag-testing the client
68 uint8_t last_token
; ///< The last random token we did send to verify the client is listening
69 uint32_t last_token_frame
; ///< The last frame we received the right token
70 ClientStatus status
; ///< Status of this client
71 CommandQueue outgoing_queue
; ///< The command-queue awaiting delivery; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client.
72 size_t receive_limit
; ///< Amount of bytes that we can receive at this moment
74 std::shared_ptr
<struct PacketWriter
> savegame
; ///< Writer used to write the savegame.
75 NetworkAddress client_address
; ///< IP-address of the client (so they can be banned)
77 ServerNetworkGameSocketHandler(SOCKET s
);
78 ~ServerNetworkGameSocketHandler();
80 std::unique_ptr
<Packet
> ReceivePacket() override
;
81 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) override
;
82 std::string
GetClientName() const;
84 void CheckNextClientToSendMap(NetworkClientSocket
*ignore_cs
= nullptr);
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
, const std::string
&command
);
93 NetworkRecvStatus
SendMove(ClientID client_id
, CompanyID company_id
);
95 NetworkRecvStatus
SendClientInfo(NetworkClientInfo
*ci
);
96 NetworkRecvStatus
SendError(NetworkErrorCode error
, const std::string
&reason
= {});
97 NetworkRecvStatus
SendChat(NetworkAction action
, ClientID client_id
, bool self_send
, const std::string
&msg
, int64_t data
);
98 NetworkRecvStatus
SendExternalChat(const std::string
&source
, TextColour colour
, const std::string
&user
, const std::string
&msg
);
99 NetworkRecvStatus
SendJoin(ClientID client_id
);
100 NetworkRecvStatus
SendFrame();
101 NetworkRecvStatus
SendSync();
102 NetworkRecvStatus
SendCommand(const CommandPacket
&cp
);
103 NetworkRecvStatus
SendConfigUpdate();
106 static void AcceptConnection(SOCKET s
, const NetworkAddress
&address
);
107 static bool AllowConnection();
110 * Get the name used by the listener.
111 * @return the name to show in debug logs and the like.
113 static const char *GetName()
118 const std::string
&GetClientIP();
119 std::string_view
GetPeerPublicKey() const { return this->peer_public_key
; }
121 static ServerNetworkGameSocketHandler
*GetByClientID(ClientID client_id
);
124 void NetworkServer_Tick(bool send_frame
);
125 void ChangeNetworkRestartTime(bool reset
);
127 #endif /* NETWORK_SERVER_H */