Changeset - r28692:fcd1cb308544
[Not reviewed]
master
0 8 0
Rubidium - 10 months ago 2024-02-04 16:20:25
rubidium@openttd.org
Codechange: use reference instead of pointer for CommandPacket
8 files changed with 46 insertions and 46 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp_game.h
Show inline comments
 
@@ -530,8 +530,8 @@ public:
 

	
 
	NetworkRecvStatus ReceivePackets();
 

	
 
	const char *ReceiveCommand(Packet &p, CommandPacket *cp);
 
	void SendCommand(Packet &p, const CommandPacket *cp);
 
	const char *ReceiveCommand(Packet &p, CommandPacket &cp);
 
	void SendCommand(Packet &p, const CommandPacket &cp);
 

	
 
	bool IsPendingDeletion() const { return this->is_pending_deletion; }
 

	
src/network/network_admin.cpp
Show inline comments
 
@@ -608,15 +608,15 @@ NetworkRecvStatus ServerNetworkAdminSock
 
 * @param client_id The client executing the command.
 
 * @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);
 

	
 
	p->Send_uint32(client_id);
 
	p->Send_uint8 (cp->company);
 
	p->Send_uint16(cp->cmd);
 
	p->Send_buffer(cp->data);
 
	p->Send_uint32(cp->frame);
 
	p->Send_uint8 (cp.company);
 
	p->Send_uint16(cp.cmd);
 
	p->Send_buffer(cp.data);
 
	p->Send_uint32(cp.frame);
 

	
 
	this->SendPacket(std::move(p));
 

	
 
@@ -959,7 +959,7 @@ void NetworkAdminGameScript(const std::s
 
 * @param owner The owner of the CommandPacket (who sent us the CommandPacket).
 
 * @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;
 

	
src/network/network_admin.h
Show inline comments
 
@@ -67,7 +67,7 @@ public:
 
	NetworkRecvStatus SendConsole(const std::string_view origin, const std::string_view command);
 
	NetworkRecvStatus SendGameScript(const std::string_view json);
 
	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);
 

	
 
	static void Send();
 
@@ -112,6 +112,6 @@ void NetworkAdminUpdate(AdminUpdateFrequ
 
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 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 */
src/network/network_client.cpp
Show inline comments
 
@@ -435,9 +435,9 @@ NetworkRecvStatus ClientNetworkGameSocke
 
 * Send a command to the server.
 
 * @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);
 
	my_client->NetworkGameSocketHandler::SendCommand(*p, cp);
 
@@ -961,7 +961,7 @@ NetworkRecvStatus ClientNetworkGameSocke
 
	if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 

	
 
	CommandPacket cp;
 
	const char *err = this->ReceiveCommand(p, &cp);
 
	const char *err = this->ReceiveCommand(p, cp);
 
	cp.frame    = p.Recv_uint32();
 
	cp.my_cmd   = p.Recv_bool();
 

	
src/network/network_client.h
Show inline comments
 
@@ -80,7 +80,7 @@ public:
 
	void ClientError(NetworkRecvStatus res);
 

	
 
	static NetworkRecvStatus SendJoin();
 
	static NetworkRecvStatus SendCommand(const CommandPacket *cp);
 
	static NetworkRecvStatus SendCommand(const CommandPacket &cp);
 
	static NetworkRecvStatus SendError(NetworkErrorCode errorno);
 
	static NetworkRecvStatus SendQuit();
 
	static NetworkRecvStatus SendAck();
src/network/network_command.cpp
Show inline comments
 
@@ -122,9 +122,9 @@ struct CallbackArgsHelper<void(*const)(C
 
/* Helpers to generate the command dispatch table from the command traits. */
 

	
 
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);
 
using UnpackNetworkCommandProc = void (*)(const CommandPacket *);
 
using UnpackNetworkCommandProc = void (*)(const CommandPacket &);
 
using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>;
 
struct CommandDispatch {
 
	CommandDataBuffer(*Sanitize)(const CommandDataBuffer &);
 
@@ -219,7 +219,7 @@ void NetworkSendCommand(Commands cmd, St
 
	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 */
 
	MyClient::SendCommand(&c);
 
	MyClient::SendCommand(c);
 
}
 

	
 
/**
 
@@ -265,7 +265,7 @@ void NetworkExecuteLocalCommandQueue()
 
		size_t cb_index = FindCallbackIndex(cp->callback);
 
		assert(cb_index < _callback_tuple_size);
 
		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);
 

	
 
@@ -337,7 +337,7 @@ static void DistributeQueue(CommandQueue
 
		}
 

	
 
		DistributeCommandPacket(*cp, owner);
 
		NetworkAdminCmdLogging(owner, &*cp);
 
		NetworkAdminCmdLogging(owner, *cp);
 
		cp = queue.erase(cp);
 
	}
 
}
 
@@ -360,19 +360,19 @@ void NetworkDistributeCommands()
 
 * @param cp the struct to write the data to.
 
 * @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->cmd     = static_cast<Commands>(p.Recv_uint16());
 
	if (!IsValidCommand(cp->cmd))               return "invalid command";
 
	if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
 
	cp->err_msg = p.Recv_uint16();
 
	cp->data    = _cmd_dispatch[cp->cmd].Sanitize(p.Recv_buffer());
 
	cp.company = (CompanyID)p.Recv_uint8();
 
	cp.cmd     = static_cast<Commands>(p.Recv_uint16());
 
	if (!IsValidCommand(cp.cmd))               return "invalid command";
 
	if (GetCommandFlags(cp.cmd) & CMD_OFFLINE) return "single-player only command";
 
	cp.err_msg = p.Recv_uint16();
 
	cp.data    = _cmd_dispatch[cp.cmd].Sanitize(p.Recv_buffer());
 

	
 
	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;
 
}
 

	
 
@@ -381,16 +381,16 @@ const char *NetworkGameSocketHandler::Re
 
 * @param p the packet to send it in.
 
 * @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_uint16(cp->cmd);
 
	p.Send_uint16(cp->err_msg);
 
	p.Send_buffer(cp->data);
 
	p.Send_uint8(cp.company);
 
	p.Send_uint16(cp.cmd);
 
	p.Send_uint16(cp.err_msg);
 
	p.Send_buffer(cp.data);
 

	
 
	size_t callback = FindCallbackIndex(cp->callback);
 
	if (callback > UINT8_MAX || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) {
 
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
 
	size_t callback = FindCallbackIndex(cp.callback);
 
	if (callback > UINT8_MAX || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) {
 
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp.cmd);
 
		callback = 0; // _callback_table[0] == nullptr
 
	}
 
	p.Send_uint8 ((uint8_t)callback);
 
@@ -473,8 +473,8 @@ CommandDataBuffer SanitizeCmdStrings(con
 
 * @param cp Command packet to unpack.
 
 */
 
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);
 
	Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, args);
 
	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);
 
}
src/network/network_server.cpp
Show inline comments
 
@@ -656,15 +656,15 @@ NetworkRecvStatus ServerNetworkGameSocke
 
 * Send a command to the client to execute.
 
 * @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);
 

	
 
	this->NetworkGameSocketHandler::SendCommand(*p, cp);
 
	p->Send_uint32(cp->frame);
 
	p->Send_bool  (cp->my_cmd);
 
	p->Send_uint32(cp.frame);
 
	p->Send_bool  (cp.my_cmd);
 

	
 
	this->SendPacket(std::move(p));
 
	return NETWORK_RECV_STATUS_OKAY;
 
@@ -1067,7 +1067,7 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	Debug(net, 9, "client[{}] Receive_CLIENT_COMMAND()", this->client_id);
 

	
 
	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;
 

	
 
@@ -1701,7 +1701,7 @@ void NetworkServerSetCompanyPassword(Com
 
 */
 
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();
 
}
 

	
src/network/network_server.h
Show inline comments
 
@@ -97,7 +97,7 @@ public:
 
	NetworkRecvStatus SendJoin(ClientID client_id);
 
	NetworkRecvStatus SendFrame();
 
	NetworkRecvStatus SendSync();
 
	NetworkRecvStatus SendCommand(const CommandPacket *cp);
 
	NetworkRecvStatus SendCommand(const CommandPacket &cp);
 
	NetworkRecvStatus SendCompanyUpdate();
 
	NetworkRecvStatus SendConfigUpdate();
 

	
0 comments (0 inline, 0 general)