OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../date_func.h"
16 #include "../command_func.h"
17 #include "../company_base.h"
18 #include "../console_func.h"
19 #include "../core/pool_func.hpp"
20 #include "../map_func.h"
22 #include "../game/game.hpp"
24 #include "../safeguards.h"
99 DEBUG(net, 1,
"[admin] Admin did not send its authorisation within %d seconds", (uint32)std::chrono::duration_cast<std::chrono::seconds>(
ADMIN_AUTHORISATION_TIMEOUT).count());
100 as->CloseConnection(
true);
137 GetString(str, strid,
lastof(str));
139 DEBUG(net, 1,
"[admin] the admin '%s' (%s) made an error and has been disconnected. Reason: '%s'", this->
admin_name, this->
admin_version, str);
322 GetString(company_name, STR_COMPANY_NAME,
lastof(company_name));
325 GetString(manager_name, STR_PRESIDENT_NAME,
lastof(manager_name));
358 GetString(company_name, STR_COMPANY_NAME,
lastof(company_name));
361 GetString(manager_name, STR_PRESIDENT_NAME,
lastof(manager_name));
404 for (uint i = 0; i <
lengthof(company->yearly_expenses[0]); i++) {
405 income -= company->yearly_expenses[0][i];
419 for (uint i = 0; i < 2; i++) {
420 p->
Send_uint64(company->old_economy[i].company_value);
421 p->
Send_uint16(company->old_economy[i].performance_history);
446 for (uint i = 0; i < NETWORK_VEH_END; i++) {
450 for (uint i = 0; i < NETWORK_VEH_END; i++) {
610 for (uint i = 0; i <
CMD_END; i++) {
672 return this->
SendError(NETWORK_ERROR_WRONG_PASSWORD);
680 return this->
SendError(NETWORK_ERROR_ILLEGAL_PACKET);
706 return this->
SendError(NETWORK_ERROR_ILLEGAL_PACKET);
729 if (d1 == UINT32_MAX) {
731 for (
const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
738 const NetworkClientSocket *cs = NetworkClientSocket::GetByClientID((
ClientID)d1);
746 if (d1 == UINT32_MAX) {
774 return this->
SendError(NETWORK_ERROR_ILLEGAL_PACKET);
792 case NETWORK_ACTION_CHAT:
793 case NETWORK_ACTION_CHAT_CLIENT:
794 case NETWORK_ACTION_CHAT_COMPANY:
795 case NETWORK_ACTION_SERVER_MESSAGE:
801 return this->
SendError(NETWORK_ERROR_ILLEGAL_PACKET);
820 as->SendClientInfo(cs, cs->GetInfo());
822 as->SendClientJoin(cs->client_id);
836 as->SendClientUpdate(ci);
849 as->SendClientQuit(client_id);
863 as->SendClientError(client_id, error_code);
875 if (company ==
nullptr) {
876 DEBUG(net, 1,
"[admin] Empty company given for update");
883 as->SendCompanyInfo(company);
885 as->SendCompanyNew(company->
index);
896 if (company ==
nullptr)
return;
901 as->SendCompanyUpdate(company);
913 as->SendCompanyRemove(company_id, bcrr);
923 if (from_admin)
return;
927 as->SendChat(action, desttype, client_id, msg, data);
952 as->SendConsole(origin,
string);
965 as->SendGameScript(json);
981 as->SendCmdLogging(client_id, cp);
1004 if (as->update_frequency[i] & freq) {
1012 as->SendCompanyEconomy();
1016 as->SendCompanyStats();
1019 default: NOT_REACHED();
uint16 num_station[NETWORK_VEH_END]
How many stations are there of this type?
@ ADMIN_PACKET_SERVER_GAMESCRIPT
The server gives the admin information from the GameScript in JSON.
NetworkRecvStatus SendGameScript(const char *json)
Send GameScript JSON output.
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Simple calculated statistics of a company.
uint32 cmd
command being executed.
DestType
Destination of our chat messages.
@ ADMIN_UPDATE_CMD_NAMES
The admin would like a list of all DoCommand names.
@ ADMIN_UPDATE_COMPANY_INFO
Updates about the generic information of companies.
NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet *p) override
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16 Upda...
uint32 generation_seed
noise seed for world generation
NetworkRecvStatus SendConsole(const char *origin, const char *command)
Send console output of other clients.
static Titem * Get(size_t index)
Returns Titem with given index.
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
AdminStatus status
Status of this admin.
NetworkServerGameInfo _network_game_info
Information about our game.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
byte landscape
the landscape we're currently in
@ ADMIN_PACKET_SERVER_CLIENT_QUIT
The server tells the admin that a client quit.
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the admin.
@ ADMIN_UPDATE_CLIENT_INFO
Updates about the information of clients.
@ ADMIN_PACKET_SERVER_COMPANY_INFO
The server gives the admin information about a company.
CompanyID client_playas
As which company is this client playing (CompanyID)
NetworkRecvStatus SendCompanyEconomy()
Send economic information of all companies.
Year inaugurated_year
Year of starting the company.
AdminUpdateFrequency update_frequency[ADMIN_UPDATE_END]
Admin requested update intervals.
@ ADMIN_UPDATE_CONSOLE
The admin would like to have console messages.
@ ADMIN_PACKET_SERVER_PONG
The server replies to a ping request from the admin.
const char * GetCommandName(uint32 cmd)
NetworkAction
Actions that can be used for NetworkTextMessage.
@ ADMIN_UPDATE_CHAT
The admin would like to have chat messages.
char client_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the client.
uint32 frame
the frame in which this packet is executed
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data, bool from_admin)
Send chat to the admin network (if they did opt in for the respective update).
Tindex index
Index of this pool item.
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
static void WelcomeAll()
Send a Welcome packet to all connected admins.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
NetworkRecvStatus Receive_ADMIN_JOIN(Packet *p) override
Join the admin network: string Password the server is expecting for this network.
@ ADMIN_PACKET_SERVER_NEWGAME
The server tells the admin its going to start a new game.
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
Date join_date
Gamedate the client has joined.
@ ADMIN_FREQUENCY_DAILY
The admin gets information about this on a daily basis.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
static Pool::IterateWrapperFiltered< ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
@ ADMIN_PACKET_SERVER_CHAT
The server received a chat message and relays it.
@ ADMIN_PACKET_SERVER_COMPANY_NEW
The server tells the admin that a new company has started.
NetworkRecvStatus SendCompanyUpdate(const Company *c)
Send an update about a company.
@ ADMIN_PACKET_SERVER_PROTOCOL
The server tells the admin its protocol version.
@ ADMIN_UPDATE_CMD_LOGGING
The admin would like to have DoCommand information.
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including '\0'.
ClientSettings _settings_client
The current settings for this game.
@ ADMIN_FREQUENCY_MONTHLY
The admin gets information about this on a monthly basis.
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
@ ADMIN_PACKET_SERVER_COMPANY_STATS
The server gives the admin some statistics about a company.
void NetworkAdminGameScript(const char *json)
Send GameScript JSON to the admin network (if they did opt in for the respective update).
char admin_password[NETWORK_PASSWORD_LENGTH]
password for the admin network
Owner
Enum for all companies/owners.
static const byte NETWORK_GAME_ADMIN_VERSION
What version of the admin network do we use?
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
NetworkRecvStatus SendCompanyInfo(const Company *c)
Send the admin some information about a company.
NetworkAddress address
Address of the admin.
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update).
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update).
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket")
The pool with sockets/clients.
static void Send()
Send the packets for the server sockets.
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
Notify the admin network of a new client (if they did opt in for the respective update).
GameCreationSettings game_creation
settings used during the creation of a game (map)
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
static uint MapSizeX()
Get the size of the map along the X.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
@ ADMIN_FREQUENCY_QUARTERLY
The admin gets information about this on a quarterly basis.
CompanyID company
company that is executing the command
byte client_lang
The language of the client.
@ ADMIN_STATUS_INACTIVE
The admin is not connected nor active.
@ ADMIN_PACKET_SERVER_DATE
The server tells the admin what the current game date is.
@ ADMIN_PACKET_SERVER_ERROR
The server tells the admin an error has occurred.
NetworkRecvStatus SendShutdown()
Tell the admin we're shutting down.
uint16 num_vehicle[NETWORK_VEH_END]
How many vehicles are there of this type?
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
byte colour
Company colour.
char admin_version[NETWORK_REVISION_LENGTH]
Version string of the admin.
std::chrono::steady_clock::time_point connect_time
Time of connection.
Everything we need to know about a command to be able to execute it.
Date _date
Current date in days (day counter)
static const size_t MAX_SIZE
Make template parameter accessible from outside.
static const AdminIndex MAX_ADMINS
Maximum number of allowed admins.
#define DEBUG(name, level,...)
Output a line of debugging information.
void Send_string(const char *data)
Sends a string over the network.
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
byte months_of_bankruptcy
Number of months that the company is unable to pay its debts.
NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Tell the admin that a company got removed.
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
ClientID
'Unique' identifier to be given to clients
PacketSize size
The size of the whole packet for received packets.
Main socket handler for admin related connections.
@ ADMIN_PACKET_SERVER_COMPANY_REMOVE
The server tells the admin that a company was removed.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
@ ADMIN_FREQUENCY_POLL
The admin can poll this.
@ ADMIN_PACKET_SERVER_CLIENT_ERROR
The server tells the admin that a client caused an error.
@ ADMIN_UPDATE_DATE
Updates about the date of the game.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static bool AllowConnection()
Whether a connection is allowed or not at this moment.
NetworkRecvStatus SendWelcome()
Send a welcome message to the admin.
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
@ MAX_COMPANIES
Maximum number of companies.
NetworkRecvStatus SendRcon(uint16 colour, const char *command)
Send the reply of an rcon command.
@ ADMIN_UPDATE_COMPANY_STATS
Updates about the statistics of companies.
@ ADMIN_UPDATE_COMPANY_ECONOMY
Updates about the economy of companies.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
@ CLIENT_ID_SERVER
Servers always have this ID.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
~ServerNetworkAdminSocketHandler()
Clear everything related to this admin.
@ ADMIN_PACKET_SERVER_RCON
The server's reply to a remove console command.
NetworkRecvStatus SendClientQuit(ClientID client_id)
Tell the admin that a client quit.
AdminCompanyRemoveReason
Reasons for removing a company - communicated to admins.
@ ADMIN_PACKET_SERVER_CONSOLE
The server gives the admin the data that got printed to its console.
@ ADMIN_PACKET_SERVER_CLIENT_UPDATE
The server gives the admin an information update on a client.
static uint MapSizeY()
Get the size of the map along the Y.
char server_name[NETWORK_NAME_LENGTH]
name of the server
Owner share_owners[4]
Owners of the 4 shares of the company. INVALID_OWNER if nobody has bought them yet.
bool _network_dedicated
are we a dedicated server?
@ ADMIN_UPDATE_GAMESCRIPT
The admin would like to have gamescript messages.
Internal entity of a packet.
@ ADMIN_FREQUENCY_ANUALLY
The admin gets information about this on a yearly basis.
NetworkRecvStatus SendCmdNames()
Send the names of the commands.
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Notify the admin network of a company to be removed (including the reason why).
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
AdminUpdateType
Update types an admin can register a frequency for.
ClientID client_id
Client identifier (same as ClientState->client_id)
@ ADMIN_PACKET_SERVER_RCON_END
The server indicates that the remote console command has completed.
static const uint NETWORK_PASSWORD_LENGTH
The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
NetworkRecvStatus SendNewGame()
Tell the admin we started a new game.
void Send_bool(bool data)
Package a boolean in the packet.
ClientID _network_own_client_id
Our client identifier.
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
@ ADMIN_PACKET_SERVER_CLIENT_JOIN
The server tells the admin that a client has joined.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
static const uint NETWORK_RCONCOMMAND_LENGTH
The maximum length of a rconsole command, in bytes including '\0'.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
NetworkRecvStatus SendDate()
Tell the admin the date.
NetworkRecvStatus SendCompanyNew(CompanyID company_id)
Tell the admin that a new company was founded.
Base class for all pools.
@ ADMIN_PACKET_SERVER_CMD_NAMES
The server sends out the names of the DoCommands to the admins.
NetworkRecvStatus Receive_ADMIN_QUIT(Packet *p) override
Notification to the server that this admin is quitting.
@ ADMIN_PACKET_SERVER_CLIENT_INFO
The server gives the admin information about a client.
@ CMD_ID_MASK
mask for the command ID
NetworkRecvStatus SendPong(uint32 d1)
Send ping-reply (pong) to admin.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
ServerNetworkAdminSocketHandler(SOCKET s)
Sanity check.
Class for handling the server side of the game connection.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
NetworkRecvStatus Receive_ADMIN_POLL(Packet *p) override
Poll the server for certain updates, an invalid poll (e.g.
NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
Send an initial set of data from some client's information.
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id, int64 data=0, bool from_admin=false)
Send an actual chat message.
@ ADMIN_FREQUENCY_AUTOMATIC
The admin gets information about this when it changes.
@ ADMIN_STATUS_ACTIVE
The admin is active.
@ ADMIN_PACKET_SERVER_COMPANY_UPDATE
The server gives the admin an information update on a company.
NetworkRecvStatus SendProtocol()
Send the protocol version to the admin.
NetworkRecvStatus SendRconEnd(const char *command)
Send a notification indicating the rcon command has completed.
NetworkRecvStatus Receive_ADMIN_PING(Packet *p) override
Ping the server, requiring the server to reply with a pong packet.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
@ ADMIN_PACKET_SERVER_WELCOME
The server welcomes the admin to a game.
char admin_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the admin.
Year starting_year
starting date
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the acception of a connection.
NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci)
Send an update for some client's information.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
@ ADMIN_PACKET_SERVER_COMPANY_ECONOMY
The server gives the admin some economy related company information.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
@ ADMIN_PACKET_SERVER_CMD_LOGGING
The server gives the admin copies of incoming command packets.
uint8 AdminIndex
Indices into the admin tables.
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update).
#define lengthof(x)
Return the length of an fixed size array.
NetworkSettings network
settings related to the network
byte _network_admins_connected
The amount of admins connected.
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
@ ADMIN_FREQUENCY_WEEKLY
The admin gets information about this on a weekly basis.
NetworkRecvStatus Receive_ADMIN_RCON(Packet *p) override
Execute a command on the servers console: string Command to be executed.
static const AdminUpdateFrequency _admin_update_type_frequencies[]
Frequencies, which may be registered for a certain update type.
@ CMD_END
Must ALWAYS be on the end of this list!! (period)
NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p) override
Send chat as the server: uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see NetworkAction).
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p) override
Send a JSON string to the current active GameScript.
TileIndex tile
tile command being executed on.
NetworkRecvStatus SendCompanyStats()
Send statistics about the companies.
static const uint NETWORK_GAMESCRIPT_JSON_LENGTH
The maximum length of a gamescript json string, in bytes including '\0'. Must not be longer than SEND...
static const std::chrono::seconds ADMIN_AUTHORISATION_TIMEOUT(10)
The timeout for authorisation of the client.
const char * GetNetworkRevisionString()
Get the network version string used by this build.
@ ADMIN_UPDATE_END
Must ALWAYS be on the end of this list!! (period)
#define lastof(x)
Get the last element of an fixed size array.
AdminUpdateFrequency
Update frequencies an admin can register.
NetworkRecvStatus SendClientJoin(ClientID client_id)
Tell the admin that a client joined.
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data)
Send a chat message.
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
Notify the admin network of company details.
NetworkErrorCode
The error codes we send around in the protocols.
Container for all information known about a client.
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).
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.
NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error)
Tell the admin that a client made an error.
@ ADMIN_PACKET_SERVER_SHUTDOWN
The server tells the admin its shutting down.
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including '\0'.
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp)
Send a command for logging purposes.