summaryrefslogtreecommitdiff
path: root/src/network/network_server.cpp
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-06-14 16:09:50 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-06-15 06:13:00 +0200
commit981cd0197a8f180dd3a29e83d1c088dcb52e4920 (patch)
treecbc8454d68266cf750526c2315a48234793dfb97 /src/network/network_server.cpp
parenta8b3afb236ba564a0c9d47adacc70ba701525c78 (diff)
downloadopenttd-981cd0197a8f180dd3a29e83d1c088dcb52e4920.tar.xz
Codechange: [Network] Use std::string for the client name in the network server
Diffstat (limited to 'src/network/network_server.cpp')
-rw-r--r--src/network/network_server.cpp41
1 files changed, 12 insertions, 29 deletions
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index d67343359..d44b61d3d 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -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));
}
}