2010-10-17 17:31:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file network_admin.cpp Server part of the admin network protocol. */
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../strings_func.h"
|
|
|
|
#include "../date_func.h"
|
|
|
|
#include "network_admin.h"
|
|
|
|
#include "network_base.h"
|
2010-12-30 18:14:37 +00:00
|
|
|
#include "network_server.h"
|
|
|
|
#include "../command_func.h"
|
2010-10-17 17:31:03 +00:00
|
|
|
#include "../company_base.h"
|
|
|
|
#include "../console_func.h"
|
|
|
|
#include "../core/pool_func.hpp"
|
|
|
|
#include "../map_func.h"
|
|
|
|
#include "../rev.h"
|
2011-12-19 21:00:32 +00:00
|
|
|
#include "../game/game.hpp"
|
2010-10-17 17:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* This file handles all the admin network commands. */
|
|
|
|
|
2010-10-17 17:41:52 +00:00
|
|
|
/** Redirection of the (remote) console to the admin. */
|
|
|
|
AdminIndex _redirect_console_to_admin = INVALID_ADMIN_ID;
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
/** The amount of admins connected. */
|
|
|
|
byte _network_admins_connected = 0;
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** The pool with sockets/clients. */
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket");
|
|
|
|
INSTANTIATE_POOL_METHODS(NetworkAdminSocket)
|
|
|
|
|
2010-10-17 17:33:46 +00:00
|
|
|
/** The timeout for authorisation of the client. */
|
|
|
|
static const int ADMIN_AUTHORISATION_TIMEOUT = 10000;
|
|
|
|
|
2010-10-17 17:36:23 +00:00
|
|
|
|
|
|
|
/** Frequencies, which may be registered for a certain update type. */
|
|
|
|
static const AdminUpdateFrequency _admin_update_type_frequencies[] = {
|
2010-10-17 17:36:59 +00:00
|
|
|
ADMIN_FREQUENCY_POLL | ADMIN_FREQUENCY_DAILY | ADMIN_FREQUENCY_WEEKLY | ADMIN_FREQUENCY_MONTHLY | ADMIN_FREQUENCY_QUARTERLY | ADMIN_FREQUENCY_ANUALLY, ///< ADMIN_UPDATE_DATE
|
2010-10-17 17:37:26 +00:00
|
|
|
ADMIN_FREQUENCY_POLL | ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CLIENT_INFO
|
2010-10-17 17:37:45 +00:00
|
|
|
ADMIN_FREQUENCY_POLL | ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_COMPANY_INFO
|
2010-10-17 17:38:16 +00:00
|
|
|
ADMIN_FREQUENCY_POLL | ADMIN_FREQUENCY_WEEKLY | ADMIN_FREQUENCY_MONTHLY | ADMIN_FREQUENCY_QUARTERLY | ADMIN_FREQUENCY_ANUALLY, ///< ADMIN_UPDATE_COMPANY_ECONOMY
|
2010-10-17 17:38:41 +00:00
|
|
|
ADMIN_FREQUENCY_POLL | ADMIN_FREQUENCY_WEEKLY | ADMIN_FREQUENCY_MONTHLY | ADMIN_FREQUENCY_QUARTERLY | ADMIN_FREQUENCY_ANUALLY, ///< ADMIN_UPDATE_COMPANY_STATS
|
2010-10-17 17:40:18 +00:00
|
|
|
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CHAT
|
2010-10-17 17:43:01 +00:00
|
|
|
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CONSOLE
|
2010-12-30 18:14:37 +00:00
|
|
|
ADMIN_FREQUENCY_POLL, ///< ADMIN_UPDATE_CMD_NAMES
|
|
|
|
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_CMD_LOGGING
|
2011-12-19 21:00:32 +00:00
|
|
|
ADMIN_FREQUENCY_AUTOMATIC, ///< ADMIN_UPDATE_GAMESCRIPT
|
2010-10-17 17:36:23 +00:00
|
|
|
};
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Sanity check. */
|
2010-10-17 17:36:23 +00:00
|
|
|
assert_compile(lengthof(_admin_update_type_frequencies) == ADMIN_UPDATE_END);
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
/**
|
|
|
|
* Create a new socket for the server side of the admin network.
|
|
|
|
* @param s The socket to connect with.
|
|
|
|
*/
|
|
|
|
ServerNetworkAdminSocketHandler::ServerNetworkAdminSocketHandler(SOCKET s) : NetworkAdminSocketHandler(s)
|
|
|
|
{
|
|
|
|
_network_admins_connected++;
|
|
|
|
this->status = ADMIN_STATUS_INACTIVE;
|
2010-10-17 17:33:46 +00:00
|
|
|
this->realtime_connect = _realtime_tick;
|
2010-10-17 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear everything related to this admin.
|
|
|
|
*/
|
|
|
|
ServerNetworkAdminSocketHandler::~ServerNetworkAdminSocketHandler()
|
|
|
|
{
|
|
|
|
_network_admins_connected--;
|
|
|
|
DEBUG(net, 1, "[admin] '%s' (%s) has disconnected", this->admin_name, this->admin_version);
|
2010-10-17 17:41:52 +00:00
|
|
|
if (_redirect_console_to_admin == this->index) _redirect_console_to_admin = INVALID_ADMIN_ID;
|
2010-10-17 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether a connection is allowed or not at this moment.
|
|
|
|
* @return Whether the connection is allowed.
|
|
|
|
*/
|
|
|
|
/* static */ bool ServerNetworkAdminSocketHandler::AllowConnection()
|
|
|
|
{
|
2011-02-15 12:11:28 +00:00
|
|
|
bool accept = !StrEmpty(_settings_client.network.admin_password) && _network_admins_connected < MAX_ADMINS;
|
|
|
|
/* We can't go over the MAX_ADMINS limit here. However, if we accept
|
|
|
|
* the connection, there has to be space in the pool. */
|
|
|
|
assert_compile(NetworkAdminSocketPool::MAX_SIZE == MAX_ADMINS);
|
|
|
|
assert(!accept || ServerNetworkAdminSocketHandler::CanAllocateItem());
|
|
|
|
return accept;
|
2010-10-17 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Send the packets for the server sockets. */
|
|
|
|
/* static */ void ServerNetworkAdminSocketHandler::Send()
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
|
|
|
FOR_ALL_ADMIN_SOCKETS(as) {
|
2010-10-17 17:33:46 +00:00
|
|
|
if (as->status == ADMIN_STATUS_INACTIVE && as->realtime_connect + ADMIN_AUTHORISATION_TIMEOUT < _realtime_tick) {
|
|
|
|
DEBUG(net, 1, "[admin] Admin did not send its authorisation within %d seconds", ADMIN_AUTHORISATION_TIMEOUT / 1000);
|
|
|
|
as->CloseConnection(true);
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-17 17:31:03 +00:00
|
|
|
if (as->writable) {
|
2010-11-30 13:38:46 +00:00
|
|
|
as->SendPackets();
|
2010-10-17 17:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Handle the acception of a connection.
|
|
|
|
* @param s The socket of the new connection.
|
|
|
|
* @param address The address of the peer.
|
|
|
|
*/
|
2010-10-17 17:31:03 +00:00
|
|
|
/* static */ void ServerNetworkAdminSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as = new ServerNetworkAdminSocketHandler(s);
|
|
|
|
as->address = address; // Save the IP of the client
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********
|
|
|
|
* Sending functions for admin network
|
|
|
|
************/
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send an error to the admin.
|
|
|
|
* @param error The error to send.
|
|
|
|
*/
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendError(NetworkErrorCode error)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_ERROR);
|
|
|
|
|
|
|
|
p->Send_uint8(error);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:31:03 +00:00
|
|
|
|
|
|
|
char str[100];
|
|
|
|
StringID strid = GetNetworkErrorMsg(error);
|
|
|
|
GetString(str, strid, lastof(str));
|
|
|
|
|
|
|
|
DEBUG(net, 1, "[admin] the admin '%s' (%s) made an error and has been disconnected. Reason: '%s'", this->admin_name, this->admin_version, str);
|
|
|
|
|
|
|
|
return this->CloseConnection(true);
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Send the protocol version to the admin. */
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendProtocol()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_PROTOCOL);
|
|
|
|
|
|
|
|
/* announce the protocol version */
|
|
|
|
p->Send_uint8(NETWORK_GAME_ADMIN_VERSION);
|
|
|
|
|
2010-10-17 17:36:23 +00:00
|
|
|
for (int i = 0; i < ADMIN_UPDATE_END; i++) {
|
|
|
|
p->Send_bool (true);
|
|
|
|
p->Send_uint16(i);
|
|
|
|
p->Send_uint16(_admin_update_type_frequencies[i]);
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
p->Send_bool(false);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:31:03 +00:00
|
|
|
|
|
|
|
return this->SendWelcome();
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Send a welcome message to the admin. */
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendWelcome()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_WELCOME);
|
|
|
|
|
|
|
|
p->Send_string(_settings_client.network.server_name);
|
|
|
|
p->Send_string(_openttd_revision);
|
|
|
|
p->Send_bool (_network_dedicated);
|
|
|
|
|
|
|
|
p->Send_string(_network_game_info.map_name);
|
|
|
|
p->Send_uint32(_settings_game.game_creation.generation_seed);
|
|
|
|
p->Send_uint8 (_settings_game.game_creation.landscape);
|
|
|
|
p->Send_uint32(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
|
|
|
|
p->Send_uint16(MapSizeX());
|
|
|
|
p->Send_uint16(MapSizeY());
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:31:03 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Tell the admin we started a new game. */
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendNewGame()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_NEWGAME);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:31:03 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Tell the admin we're shutting down. */
|
2010-10-17 17:31:03 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendShutdown()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_SHUTDOWN);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:31:03 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Tell the admin the date. */
|
2010-10-17 17:36:59 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendDate()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_DATE);
|
|
|
|
|
|
|
|
p->Send_uint32(_date);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:36:59 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Tell the admin that a client joined.
|
|
|
|
* @param client_id The client that joined.
|
|
|
|
*/
|
2010-10-17 17:37:26 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendClientJoin(ClientID client_id)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CLIENT_JOIN);
|
|
|
|
|
|
|
|
p->Send_uint32(client_id);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:26 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send an initial set of data from some client's information.
|
2011-11-04 22:32:21 +00:00
|
|
|
* @param cs The socket of the client.
|
|
|
|
* @param ci The information about the client.
|
2011-05-05 16:24:48 +00:00
|
|
|
*/
|
2011-11-04 22:32:21 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
|
2010-10-17 17:37:26 +00:00
|
|
|
{
|
2011-04-22 16:05:05 +00:00
|
|
|
/* Only send data when we're a proper client, not just someone trying to query the server. */
|
|
|
|
if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
2010-10-17 17:37:26 +00:00
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CLIENT_INFO);
|
|
|
|
|
|
|
|
p->Send_uint32(ci->client_id);
|
2011-11-04 22:32:21 +00:00
|
|
|
p->Send_string(cs == NULL ? "" : const_cast<NetworkAddress &>(cs->client_address).GetHostname());
|
2010-10-17 17:37:26 +00:00
|
|
|
p->Send_string(ci->client_name);
|
|
|
|
p->Send_uint8 (ci->client_lang);
|
|
|
|
p->Send_uint32(ci->join_date);
|
|
|
|
p->Send_uint8 (ci->client_playas);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:26 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an update for some client's information.
|
|
|
|
* @param ci The information about a client.
|
|
|
|
*/
|
2010-10-17 17:37:26 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendClientUpdate(const NetworkClientInfo *ci)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CLIENT_UPDATE);
|
|
|
|
|
|
|
|
p->Send_uint32(ci->client_id);
|
|
|
|
p->Send_string(ci->client_name);
|
|
|
|
p->Send_uint8 (ci->client_playas);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:26 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Tell the admin that a client quit.
|
|
|
|
* @param client_id The client that quit.
|
|
|
|
*/
|
2010-10-17 17:37:26 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendClientQuit(ClientID client_id)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CLIENT_QUIT);
|
|
|
|
|
|
|
|
p->Send_uint32(client_id);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:26 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Tell the admin that a client made an error.
|
|
|
|
* @param client_id The client that made the error.
|
|
|
|
* @param error The error that was made.
|
|
|
|
*/
|
2010-10-17 17:37:26 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendClientError(ClientID client_id, NetworkErrorCode error)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CLIENT_ERROR);
|
|
|
|
|
|
|
|
p->Send_uint32(client_id);
|
|
|
|
p->Send_uint8 (error);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:26 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Tell the admin that a new company was founded.
|
|
|
|
* @param company_id The company that was founded.
|
|
|
|
*/
|
2010-10-17 17:37:45 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyNew(CompanyID company_id)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_NEW);
|
|
|
|
p->Send_uint8(company_id);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:45 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send the admin some information about a company.
|
|
|
|
* @param c The company to send the information about.
|
|
|
|
*/
|
2010-10-17 17:37:45 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyInfo(const Company *c)
|
|
|
|
{
|
|
|
|
char company_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
|
char manager_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
|
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
|
|
|
|
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
|
|
|
|
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_INFO);
|
|
|
|
|
|
|
|
p->Send_uint8 (c->index);
|
|
|
|
p->Send_string(company_name);
|
|
|
|
p->Send_string(manager_name);
|
|
|
|
p->Send_uint8 (c->colour);
|
|
|
|
p->Send_bool (NetworkCompanyIsPassworded(c->index));
|
|
|
|
p->Send_uint32(c->inaugurated_year);
|
|
|
|
p->Send_bool (c->is_ai);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:45 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an update about a company.
|
|
|
|
* @param c The company to send the update of.
|
|
|
|
*/
|
2010-10-17 17:37:45 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyUpdate(const Company *c)
|
|
|
|
{
|
|
|
|
char company_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
|
char manager_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
|
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
|
|
|
|
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
|
|
|
|
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_UPDATE);
|
|
|
|
|
|
|
|
p->Send_uint8 (c->index);
|
|
|
|
p->Send_string(company_name);
|
|
|
|
p->Send_string(manager_name);
|
|
|
|
p->Send_uint8 (c->colour);
|
|
|
|
p->Send_bool (NetworkCompanyIsPassworded(c->index));
|
|
|
|
p->Send_uint8 (c->quarters_of_bankruptcy);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lengthof(c->share_owners); i++) {
|
|
|
|
p->Send_uint8(c->share_owners[i]);
|
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:45 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Tell the admin that a company got removed.
|
|
|
|
* @param company_id The company that got removed.
|
|
|
|
* @param acrr The reason for removal, e.g. bankruptcy or merger.
|
|
|
|
*/
|
2010-10-17 17:37:45 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason acrr)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_REMOVE);
|
|
|
|
|
|
|
|
p->Send_uint8(company_id);
|
|
|
|
p->Send_uint8(acrr);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:37:45 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Send economic information of all companies. */
|
2010-10-17 17:38:16 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyEconomy()
|
|
|
|
{
|
|
|
|
const Company *company;
|
|
|
|
FOR_ALL_COMPANIES(company) {
|
|
|
|
/* Get the income. */
|
|
|
|
Money income = 0;
|
|
|
|
for (uint i = 0; i < lengthof(company->yearly_expenses[0]); i++) {
|
|
|
|
income -= company->yearly_expenses[0][i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_ECONOMY);
|
|
|
|
|
|
|
|
p->Send_uint8(company->index);
|
|
|
|
|
|
|
|
/* Current information. */
|
|
|
|
p->Send_uint64(company->money);
|
|
|
|
p->Send_uint64(company->current_loan);
|
|
|
|
p->Send_uint64(income);
|
2012-01-20 20:18:19 +00:00
|
|
|
p->Send_uint16(min(UINT16_MAX, company->cur_economy.delivered_cargo.GetSum<OverflowSafeInt64>()));
|
2010-10-17 17:38:16 +00:00
|
|
|
|
|
|
|
/* Send stats for the last 2 quarters. */
|
|
|
|
for (uint i = 0; i < 2; i++) {
|
|
|
|
p->Send_uint64(company->old_economy[i].company_value);
|
|
|
|
p->Send_uint16(company->old_economy[i].performance_history);
|
2012-01-20 20:18:19 +00:00
|
|
|
p->Send_uint16(min(UINT16_MAX, company->old_economy[i].delivered_cargo.GetSum<OverflowSafeInt64>()));
|
2010-10-17 17:38:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Send statistics about the companies. */
|
2010-10-17 17:38:41 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyStats()
|
|
|
|
{
|
|
|
|
/* Fetch the latest version of the stats. */
|
|
|
|
NetworkCompanyStats company_stats[MAX_COMPANIES];
|
|
|
|
NetworkPopulateCompanyStats(company_stats);
|
|
|
|
|
|
|
|
const Company *company;
|
|
|
|
|
|
|
|
/* Go through all the companies. */
|
|
|
|
FOR_ALL_COMPANIES(company) {
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_STATS);
|
|
|
|
|
|
|
|
/* Send the information. */
|
|
|
|
p->Send_uint8(company->index);
|
|
|
|
|
|
|
|
for (uint i = 0; i < NETWORK_VEH_END; i++) {
|
2010-12-06 14:55:47 +00:00
|
|
|
p->Send_uint16(company_stats[company->index].num_vehicle[i]);
|
2010-10-17 17:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < NETWORK_VEH_END; i++) {
|
2010-12-06 14:55:47 +00:00
|
|
|
p->Send_uint16(company_stats[company->index].num_station[i]);
|
2010-10-17 17:38:41 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send a chat message.
|
|
|
|
* @param action The action associated with the message.
|
|
|
|
* @param desttype The destination type.
|
|
|
|
* @param client_id The origin of the chat message.
|
|
|
|
* @param msg The actual message.
|
|
|
|
* @param data Arbitrary extra data.
|
|
|
|
*/
|
2010-10-17 17:40:18 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CHAT);
|
|
|
|
|
|
|
|
p->Send_uint8 (action);
|
|
|
|
p->Send_uint8 (desttype);
|
|
|
|
p->Send_uint32(client_id);
|
|
|
|
p->Send_string(msg);
|
|
|
|
p->Send_uint64(data);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:40:18 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send the reply of an rcon command.
|
|
|
|
* @param colour The colour of the text.
|
|
|
|
* @param result The result of the command.
|
|
|
|
*/
|
2010-10-17 17:41:52 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendRcon(uint16 colour, const char *result)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_RCON);
|
|
|
|
|
|
|
|
p->Send_uint16(colour);
|
|
|
|
p->Send_string(result);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:41:52 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_RCON(Packet *p)
|
2010-10-17 17:41:52 +00:00
|
|
|
{
|
|
|
|
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
char command[NETWORK_RCONCOMMAND_LENGTH];
|
|
|
|
|
|
|
|
p->Recv_string(command, sizeof(command));
|
|
|
|
|
|
|
|
DEBUG(net, 2, "[admin] Rcon command from '%s' (%s): '%s'", this->admin_name, this->admin_version, command);
|
|
|
|
|
|
|
|
_redirect_console_to_admin = this->index;
|
|
|
|
IConsoleCmdExec(command);
|
|
|
|
_redirect_console_to_admin = INVALID_ADMIN_ID;
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:32 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Packet *p)
|
|
|
|
{
|
|
|
|
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
char json[NETWORK_GAMESCRIPT_JSON_LENGTH];
|
|
|
|
|
|
|
|
p->Recv_string(json, sizeof(json));
|
|
|
|
|
|
|
|
DEBUG(net, 2, "[admin] GameScript JSON from '%s' (%s): '%s'", this->admin_name, this->admin_version, json);
|
|
|
|
|
|
|
|
Game::NewEvent(new ScriptEventAdminPort(json));
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send console output of other clients.
|
|
|
|
* @param origin The origin of the string.
|
|
|
|
* @param string The string that's put on the console.
|
|
|
|
*/
|
2010-10-17 17:43:01 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendConsole(const char *origin, const char *string)
|
|
|
|
{
|
|
|
|
/* If the length of both strings, plus the 2 '\0' terminations and 3 bytes of the packet
|
|
|
|
* are bigger than the MTU, just ignore the message. Better safe than sorry. It should
|
|
|
|
* never occur though as the longest strings are chat messages, which are still 30%
|
|
|
|
* smaller than SEND_MTU. */
|
|
|
|
if (strlen(origin) + strlen(string) + 2 + 3 >= SEND_MTU) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CONSOLE);
|
|
|
|
|
|
|
|
p->Send_string(origin);
|
|
|
|
p->Send_string(string);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2010-10-17 17:43:01 +00:00
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:32 +00:00
|
|
|
/**
|
|
|
|
* Send GameScript JSON output.
|
|
|
|
* @param json The JSON string.
|
|
|
|
*/
|
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendGameScript(const char *json)
|
|
|
|
{
|
|
|
|
/* At the moment we cannot transmit anything larger than MTU. So the string
|
|
|
|
* has to be no longer than the length of the json + '\0' + 3 bytes of the
|
|
|
|
* packet header. */
|
|
|
|
if (strlen(json) + 1 + 3 >= SEND_MTU) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_GAMESCRIPT);
|
|
|
|
|
|
|
|
p->Send_string(json);
|
|
|
|
this->SendPacket(p);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/** Send the names of the commands. */
|
2010-12-30 18:14:37 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdNames()
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CMD_NAMES);
|
|
|
|
|
|
|
|
for (uint i = 0; i < CMD_END; i++) {
|
|
|
|
const char *cmdname = GetCommandName(i);
|
|
|
|
|
|
|
|
/* Should SEND_MTU be exceeded, start a new packet
|
|
|
|
* (magic 5: 1 bool "more data" and one uint16 "command id", one
|
2011-01-08 16:10:07 +00:00
|
|
|
* byte for string '\0' termination and 1 bool "no more data" */
|
2010-12-30 18:14:37 +00:00
|
|
|
if (p->size + strlen(cmdname) + 5 >= SEND_MTU) {
|
|
|
|
p->Send_bool(false);
|
|
|
|
this->SendPacket(p);
|
|
|
|
|
|
|
|
p = new Packet(ADMIN_PACKET_SERVER_CMD_NAMES);
|
|
|
|
}
|
|
|
|
|
|
|
|
p->Send_bool(true);
|
|
|
|
p->Send_uint16(i);
|
|
|
|
p->Send_string(cmdname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Marker to notify the end of the packet has been reached. */
|
|
|
|
p->Send_bool(false);
|
|
|
|
this->SendPacket(p);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:24:48 +00:00
|
|
|
/**
|
|
|
|
* Send a command for logging purposes.
|
|
|
|
* @param client_id The client executing the command.
|
|
|
|
* @param cp The command that would be executed.
|
|
|
|
*/
|
2010-12-30 18:14:37 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID client_id, const CommandPacket *cp)
|
|
|
|
{
|
|
|
|
Packet *p = new Packet(ADMIN_PACKET_SERVER_CMD_LOGGING);
|
|
|
|
|
|
|
|
p->Send_uint32(client_id);
|
|
|
|
p->Send_uint8 (cp->company);
|
|
|
|
p->Send_uint16(cp->cmd & CMD_ID_MASK);
|
|
|
|
p->Send_uint32(cp->p1);
|
|
|
|
p->Send_uint32(cp->p2);
|
|
|
|
p->Send_uint32(cp->tile);
|
|
|
|
p->Send_string(cp->text);
|
|
|
|
p->Send_uint32(cp->frame);
|
|
|
|
|
|
|
|
this->SendPacket(p);
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
/***********
|
|
|
|
* Receiving functions
|
|
|
|
************/
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN(Packet *p)
|
2010-10-17 17:31:03 +00:00
|
|
|
{
|
|
|
|
if (this->status != ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
char password[NETWORK_PASSWORD_LENGTH];
|
|
|
|
p->Recv_string(password, sizeof(password));
|
|
|
|
|
|
|
|
if (StrEmpty(_settings_client.network.admin_password) ||
|
|
|
|
strcmp(password, _settings_client.network.admin_password) != 0) {
|
|
|
|
/* Password is invalid */
|
|
|
|
return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
|
|
|
|
}
|
|
|
|
|
|
|
|
p->Recv_string(this->admin_name, sizeof(this->admin_name));
|
|
|
|
p->Recv_string(this->admin_version, sizeof(this->admin_version));
|
|
|
|
|
|
|
|
if (StrEmpty(this->admin_name) || StrEmpty(this->admin_version)) {
|
|
|
|
/* no name or version supplied */
|
|
|
|
return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
|
|
|
|
}
|
|
|
|
|
2011-09-15 18:28:39 +00:00
|
|
|
this->status = ADMIN_STATUS_ACTIVE;
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
DEBUG(net, 1, "[admin] '%s' (%s) has connected", this->admin_name, this->admin_version);
|
|
|
|
|
|
|
|
return this->SendProtocol();
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_QUIT(Packet *p)
|
2010-10-17 17:31:03 +00:00
|
|
|
{
|
|
|
|
/* The admin is leaving nothing else to do */
|
|
|
|
return this->CloseConnection();
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_UPDATE_FREQUENCY(Packet *p)
|
2010-10-17 17:36:23 +00:00
|
|
|
{
|
|
|
|
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
AdminUpdateType type = (AdminUpdateType)p->Recv_uint16();
|
|
|
|
AdminUpdateFrequency freq = (AdminUpdateFrequency)p->Recv_uint16();
|
|
|
|
|
|
|
|
if (type >= ADMIN_UPDATE_END || (_admin_update_type_frequencies[type] & freq) != freq) {
|
|
|
|
/* The server does not know of this UpdateType. */
|
|
|
|
DEBUG(net, 3, "[admin] Not supported update frequency %d (%d) from '%s' (%s).", type, freq, this->admin_name, this->admin_version);
|
|
|
|
return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->update_frequency[type] = freq;
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_POLL(Packet *p)
|
2010-10-17 17:36:23 +00:00
|
|
|
{
|
|
|
|
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
AdminUpdateType type = (AdminUpdateType)p->Recv_uint8();
|
|
|
|
uint32 d1 = p->Recv_uint32();
|
|
|
|
|
|
|
|
switch (type) {
|
2010-10-17 17:36:59 +00:00
|
|
|
case ADMIN_UPDATE_DATE:
|
|
|
|
/* The admin is requesting the current date. */
|
|
|
|
this->SendDate();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:37:26 +00:00
|
|
|
case ADMIN_UPDATE_CLIENT_INFO:
|
|
|
|
/* The admin is requesting client info. */
|
2011-04-22 16:03:13 +00:00
|
|
|
const NetworkClientSocket *cs;
|
2010-10-17 17:37:26 +00:00
|
|
|
if (d1 == UINT32_MAX) {
|
2011-11-04 22:32:21 +00:00
|
|
|
this->SendClientInfo(NULL, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
|
2011-04-22 16:03:13 +00:00
|
|
|
FOR_ALL_CLIENT_SOCKETS(cs) {
|
2011-11-04 22:32:21 +00:00
|
|
|
this->SendClientInfo(cs, cs->GetInfo());
|
2010-10-17 17:37:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-11-04 22:32:21 +00:00
|
|
|
if (d1 == CLIENT_ID_SERVER) {
|
|
|
|
this->SendClientInfo(NULL, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
|
|
|
|
} else {
|
|
|
|
cs = NetworkClientSocket::GetByClientID((ClientID)d1);
|
|
|
|
if (cs != NULL) this->SendClientInfo(cs, cs->GetInfo());
|
|
|
|
}
|
2010-10-17 17:37:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:37:45 +00:00
|
|
|
case ADMIN_UPDATE_COMPANY_INFO:
|
|
|
|
/* The admin is asking for company info. */
|
|
|
|
const Company *company;
|
|
|
|
if (d1 == UINT32_MAX) {
|
|
|
|
FOR_ALL_COMPANIES(company) {
|
|
|
|
this->SendCompanyInfo(company);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
company = Company::GetIfValid(d1);
|
|
|
|
if (company != NULL) this->SendCompanyInfo(company);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:38:16 +00:00
|
|
|
case ADMIN_UPDATE_COMPANY_ECONOMY:
|
|
|
|
/* The admin is requesting economy info. */
|
|
|
|
this->SendCompanyEconomy();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:38:41 +00:00
|
|
|
case ADMIN_UPDATE_COMPANY_STATS:
|
|
|
|
/* the admin is requesting company stats. */
|
|
|
|
this->SendCompanyStats();
|
|
|
|
break;
|
|
|
|
|
2010-12-30 18:14:37 +00:00
|
|
|
case ADMIN_UPDATE_CMD_NAMES:
|
|
|
|
/* The admin is requesting the names of DoCommands. */
|
|
|
|
this->SendCmdNames();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:36:23 +00:00
|
|
|
default:
|
|
|
|
/* An unsupported "poll" update type. */
|
|
|
|
DEBUG(net, 3, "[admin] Not supported poll %d (%d) from '%s' (%s).", type, d1, this->admin_name, this->admin_version);
|
|
|
|
return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:01:57 +00:00
|
|
|
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_CHAT(Packet *p)
|
2010-10-17 17:40:18 +00:00
|
|
|
{
|
|
|
|
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
|
|
NetworkAction action = (NetworkAction)p->Recv_uint8();
|
|
|
|
DestType desttype = (DestType)p->Recv_uint8();
|
|
|
|
int dest = p->Recv_uint32();
|
|
|
|
|
|
|
|
char msg[NETWORK_CHAT_LENGTH];
|
|
|
|
p->Recv_string(msg, NETWORK_CHAT_LENGTH);
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case NETWORK_ACTION_CHAT:
|
|
|
|
case NETWORK_ACTION_CHAT_CLIENT:
|
|
|
|
case NETWORK_ACTION_CHAT_COMPANY:
|
|
|
|
case NETWORK_ACTION_SERVER_MESSAGE:
|
|
|
|
NetworkServerSendChat(action, desttype, dest, msg, _network_own_client_id, 0, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG(net, 3, "[admin] Invalid chat action %d from admin '%s' (%s).", action, this->admin_name, this->admin_version);
|
|
|
|
return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
/*
|
|
|
|
* Useful wrapper functions
|
|
|
|
*/
|
|
|
|
|
2010-10-17 17:37:26 +00:00
|
|
|
/**
|
|
|
|
* Notify the admin network of a new client (if they did opt in for the respective update).
|
2011-05-04 20:24:23 +00:00
|
|
|
* @param cs the client info.
|
2010-10-17 17:37:26 +00:00
|
|
|
* @param new_client if this is a new client, send the respective packet too.
|
|
|
|
*/
|
2011-04-22 16:03:13 +00:00
|
|
|
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
|
2010-10-17 17:37:26 +00:00
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:26 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
|
2011-11-04 22:32:21 +00:00
|
|
|
as->SendClientInfo(cs, cs->GetInfo());
|
2010-10-17 17:37:26 +00:00
|
|
|
if (new_client) {
|
2011-04-22 16:03:13 +00:00
|
|
|
as->SendClientJoin(cs->client_id);
|
2010-10-17 17:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admin network of a client update (if they did opt in for the respective update).
|
|
|
|
* @param ci the client info.
|
|
|
|
*/
|
|
|
|
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:26 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendClientUpdate(ci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admin network that a client quit (if they have opt in for the respective update).
|
|
|
|
* @param client_id of the client that quit.
|
|
|
|
*/
|
|
|
|
void NetworkAdminClientQuit(ClientID client_id)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:26 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendClientQuit(client_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admin network of a client error (if they have opt in for the respective update).
|
|
|
|
* @param client_id the client that made the error.
|
|
|
|
* @param error_code the error that was caused.
|
|
|
|
*/
|
|
|
|
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:26 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendClientError(client_id, error_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:37:45 +00:00
|
|
|
/**
|
|
|
|
* Notify the admin network of company details.
|
|
|
|
* @param company the company of which details will be sent into the admin network.
|
|
|
|
* @param new_company whether this is a new company or not.
|
|
|
|
*/
|
|
|
|
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
|
|
|
|
{
|
|
|
|
if (company == NULL) {
|
|
|
|
DEBUG(net, 1, "[admin] Empty company given for update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:45 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue;
|
|
|
|
|
|
|
|
as->SendCompanyInfo(company);
|
|
|
|
if (new_company) {
|
|
|
|
as->SendCompanyNew(company->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admin network of company updates.
|
|
|
|
* @param company company of which updates are going to be sent into the admin network.
|
|
|
|
*/
|
|
|
|
void NetworkAdminCompanyUpdate(const Company *company)
|
|
|
|
{
|
|
|
|
if (company == NULL) return;
|
|
|
|
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:45 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue;
|
|
|
|
|
|
|
|
as->SendCompanyUpdate(company);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admin network of a company to be removed (including the reason why).
|
|
|
|
* @param company_id ID of the company that got removed.
|
|
|
|
* @param bcrr the reason why the company got removed (e.g. bankruptcy).
|
|
|
|
*/
|
|
|
|
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:37:45 +00:00
|
|
|
as->SendCompanyRemove(company_id, bcrr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:40:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send chat to the admin network (if they did opt in for the respective update).
|
|
|
|
*/
|
|
|
|
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data, bool from_admin)
|
|
|
|
{
|
|
|
|
if (from_admin) return;
|
|
|
|
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:40:18 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CHAT] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendChat(action, desttype, client_id, msg, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:41:52 +00:00
|
|
|
/**
|
|
|
|
* Pass the rcon reply to the admin.
|
|
|
|
* @param admin_index The admin to give the reply.
|
|
|
|
* @param colour_code The colour of the string.
|
|
|
|
* @param string The string to show.
|
|
|
|
*/
|
2011-01-03 12:01:41 +00:00
|
|
|
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
|
2010-10-17 17:41:52 +00:00
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler::Get(admin_index)->SendRcon(colour_code, string);
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:43:01 +00:00
|
|
|
/**
|
|
|
|
* Send console to the admin network (if they did opt in for the respective update).
|
|
|
|
* @param origin the origin of the message.
|
|
|
|
* @param string the message as printed on the console.
|
|
|
|
*/
|
|
|
|
void NetworkAdminConsole(const char *origin, const char *string)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:43:01 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CONSOLE] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendConsole(origin, string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:32 +00:00
|
|
|
/**
|
|
|
|
* Send GameScript JSON to the admin network (if they did opt in for the respective update).
|
|
|
|
* @param json The JSON data as received from the GameScript.
|
|
|
|
*/
|
|
|
|
void NetworkAdminGameScript(const char *json)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
|
|
|
if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendGameScript(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-30 18:14:37 +00:00
|
|
|
/**
|
|
|
|
* Distribute CommandPacket details over the admin network for logging purposes.
|
|
|
|
* @param owner The owner of the CommandPacket (who sent us the CommandPacket).
|
|
|
|
* @param cp The CommandPacket to be distributed.
|
|
|
|
*/
|
|
|
|
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
|
|
|
|
{
|
|
|
|
ClientID client_id = owner == NULL ? _network_own_client_id : owner->client_id;
|
|
|
|
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-12-30 18:14:37 +00:00
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CMD_LOGGING] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
as->SendCmdLogging(client_id, cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
/**
|
|
|
|
* Send a Welcome packet to all connected admins
|
|
|
|
*/
|
|
|
|
void ServerNetworkAdminSocketHandler::WelcomeAll()
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:31:03 +00:00
|
|
|
as->SendWelcome();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:36:23 +00:00
|
|
|
/**
|
|
|
|
* Send (push) updates to the admin network as they have registered for these updates.
|
|
|
|
* @param freq the frequency to be processd.
|
|
|
|
*/
|
|
|
|
void NetworkAdminUpdate(AdminUpdateFrequency freq)
|
|
|
|
{
|
|
|
|
ServerNetworkAdminSocketHandler *as;
|
2011-09-15 18:28:39 +00:00
|
|
|
FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
|
2010-10-17 17:36:23 +00:00
|
|
|
for (int i = 0; i < ADMIN_UPDATE_END; i++) {
|
|
|
|
if (as->update_frequency[i] & freq) {
|
|
|
|
/* Update the admin for the required details */
|
|
|
|
switch (i) {
|
2010-10-17 17:36:59 +00:00
|
|
|
case ADMIN_UPDATE_DATE:
|
|
|
|
as->SendDate();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:38:16 +00:00
|
|
|
case ADMIN_UPDATE_COMPANY_ECONOMY:
|
|
|
|
as->SendCompanyEconomy();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:38:41 +00:00
|
|
|
case ADMIN_UPDATE_COMPANY_STATS:
|
|
|
|
as->SendCompanyStats();
|
|
|
|
break;
|
|
|
|
|
2010-10-17 17:36:23 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:31:03 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|