1 /* $Id: network_admin.h 25588 2013-07-11 20:31:39Z planetmaker $ */
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_admin.h Server part of the admin network protocol. */
12 #ifndef NETWORK_ADMIN_H
13 #define NETWORK_ADMIN_H
17 #include "network_internal.h"
18 #include "core/tcp_listen.h"
19 #include "core/tcp_admin.h"
21 extern AdminIndex _redirect_console_to_admin
;
23 class ServerNetworkAdminSocketHandler
;
24 /** Pool with all admin connections. */
25 typedef Pool
<ServerNetworkAdminSocketHandler
, AdminIndex
, 2, MAX_ADMINS
, PT_NADMIN
> NetworkAdminSocketPool
;
26 extern NetworkAdminSocketPool _networkadminsocket_pool
;
28 /** Class for handling the server side of the game connection. */
29 class ServerNetworkAdminSocketHandler
: public NetworkAdminSocketPool::PoolItem
<&_networkadminsocket_pool
>, public NetworkAdminSocketHandler
, public TCPListenHandler
<ServerNetworkAdminSocketHandler
, ADMIN_PACKET_SERVER_FULL
, ADMIN_PACKET_SERVER_BANNED
> {
31 virtual NetworkRecvStatus
Receive_ADMIN_JOIN(Packet
*p
);
32 virtual NetworkRecvStatus
Receive_ADMIN_QUIT(Packet
*p
);
33 virtual NetworkRecvStatus
Receive_ADMIN_UPDATE_FREQUENCY(Packet
*p
);
34 virtual NetworkRecvStatus
Receive_ADMIN_POLL(Packet
*p
);
35 virtual NetworkRecvStatus
Receive_ADMIN_CHAT(Packet
*p
);
36 virtual NetworkRecvStatus
Receive_ADMIN_RCON(Packet
*p
);
37 virtual NetworkRecvStatus
Receive_ADMIN_GAMESCRIPT(Packet
*p
);
38 virtual NetworkRecvStatus
Receive_ADMIN_PING(Packet
*p
);
40 NetworkRecvStatus
SendProtocol();
41 NetworkRecvStatus
SendPong(uint32 d1
);
43 AdminUpdateFrequency update_frequency
[ADMIN_UPDATE_END
]; ///< Admin requested update intervals.
44 uint32 realtime_connect
; ///< Time of connection.
45 NetworkAddress address
; ///< Address of the admin.
47 ServerNetworkAdminSocketHandler(SOCKET s
);
48 ~ServerNetworkAdminSocketHandler();
50 NetworkRecvStatus
SendError(NetworkErrorCode error
);
51 NetworkRecvStatus
SendWelcome();
52 NetworkRecvStatus
SendNewGame();
53 NetworkRecvStatus
SendShutdown();
55 NetworkRecvStatus
SendDate();
56 NetworkRecvStatus
SendClientJoin(ClientID client_id
);
57 NetworkRecvStatus
SendClientInfo(const NetworkClientSocket
*cs
, const NetworkClientInfo
*ci
);
58 NetworkRecvStatus
SendClientUpdate(const NetworkClientInfo
*ci
);
59 NetworkRecvStatus
SendClientQuit(ClientID client_id
);
60 NetworkRecvStatus
SendClientError(ClientID client_id
, NetworkErrorCode error
);
61 NetworkRecvStatus
SendCompanyNew(CompanyID company_id
);
62 NetworkRecvStatus
SendCompanyInfo(const Company
*c
);
63 NetworkRecvStatus
SendCompanyUpdate(const Company
*c
);
64 NetworkRecvStatus
SendCompanyRemove(CompanyID company_id
, AdminCompanyRemoveReason bcrr
);
65 NetworkRecvStatus
SendCompanyEconomy();
66 NetworkRecvStatus
SendCompanyStats();
68 NetworkRecvStatus
SendChat(NetworkAction action
, DestType desttype
, ClientID client_id
, const char *msg
, NetworkTextMessageData data
);
69 NetworkRecvStatus
SendRcon(uint16 colour
, const char *command
);
70 NetworkRecvStatus
SendConsole(const char *origin
, const char *command
);
71 NetworkRecvStatus
SendGameScript(const char *json
);
72 NetworkRecvStatus
SendCmdNames();
73 NetworkRecvStatus
SendCmdLogging(ClientID client_id
, const CommandPacket
*cp
);
74 NetworkRecvStatus
SendRconEnd(const char *command
);
77 static void AcceptConnection(SOCKET s
, const NetworkAddress
&address
);
78 static bool AllowConnection();
79 static void WelcomeAll();
82 * Get the name used by the listener.
83 * @return the name to show in debug logs and the like.
85 static const char *GetName()
92 * Iterate over all the sockets from a given starting point.
93 * @param var The variable to iterate with.
94 * @param start The start of the iteration.
96 #define FOR_ALL_ADMIN_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(ServerNetworkAdminSocketHandler, adminsocket_index, var, start)
99 * Iterate over all the sockets.
100 * @param var The variable to iterate with.
102 #define FOR_ALL_ADMIN_SOCKETS(var) FOR_ALL_ADMIN_SOCKETS_FROM(var, 0)
105 * Iterate over all the active sockets.
106 * @param var The variable to iterate with.
108 #define FOR_ALL_ACTIVE_ADMIN_SOCKETS(var) \
109 FOR_ALL_ADMIN_SOCKETS(var) \
110 if (var->GetAdminStatus() == ADMIN_STATUS_ACTIVE)
112 void NetworkAdminClientInfo(const NetworkClientSocket
*cs
, bool new_client
= false);
113 void NetworkAdminClientUpdate(const NetworkClientInfo
*ci
);
114 void NetworkAdminClientQuit(ClientID client_id
);
115 void NetworkAdminClientError(ClientID client_id
, NetworkErrorCode error_code
);
116 void NetworkAdminCompanyInfo(const Company
*company
, bool new_company
);
117 void NetworkAdminCompanyUpdate(const Company
*company
);
118 void NetworkAdminCompanyRemove(CompanyID company_id
, AdminCompanyRemoveReason bcrr
);
120 void NetworkAdminChat(NetworkAction action
, DestType desttype
, ClientID client_id
, const char *msg
, NetworkTextMessageData data
= NetworkTextMessageData(), bool from_admin
= false);
121 void NetworkAdminUpdate(AdminUpdateFrequency freq
);
122 void NetworkServerSendAdminRcon(AdminIndex admin_index
, TextColour colour_code
, const char *string
);
123 void NetworkAdminConsole(const char *origin
, const char *string
);
124 void NetworkAdminGameScript(const char *json
);
125 void NetworkAdminCmdLogging(const NetworkClientSocket
*owner
, const CommandPacket
*cp
);
127 #endif /* ENABLE_NETWORK */
128 #endif /* NETWORK_ADMIN_H */