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 NetworkRecvStatus
Receive_CLIENT_JOIN(Packet
*p
) override
;
27 NetworkRecvStatus
Receive_CLIENT_COMPANY_INFO(Packet
*p
) override
;
28 NetworkRecvStatus
Receive_CLIENT_GAME_PASSWORD(Packet
*p
) override
;
29 NetworkRecvStatus
Receive_CLIENT_COMPANY_PASSWORD(Packet
*p
) override
;
30 NetworkRecvStatus
Receive_CLIENT_GETMAP(Packet
*p
) override
;
31 NetworkRecvStatus
Receive_CLIENT_MAP_OK(Packet
*p
) override
;
32 NetworkRecvStatus
Receive_CLIENT_ACK(Packet
*p
) override
;
33 NetworkRecvStatus
Receive_CLIENT_COMMAND(Packet
*p
) override
;
34 NetworkRecvStatus
Receive_CLIENT_CHAT(Packet
*p
) override
;
35 NetworkRecvStatus
Receive_CLIENT_SET_PASSWORD(Packet
*p
) override
;
36 NetworkRecvStatus
Receive_CLIENT_SET_NAME(Packet
*p
) override
;
37 NetworkRecvStatus
Receive_CLIENT_QUIT(Packet
*p
) override
;
38 NetworkRecvStatus
Receive_CLIENT_ERROR(Packet
*p
) override
;
39 NetworkRecvStatus
Receive_CLIENT_RCON(Packet
*p
) override
;
40 NetworkRecvStatus
Receive_CLIENT_NEWGRFS_CHECKED(Packet
*p
) override
;
41 NetworkRecvStatus
Receive_CLIENT_MOVE(Packet
*p
) override
;
43 NetworkRecvStatus
SendCompanyInfo();
44 NetworkRecvStatus
SendNewGRFCheck();
45 NetworkRecvStatus
SendWelcome();
46 NetworkRecvStatus
SendNeedGamePassword();
47 NetworkRecvStatus
SendNeedCompanyPassword();
50 /** Status of a client */
52 STATUS_INACTIVE
, ///< The client is not connected nor active.
53 STATUS_NEWGRFS_CHECK
, ///< The client is checking NewGRFs.
54 STATUS_AUTH_GAME
, ///< The client is authorizing with game (server) password.
55 STATUS_AUTH_COMPANY
, ///< The client is authorizing with company password.
56 STATUS_AUTHORIZED
, ///< The client is authorized.
57 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
58 STATUS_MAP
, ///< The client is downloading the map.
59 STATUS_DONE_MAP
, ///< The client has downloaded the map.
60 STATUS_PRE_ACTIVE
, ///< The client is catching up the delayed frames.
61 STATUS_ACTIVE
, ///< The client is active within in the game.
62 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period).
65 byte lag_test
; ///< Byte used for lag-testing the client
66 byte last_token
; ///< The last random token we did send to verify the client is listening
67 uint32 last_token_frame
; ///< The last frame we received the right token
68 ClientStatus status
; ///< Status of this client
69 CommandQueue outgoing_queue
; ///< The command-queue awaiting delivery
70 int receive_limit
; ///< Amount of bytes that we can receive at this moment
72 struct PacketWriter
*savegame
; ///< Writer used to write the savegame.
73 NetworkAddress client_address
; ///< IP-address of the client (so he can be banned)
75 ServerNetworkGameSocketHandler(SOCKET s
);
76 ~ServerNetworkGameSocketHandler();
78 virtual Packet
*ReceivePacket() override
;
79 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) override
;
80 void GetClientName(char *client_name
, const char *last
) const;
82 void CheckNextClientToSendMap(NetworkClientSocket
*ignore_cs
= nullptr);
84 NetworkRecvStatus
SendWait();
85 NetworkRecvStatus
SendMap();
86 NetworkRecvStatus
SendErrorQuit(ClientID client_id
, NetworkErrorCode errorno
);
87 NetworkRecvStatus
SendQuit(ClientID client_id
);
88 NetworkRecvStatus
SendShutdown();
89 NetworkRecvStatus
SendNewGame();
90 NetworkRecvStatus
SendRConResult(uint16 colour
, const char *command
);
91 NetworkRecvStatus
SendMove(ClientID client_id
, CompanyID company_id
);
93 NetworkRecvStatus
SendClientInfo(NetworkClientInfo
*ci
);
94 NetworkRecvStatus
SendError(NetworkErrorCode error
, const char *reason
= nullptr);
95 NetworkRecvStatus
SendChat(NetworkAction action
, ClientID client_id
, bool self_send
, const char *msg
, int64 data
);
96 NetworkRecvStatus
SendJoin(ClientID client_id
);
97 NetworkRecvStatus
SendFrame();
98 NetworkRecvStatus
SendSync();
99 NetworkRecvStatus
SendCommand(const CommandPacket
*cp
);
100 NetworkRecvStatus
SendCompanyUpdate();
101 NetworkRecvStatus
SendConfigUpdate();
104 static void AcceptConnection(SOCKET s
, const NetworkAddress
&address
);
105 static bool AllowConnection();
108 * Get the name used by the listener.
109 * @return the name to show in debug logs and the like.
111 static const char *GetName()
116 const char *GetClientIP();
118 static ServerNetworkGameSocketHandler
*GetByClientID(ClientID client_id
);
121 void NetworkServer_Tick(bool send_frame
);
122 void NetworkServerSetCompanyPassword(CompanyID company_id
, const char *password
, bool already_hashed
= true);
123 void NetworkServerUpdateCompanyPassworded(CompanyID company_id
, bool passworded
);
125 #endif /* NETWORK_SERVER_H */