From 031eee80a1a31cbab4aca97029372b6a7f93fa5d Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:20:20 +0100 Subject: [PATCH] RCONServer: send/serialize optimizations Check if we are initialized, have at least one socket, and in case of a console log, if we have at least 1 authorized netconsole before serializing the message and sending the result. --- r5dev/engine/server/sv_rcon.cpp | 74 +++++++++++++++++---------------- r5dev/engine/server/sv_rcon.h | 3 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/r5dev/engine/server/sv_rcon.cpp b/r5dev/engine/server/sv_rcon.cpp index c56e9903..4ce6c357 100644 --- a/r5dev/engine/server/sv_rcon.cpp +++ b/r5dev/engine/server/sv_rcon.cpp @@ -150,22 +150,20 @@ void CRConServer::RunFrame(void) //----------------------------------------------------------------------------- void CRConServer::Send(const std::string& svMessage) const { - if (const int nCount = m_Socket.GetAcceptedSocketCount()) + std::ostringstream ssSendBuf; + const u_long nLen = htonl(svMessage.size()); + + ssSendBuf.write(reinterpret_cast(&nLen), sizeof(u_long)); + ssSendBuf.write(svMessage.data(), svMessage.size()); + + const int nCount = m_Socket.GetAcceptedSocketCount(); + for (int i = nCount - 1; i >= 0; i--) { - std::ostringstream ssSendBuf; - const u_long nLen = htonl(svMessage.size()); + CConnectedNetConsoleData* pData = m_Socket.GetAcceptedSocketData(i); - ssSendBuf.write(reinterpret_cast(&nLen), sizeof(u_long)); - ssSendBuf.write(svMessage.data(), svMessage.size()); - - for (int i = nCount - 1; i >= 0; i--) + if (pData->m_bAuthorized) { - CConnectedNetConsoleData* pData = m_Socket.GetAcceptedSocketData(i); - - if (pData->m_bAuthorized) - { - ::send(pData->m_hSocket, ssSendBuf.str().data(), static_cast(ssSendBuf.str().size()), MSG_NOSIGNAL); - } + ::send(pData->m_hSocket, ssSendBuf.str().data(), static_cast(ssSendBuf.str().size()), MSG_NOSIGNAL); } } } @@ -195,19 +193,10 @@ void CRConServer::Send(const SocketHandle_t hSocket, const std::string& svMessag //----------------------------------------------------------------------------- void CRConServer::Send(const std::string& svRspBuf, const std::string& svRspVal, const sv_rcon::response_t responseType, const int nResponseId) { - if (!m_bInitialized) + if (this->ShouldSend(responseType)) { - return; + this->Send(this->Serialize(svRspBuf, svRspVal, responseType, nResponseId)); } - - if (responseType == sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG) - { - if (!sv_rcon_sendlogs->GetBool()) - { - return; - } - } - this->Send(this->Serialize(svRspBuf, svRspVal, responseType, nResponseId)); } //----------------------------------------------------------------------------- @@ -220,19 +209,10 @@ void CRConServer::Send(const std::string& svRspBuf, const std::string& svRspVal, //----------------------------------------------------------------------------- void CRConServer::Send(const SocketHandle_t hSocket, const std::string& svRspBuf, const std::string& svRspVal, const sv_rcon::response_t responseType, const int nResponseId) { - if (!m_bInitialized) + if (this->ShouldSend(responseType)) { - return; + this->Send(hSocket, this->Serialize(svRspBuf, svRspVal, responseType, nResponseId)); } - - if (responseType == sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG) - { - if (!sv_rcon_sendlogs->GetBool()) - { - return; - } - } - this->Send(hSocket, this->Serialize(svRspBuf, svRspVal, responseType, nResponseId)); } //----------------------------------------------------------------------------- @@ -612,6 +592,30 @@ void CRConServer::CloseNonAuthConnection(void) } } +//----------------------------------------------------------------------------- +// Purpose: checks if this message should be send or not +// Output : true if it should send, false otherwise +//----------------------------------------------------------------------------- +bool CRConServer::ShouldSend(const sv_rcon::response_t responseType) const +{ + if (!this->IsInitialized() || !m_Socket.GetAcceptedSocketCount()) + { + // Not initialized or no sockets... + return false; + } + + if (responseType == sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG) + { + if (!sv_rcon_sendlogs->GetBool() || !m_Socket.GetAuthorizedSocketCount()) + { + // Disabled or no authorized clients to send to... + return false; + } + } + + return true; +} + //----------------------------------------------------------------------------- // Purpose: checks if server rcon is initialized // Output : true if initialized, false otherwise diff --git a/r5dev/engine/server/sv_rcon.h b/r5dev/engine/server/sv_rcon.h index 0485a9b4..7f57ed9f 100644 --- a/r5dev/engine/server/sv_rcon.h +++ b/r5dev/engine/server/sv_rcon.h @@ -4,7 +4,7 @@ #include "protoc/sv_rcon.pb.h" #include "protoc/cl_rcon.pb.h" -constexpr char s_pszNoAuthMessage[] = "This server is password protected for console access. Authenticate with 'PASS ' command.\n"; +constexpr char s_pszNoAuthMessage[] = "This server is password protected for console access; authenticate with 'PASS ' command.\n"; constexpr char s_pszWrongPwMessage[] = "Admin password incorrect.\n"; constexpr char s_pszBannedMessage[] = "Go away.\n"; constexpr char s_pszAuthMessage[] = "Authentication successful.\n"; @@ -41,6 +41,7 @@ public: void CloseConnection(void); void CloseNonAuthConnection(void); + bool ShouldSend(const sv_rcon::response_t responseType) const; bool IsInitialized(void) const; private: