2021-12-25 22:36:38 +01:00
|
|
|
//=============================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Netchannel system utilities
|
|
|
|
//
|
|
|
|
//=============================================================================//
|
|
|
|
|
|
|
|
#include "core/stdafx.h"
|
2023-05-16 00:44:59 +02:00
|
|
|
#include "tier0/frametask.h"
|
2022-04-09 16:16:40 +02:00
|
|
|
#include "tier1/cvar.h"
|
2023-05-16 00:44:59 +02:00
|
|
|
#include "vpc/keyvalues.h"
|
|
|
|
#include "common/callback.h"
|
2022-04-06 00:49:17 +02:00
|
|
|
#include "engine/net.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "engine/net_chan.h"
|
2022-09-18 23:19:50 +02:00
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
#include "engine/server/server.h"
|
|
|
|
#include "engine/client/client.h"
|
|
|
|
#include "server/vengineserver_impl.h"
|
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-06-22 00:33:00 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel network loss
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetNetworkLoss() const
|
|
|
|
{
|
|
|
|
float v1 = *&m_DataFlow[1].frames[0].one;
|
|
|
|
if (!v1 && !m_nSequencesSkipped_MAYBE)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
float v4 = (v1 + m_nSequencesSkipped_MAYBE);
|
|
|
|
if (v1 + m_nSequencesSkipped_MAYBE < 0)
|
|
|
|
v4 = v4 + float(2 ^ 64);
|
|
|
|
|
|
|
|
return m_nSequencesSkipped_MAYBE / v4;
|
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel sequence number
|
|
|
|
// Input : flow -
|
|
|
|
// Output : int
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int CNetChan::GetSequenceNr(int flow) const
|
|
|
|
{
|
|
|
|
if (flow == FLOW_OUTGOING)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-07-12 17:43:55 +02:00
|
|
|
return m_nOutSequenceNr;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-04-02 02:48:54 +02:00
|
|
|
else if (flow == FLOW_INCOMING)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-07-12 17:43:55 +02:00
|
|
|
return m_nInSequenceNr;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
return NULL;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel connect time
|
|
|
|
// Output : double
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-02-15 20:49:14 +01:00
|
|
|
double CNetChan::GetTimeConnected(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-02-15 20:49:14 +01:00
|
|
|
double t = *g_pNetTime - connect_time;
|
|
|
|
return (t > 0.0) ? t : 0.0;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2023-05-16 00:44:59 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: shutdown netchannel
|
|
|
|
// Input : *this -
|
|
|
|
// *szReason -
|
|
|
|
// bBadRep -
|
|
|
|
// bRemoveNow -
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CNetChan::_Shutdown(CNetChan* pChan, const char* szReason, uint8_t bBadRep, bool bRemoveNow)
|
|
|
|
{
|
|
|
|
v_NetChan_Shutdown(pChan, szReason, bBadRep, bRemoveNow);
|
|
|
|
}
|
|
|
|
|
2022-09-18 23:19:50 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: process message
|
|
|
|
// Input : *pChan -
|
|
|
|
// *pMsg -
|
|
|
|
// Output : true on success, false on failure
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-05-16 00:44:59 +02:00
|
|
|
bool CNetChan::_ProcessMessages(CNetChan* pChan, bf_read* pMsg)
|
2022-09-17 23:34:36 +02:00
|
|
|
{
|
2022-09-18 23:19:50 +02:00
|
|
|
#ifndef CLIENT_DLL
|
2022-09-20 02:00:52 +02:00
|
|
|
if (!ThreadInServerFrameThread() || !net_processTimeBudget->GetInt())
|
2022-09-18 23:19:50 +02:00
|
|
|
return v_NetChan_ProcessMessages(pChan, pMsg);
|
|
|
|
|
|
|
|
const double flStartTime = Plat_FloatTime();
|
|
|
|
const bool bResult = v_NetChan_ProcessMessages(pChan, pMsg);
|
|
|
|
|
2022-09-20 02:00:52 +02:00
|
|
|
if (!pChan->m_MessageHandler) // NetChannel removed?
|
|
|
|
return bResult;
|
|
|
|
|
2022-09-18 23:19:50 +02:00
|
|
|
CClient* pClient = reinterpret_cast<CClient*>(pChan->m_MessageHandler);
|
2022-09-19 01:28:43 +02:00
|
|
|
ServerPlayer_t* pSlot = &g_ServerPlayer[pClient->GetUserID()];
|
2022-09-18 23:19:50 +02:00
|
|
|
|
|
|
|
if (flStartTime - pSlot->m_flLastNetProcessTime >= 1.0 ||
|
|
|
|
pSlot->m_flLastNetProcessTime == -1.0)
|
|
|
|
{
|
|
|
|
pSlot->m_flLastNetProcessTime = flStartTime;
|
|
|
|
pSlot->m_flCurrentNetProcessTime = 0.0;
|
|
|
|
}
|
|
|
|
pSlot->m_flCurrentNetProcessTime +=
|
|
|
|
(Plat_FloatTime() * 1000) - (flStartTime * 1000);
|
|
|
|
|
2022-09-19 01:28:43 +02:00
|
|
|
if (pSlot->m_flCurrentNetProcessTime >
|
2022-09-20 02:00:52 +02:00
|
|
|
net_processTimeBudget->GetDouble())
|
2022-09-18 23:19:50 +02:00
|
|
|
{
|
2022-11-25 23:03:56 +01:00
|
|
|
Warning(eDLL_T::SERVER, "Removing netchannel '%s' ('%s' exceeded frame budget by '%3.1f'ms!)\n",
|
2022-09-20 02:00:52 +02:00
|
|
|
pChan->GetName(), pChan->GetAddress(), (pSlot->m_flCurrentNetProcessTime - net_processTimeBudget->GetDouble()));
|
2022-09-20 02:04:25 +02:00
|
|
|
pClient->Disconnect(Reputation_t::REP_MARK_BAD, "#DISCONNECT_NETCHAN_OVERFLOW");
|
2022-09-20 02:00:52 +02:00
|
|
|
|
2022-09-18 23:19:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
#else // !CLIENT_DLL
|
2022-09-17 23:34:36 +02:00
|
|
|
return v_NetChan_ProcessMessages(pChan, pMsg);
|
2022-09-18 23:19:50 +02:00
|
|
|
#endif
|
2022-09-17 23:34:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:19:28 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: send message
|
|
|
|
// Input : &msg -
|
|
|
|
// bForceReliable -
|
|
|
|
// bVoice -
|
|
|
|
// Output : true on success, false on failure
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool CNetChan::SendNetMsg(INetMessage& msg, bool bForceReliable, bool bVoice)
|
|
|
|
{
|
|
|
|
if (remote_address.GetType() == netadrtype_t::NA_NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bf_write* pStream = &m_StreamUnreliable;
|
|
|
|
|
|
|
|
if (msg.IsReliable() || bForceReliable)
|
|
|
|
pStream = &m_StreamReliable;
|
|
|
|
|
|
|
|
if (bVoice)
|
|
|
|
pStream = &m_StreamVoice;
|
|
|
|
|
|
|
|
if (pStream != &m_StreamUnreliable ||
|
|
|
|
pStream->GetNumBytesLeft() >= NET_UNRELIABLE_STREAM_MINSIZE)
|
|
|
|
{
|
|
|
|
AcquireSRWLockExclusive(&LOCK);
|
|
|
|
|
|
|
|
pStream->WriteUBitLong(msg.GetType(), NETMSG_TYPE_BITS);
|
|
|
|
if (!pStream->IsOverflowed())
|
|
|
|
msg.WriteToBuffer(pStream);
|
|
|
|
|
|
|
|
ReleaseSRWLockExclusive(&LOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-17 23:34:36 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2023-02-18 00:05:55 +01:00
|
|
|
void VNetChan::Attach() const
|
2022-09-17 23:34:36 +02:00
|
|
|
{
|
2023-05-16 00:44:59 +02:00
|
|
|
DetourAttach((PVOID*)&v_NetChan_Shutdown, &CNetChan::_Shutdown);
|
|
|
|
DetourAttach((PVOID*)&v_NetChan_ProcessMessages, &CNetChan::_ProcessMessages);
|
2022-09-17 23:34:36 +02:00
|
|
|
}
|
2023-02-18 00:05:55 +01:00
|
|
|
void VNetChan::Detach() const
|
2022-09-17 23:34:36 +02:00
|
|
|
{
|
2023-05-16 00:44:59 +02:00
|
|
|
DetourDetach((PVOID*)&v_NetChan_Shutdown, &CNetChan::_Shutdown);
|
|
|
|
DetourDetach((PVOID*)&v_NetChan_ProcessMessages, &CNetChan::_ProcessMessages);
|
2022-09-18 23:19:50 +02:00
|
|
|
}
|