|
@@ -262,49 +262,49 @@ NetworkRecvStatus ServerNetworkGameSocke
|
|
|
|
|
|
this->GetClientName(client_name, lastof(client_name));
|
|
|
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
|
|
|
|
|
|
/* Inform other clients of this... strange leaving ;) */
|
|
|
for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
|
|
|
if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
|
|
|
new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* If we were transfering a map to this client, stop the savegame creation
|
|
|
* process and queue the next client to receive the map. */
|
|
|
if (this->status == STATUS_MAP) {
|
|
|
/* Ensure the saving of the game is stopped too. */
|
|
|
this->savegame->Destroy();
|
|
|
this->savegame = nullptr;
|
|
|
|
|
|
this->CheckNextClientToSendMap(this);
|
|
|
}
|
|
|
|
|
|
NetworkAdminClientError(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
|
|
|
DEBUG(net, 3, "Closed client connection %d", this->client_id);
|
|
|
Debug(net, 3, "Closed client connection {}", this->client_id);
|
|
|
|
|
|
/* We just lost one client :( */
|
|
|
if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--;
|
|
|
extern byte _network_clients_connected;
|
|
|
_network_clients_connected--;
|
|
|
|
|
|
this->SendPackets(true);
|
|
|
|
|
|
delete this->GetInfo();
|
|
|
delete this;
|
|
|
|
|
|
InvalidateWindowData(WC_CLIENT_LIST, 0);
|
|
|
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Whether an connection is allowed or not at this moment.
|
|
|
* @return true if the connection is allowed.
|
|
|
*/
|
|
|
/* static */ bool ServerNetworkGameSocketHandler::AllowConnection()
|
|
|
{
|
|
|
extern byte _network_clients_connected;
|
|
|
bool accept = _network_clients_connected < MAX_CLIENTS && _network_game_info.clients_on < _settings_client.network.max_clients;
|
|
@@ -424,70 +424,70 @@ NetworkRecvStatus ServerNetworkGameSocke
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Send an error to the client, and close its connection.
|
|
|
* @param error The error to disconnect for.
|
|
|
* @param reason In case of kicking a client, specifies the reason for kicking the client.
|
|
|
*/
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason)
|
|
|
{
|
|
|
Packet *p = new Packet(PACKET_SERVER_ERROR);
|
|
|
|
|
|
p->Send_uint8(error);
|
|
|
if (!reason.empty()) p->Send_string(reason);
|
|
|
this->SendPacket(p);
|
|
|
|
|
|
StringID strid = GetNetworkErrorMsg(error);
|
|
|
|
|
|
/* Only send when the current client was in game */
|
|
|
if (this->status > STATUS_AUTHORIZED) {
|
|
|
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
|
|
|
|
|
this->GetClientName(client_name, lastof(client_name));
|
|
|
|
|
|
DEBUG(net, 1, "'%s' made an error and has been disconnected: %s", client_name, GetString(strid).c_str());
|
|
|
Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
|
|
|
|
|
|
if (error == NETWORK_ERROR_KICKED && !reason.empty()) {
|
|
|
NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
|
|
|
} else {
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
|
|
|
}
|
|
|
|
|
|
for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
|
|
|
if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
|
|
|
/* Some errors we filter to a more general error. Clients don't have to know the real
|
|
|
* reason a joining failed. */
|
|
|
if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
|
|
|
error = NETWORK_ERROR_ILLEGAL_PACKET;
|
|
|
}
|
|
|
new_cs->SendErrorQuit(this->client_id, error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NetworkAdminClientError(this->client_id, error);
|
|
|
} else {
|
|
|
DEBUG(net, 1, "Client %d made an error and has been disconnected: %s", this->client_id, GetString(strid).c_str());
|
|
|
Debug(net, 1, "Client {} made an error and has been disconnected: {}", this->client_id, GetString(strid));
|
|
|
}
|
|
|
|
|
|
/* The client made a mistake, so drop the connection now! */
|
|
|
return this->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
|
|
|
}
|
|
|
|
|
|
/** Send the check for the NewGRFs. */
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
|
|
|
{
|
|
|
Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS);
|
|
|
const GRFConfig *c;
|
|
|
uint grf_count = 0;
|
|
|
|
|
|
for (c = _grfconfig; c != nullptr; c = c->next) {
|
|
|
if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
|
|
|
}
|
|
|
|
|
|
p->Send_uint8 (grf_count);
|
|
|
for (c = _grfconfig; c != nullptr; c = c->next) {
|
|
|
if (!HasBit(c->flags, GCF_STATIC)) SerializeGRFIdentifier(p, &c->ident);
|
|
|
}
|
|
|
|
|
|
this->SendPacket(p);
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
@@ -904,49 +904,49 @@ NetworkRecvStatus ServerNetworkGameSocke
|
|
|
if (!Company::IsValidHumanID(playas)) {
|
|
|
return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (!NetworkIsValidClientName(client_name)) {
|
|
|
/* An invalid client name was given. However, the client ensures the name
|
|
|
* is valid before it is sent over the network, so something went horribly
|
|
|
* wrong. This is probably someone trying to troll us. */
|
|
|
return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
|
|
|
}
|
|
|
|
|
|
if (!NetworkMakeClientNameUnique(client_name)) { // Change name if duplicate
|
|
|
/* We could not create a name for this client */
|
|
|
return this->SendError(NETWORK_ERROR_NAME_IN_USE);
|
|
|
}
|
|
|
|
|
|
assert(NetworkClientInfo::CanAllocateItem());
|
|
|
NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
|
|
|
this->SetInfo(ci);
|
|
|
ci->join_date = _date;
|
|
|
ci->client_name = client_name;
|
|
|
ci->client_playas = playas;
|
|
|
DEBUG(desync, 1, "client: %08x; %02x; %02x; %02x", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
|
|
|
Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:02x}", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
|
|
|
|
|
|
/* Make sure companies to which people try to join are not autocleaned */
|
|
|
if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
|
|
|
|
|
|
this->status = STATUS_NEWGRFS_CHECK;
|
|
|
|
|
|
if (_grfconfig == nullptr) {
|
|
|
/* Behave as if we received PACKET_CLIENT_NEWGRFS_CHECKED */
|
|
|
return this->Receive_CLIENT_NEWGRFS_CHECKED(nullptr);
|
|
|
}
|
|
|
|
|
|
return this->SendNewGRFCheck();
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(Packet *p)
|
|
|
{
|
|
|
if (this->status != STATUS_AUTH_GAME) {
|
|
|
return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
}
|
|
|
|
|
|
std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
|
|
|
|
|
|
/* Check game password. Allow joining if we cleared the password meanwhile */
|
|
|
if (!_settings_client.network.server_password.empty() &&
|
|
@@ -1112,49 +1112,49 @@ NetworkRecvStatus ServerNetworkGameSocke
|
|
|
}
|
|
|
|
|
|
if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) cp.p2 = this->client_id;
|
|
|
|
|
|
this->incoming_queue.Append(&cp);
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p)
|
|
|
{
|
|
|
/* This packets means a client noticed an error and is reporting this
|
|
|
* to us. Display the error and report it to the other clients */
|
|
|
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
|
|
NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
|
|
|
|
|
|
/* The client was never joined.. thank the client for the packet, but ignore it */
|
|
|
if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
|
|
|
return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
|
|
|
}
|
|
|
|
|
|
this->GetClientName(client_name, lastof(client_name));
|
|
|
|
|
|
StringID strid = GetNetworkErrorMsg(errorno);
|
|
|
|
|
|
DEBUG(net, 1, "'%s' reported an error and is closing its connection: %s", client_name, GetString(strid).c_str());
|
|
|
Debug(net, 1, "'{}' reported an error and is closing its connection: {}", client_name, GetString(strid));
|
|
|
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
|
|
|
|
|
|
for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
|
|
|
if (new_cs->status >= STATUS_AUTHORIZED) {
|
|
|
new_cs->SendErrorQuit(this->client_id, errorno);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NetworkAdminClientError(this->client_id, errorno);
|
|
|
|
|
|
return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p)
|
|
|
{
|
|
|
/* The client wants to leave. Display this and report it to the other
|
|
|
* clients. */
|
|
|
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
|
|
|
|
|
/* The client was never joined.. thank the client for the packet, but ignore it */
|
|
|
if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
|
|
|
return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
|
|
|
}
|
|
@@ -1302,49 +1302,49 @@ void NetworkServerSendChat(NetworkAction
|
|
|
ci_to = ci_own;
|
|
|
}
|
|
|
|
|
|
/* There is no such client */
|
|
|
if (ci_to == nullptr) break;
|
|
|
|
|
|
/* Display the message locally (so you know you have sent it) */
|
|
|
if (ci != nullptr && show_local) {
|
|
|
if (from_id == CLIENT_ID_SERVER) {
|
|
|
StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
|
|
|
SetDParam(0, ci_to->client_playas);
|
|
|
std::string name = GetString(str);
|
|
|
NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
|
|
|
} else {
|
|
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
|
|
if (cs->client_id == from_id) {
|
|
|
cs->SendChat(action, ci_to->client_id, true, msg, data);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
default:
|
|
|
DEBUG(net, 1, "Received unknown chat destination type %d; doing broadcast instead", desttype);
|
|
|
Debug(net, 1, "Received unknown chat destination type {}; doing broadcast instead", desttype);
|
|
|
FALLTHROUGH;
|
|
|
|
|
|
case DESTTYPE_BROADCAST:
|
|
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
|
|
cs->SendChat(action, from_id, false, msg, data);
|
|
|
}
|
|
|
|
|
|
NetworkAdminChat(action, desttype, from_id, msg, data, from_admin);
|
|
|
|
|
|
ci = NetworkClientInfo::GetByClientID(from_id);
|
|
|
if (ci != nullptr) {
|
|
|
NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT(Packet *p)
|
|
|
{
|
|
|
if (this->status < STATUS_PRE_ACTIVE) {
|
|
|
/* Illegal call, return error and ignore the packet */
|
|
|
return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
|
|
|
}
|
|
|
|
|
@@ -1404,77 +1404,77 @@ NetworkRecvStatus ServerNetworkGameSocke
|
|
|
* wrong. This is probably someone trying to troll us. */
|
|
|
return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
|
|
|
}
|
|
|
|
|
|
/* Display change */
|
|
|
if (NetworkMakeClientNameUnique(client_name)) {
|
|
|
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
|
|
|
ci->client_name = client_name;
|
|
|
NetworkUpdateClientInfo(ci->client_id);
|
|
|
}
|
|
|
}
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_RCON(Packet *p)
|
|
|
{
|
|
|
if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
if (_settings_client.network.rcon_password.empty()) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
|
|
|
std::string command = p->Recv_string(NETWORK_RCONCOMMAND_LENGTH);
|
|
|
|
|
|
if (_settings_client.network.rcon_password.compare(password) != 0) {
|
|
|
DEBUG(net, 1, "[rcon] Wrong password from client-id %d", this->client_id);
|
|
|
Debug(net, 1, "[rcon] Wrong password from client-id {}", this->client_id);
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
DEBUG(net, 3, "[rcon] Client-id %d executed: %s", this->client_id, command.c_str());
|
|
|
Debug(net, 3, "[rcon] Client-id {} executed: {}", this->client_id, command);
|
|
|
|
|
|
_redirect_console_to_client = this->client_id;
|
|
|
IConsoleCmdExec(command.c_str());
|
|
|
_redirect_console_to_client = INVALID_CLIENT_ID;
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet *p)
|
|
|
{
|
|
|
if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
|
|
|
|
|
|
CompanyID company_id = (Owner)p->Recv_uint8();
|
|
|
|
|
|
/* Check if the company is valid, we don't allow moving to AI companies */
|
|
|
if (company_id != COMPANY_SPECTATOR && !Company::IsValidHumanID(company_id)) return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
/* Check if we require a password for this company */
|
|
|
if (company_id != COMPANY_SPECTATOR && !_network_company_states[company_id].password.empty()) {
|
|
|
/* we need a password from the client - should be in this packet */
|
|
|
std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
|
|
|
|
|
|
/* Incorrect password sent, return! */
|
|
|
if (_network_company_states[company_id].password.compare(password) != 0) {
|
|
|
DEBUG(net, 2, "Wrong password from client-id #%d for company #%d", this->client_id, company_id + 1);
|
|
|
Debug(net, 2, "Wrong password from client-id #{} for company #{}", this->client_id, company_id + 1);
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* if we get here we can move the client */
|
|
|
NetworkServerDoMove(this->client_id, company_id);
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Package some generic company information into a packet.
|
|
|
* @param p The packet that will contain the data.
|
|
|
* @param c The company to put the of into the packet.
|
|
|
* @param stats The statistics to put in the packet.
|
|
|
* @param max_len The maximum length of the company name.
|
|
|
*/
|
|
|
void NetworkSocketHandler::SendCompanyInformation(Packet *p, const Company *c, const NetworkCompanyStats *stats, uint max_len)
|
|
|
{
|
|
|
/* Grab the company name */
|
|
|
char company_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
SetDParam(0, c->index);
|
|
|
|
|
|
assert(max_len <= lengthof(company_name));
|
|
|
GetString(company_name, STR_COMPANY_NAME, company_name + max_len - 1);
|
|
@@ -1540,64 +1540,64 @@ void NetworkPopulateCompanyStats(Network
|
|
|
/* Go through all stations and count the types of stations */
|
|
|
for (const Station *s : Station::Iterate()) {
|
|
|
if (Company::IsValidID(s->owner)) {
|
|
|
NetworkCompanyStats *npi = &stats[s->owner];
|
|
|
|
|
|
if (s->facilities & FACIL_TRAIN) npi->num_station[NETWORK_VEH_TRAIN]++;
|
|
|
if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[NETWORK_VEH_LORRY]++;
|
|
|
if (s->facilities & FACIL_BUS_STOP) npi->num_station[NETWORK_VEH_BUS]++;
|
|
|
if (s->facilities & FACIL_AIRPORT) npi->num_station[NETWORK_VEH_PLANE]++;
|
|
|
if (s->facilities & FACIL_DOCK) npi->num_station[NETWORK_VEH_SHIP]++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Send updated client info of a particular client.
|
|
|
* @param client_id The client to send it for.
|
|
|
*/
|
|
|
void NetworkUpdateClientInfo(ClientID client_id)
|
|
|
{
|
|
|
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
|
|
|
|
|
|
if (ci == nullptr) return;
|
|
|
|
|
|
DEBUG(desync, 1, "client: %08x; %02x; %02x; %04x", _date, _date_fract, (int)ci->client_playas, client_id);
|
|
|
Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:04x}", _date, _date_fract, (int)ci->client_playas, client_id);
|
|
|
|
|
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
|
|
if (cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) {
|
|
|
cs->SendClientInfo(ci);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NetworkAdminClientUpdate(ci);
|
|
|
}
|
|
|
|
|
|
/** Check if we want to restart the map */
|
|
|
static void NetworkCheckRestartMap()
|
|
|
{
|
|
|
if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
|
|
|
DEBUG(net, 3, "Auto-restarting map: year %d reached", _cur_year);
|
|
|
Debug(net, 3, "Auto-restarting map: year {} reached", _cur_year);
|
|
|
|
|
|
_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
|
|
|
switch(_file_to_saveload.abstract_ftype) {
|
|
|
case FT_SAVEGAME:
|
|
|
case FT_SCENARIO:
|
|
|
_switch_mode = SM_LOAD_GAME;
|
|
|
break;
|
|
|
|
|
|
case FT_HEIGHTMAP:
|
|
|
_switch_mode = SM_START_HEIGHTMAP;
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
_switch_mode = SM_NEWGAME;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/** Check if the server has autoclean_companies activated
|
|
|
* Two things happen:
|
|
|
* 1) If a company is not protected, it is closed after 1 year (for example)
|
|
|
* 2) If a company is protected, protection is disabled after 3 years (for example)
|
|
|
* (and item 1. happens a year later)
|
|
|
*/
|