Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / network / network_server.h
blob76bc95dbc91c54f4b3018c285b10041579408f34
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_COMPANY_INFO(Packet *p) override;
29 NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p) override;
30 NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p) override;
31 NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p) override;
32 NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p) override;
33 NetworkRecvStatus Receive_CLIENT_ACK(Packet *p) override;
34 NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p) override;
35 NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p) override;
36 NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p) override;
37 NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p) override;
38 NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p) override;
39 NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p) override;
40 NetworkRecvStatus Receive_CLIENT_RCON(Packet *p) override;
41 NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) override;
42 NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p) override;
44 NetworkRecvStatus SendGameInfo();
45 NetworkRecvStatus SendCompanyInfo();
46 NetworkRecvStatus SendNewGRFCheck();
47 NetworkRecvStatus SendWelcome();
48 NetworkRecvStatus SendNeedGamePassword();
49 NetworkRecvStatus SendNeedCompanyPassword();
51 public:
52 /** Status of a client */
53 enum ClientStatus {
54 STATUS_INACTIVE, ///< The client is not connected nor active.
55 STATUS_NEWGRFS_CHECK, ///< The client is checking NewGRFs.
56 STATUS_AUTH_GAME, ///< The client is authorizing with game (server) password.
57 STATUS_AUTH_COMPANY, ///< The client is authorizing with company password.
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 byte lag_test; ///< Byte used for lag-testing the client
68 byte last_token; ///< The last random token we did send to verify the client is listening
69 uint32 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
72 size_t receive_limit; ///< Amount of bytes that we can receive at this moment
74 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 virtual 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 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 data);
98 NetworkRecvStatus SendJoin(ClientID client_id);
99 NetworkRecvStatus SendFrame();
100 NetworkRecvStatus SendSync();
101 NetworkRecvStatus SendCommand(const CommandPacket *cp);
102 NetworkRecvStatus SendCompanyUpdate();
103 NetworkRecvStatus SendConfigUpdate();
105 static void Send();
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()
115 return "server";
118 const std::string &GetClientIP();
120 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
123 void NetworkServer_Tick(bool send_frame);
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 */