1
0
Fork 0

Codechange: use reference instead of pointer for CommandPacket

pull/12011/head
Rubidium 2024-02-04 17:20:25 +01:00 committed by rubidium42
parent cb588d8d3f
commit 3534214dfc
8 changed files with 46 additions and 46 deletions

View File

@ -530,8 +530,8 @@ public:
NetworkRecvStatus ReceivePackets(); NetworkRecvStatus ReceivePackets();
const char *ReceiveCommand(Packet &p, CommandPacket *cp); const char *ReceiveCommand(Packet &p, CommandPacket &cp);
void SendCommand(Packet &p, const CommandPacket *cp); void SendCommand(Packet &p, const CommandPacket &cp);
bool IsPendingDeletion() const { return this->is_pending_deletion; } bool IsPendingDeletion() const { return this->is_pending_deletion; }

View File

@ -608,15 +608,15 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdNames()
* @param client_id The client executing the command. * @param client_id The client executing the command.
* @param cp The command that would be executed. * @param cp The command that would be executed.
*/ */
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID client_id, const CommandPacket *cp) NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID client_id, const CommandPacket &cp)
{ {
auto p = std::make_unique<Packet>(ADMIN_PACKET_SERVER_CMD_LOGGING); auto p = std::make_unique<Packet>(ADMIN_PACKET_SERVER_CMD_LOGGING);
p->Send_uint32(client_id); p->Send_uint32(client_id);
p->Send_uint8 (cp->company); p->Send_uint8 (cp.company);
p->Send_uint16(cp->cmd); p->Send_uint16(cp.cmd);
p->Send_buffer(cp->data); p->Send_buffer(cp.data);
p->Send_uint32(cp->frame); p->Send_uint32(cp.frame);
this->SendPacket(std::move(p)); this->SendPacket(std::move(p));
@ -959,7 +959,7 @@ void NetworkAdminGameScript(const std::string_view json)
* @param owner The owner of the CommandPacket (who sent us the CommandPacket). * @param owner The owner of the CommandPacket (who sent us the CommandPacket).
* @param cp The CommandPacket to be distributed. * @param cp The CommandPacket to be distributed.
*/ */
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp) void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp)
{ {
ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id; ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id;

View File

@ -67,7 +67,7 @@ public:
NetworkRecvStatus SendConsole(const std::string_view origin, const std::string_view command); NetworkRecvStatus SendConsole(const std::string_view origin, const std::string_view command);
NetworkRecvStatus SendGameScript(const std::string_view json); NetworkRecvStatus SendGameScript(const std::string_view json);
NetworkRecvStatus SendCmdNames(); NetworkRecvStatus SendCmdNames();
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp); NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket &cp);
NetworkRecvStatus SendRconEnd(const std::string_view command); NetworkRecvStatus SendRconEnd(const std::string_view command);
static void Send(); static void Send();
@ -112,6 +112,6 @@ void NetworkAdminUpdate(AdminUpdateFrequency freq);
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string); void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string);
void NetworkAdminConsole(const std::string_view origin, const std::string_view string); void NetworkAdminConsole(const std::string_view origin, const std::string_view string);
void NetworkAdminGameScript(const std::string_view json); void NetworkAdminGameScript(const std::string_view json);
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp); void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp);
#endif /* NETWORK_ADMIN_H */ #endif /* NETWORK_ADMIN_H */

View File

@ -435,9 +435,9 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendAck()
* Send a command to the server. * Send a command to the server.
* @param cp The command to send. * @param cp The command to send.
*/ */
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket *cp) NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket &cp)
{ {
Debug(net, 9, "Client::SendCommand(): cmd={}", cp->cmd); Debug(net, 9, "Client::SendCommand(): cmd={}", cp.cmd);
auto p = std::make_unique<Packet>(PACKET_CLIENT_COMMAND); auto p = std::make_unique<Packet>(PACKET_CLIENT_COMMAND);
my_client->NetworkGameSocketHandler::SendCommand(*p, cp); my_client->NetworkGameSocketHandler::SendCommand(*p, cp);
@ -961,7 +961,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet
if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
CommandPacket cp; CommandPacket cp;
const char *err = this->ReceiveCommand(p, &cp); const char *err = this->ReceiveCommand(p, cp);
cp.frame = p.Recv_uint32(); cp.frame = p.Recv_uint32();
cp.my_cmd = p.Recv_bool(); cp.my_cmd = p.Recv_bool();

View File

@ -80,7 +80,7 @@ public:
void ClientError(NetworkRecvStatus res); void ClientError(NetworkRecvStatus res);
static NetworkRecvStatus SendJoin(); static NetworkRecvStatus SendJoin();
static NetworkRecvStatus SendCommand(const CommandPacket *cp); static NetworkRecvStatus SendCommand(const CommandPacket &cp);
static NetworkRecvStatus SendError(NetworkErrorCode errorno); static NetworkRecvStatus SendError(NetworkErrorCode errorno);
static NetworkRecvStatus SendQuit(); static NetworkRecvStatus SendQuit();
static NetworkRecvStatus SendAck(); static NetworkRecvStatus SendAck();

View File

@ -122,9 +122,9 @@ struct CallbackArgsHelper<void(*const)(Commands, const CommandCost &, Targs...)>
/* Helpers to generate the command dispatch table from the command traits. */ /* Helpers to generate the command dispatch table from the command traits. */
template <Commands Tcmd> static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data); template <Commands Tcmd> static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data);
template <Commands Tcmd, size_t cb> static void UnpackNetworkCommand(const CommandPacket *cp); template <Commands Tcmd, size_t cb> static void UnpackNetworkCommand(const CommandPacket &cp);
template <Commands Tcmd> static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id); template <Commands Tcmd> static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
using UnpackNetworkCommandProc = void (*)(const CommandPacket *); using UnpackNetworkCommandProc = void (*)(const CommandPacket &);
using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>; using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>;
struct CommandDispatch { struct CommandDispatch {
CommandDataBuffer(*Sanitize)(const CommandDataBuffer &); CommandDataBuffer(*Sanitize)(const CommandDataBuffer &);
@ -219,7 +219,7 @@ void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *cal
c.frame = 0; // The client can't tell which frame, so just make it 0 c.frame = 0; // The client can't tell which frame, so just make it 0
/* Clients send their command to the server and forget all about the packet */ /* Clients send their command to the server and forget all about the packet */
MyClient::SendCommand(&c); MyClient::SendCommand(c);
} }
/** /**
@ -265,7 +265,7 @@ void NetworkExecuteLocalCommandQueue()
size_t cb_index = FindCallbackIndex(cp->callback); size_t cb_index = FindCallbackIndex(cp->callback);
assert(cb_index < _callback_tuple_size); assert(cb_index < _callback_tuple_size);
assert(_cmd_dispatch[cp->cmd].Unpack[cb_index] != nullptr); assert(_cmd_dispatch[cp->cmd].Unpack[cb_index] != nullptr);
_cmd_dispatch[cp->cmd].Unpack[cb_index](&*cp); _cmd_dispatch[cp->cmd].Unpack[cb_index](*cp);
} }
queue.erase(queue.begin(), cp); queue.erase(queue.begin(), cp);
@ -337,7 +337,7 @@ static void DistributeQueue(CommandQueue &queue, const NetworkClientSocket *owne
} }
DistributeCommandPacket(*cp, owner); DistributeCommandPacket(*cp, owner);
NetworkAdminCmdLogging(owner, &*cp); NetworkAdminCmdLogging(owner, *cp);
cp = queue.erase(cp); cp = queue.erase(cp);
} }
} }
@ -360,19 +360,19 @@ void NetworkDistributeCommands()
* @param cp the struct to write the data to. * @param cp the struct to write the data to.
* @return an error message. When nullptr there has been no error. * @return an error message. When nullptr there has been no error.
*/ */
const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket *cp) const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket &cp)
{ {
cp->company = (CompanyID)p.Recv_uint8(); cp.company = (CompanyID)p.Recv_uint8();
cp->cmd = static_cast<Commands>(p.Recv_uint16()); 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) & CMD_OFFLINE) return "single-player only command"; if (GetCommandFlags(cp.cmd) & CMD_OFFLINE) return "single-player only command";
cp->err_msg = p.Recv_uint16(); cp.err_msg = p.Recv_uint16();
cp->data = _cmd_dispatch[cp->cmd].Sanitize(p.Recv_buffer()); cp.data = _cmd_dispatch[cp.cmd].Sanitize(p.Recv_buffer());
byte callback = p.Recv_uint8(); byte 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]; cp.callback = _callback_table[callback];
return nullptr; return nullptr;
} }
@ -381,16 +381,16 @@ const char *NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket *c
* @param p the packet to send it in. * @param p the packet to send it in.
* @param cp the packet to actually send. * @param cp the packet to actually send.
*/ */
void NetworkGameSocketHandler::SendCommand(Packet &p, const CommandPacket *cp) void NetworkGameSocketHandler::SendCommand(Packet &p, const CommandPacket &cp)
{ {
p.Send_uint8(cp->company); p.Send_uint8(cp.company);
p.Send_uint16(cp->cmd); p.Send_uint16(cp.cmd);
p.Send_uint16(cp->err_msg); p.Send_uint16(cp.err_msg);
p.Send_buffer(cp->data); p.Send_buffer(cp.data);
size_t callback = FindCallbackIndex(cp->callback); size_t callback = FindCallbackIndex(cp.callback);
if (callback > UINT8_MAX || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) { if (callback > UINT8_MAX || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) {
Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd); Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp.cmd);
callback = 0; // _callback_table[0] == nullptr callback = 0; // _callback_table[0] == nullptr
} }
p.Send_uint8 ((uint8_t)callback); p.Send_uint8 ((uint8_t)callback);
@ -473,8 +473,8 @@ CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data)
* @param cp Command packet to unpack. * @param cp Command packet to unpack.
*/ */
template <Commands Tcmd, size_t Tcb> template <Commands Tcmd, size_t Tcb>
void UnpackNetworkCommand(const CommandPacket *cp) void UnpackNetworkCommand(const CommandPacket &cp)
{ {
auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp->data); auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp.data);
Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, args); Command<Tcmd>::PostFromNet(cp.err_msg, std::get<Tcb>(_callback_tuple), cp.my_cmd, args);
} }

View File

@ -656,15 +656,15 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync()
* Send a command to the client to execute. * Send a command to the client to execute.
* @param cp The command to send. * @param cp The command to send.
*/ */
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket *cp) NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket &cp)
{ {
Debug(net, 9, "client[{}] SendCommand(): cmd={}", this->client_id, cp->cmd); Debug(net, 9, "client[{}] SendCommand(): cmd={}", this->client_id, cp.cmd);
auto p = std::make_unique<Packet>(PACKET_SERVER_COMMAND); auto p = std::make_unique<Packet>(PACKET_SERVER_COMMAND);
this->NetworkGameSocketHandler::SendCommand(*p, cp); this->NetworkGameSocketHandler::SendCommand(*p, cp);
p->Send_uint32(cp->frame); p->Send_uint32(cp.frame);
p->Send_bool (cp->my_cmd); p->Send_bool (cp.my_cmd);
this->SendPacket(std::move(p)); this->SendPacket(std::move(p));
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
@ -1067,7 +1067,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
Debug(net, 9, "client[{}] Receive_CLIENT_COMMAND()", this->client_id); Debug(net, 9, "client[{}] Receive_CLIENT_COMMAND()", this->client_id);
CommandPacket cp; CommandPacket cp;
const char *err = this->ReceiveCommand(p, &cp); const char *err = this->ReceiveCommand(p, cp);
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT; if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
@ -1701,7 +1701,7 @@ void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &pa
*/ */
static void NetworkHandleCommandQueue(NetworkClientSocket *cs) static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
{ {
for (auto &cp : cs->outgoing_queue) cs->SendCommand(&cp); for (auto &cp : cs->outgoing_queue) cs->SendCommand(cp);
cs->outgoing_queue.clear(); cs->outgoing_queue.clear();
} }

View File

@ -97,7 +97,7 @@ public:
NetworkRecvStatus SendJoin(ClientID client_id); NetworkRecvStatus SendJoin(ClientID client_id);
NetworkRecvStatus SendFrame(); NetworkRecvStatus SendFrame();
NetworkRecvStatus SendSync(); NetworkRecvStatus SendSync();
NetworkRecvStatus SendCommand(const CommandPacket *cp); NetworkRecvStatus SendCommand(const CommandPacket &cp);
NetworkRecvStatus SendCompanyUpdate(); NetworkRecvStatus SendCompanyUpdate();
NetworkRecvStatus SendConfigUpdate(); NetworkRecvStatus SendConfigUpdate();