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_admin.h Server part of the admin network protocol. */
10 #ifndef NETWORK_ADMIN_H
11 #define NETWORK_ADMIN_H
13 #include "network_internal.h"
14 #include "core/tcp_listen.h"
15 #include "core/tcp_admin.h"
17 extern AdminIndex _redirect_console_to_admin
;
19 class ServerNetworkAdminSocketHandler
;
20 /** Pool with all admin connections. */
21 typedef Pool
<ServerNetworkAdminSocketHandler
, AdminIndex
, 2, MAX_ADMINS
, PT_NADMIN
> NetworkAdminSocketPool
;
22 extern NetworkAdminSocketPool _networkadminsocket_pool
;
24 /** Class for handling the server side of the game connection. */
25 class ServerNetworkAdminSocketHandler
: public NetworkAdminSocketPool::PoolItem
<&_networkadminsocket_pool
>, public NetworkAdminSocketHandler
, public TCPListenHandler
<ServerNetworkAdminSocketHandler
, ADMIN_PACKET_SERVER_FULL
, ADMIN_PACKET_SERVER_BANNED
> {
27 NetworkRecvStatus
Receive_ADMIN_JOIN(Packet
*p
) override
;
28 NetworkRecvStatus
Receive_ADMIN_QUIT(Packet
*p
) override
;
29 NetworkRecvStatus
Receive_ADMIN_UPDATE_FREQUENCY(Packet
*p
) override
;
30 NetworkRecvStatus
Receive_ADMIN_POLL(Packet
*p
) override
;
31 NetworkRecvStatus
Receive_ADMIN_CHAT(Packet
*p
) override
;
32 NetworkRecvStatus
Receive_ADMIN_RCON(Packet
*p
) override
;
33 NetworkRecvStatus
Receive_ADMIN_GAMESCRIPT(Packet
*p
) override
;
34 NetworkRecvStatus
Receive_ADMIN_PING(Packet
*p
) override
;
36 NetworkRecvStatus
SendProtocol();
37 NetworkRecvStatus
SendPong(uint32 d1
);
39 AdminUpdateFrequency update_frequency
[ADMIN_UPDATE_END
]; ///< Admin requested update intervals.
40 std::chrono::steady_clock::time_point connect_time
; ///< Time of connection.
41 NetworkAddress address
; ///< Address of the admin.
43 ServerNetworkAdminSocketHandler(SOCKET s
);
44 ~ServerNetworkAdminSocketHandler();
46 NetworkRecvStatus
SendError(NetworkErrorCode error
);
47 NetworkRecvStatus
SendWelcome();
48 NetworkRecvStatus
SendNewGame();
49 NetworkRecvStatus
SendShutdown();
51 NetworkRecvStatus
SendDate();
52 NetworkRecvStatus
SendClientJoin(ClientID client_id
);
53 NetworkRecvStatus
SendClientInfo(const NetworkClientSocket
*cs
, const NetworkClientInfo
*ci
);
54 NetworkRecvStatus
SendClientUpdate(const NetworkClientInfo
*ci
);
55 NetworkRecvStatus
SendClientQuit(ClientID client_id
);
56 NetworkRecvStatus
SendClientError(ClientID client_id
, NetworkErrorCode error
);
57 NetworkRecvStatus
SendCompanyNew(CompanyID company_id
);
58 NetworkRecvStatus
SendCompanyInfo(const Company
*c
);
59 NetworkRecvStatus
SendCompanyUpdate(const Company
*c
);
60 NetworkRecvStatus
SendCompanyRemove(CompanyID company_id
, AdminCompanyRemoveReason bcrr
);
61 NetworkRecvStatus
SendCompanyEconomy();
62 NetworkRecvStatus
SendCompanyStats();
64 NetworkRecvStatus
SendChat(NetworkAction action
, DestType desttype
, ClientID client_id
, const char *msg
, int64 data
);
65 NetworkRecvStatus
SendRcon(uint16 colour
, const char *command
);
66 NetworkRecvStatus
SendConsole(const char *origin
, const char *command
);
67 NetworkRecvStatus
SendGameScript(const char *json
);
68 NetworkRecvStatus
SendCmdNames();
69 NetworkRecvStatus
SendCmdLogging(ClientID client_id
, const CommandPacket
*cp
);
70 NetworkRecvStatus
SendRconEnd(const char *command
);
73 static void AcceptConnection(SOCKET s
, const NetworkAddress
&address
);
74 static bool AllowConnection();
75 static void WelcomeAll();
78 * Get the name used by the listener.
79 * @return the name to show in debug logs and the like.
81 static const char *GetName()
86 struct ServerNetworkAdminSocketHandlerFilter
{
87 bool operator() (size_t index
) { return ServerNetworkAdminSocketHandler::Get(index
)->GetAdminStatus() == ADMIN_STATUS_ACTIVE
; }
91 * Returns an iterable ensemble of all active admin sockets
92 * @param from index of the first socket to consider
93 * @return an iterable ensemble of all active admin sockets
95 static Pool::IterateWrapperFiltered
<ServerNetworkAdminSocketHandler
, ServerNetworkAdminSocketHandlerFilter
> IterateActive(size_t from
= 0)
97 return Pool::IterateWrapperFiltered
<ServerNetworkAdminSocketHandler
, ServerNetworkAdminSocketHandlerFilter
>(from
, ServerNetworkAdminSocketHandlerFilter
{});
101 void NetworkAdminClientInfo(const NetworkClientSocket
*cs
, bool new_client
= false);
102 void NetworkAdminClientUpdate(const NetworkClientInfo
*ci
);
103 void NetworkAdminClientQuit(ClientID client_id
);
104 void NetworkAdminClientError(ClientID client_id
, NetworkErrorCode error_code
);
105 void NetworkAdminCompanyInfo(const Company
*company
, bool new_company
);
106 void NetworkAdminCompanyUpdate(const Company
*company
);
107 void NetworkAdminCompanyRemove(CompanyID company_id
, AdminCompanyRemoveReason bcrr
);
109 void NetworkAdminChat(NetworkAction action
, DestType desttype
, ClientID client_id
, const char *msg
, int64 data
= 0, bool from_admin
= false);
110 void NetworkAdminUpdate(AdminUpdateFrequency freq
);
111 void NetworkServerSendAdminRcon(AdminIndex admin_index
, TextColour colour_code
, const char *string
);
112 void NetworkAdminConsole(const char *origin
, const char *string
);
113 void NetworkAdminGameScript(const char *json
);
114 void NetworkAdminCmdLogging(const NetworkClientSocket
*owner
, const CommandPacket
*cp
);
116 #endif /* NETWORK_ADMIN_H */