[Network]: Pass Address obj by reference (#867)

This commit is contained in:
Edo 2023-03-22 12:55:39 +00:00 committed by GitHub
parent 5766ecef1f
commit cb24ddabf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 37 deletions

View File

@ -28,25 +28,19 @@ namespace Components
void Logger::MessagePrint(const int channel, const std::string& msg)
{
std::string out = msg;
// Filter out coloured strings for stdout
if (out[0] == '^' && out[1] != '\0')
{
out = out.substr(2);
}
if (Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests())
{
printf("%s", out.data());
fflush(stdout);
std::printf("%s", msg.data());
std::fflush(stdout);
return;
}
#ifdef _DEBUG
if (!IsConsoleReady())
{
OutputDebugStringA(out.data());
OutputDebugStringA(msg.data());
}
#endif
if (!Game::Sys_IsMainThread())
{
@ -141,7 +135,7 @@ namespace Components
void Logger::NetworkLog(const char* data, bool gLog)
{
if (data == nullptr)
if (!data)
{
return;
}
@ -163,7 +157,7 @@ namespace Components
va_end(ap);
const auto time = Game::level->time / 1000;
const auto len = _snprintf_s(string, _TRUNCATE, "%3i:%i%i %s", time / 60, time % 60 / 10, time % 60 % 10, string2);
const auto len = sprintf_s(string, "%3i:%i%i %s", time / 60, time % 60 / 10, time % 60 % 10, string2);
if (Game::level->logFile)
{
@ -183,25 +177,29 @@ namespace Components
jz returnPrint
pushad
push [esp + 28h]
call PrintMessagePipe
add esp, 4h
popad
retn
ret
returnPrint:
pushad
push 0
push [esp + 2Ch]
push 0 // gLog
push [esp + 2Ch] // data
call NetworkLog
add esp, 8h
popad
push esi
mov esi, [esp + 0Ch]
push 4AA835h
retn
ret
}
}

View File

@ -6,13 +6,21 @@ namespace Components
// Packet interception
std::unordered_map<std::string, Network::NetworkCallback> Network::CL_Callbacks;
Network::Address::Address()
{
ZeroMemory(&this->address, sizeof(Game::netadr_t));
this->setType(Game::NA_BAD);
}
Network::Address::Address(const std::string& addrString)
{
ZeroMemory(&this->address, sizeof(Game::netadr_t));
Game::NET_StringToAdr(addrString.data(), &this->address);
}
Network::Address::Address(sockaddr* addr)
{
ZeroMemory(&this->address, sizeof(Game::netadr_t));
Game::SockadrToNetadr(addr, &this->address);
}
@ -23,7 +31,7 @@ namespace Components
void Network::Address::setPort(unsigned short port)
{
this->address.port = htons(port);
this->address.port = ::htons(port);
}
unsigned short Network::Address::getPort() const
@ -76,7 +84,7 @@ namespace Components
this->toSockAddr(reinterpret_cast<sockaddr*>(addr));
}
Game::netadr_t* Network::Address::get()
const Game::netadr_t* Network::Address::get() const noexcept
{
return &this->address;
}
@ -88,7 +96,7 @@ namespace Components
std::string Network::Address::getString() const
{
return {this->getCString()};
return std::string{ this->getCString() };
}
bool Network::Address::isLocal() const noexcept
@ -148,7 +156,7 @@ namespace Components
StartupSignal.connect(callback);
}
void Network::Send(Game::netsrc_t type, Address target, const std::string& data)
void Network::Send(Game::netsrc_t type, const Address& target, const std::string& data)
{
// Do not use NET_OutOfBandPrint. It only supports non-binary data!
@ -159,12 +167,12 @@ namespace Components
SendRaw(type, target, rawData);
}
void Network::Send(Address target, const std::string& data)
void Network::Send(const Address& target, const std::string& data)
{
Send(Game::netsrc_t::NS_CLIENT1, target, data);
}
void Network::SendRaw(Game::netsrc_t type, Address target, const std::string& data)
void Network::SendRaw(Game::netsrc_t type, const Address& target, const std::string& data)
{
if (!target.isValid()) return;
@ -172,12 +180,12 @@ namespace Components
Game::Sys_SendPacket(type, data.size(), data.data(), *target.get());
}
void Network::SendRaw(Address target, const std::string& data)
void Network::SendRaw(const Address& target, const std::string& data)
{
SendRaw(Game::NS_CLIENT1, target, data);
}
void Network::SendCommand(Game::netsrc_t type, Address target, const std::string& command, const std::string& data)
void Network::SendCommand(Game::netsrc_t type, const Address& target, const std::string& command, const std::string& data)
{
// Use space as separator (possible separators are '\n', ' ').
// Though, our handler only needs exactly 1 char as separator and doesn't care which char it is.
@ -190,7 +198,7 @@ namespace Components
Send(type, target, packet);
}
void Network::SendCommand(Address target, const std::string& command, const std::string& data)
void Network::SendCommand(const Address& target, const std::string& command, const std::string& data)
{
SendCommand(Game::NS_CLIENT1, target, command, data);
}
@ -293,8 +301,8 @@ namespace Components
const std::string data(reinterpret_cast<char*>(message->data) + offset, message->cursize - offset);
auto address_ = Address(address);
handler->second(address_, data);
auto target = Address{ address };
handler->second(target, data);
return true;
}

View File

@ -8,7 +8,7 @@ namespace Components
class Address
{
public:
Address() { setType(Game::NA_BAD); }
Address();
Address(const std::string& addrString);
Address(sockaddr* addr);
Address(sockaddr addr) : Address(&addr) {}
@ -33,7 +33,7 @@ namespace Components
[[nodiscard]] sockaddr getSockAddr();
void toSockAddr(sockaddr* addr);
void toSockAddr(sockaddr_in* addr);
Game::netadr_t* get();
[[nodiscard]] const Game::netadr_t* get() const noexcept;
[[nodiscard]] const char* getCString() const;
[[nodiscard]] std::string getString() const;
@ -57,16 +57,16 @@ namespace Components
static void OnStart(const Utils::Slot<CallbackRaw>& callback);
// Send quake-styled binary data
static void Send(Address target, const std::string& data);
static void Send(Game::netsrc_t type, Address target, const std::string& data);
static void Send(const Address& target, const std::string& data);
static void Send(Game::netsrc_t type, const Address& target, const std::string& data);
// Allows sending raw data without quake header
static void SendRaw(Address target, const std::string& data);
static void SendRaw(Game::netsrc_t type, Address target, const std::string& data);
static void SendRaw(const Address& target, const std::string& data);
static void SendRaw(Game::netsrc_t type, const Address& target, const std::string& data);
// Send quake-style command using binary data
static void SendCommand(Address target, const std::string& command, const std::string& data = {});
static void SendCommand(Game::netsrc_t type, Address target, const std::string& command, const std::string& data = {});
static void SendCommand(const Address& target, const std::string& command, const std::string& data = {});
static void SendCommand(Game::netsrc_t type, const Address& target, const std::string& command, const std::string& data = {});
static void Broadcast(unsigned short port, const std::string& data);
static void BroadcastRange(unsigned int min, unsigned int max, const std::string& data);

View File

@ -119,7 +119,7 @@ namespace Components
{
sv_allowColoredNames = Dvar::Register<bool>("sv_allowColoredNames", true, Game::DVAR_NONE, "Allow colored names on the server");
// Disable SV_UpdateUserinfo_f, to block changing the name ingame
// Disable SV_UpdateUserinfo_f to block changing the name ingame
Utils::Hook::Set<BYTE>(0x6258D0, 0xC3);
// Allow colored names ingame. Hook placed in ClientUserinfoChanged