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.
This commit is contained in:
Kawe Mazidjatari 2023-03-13 21:20:20 +01:00
parent c58d8f646a
commit 031eee80a1
2 changed files with 41 additions and 36 deletions

View File

@ -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<const char*>(&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<const char*>(&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<int>(ssSendBuf.str().size()), MSG_NOSIGNAL);
}
::send(pData->m_hSocket, ssSendBuf.str().data(), static_cast<int>(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

View File

@ -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 <password>' command.\n";
constexpr char s_pszNoAuthMessage[] = "This server is password protected for console access; authenticate with 'PASS <password>' 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: