Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / network / network_server.h
blobbbf98172486e86caf01c3930e0998b17ef57983c
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 <http://www.gnu.org/licenses/>.
6 */
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> {
25 protected:
26 NetworkRecvStatus Receive_CLIENT_JOIN(Packet &p) override;
27 NetworkRecvStatus Receive_CLIENT_GAME_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 SendGameInfo();
44 NetworkRecvStatus SendNewGRFCheck();
45 NetworkRecvStatus SendWelcome();
46 NetworkRecvStatus SendNeedGamePassword();
47 NetworkRecvStatus SendNeedCompanyPassword();
49 public:
50 /** Status of a client */
51 enum ClientStatus {
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_t 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; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client.
70 size_t receive_limit; ///< Amount of bytes that we can receive at this moment
72 std::shared_ptr<struct PacketWriter> savegame; ///< Writer used to write the savegame.
73 NetworkAddress client_address; ///< IP-address of the client (so they can be banned)
75 ServerNetworkGameSocketHandler(SOCKET s);
76 ~ServerNetworkGameSocketHandler();
78 std::unique_ptr<Packet> ReceivePacket() override;
79 NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
80 std::string GetClientName() 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_t colour, const std::string &command);
91 NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
93 NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
94 NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason = {});
95 NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64_t data);
96 NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg);
97 NetworkRecvStatus SendJoin(ClientID client_id);
98 NetworkRecvStatus SendFrame();
99 NetworkRecvStatus SendSync();
100 NetworkRecvStatus SendCommand(const CommandPacket &cp);
101 NetworkRecvStatus SendCompanyUpdate();
102 NetworkRecvStatus SendConfigUpdate();
104 static void Send();
105 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
106 static bool AllowConnection();
109 * Get the name used by the listener.
110 * @return the name to show in debug logs and the like.
112 static const char *GetName()
114 return "server";
117 const std::string &GetClientIP();
119 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
122 void NetworkServer_Tick(bool send_frame);
123 void ChangeNetworkRestartTime(bool reset);
124 void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed = true);
125 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
127 #endif /* NETWORK_SERVER_H */