mirror of https://github.com/OpenTTD/OpenTTD
Codechange: [Network] Use std::string for the client name in the network server
parent
a8b3afb236
commit
981cd0197a
|
@ -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) {
|
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... */
|
/* We did not receive a leave message from this client... */
|
||||||
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
std::string client_name = this->GetClientName();
|
||||||
|
|
||||||
this->GetClientName(client_name, lastof(client_name));
|
|
||||||
|
|
||||||
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
|
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()) {
|
for (NetworkClientSocket *csi : NetworkClientSocket::Iterate()) {
|
||||||
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
std::string client_name = static_cast<ServerNetworkGameSocketHandler*>(csi)->GetClientName();
|
||||||
|
|
||||||
((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, lastof(client_name));
|
|
||||||
|
|
||||||
ci = csi->GetInfo();
|
ci = csi->GetInfo();
|
||||||
if (ci != nullptr && Company::IsValidID(ci->client_playas)) {
|
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 */
|
/* Only send when the current client was in game */
|
||||||
if (this->status > STATUS_AUTHORIZED) {
|
if (this->status > STATUS_AUTHORIZED) {
|
||||||
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
std::string client_name = this->GetClientName();
|
||||||
|
|
||||||
this->GetClientName(client_name, lastof(client_name));
|
|
||||||
|
|
||||||
Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
|
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 */
|
/* Client has the map, now start syncing */
|
||||||
if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
|
if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
|
||||||
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
std::string client_name = this->GetClientName();
|
||||||
|
|
||||||
this->GetClientName(client_name, lastof(client_name));
|
|
||||||
|
|
||||||
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "", this->client_id);
|
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "", this->client_id);
|
||||||
InvalidateWindowData(WC_CLIENT_LIST, 0);
|
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
|
/* This packets means a client noticed an error and is reporting this
|
||||||
* to us. Display the error and report it to the other clients */
|
* 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();
|
NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
|
||||||
|
|
||||||
/* The client was never joined.. thank the client for the packet, but ignore it */
|
/* 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);
|
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);
|
StringID strid = GetNetworkErrorMsg(errorno);
|
||||||
|
|
||||||
Debug(net, 1, "'{}' reported an error and is closing its connection: {}", client_name, GetString(strid));
|
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)
|
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 */
|
/* The client was never joined.. thank the client for the packet, but ignore it */
|
||||||
if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
|
if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
|
||||||
return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
|
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);
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_MESSAGE_CLIENT_LEAVING);
|
||||||
|
|
||||||
for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
|
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 client_name The variable to write the name to.
|
||||||
* @param last The pointer to the last element of the destination buffer
|
* @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();
|
const NetworkClientInfo *ci = this->GetInfo();
|
||||||
|
if (ci != nullptr && !ci->client_name.empty()) return ci->client_name;
|
||||||
|
|
||||||
if (ci == nullptr || ci->client_name.empty()) {
|
return fmt::format("Client #{}", this->client_id);
|
||||||
seprintf(client_name, last, "Client #%4d", this->client_id);
|
|
||||||
} else {
|
|
||||||
strecpy(client_name, ci->client_name.c_str(), last);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2142,13 +2125,13 @@ void NetworkPrintClients()
|
||||||
if (_network_server) {
|
if (_network_server) {
|
||||||
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {} IP: {}",
|
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {} IP: {}",
|
||||||
ci->client_id,
|
ci->client_id,
|
||||||
ci->client_name.c_str(),
|
ci->client_name,
|
||||||
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
|
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
|
||||||
ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
|
ci->client_id == CLIENT_ID_SERVER ? "server" : NetworkClientSocket::GetByClientID(ci->client_id)->GetClientIP());
|
||||||
} else {
|
} else {
|
||||||
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {}",
|
IConsolePrint(CC_INFO, "Client #{} name: '{}' company: {}",
|
||||||
ci->client_id,
|
ci->client_id,
|
||||||
ci->client_name.c_str(),
|
ci->client_name,
|
||||||
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0));
|
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ public:
|
||||||
|
|
||||||
virtual Packet *ReceivePacket() override;
|
virtual Packet *ReceivePacket() override;
|
||||||
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) 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);
|
void CheckNextClientToSendMap(NetworkClientSocket *ignore_cs = nullptr);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue