4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file network_server.h Server part of the network protocol. */
12 #ifndef NETWORK_SERVER_H
13 #define NETWORK_SERVER_H
17 #include "network_internal.h"
18 #include "core/tcp_listen.h"
19 #include "../thread/thread.h"
21 class ServerNetworkGameSocketHandler
;
22 /** Make the code look slightly nicer/simpler. */
23 typedef ServerNetworkGameSocketHandler NetworkClientSocket
;
24 /** Pool with all client sockets. */
25 typedef Pool
<NetworkClientSocket
, ClientIndex
, 8, MAX_CLIENT_SLOTS
, PT_NCLIENT
> NetworkClientSocketPool
;
26 extern NetworkClientSocketPool _networkclientsocket_pool
;
28 /** Class for handling the server side of the game connection. */
29 class ServerNetworkGameSocketHandler
: public NetworkClientSocketPool::PoolItem
<&_networkclientsocket_pool
>, public NetworkGameSocketHandler
, public TCPListenHandler
<ServerNetworkGameSocketHandler
, PACKET_SERVER_FULL
, PACKET_SERVER_BANNED
> {
31 virtual NetworkRecvStatus
Receive_CLIENT_JOIN(Packet
*p
);
32 virtual NetworkRecvStatus
Receive_CLIENT_COMPANY_INFO(Packet
*p
);
33 virtual NetworkRecvStatus
Receive_CLIENT_GAME_PASSWORD(Packet
*p
);
34 virtual NetworkRecvStatus
Receive_CLIENT_COMPANY_PASSWORD(Packet
*p
);
35 virtual NetworkRecvStatus
Receive_CLIENT_GETMAP(Packet
*p
);
36 virtual NetworkRecvStatus
Receive_CLIENT_MAP_OK(Packet
*p
);
37 virtual NetworkRecvStatus
Receive_CLIENT_ACK(Packet
*p
);
38 virtual NetworkRecvStatus
Receive_CLIENT_COMMAND(Packet
*p
);
39 virtual NetworkRecvStatus
Receive_CLIENT_CHAT(Packet
*p
);
40 virtual NetworkRecvStatus
Receive_CLIENT_SET_PASSWORD(Packet
*p
);
41 virtual NetworkRecvStatus
Receive_CLIENT_SET_NAME(Packet
*p
);
42 virtual NetworkRecvStatus
Receive_CLIENT_QUIT(Packet
*p
);
43 virtual NetworkRecvStatus
Receive_CLIENT_ERROR(Packet
*p
);
44 virtual NetworkRecvStatus
Receive_CLIENT_RCON(Packet
*p
);
45 virtual NetworkRecvStatus
Receive_CLIENT_NEWGRFS_CHECKED(Packet
*p
);
46 virtual NetworkRecvStatus
Receive_CLIENT_MOVE(Packet
*p
);
48 NetworkRecvStatus
SendCompanyInfo();
49 NetworkRecvStatus
SendNewGRFCheck();
50 NetworkRecvStatus
SendWelcome();
51 NetworkRecvStatus
SendWait();
52 NetworkRecvStatus
SendNeedGamePassword();
53 NetworkRecvStatus
SendNeedCompanyPassword();
56 /** Status of a client */
58 STATUS_INACTIVE
, ///< The client is not connected nor active.
59 STATUS_NEWGRFS_CHECK
, ///< The client is checking NewGRFs.
60 STATUS_AUTH_GAME
, ///< The client is authorizing with game (server) password.
61 STATUS_AUTH_COMPANY
, ///< The client is authorizing with company password.
62 STATUS_AUTHORIZED
, ///< The client is authorized.
63 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
64 STATUS_MAP
, ///< The client is downloading the map.
65 STATUS_DONE_MAP
, ///< The client has downloaded the map.
66 STATUS_PRE_ACTIVE
, ///< The client is catching up the delayed frames.
67 STATUS_ACTIVE
, ///< The client is active within in the game.
68 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period).
71 byte lag_test
; ///< Byte used for lag-testing the client
72 byte last_token
; ///< The last random token we did send to verify the client is listening
73 uint32 last_token_frame
; ///< The last frame we received the right token
74 ClientStatus status
; ///< Status of this client
75 CommandQueue outgoing_queue
; ///< The command-queue awaiting delivery
76 int receive_limit
; ///< Amount of bytes that we can receive at this moment
78 struct PacketWriter
*savegame
; ///< Writer used to write the savegame.
79 NetworkAddress client_address
; ///< IP-address of the client (so he can be banned)
81 ServerNetworkGameSocketHandler(SOCKET s
);
82 ~ServerNetworkGameSocketHandler();
84 virtual Packet
*ReceivePacket();
85 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
);
86 void GetClientName(char *client_name
, const char *last
) const;
88 NetworkRecvStatus
SendMap();
89 NetworkRecvStatus
SendErrorQuit(ClientID client_id
, NetworkErrorCode errorno
);
90 NetworkRecvStatus
SendQuit(ClientID client_id
);
91 NetworkRecvStatus
SendShutdown();
92 NetworkRecvStatus
SendNewGame();
93 NetworkRecvStatus
SendRConResult(uint16 colour
, const char *command
);
94 NetworkRecvStatus
SendMove(ClientID client_id
, CompanyID company_id
);
96 NetworkRecvStatus
SendClientInfo(NetworkClientInfo
*ci
);
97 NetworkRecvStatus
SendError(NetworkErrorCode error
);
98 NetworkRecvStatus
SendChat(NetworkAction action
, ClientID client_id
, bool self_send
, const char *msg
, int64 data
);
99 NetworkRecvStatus
SendJoin(ClientID client_id
);
100 NetworkRecvStatus
SendFrame();
101 NetworkRecvStatus
SendSync();
102 NetworkRecvStatus
SendCommand(const CommandPacket
*cp
);
103 NetworkRecvStatus
SendCompanyUpdate();
104 NetworkRecvStatus
SendConfigUpdate();
107 static void AcceptConnection(SOCKET s
, const NetworkAddress
&address
);
108 static bool AllowConnection();
111 * Get the name used by the listener.
112 * @return the name to show in debug logs and the like.
114 static const char *GetName()
119 const char *GetClientIP();
121 static ServerNetworkGameSocketHandler
*GetByClientID(ClientID client_id
);
124 void NetworkServer_Tick(bool send_frame
);
125 void NetworkServerSetCompanyPassword(CompanyID company_id
, const char *password
, bool already_hashed
= true);
126 void NetworkServerUpdateCompanyPassworded(CompanyID company_id
, bool passworded
);
129 * Iterate over all the sockets from a given starting point.
130 * @param var The variable to iterate with.
131 * @param start The start of the iteration.
133 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
136 * Iterate over all the sockets.
137 * @param var The variable to iterate with.
139 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
141 #else /* ENABLE_NETWORK */
142 /* Network function stubs when networking is disabled */
144 static inline void NetworkServerMonthlyLoop() {}
145 static inline void NetworkServerYearlyLoop() {}
147 #endif /* ENABLE_NETWORK */
149 #endif /* NETWORK_SERVER_H */