OpenTTD Source 20260421-master-gc2fbc6fdeb
network_admin.h
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NETWORK_ADMIN_H
11#define NETWORK_ADMIN_H
12
13#include "network_internal.h"
14#include "core/tcp_listen.h"
15#include "core/tcp_admin.h"
16
18
24
26class ServerNetworkAdminSocketHandler : public NetworkAdminSocketPool::PoolItem<&_networkadminsocket_pool>, public NetworkAdminSocketHandler, public TCPListenHandler<ServerNetworkAdminSocketHandler, PacketAdminType, PacketAdminType::ServerFull, PacketAdminType::ServerBanned> {
27private:
28 std::unique_ptr<NetworkAuthenticationServerHandler> authentication_handler = nullptr;
29protected:
30 NetworkRecvStatus ReceiveAdminJoin(Packet &p) override;
31 NetworkRecvStatus ReceiveAdminQuit(Packet &p) override;
33 NetworkRecvStatus ReceiveAdminPoll(Packet &p) override;
34 NetworkRecvStatus ReceiveAdminChat(Packet &p) override;
37 NetworkRecvStatus ReceiveAdminGameScript(Packet &p) override;
38 NetworkRecvStatus ReceiveAdminPing(Packet &p) override;
39 NetworkRecvStatus ReceiveAdminJoinSecure(Packet &p) override;
41
43 NetworkRecvStatus SendPong(uint32_t d1);
46public:
47 std::array<AdminUpdateFrequencies, ADMIN_UPDATE_END> update_frequency{};
48 std::chrono::steady_clock::time_point connect_time{};
50
53
58
65 NetworkRecvStatus SendCompanyNew(CompanyID company_id);
71
72 NetworkRecvStatus SendChat(NetworkAction action, NetworkChatDestinationType desttype, ClientID client_id, std::string_view msg, int64_t data);
73 NetworkRecvStatus SendRcon(uint16_t colour, std::string_view command);
74 NetworkRecvStatus SendConsole(std::string_view origin, std::string_view command);
75 NetworkRecvStatus SendGameScript(std::string_view json);
78 NetworkRecvStatus SendRconEnd(std::string_view command);
79
80 static void Send();
81 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
82 static bool AllowConnection();
83 static void WelcomeAll();
84
89 static std::string_view GetName()
90 {
91 return "admin";
92 }
93
103
113};
114
115void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client = false);
117void NetworkAdminClientQuit(ClientID client_id);
118void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code);
119void NetworkAdminCompanyNew(const Company *company);
120void NetworkAdminCompanyUpdate(const Company *company);
121void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr);
122
123void NetworkAdminChat(NetworkAction action, NetworkChatDestinationType desttype, ClientID client_id, std::string_view msg, int64_t data = 0, bool from_admin = false);
125void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string);
126void NetworkAdminConsole(std::string_view origin, std::string_view string);
127void NetworkAdminGameScript(std::string_view json);
128void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp);
129
130#endif /* NETWORK_ADMIN_H */
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkAdminSocketHandler(SOCKET s)
Create the admin handler for the given socket.
Definition tcp_admin.h:550
Class for handling the server side of the game connection.
static void Send()
Send the packets for the server sockets.
NetworkRecvStatus SendGameScript(std::string_view json)
Send GameScript JSON output.
NetworkRecvStatus SendPong(uint32_t d1)
Send ping-reply (pong) to admin.
NetworkRecvStatus SendDate()
Tell the admin the date.
~ServerNetworkAdminSocketHandler() override
Clear everything related to this admin.
NetworkRecvStatus SendChat(NetworkAction action, NetworkChatDestinationType desttype, ClientID client_id, std::string_view msg, int64_t data)
Send a chat message.
NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci)
Send an update for some client's information.
NetworkRecvStatus ReceiveAdminAuthenticationResponse(Packet &p) override
Admin responds to PacketAdminType::ServerAuthenticationRequest with the appropriate data given the ag...
NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
Send an initial set of data from some client's information.
NetworkRecvStatus SendRconEnd(std::string_view command)
Send a notification indicating the rcon command has completed.
NetworkRecvStatus SendNewGame()
Tell the admin we started a new game.
NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Tell the admin that a company got removed.
NetworkRecvStatus ReceiveAdminGameScript(Packet &p) override
Send a JSON string to the current active GameScript.
std::array< AdminUpdateFrequencies, ADMIN_UPDATE_END > update_frequency
Admin requested update intervals.
NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error)
Tell the admin that a client made an error.
NetworkRecvStatus ReceiveAdminQuit(Packet &p) override
Notification to the server that this admin is quitting.
NetworkRecvStatus SendCmdNames()
Send the names of the commands.
NetworkRecvStatus ReceiveAdminExternalChat(Packet &p) override
Send chat from the external source: string Name of the source this message came from.
NetworkRecvStatus ReceiveAdminPoll(Packet &p) override
Poll the server for certain updates, an invalid poll (e.g.
NetworkRecvStatus SendShutdown()
Tell the admin we're shutting down.
NetworkRecvStatus SendCompanyStats()
Send statistics about the companies.
NetworkRecvStatus ReceiveAdminUpdateFrequency(Packet &p) override
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16_t Up...
NetworkRecvStatus SendWelcome()
Send a welcome message to the admin.
NetworkRecvStatus SendCompanyInfo(const Company *c)
Send the admin some information about a company.
static void WelcomeAll()
Send a Welcome packet to all connected admins.
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the acceptance of a connection.
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket &cp)
Send a command for logging purposes.
NetworkRecvStatus SendEnableEncryption()
Send the client the message to enable encryption.
std::unique_ptr< NetworkAuthenticationServerHandler > authentication_handler
The handler for the authentication.
static bool AllowConnection()
Whether a connection is allowed or not at this moment.
NetworkRecvStatus ReceiveAdminJoinSecure(Packet &p) override
Join the admin network using a secure authentication method: string Name of the application being use...
static Pool::IterateWrapperFiltered< ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
NetworkRecvStatus SendRcon(uint16_t colour, std::string_view command)
Send the reply of an rcon command.
NetworkRecvStatus SendConsole(std::string_view origin, std::string_view command)
Send console output of other clients.
NetworkRecvStatus ReceiveAdminJoin(Packet &p) override
Join the admin network using an unsecured password exchange: string Unsecured password the server is ...
NetworkRecvStatus SendCompanyEconomy()
Send economic information of all companies.
NetworkRecvStatus SendCompanyUpdate(const Company *c)
Send an update about a company.
NetworkRecvStatus ReceiveAdminRemoteConsoleCommand(Packet &p) override
Execute a command on the servers console: string Command to be executed.
NetworkRecvStatus SendAuthRequest()
Send the client a request to authenticate.
NetworkAddress address
Address of the admin.
ServerNetworkAdminSocketHandler(AdminID index, SOCKET s)
Sanity check.
NetworkRecvStatus SendClientQuit(ClientID client_id)
Tell the admin that a client quit.
NetworkRecvStatus SendCompanyNew(CompanyID company_id)
Tell the admin that a new company was founded.
NetworkRecvStatus SendProtocol()
Send the protocol version to the admin.
NetworkRecvStatus SendClientJoin(ClientID client_id)
Tell the admin that a client joined.
std::chrono::steady_clock::time_point connect_time
Time of connection.
NetworkRecvStatus ReceiveAdminChat(Packet &p) override
Send chat as the server: uint8_t Action such as NetworkAction::ChatClient (see NetworkAction).
NetworkRecvStatus ReceiveAdminPing(Packet &p) override
Ping the server, requiring the server to reply with a pong packet.
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the admin.
static std::string_view GetName()
Get the name used by the listener.
Template for TCP listeners.
Definition tcp_listen.h:29
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:21
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket")
The pool with sockets/clients.
AdminID _redirect_console_to_admin
Redirection of the (remote) console to the admin.
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update).
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update).
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp)
Distribute CommandPacket details over the admin network for logging purposes.
void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string)
Pass the rcon reply to the admin.
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code)
Notify the admin network of a client error (if they have opt in for the respective update).
Pool< ServerNetworkAdminSocketHandler, AdminID, 2, PoolType::NetworkAdmin > NetworkAdminSocketPool
Pool type for admin connections.
void NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
void NetworkAdminGameScript(std::string_view json)
Send GameScript JSON to the admin network (if they did opt in for the respective update).
void NetworkAdminCompanyNew(const Company *company)
Notify the admin network of a new company.
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client=false)
Notify the admin network of a new client (if they did opt in for the respective update).
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Notify the admin network of a company to be removed (including the reason why).
void NetworkAdminChat(NetworkAction action, NetworkChatDestinationType desttype, ClientID client_id, std::string_view msg, int64_t data=0, bool from_admin=false)
Send chat to the admin network (if they did opt in for the respective update).
Variables and function used internally.
class ServerNetworkGameSocketHandler NetworkClientSocket
Class for handling the server side of the game connection.
PoolID< uint8_t, struct AdminIDTag, 16, 0xFF > AdminID
Indices into the admin tables.
NetworkErrorCode
The error codes we send around in the protocols.
NetworkAction
Actions that can be used for NetworkTextMessage.
NetworkChatDestinationType
Destination of our chat messages.
ClientID
'Unique' identifier to be given to clients
Everything we need to know about a command to be able to execute it.
Container for all information known about a client.
Base class for all pools.
bool operator()(size_t index)
Check whether the given admin is active.
Basic functions to receive and send TCP packets to and from the admin network.
AdminCompanyRemoveReason
Reasons for removing a company - communicated to admins.
Definition tcp_admin.h:108
AdminUpdateFrequency
Update frequencies an admin can register.
Definition tcp_admin.h:96
@ ADMIN_STATUS_ACTIVE
The admin is active.
Definition tcp_admin.h:76
Basic functions to listen for TCP connections.