2021-12-25 22:36:38 +01:00
|
|
|
//=============================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Netchannel system utilities
|
|
|
|
//
|
|
|
|
//=============================================================================//
|
|
|
|
|
|
|
|
#include "core/stdafx.h"
|
2022-04-09 16:16:40 +02:00
|
|
|
#include "tier1/cvar.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
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel name
|
|
|
|
// Output : const char*
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-11 11:07:45 +02:00
|
|
|
const char* CNetChan::GetName(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-11 11:07:45 +02:00
|
|
|
return this->m_Name;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel address
|
|
|
|
// Output : const char*
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-11 11:07:45 +02:00
|
|
|
const char* CNetChan::GetAddress(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-11 11:07:45 +02:00
|
|
|
// Select a static buffer
|
|
|
|
static char s[4][INET6_ADDRSTRLEN];
|
|
|
|
static int slot = 0;
|
|
|
|
int useSlot = (slot++) % 4;
|
|
|
|
|
|
|
|
// Render into it
|
|
|
|
|
|
|
|
if (!inet_ntop(AF_INET6, &this->remote_address.adr, s[useSlot], sizeof(s[0])))
|
2022-04-06 00:49:17 +02:00
|
|
|
{
|
2022-07-21 15:13:48 +02:00
|
|
|
Warning(eDLL_T::ENGINE, "%s - Address conversion failed: %s", __FUNCTION__, NET_ErrorString(WSAGetLastError()));
|
2022-04-06 00:49:17 +02:00
|
|
|
}
|
2022-08-11 11:07:45 +02:00
|
|
|
|
|
|
|
// Pray the caller uses it before it gets clobbered
|
|
|
|
return s[useSlot];
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel data rate
|
|
|
|
// Output : int
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
int CNetChan::GetDataRate(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_Rate;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel buffer size (NET_FRAMES_BACKUP)
|
|
|
|
// Output : int
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
int CNetChan::GetBufferSize(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return NET_FRAMES_BACKUP;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel latency
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
float CNetChan::GetLatency(int flow) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_DataFlow[flow].latency;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel average choke
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetAvgChoke(int flow) const
|
|
|
|
{
|
|
|
|
return this->m_DataFlow[flow].avgchoke;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel average latency
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
float CNetChan::GetAvgLatency(int flow) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_DataFlow[flow].avglatency;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel average loss
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetAvgLoss(int flow) const
|
|
|
|
{
|
|
|
|
return this->m_DataFlow[flow].avgloss;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel average packets
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetAvgPackets(int flow) const
|
|
|
|
{
|
|
|
|
return this->m_DataFlow[flow].avgpacketspersec;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel average data
|
|
|
|
// Input : flow -
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetAvgData(int flow) const
|
|
|
|
{
|
|
|
|
return this->m_DataFlow[flow].avgbytespersec;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel total data
|
|
|
|
// Input : flow -
|
2022-08-14 18:35:01 +02:00
|
|
|
// Output : int64_t
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-14 18:35:01 +02:00
|
|
|
int64_t CNetChan::GetTotalData(int flow) const
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
|
|
|
return this->m_DataFlow[flow].totalbytes;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-02 02:48:54 +02:00
|
|
|
// Purpose: gets the netchannel total packets
|
|
|
|
// Input : flow -
|
2022-08-14 18:35:01 +02:00
|
|
|
// Output : int64_t
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-14 18:35:01 +02:00
|
|
|
int64_t CNetChan::GetTotalPackets(int flow) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_DataFlow[flow].totalpackets;
|
|
|
|
}
|
2021-12-26 04:14:28 +01:00
|
|
|
|
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
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->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
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->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
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
double CNetChan::GetConnectTime(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->connect_time;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel timeout
|
|
|
|
// Output : float
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float CNetChan::GetTimeoutSeconds(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_Timeout;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the netchannel socket
|
|
|
|
// Output : int
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int CNetChan::GetSocket(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_Socket;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: checks if the reliable stream is overflowed
|
|
|
|
// Output : true if overflowed, false otherwise
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool CNetChan::IsOverflowed(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
return this->m_StreamReliable.IsOverflowed();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-05-21 18:56:56 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: clears the netchannel
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CNetChan::Clear(bool bStopProcessing)
|
|
|
|
{
|
|
|
|
v_NetChan_Clear(this, bStopProcessing);
|
|
|
|
}
|
2022-09-17 23:34:36 +02:00
|
|
|
|
2022-09-18 23:19:50 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: process message
|
|
|
|
// Input : *pChan -
|
|
|
|
// *pMsg -
|
|
|
|
// Output : true on success, false on failure
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-17 23:34:36 +02:00
|
|
|
bool CNetChan::ProcessMessages(CNetChan* pChan, bf_read* pMsg)
|
|
|
|
{
|
2022-09-18 23:19:50 +02:00
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
if (!ThreadInServerFrameThread() || !net_processLimit->GetInt())
|
|
|
|
return v_NetChan_ProcessMessages(pChan, pMsg);
|
|
|
|
|
|
|
|
const double flStartTime = Plat_FloatTime();
|
|
|
|
const bool bResult = v_NetChan_ProcessMessages(pChan, pMsg);
|
|
|
|
|
|
|
|
CClient* pClient = reinterpret_cast<CClient*>(pChan->m_MessageHandler);
|
|
|
|
uint16_t nSlot = pClient->GetUserID();
|
|
|
|
ServerPlayer_t* pSlot = &g_ServerPlayer[nSlot];
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (pSlot->m_flCurrentNetProcessTime >=
|
|
|
|
net_processLimit->GetDouble())
|
|
|
|
{
|
|
|
|
pClient->Disconnect(2, "#DISCONNECT_NETCHAN_OVERFLOW");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void NetChan_Attach()
|
|
|
|
{
|
|
|
|
DetourAttach((LPVOID*)&v_NetChan_ProcessMessages, &CNetChan::ProcessMessages);
|
|
|
|
}
|
|
|
|
void NetChan_Detach()
|
|
|
|
{
|
|
|
|
DetourDetach((LPVOID*)&v_NetChan_ProcessMessages, &CNetChan::ProcessMessages);
|
2022-09-18 23:19:50 +02:00
|
|
|
}
|