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" 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);
239 p->
Send_string(cs ==
nullptr ?
"" : const_cast<NetworkAddress &>(cs->client_address).GetHostname());
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();
char admin_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the admin.
The admin would like to have console messages.
Owner
Enum for all companies/owners.
static const byte NETWORK_GAME_ADMIN_VERSION
What version of the admin network do we use?
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
static uint MapSizeX()
Get the size of the map along the X.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
const char * GetCommandName(uint32 cmd)
static uint MapSizeY()
Get the size of the map along the Y.
Container for all information known about a client.
NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci)
Send an update for some client's information.
uint32 _realtime_tick
The real time in the game.
Internal entity of a packet.
The server replies to a ping request from the admin.
AdminCompanyRemoveReason
Reasons for removing a company - communicated to admins.
byte landscape
the landscape we're currently in
static void WelcomeAll()
Send a Welcome packet to all connected admins.
The admin would like to have chat messages.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
The server gives the admin information from the GameScript in JSON.
char server_name[NETWORK_NAME_LENGTH]
name of the server
static Titem * Get(size_t index)
Returns Titem with given index.
The server tells the admin its going to start a new game.
void NetworkAdminGameScript(const char *json)
Send GameScript JSON to the admin network (if they did opt in for the respective update).
void Send_string(const char *data)
Sends a string over the network.
Year inaugurated_year
Year of starting the company.
The server tells the admin its shutting down.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
NetworkErrorCode
The error codes we send around in the protocols.
The server received a chat message and relays it.
The server tells the admin that a new company has started.
The server tells the admin its protocol version.
ClientID client_id
Client identifier (same as ClientState->client_id)
The admin would like a list of all DoCommand names.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
NetworkRecvStatus SendWelcome()
Send a welcome message to the admin.
The server tells the admin that a client quit.
static void Send()
Send the packets for the server sockets.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Tindex index
Index of this pool item.
byte months_of_bankruptcy
Number of months that the company is unable to pay its debts.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
The server tells the admin an error has occurred.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
#define lastof(x)
Get the last element of an fixed size array.
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the acception of a connection.
Updates about the information of clients.
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
char admin_version[NETWORK_REVISION_LENGTH]
Version string of the admin.
Base core network types and some helper functions to access them.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
const char * GetNetworkRevisionString()
Get the network version string used by this build.
uint32 realtime_connect
Time of connection.
ClientID
'Unique' identifier to be given to clients
The admin gets information about this on a daily basis.
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including '\0'.
The server tells the admin what the current game date is.
The admin would like to have DoCommand information.
AdminUpdateFrequency
Update frequencies an admin can register.
NetworkRecvStatus SendConsole(const char *origin, const char *command)
Send console output of other clients.
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
The admin gets information about this on a monthly basis.
bool _network_dedicated
are we a dedicated server?
NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Tell the admin that a company got removed.
The server gives the admin some statistics about a company.
uint16 num_vehicle[NETWORK_VEH_END]
How many vehicles are there of this type?
static const size_t MAX_SIZE
Make template parameter accessible from outside.
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). ...
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
NetworkSettings network
settings related to the network
NetworkRecvStatus SendDate()
Tell the admin the date.
NetworkRecvStatus Receive_ADMIN_QUIT(Packet *p) override
Notification to the server that this admin is quitting.
CompanyID client_playas
As which company is this client playing (CompanyID)
static const uint NETWORK_RCONCOMMAND_LENGTH
The maximum length of a rconsole command, in bytes including '\0'.
AdminStatus status
Status of this admin.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
The server's reply to a remove console command.
NetworkRecvStatus SendCompanyEconomy()
Send economic information of all companies.
char client_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the client.
Updates about the statistics of companies.
static Pool::IterateWrapper< ServerNetworkAdminSocketHandler > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
Server part of the network protocol.
The server gives the admin an information update on a client.
NetworkRecvStatus SendRcon(uint16 colour, const char *command)
Send the reply of an rcon command.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Updates about the economy of companies.
NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
Send an initial set of data from some client's information.
The admin is not connected nor active.
AdminUpdateFrequency update_frequency[ADMIN_UPDATE_END]
Admin requested update intervals.
NetworkRecvStatus SendClientQuit(ClientID client_id)
Tell the admin that a client quit.
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)...
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
~ServerNetworkAdminSocketHandler()
Clear everything related to this admin.
ClientID _network_own_client_id
Our client identifier.
ClientSettings _settings_client
The current settings for this game.
NetworkRecvStatus SendRconEnd(const char *command)
Send a notification indicating the rcon command has completed.
NetworkRecvStatus SendClientJoin(ClientID client_id)
Tell the admin that a client joined.
NetworkRecvStatus Receive_ADMIN_JOIN(Packet *p) override
Join the admin network: string Password the server is expecting for this network. ...
The admin gets information about this on a quarterly basis.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
NetworkRecvStatus Receive_ADMIN_PING(Packet *p) override
Ping the server, requiring the server to reply with a pong packet.
The admin would like to have gamescript messages.
NetworkRecvStatus SendNewGame()
Tell the admin we started a new game.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
NetworkRecvStatus SendCmdNames()
Send the names of the commands.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
The server indicates that the remote console command has completed.
NetworkRecvStatus SendCompanyUpdate(const Company *c)
Send an update about a company.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Updates about the date of the game.
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).
PacketSize size
The size of the whole packet for received packets.
NetworkRecvStatus SendCompanyInfo(const Company *c)
Send the admin some information about a company.
CompanyID company
company that is executing the command
byte _network_admins_connected
The amount of admins connected.
#define lengthof(x)
Return the length of an fixed size array.
NetworkServerGameInfo _network_game_info
Information about our game.
NetworkRecvStatus Receive_ADMIN_RCON(Packet *p) override
Execute a command on the servers console: string Command to be executed.
static T min(const T a, const T b)
Returns the minimum of two values.
static bool AllowConnection()
Whether a connection is allowed or not at this moment.
The server tells the admin that a company was removed.
static const AdminIndex MAX_ADMINS
Maximum number of allowed admins.
The server sends out the names of the DoCommands to the admins.
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).
uint32 frame
the frame in which this packet is executed
uint32 StringID
Numeric value that represents a string, independent of the selected language.
The server tells the admin that a client caused an error.
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.
The server gives the admin the data that got printed to its console.
uint8 AdminIndex
Indices into the admin tables.
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...
Date join_date
Gamedate the client has joined.
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Notify the admin network of a company to be removed (including the reason why).
Base class for all pools.
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the admin.
#define DEBUG(name, level,...)
Output a line of debugging information.
The server gives the admin an information update on a company.
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
assert_compile(lengthof(_admin_update_type_frequencies)==ADMIN_UPDATE_END)
Sanity check.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Owner share_owners[4]
Owners of the 4 shares of the company. INVALID_OWNER if nobody has bought them yet.
NetworkRecvStatus SendProtocol()
Send the protocol version to the admin.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
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.
uint32 generation_seed
noise seed for world generation
NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error)
Tell the admin that a client made an error.
The admin gets information about this on a yearly basis.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
static const AdminUpdateFrequency _admin_update_type_frequencies[]
Frequencies, which may be registered for a certain update type.
Must ALWAYS be on the end of this list!! (period)
The server tells the admin that a client has joined.
The admin gets information about this on a weekly basis.
byte colour
Company colour.
NetworkRecvStatus SendGameScript(const char *json)
Send GameScript JSON output.
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including '\0'.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
ServerNetworkAdminSocketHandler(SOCKET s)
Create a new socket for the server side of the admin network.
uint16 num_station[NETWORK_VEH_END]
How many stations are there of this type?
Class for handling the server side of the game connection.
Must ALWAYS be on the end of this list!! (period)
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
Notify the admin network of company details.
Maximum number of companies.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
The server gives the admin information about a client.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
char admin_password[NETWORK_PASSWORD_LENGTH]
password for the admin network
Main socket handler for admin related connections.
NetworkRecvStatus SendCompanyStats()
Send statistics about the companies.
TileIndex tile
tile command being executed on.
The server welcomes the admin to a game.
NetworkRecvStatus Receive_ADMIN_POLL(Packet *p) override
Poll the server for certain updates, an invalid poll (e.g.
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data)
Send a chat message.
Everything we need to know about a command to be able to execute it.
NetworkRecvStatus SendShutdown()
Tell the admin we're shutting down.
The admin gets information about this when it changes.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Simple calculated statistics of a company.
static const uint NETWORK_PASSWORD_LENGTH
The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH) ...
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update)...
The server gives the admin copies of incoming command packets.
Servers always have this ID.
GameCreationSettings game_creation
settings used during the creation of a game (map)
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
void Send_bool(bool data)
Package a boolean in the packet.
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp)
Send a command for logging purposes.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket")
The pool with sockets/clients.
Updates about the generic information of companies.
The server gives the admin some economy related company information.
Date _date
Current date in days (day counter)
NetworkRecvStatus SendCompanyNew(CompanyID company_id)
Tell the admin that a new company was founded.
AdminUpdateType
Update types an admin can register a frequency for.
NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet *p) override
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16 Upda...
NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p) override
Send chat as the server: uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see NetworkAction).
Year starting_year
starting date
uint32 cmd
command being executed.
DestType
Destination of our chat messages.
byte client_lang
The language of the client.
static const int ADMIN_AUTHORISATION_TIMEOUT
The timeout for authorisation of the client.
NetworkAction
Actions that can be used for NetworkTextMessage.
NetworkRecvStatus SendPong(uint32 d1)
Send ping-reply (pong) to admin.
NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p) override
Send a JSON string to the current active GameScript.
NetworkAddress address
Address of the admin.
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.
Server part of the admin network protocol.
The server gives the admin information about a company.
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.