1
0
Fork 0

Codechange: [Network] Use std::string for the client name in the network server

pull/9369/head
rubidium42 2021-06-14 16:09:50 +02:00 committed by rubidium42
parent a8b3afb236
commit 981cd0197a
2 changed files with 13 additions and 30 deletions

View File

@ -258,9 +258,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta
if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
/* We did not receive a leave message from this client... */
char client_name[NETWORK_CLIENT_NAME_LENGTH];
this->GetClientName(client_name, lastof(client_name));
std::string client_name = this->GetClientName();
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
@ -381,9 +379,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo()
}
for (NetworkClientSocket *csi : NetworkClientSocket::Iterate()) {
char client_name[NETWORK_CLIENT_NAME_LENGTH];
((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, lastof(client_name));
std::string client_name = static_cast<ServerNetworkGameSocketHandler*>(csi)->GetClientName();
ci = csi->GetInfo();
if (ci != nullptr && Company::IsValidID(ci->client_playas)) {
@ -441,9 +437,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err
/* 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));
std::string client_name = this->GetClientName();
Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
@ -1010,9 +1004,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *
{
/* Client has the map, now start syncing */
if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
char client_name[NETWORK_CLIENT_NAME_LENGTH];
this->GetClientName(client_name, lastof(client_name));
std::string client_name = this->GetClientName();
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "", this->client_id);
InvalidateWindowData(WC_CLIENT_LIST, 0);
@ -1121,7 +1113,6 @@ 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 */
@ -1129,8 +1120,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p
return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
}
this->GetClientName(client_name, lastof(client_name));
std::string client_name = this->GetClientName();
StringID strid = GetNetworkErrorMsg(errorno);
Debug(net, 1, "'{}' reported an error and is closing its connection: {}", client_name, GetString(strid));
@ -1150,17 +1140,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p
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);
}
this->GetClientName(client_name, lastof(client_name));
/* The client wants to leave. Display this and report it to the other clients. */
std::string client_name = this->GetClientName();
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_MESSAGE_CLIENT_LEAVING);
for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
@ -2122,15 +2108,12 @@ bool NetworkCompanyHasClients(CompanyID company)
* @param client_name The variable to write the name to.
* @param last The pointer to the last element of the destination buffer
*/
void ServerNetworkGameSocketHandler::GetClientName(char *client_name, const char *last) const
std::string ServerNetworkGameSocketHandler::GetClientName() const
{
const NetworkClientInfo *ci = this->GetInfo();
if (ci != nullptr && !ci->client_name.empty()) return 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.c_str(), last);
}
return fmt::format("Client #{}", this->client_id);
}
/**
@ -2142,13 +2125,13 @@ void NetworkPrintClients()
if (_network_server) {
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {} IP: {}",
ci->client_id,
ci->client_name.c_str(),
ci->client_name,
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
} else {
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {}",
ci->client_id,
ci->client_name.c_str(),
ci->client_name,
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0));
}
}

View File

@ -79,7 +79,7 @@ public:
virtual Packet *ReceivePacket() override;
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
void GetClientName(char *client_name, const char *last) const;
std::string GetClientName() const;
void CheckNextClientToSendMap(NetworkClientSocket *ignore_cs = nullptr);