1
0
Fork 0

Codechange: replace char * with std::string_view

pull/14144/head
Rubidium 2025-04-27 21:10:02 +02:00 committed by rubidium42
parent c6ea0ce961
commit 9116f96e2e
6 changed files with 16 additions and 18 deletions

View File

@ -519,7 +519,7 @@ public:
NetworkRecvStatus ReceivePackets();
const char *ReceiveCommand(Packet &p, CommandPacket &cp);
std::optional<std::string_view> ReceiveCommand(Packet &p, CommandPacket &cp);
void SendCommand(Packet &p, const CommandPacket &cp);
bool IsPendingDeletion() const { return this->is_pending_deletion; }

View File

@ -85,7 +85,7 @@ public:
* Get the name used by the listener.
* @return the name to show in debug logs and the like.
*/
static const char *GetName()
static std::string_view GetName()
{
return "admin";
}

View File

@ -915,14 +915,14 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet
if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
CommandPacket cp;
const char *err = this->ReceiveCommand(p, cp);
auto err = this->ReceiveCommand(p, cp);
cp.frame = p.Recv_uint32();
cp.my_cmd = p.Recv_bool();
Debug(net, 9, "Client::Receive_SERVER_COMMAND(): cmd={}, frame={}", cp.cmd, cp.frame);
if (err != nullptr) {
IConsolePrint(CC_WARNING, "Dropping server connection due to {}.", err);
if (err.has_value()) {
IConsolePrint(CC_WARNING, "Dropping server connection due to {}.", *err);
return NETWORK_RECV_STATUS_MALFORMED_PACKET;
}

View File

@ -358,22 +358,22 @@ void NetworkDistributeCommands()
* Receives a command from the network.
* @param p the packet to read from.
* @param cp the struct to write the data to.
* @return an error message. When nullptr there has been no error.
* @return An error message, or std::nullopt there has been no error.
*/
const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket &cp)
std::optional<std::string_view> NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket &cp)
{
cp.company = (CompanyID)p.Recv_uint8();
cp.cmd = static_cast<Commands>(p.Recv_uint16());
if (!IsValidCommand(cp.cmd)) return "invalid command";
if (!IsValidCommand(cp.cmd)) return "invalid command";
if (GetCommandFlags(cp.cmd).Test(CommandFlag::Offline)) return "single-player only command";
cp.err_msg = p.Recv_uint16();
cp.data = _cmd_dispatch[cp.cmd].Sanitize(p.Recv_buffer());
uint8_t callback = p.Recv_uint8();
if (callback >= _callback_table.size() || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) return "invalid callback";
if (callback >= _callback_table.size() || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) return "invalid callback";
cp.callback = _callback_table[callback];
return nullptr;
return std::nullopt;
}
/**

View File

@ -1066,18 +1066,17 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
Debug(net, 9, "client[{}] Receive_CLIENT_COMMAND()", this->client_id);
CommandPacket cp;
const char *err = this->ReceiveCommand(p, cp);
auto err = this->ReceiveCommand(p, cp);
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
NetworkClientInfo *ci = this->GetInfo();
if (err != nullptr) {
IConsolePrint(CC_WARNING, "Dropping client #{} (IP: {}) due to {}.", ci->client_id, this->GetClientIP(), err);
if (err.has_value()) {
IConsolePrint(CC_WARNING, "Dropping client #{} (IP: {}) due to {}.", ci->client_id, this->GetClientIP(), *err);
return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
}
if (GetCommandFlags(cp.cmd).Test(CommandFlag::Server) && ci->client_id != CLIENT_ID_SERVER) {
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a server only command {}.", ci->client_id, this->GetClientIP(), cp.cmd);
return this->SendError(NETWORK_ERROR_KICKED);
@ -1925,7 +1924,7 @@ std::string_view ServerNetworkGameSocketHandler::GetClientIP()
/** Show the status message of all clients on the console. */
void NetworkServerShowStatusToConsole()
{
static const char * const stat_str[] = {
static const std::string_view stat_str[] = {
"inactive",
"authorizing",
"identifying client",
@ -1943,9 +1942,8 @@ void NetworkServerShowStatusToConsole()
NetworkClientInfo *ci = cs->GetInfo();
if (ci == nullptr) continue;
uint lag = NetworkCalculateLag(cs);
const char *status;
status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
std::string_view status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
IConsolePrint(CC_INFO, "Client #{} name: '{}' status: '{}' frame-lag: {} company: {} IP: {}",
cs->client_id, ci->client_name, status, lag,
ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),

View File

@ -110,7 +110,7 @@ public:
* Get the name used by the listener.
* @return the name to show in debug logs and the like.
*/
static const char *GetName()
static std::string_view GetName()
{
return "server";
}