Codechange: [Network] Let NetworkClientInfo use std::string

This commit is contained in:
rubidium42 2021-05-15 08:31:45 +02:00 committed by rubidium42
parent 83679c0e57
commit 4d246cda73
6 changed files with 27 additions and 30 deletions

View File

@ -844,7 +844,7 @@ static void NetworkInitGameInfo()
NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
strecpy(ci->client_name, _settings_client.network.client_name.c_str(), lastof(ci->client_name));
ci->client_name = _settings_client.network.client_name;
}
/**

View File

@ -22,10 +22,10 @@ extern NetworkClientInfoPool _networkclientinfo_pool;
/** Container for all information known about a client. */
struct NetworkClientInfo : NetworkClientInfoPool::PoolItem<&_networkclientinfo_pool> {
ClientID client_id; ///< Client identifier (same as ClientState->client_id)
char client_name[NETWORK_CLIENT_NAME_LENGTH]; ///< Name of the client
CompanyID client_playas; ///< As which company is this client playing (CompanyID)
Date join_date; ///< Gamedate the client has joined
ClientID client_id; ///< Client identifier (same as ClientState->client_id)
std::string client_name; ///< Name of the client
CompanyID client_playas; ///< As which company is this client playing (CompanyID)
Date join_date; ///< Gamedate the client has joined
/**
* Create a new client.

View File

@ -332,7 +332,7 @@ struct NetworkChatWindow : public Window {
/* Skip inactive clients */
for (NetworkClientInfo *ci : NetworkClientInfo::Iterate(*item)) {
*item = ci->index;
return ci->client_name;
return ci->client_name.c_str();
}
*item = MAX_CLIENT_SLOTS;
}

View File

@ -651,9 +651,8 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
NetworkClientInfo *ci;
ClientID client_id = (ClientID)p->Recv_uint32();
CompanyID playas = (CompanyID)p->Recv_uint8();
char name[NETWORK_NAME_LENGTH];
p->Recv_string(name, sizeof(name));
std::string name = p->Recv_string(NETWORK_NAME_LENGTH);
if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
@ -664,7 +663,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != nullptr) {
if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
if (playas == ci->client_playas && name.compare(ci->client_name) != 0) {
/* Client name changed, display the change */
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
} else if (playas != ci->client_playas) {
@ -677,7 +676,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
if (client_id == _network_own_client_id) SetLocalCompany(!Company::IsValidID(playas) ? COMPANY_SPECTATOR : playas);
ci->client_playas = playas;
strecpy(ci->client_name, name, lastof(ci->client_name));
ci->client_name = name;
InvalidateWindowData(WC_CLIENT_LIST, 0);
@ -696,7 +695,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
ci->client_playas = playas;
if (client_id == _network_own_client_id) this->SetInfo(ci);
strecpy(ci->client_name, name, lastof(ci->client_name));
ci->client_name = name;
InvalidateWindowData(WC_CLIENT_LIST, 0);
@ -745,8 +744,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(Packet *p
if (error < (ptrdiff_t)lengthof(network_error_strings)) err = network_error_strings[error];
/* In case of kicking a client, we assume there is a kick message in the packet if we can read one byte */
if (error == NETWORK_ERROR_KICKED && p->CanReadFromPacket(1)) {
char kick_msg[255];
p->Recv_string(kick_msg, sizeof(kick_msg));
std::string kick_msg = p->Recv_string(NETWORK_CHAT_LENGTH);
SetDParamStr(0, kick_msg);
ShowErrorMessage(err, STR_NETWORK_ERROR_KICK_MESSAGE, WL_CRITICAL);
} else {
@ -1160,10 +1158,9 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_RCON(Packet *p)
TextColour colour_code = (TextColour)p->Recv_uint16();
if (!IsValidConsoleColour(colour_code)) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
p->Recv_string(rcon_out, sizeof(rcon_out));
std::string rcon_out = p->Recv_string(NETWORK_RCONCOMMAND_LENGTH);
IConsolePrint(colour_code, rcon_out);
IConsolePrint(colour_code, rcon_out.c_str());
return NETWORK_RECV_STATUS_OKAY;
}
@ -1370,7 +1367,7 @@ void NetworkUpdateClientName()
strecpy(temporary_name, _settings_client.network.client_name.c_str(), lastof(temporary_name));
if (NetworkFindName(temporary_name, lastof(temporary_name))) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, temporary_name);
strecpy(ci->client_name, temporary_name, lastof(ci->client_name));
ci->client_name = temporary_name;
NetworkUpdateClientInfo(CLIENT_ID_SERVER);
}
}

View File

@ -927,7 +927,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
this->SetInfo(ci);
ci->join_date = _date;
strecpy(ci->client_name, name, lastof(ci->client_name));
ci->client_name = name;
ci->client_playas = playas;
DEBUG(desync, 1, "client: %08x; %02x; %02x; %02x", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
@ -1413,7 +1413,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME(Packet
/* Display change */
if (NetworkFindName(client_name, lastof(client_name))) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
strecpy(ci->client_name, client_name, lastof(ci->client_name));
ci->client_name = client_name;
NetworkUpdateClientInfo(ci->client_id);
}
}
@ -1689,7 +1689,7 @@ bool NetworkFindName(char *new_name, const char *last)
while (!found_name) {
found_name = true;
for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
if (strcmp(ci->client_name, new_name) == 0) {
if (ci->client_name.compare(new_name) == 0) {
/* Name already in use */
found_name = false;
break;
@ -1698,7 +1698,7 @@ bool NetworkFindName(char *new_name, const char *last)
/* Check if it is the same as the server-name */
const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
if (ci != nullptr) {
if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use
if (ci->client_name.compare(new_name) == 0) found_name = false; // name already in use
}
if (!found_name) {
@ -1723,7 +1723,7 @@ bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
{
/* Check if the name's already in use */
for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
if (strcmp(ci->client_name, new_name) == 0) return false;
if (ci->client_name.compare(new_name) == 0) return false;
}
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
@ -1731,7 +1731,7 @@ bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name);
strecpy(ci->client_name, new_name, lastof(ci->client_name));
ci->client_name = new_name;
NetworkUpdateClientInfo(client_id);
return true;
@ -1963,7 +1963,7 @@ void NetworkServerShowStatusToConsole()
status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
IConsolePrintF(CC_INFO, "Client #%1d name: '%s' status: '%s' frame-lag: %3d company: %1d IP: %s",
cs->client_id, ci->client_name, status, lag,
cs->client_id, ci->client_name.c_str(), status, lag,
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
cs->GetClientIP());
}
@ -2135,10 +2135,10 @@ void ServerNetworkGameSocketHandler::GetClientName(char *client_name, const char
{
const NetworkClientInfo *ci = this->GetInfo();
if (ci == nullptr || StrEmpty(ci->client_name)) {
if (ci == nullptr || ci->client_name.empty()) {
seprintf(client_name, last, "Client #%4d", this->client_id);
} else {
strecpy(client_name, ci->client_name, last);
strecpy(client_name, ci->client_name.c_str(), last);
}
}
@ -2151,13 +2151,13 @@ void NetworkPrintClients()
if (_network_server) {
IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d IP: %s",
ci->client_id,
ci->client_name,
ci->client_name.c_str(),
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
} else {
IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d",
ci->client_id,
ci->client_name,
ci->client_name.c_str(),
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0));
}
}
@ -2182,7 +2182,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
ci->client_playas = c->index;
NetworkUpdateClientInfo(ci->client_id);
NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name, c->index);
NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name.c_str(), c->index);
}
/* Announce new company on network. */

View File

@ -36,7 +36,7 @@ static NetworkClientInfo *FindClientInfo(ScriptClient::ClientID client)
{
NetworkClientInfo *ci = FindClientInfo(client);
if (ci == nullptr) return nullptr;
return stredup(ci->client_name);
return stredup(ci->client_name.c_str());
}
/* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client)