From a5667b7dbc783627bb5038fa1fe8410f7ad3b18b Mon Sep 17 00:00:00 2001 From: s1lentq Date: Fri, 5 Feb 2016 03:51:42 +0600 Subject: [PATCH] Rethink authorize a client --- revoice/src/revoice_main.cpp | 58 ++++++++++++---------------------- revoice/src/revoice_player.cpp | 50 +++++++++++++++++++---------- revoice/src/revoice_player.h | 16 ++++++---- 3 files changed, 64 insertions(+), 60 deletions(-) diff --git a/revoice/src/revoice_main.cpp b/revoice/src/revoice_main.cpp index 12ed5c2..f04a0c3 100644 --- a/revoice/src/revoice_main.cpp +++ b/revoice/src/revoice_main.cpp @@ -3,44 +3,29 @@ cvar_t* pcv_sv_voiceenable = NULL; -void Rehlds_ClientConnected_Hook(IRehldsHook_ClientConnected* chain, IGameClient* cl) { - int protocol = g_ReunionApi->GetClientProtocol(cl->GetId()); - if (protocol == 47 || protocol == 48) { - CRevoicePlayer* plr = GetPlayerByClientPtr(cl); - plr->OnConnected(protocol); - - // default codec - plr->InitVoice(vct_speex); - - //for p48 we will query sv_version cvar value later in mm_ClientConnect - } - - chain->callNext(cl); -} - void SV_DropClient_hook(IRehldsHook_SV_DropClient* chain, IGameClient* cl, bool crash, const char* msg) { + CRevoicePlayer* plr = GetPlayerByClientPtr(cl); plr->OnDisconnected(); chain->callNext(cl, crash, msg); } void mm_CvarValue2(const edict_t *pEnt, int requestID, const char *cvarName, const char *cvarValue) { - if (!strcmp("sv_version", cvarName)) { - // ] sv_version - // "sv_version" is "1.1.2.1/2.0.0.0,48,4554" - const char* lastSeparator = strrchr(cvarValue, ','); - int buildNumber = 0; - if (lastSeparator) { - buildNumber = atoi(lastSeparator + 1); - } + CRevoicePlayer* plr = GetPlayerByEdict(pEnt); - CRevoicePlayer* plr = GetPlayerByEdict(pEnt); - if (buildNumber > 4554) { - plr->InitVoice(vct_silk); - } else { - plr->InitVoice(vct_speex); - } + if (plr->GetRequestId() != requestID) { + RETURN_META(MRES_IGNORED); + } + + const char* lastSeparator = strrchr(cvarValue, ','); + int buildNumber = 0; + if (lastSeparator) { + buildNumber = atoi(lastSeparator + 1); + } + + if (buildNumber > 4554) { + plr->SetCodecType(vct_silk); } RETURN_META(MRES_IGNORED); @@ -100,8 +85,8 @@ void SV_ParseVoiceData_emu(IGameClient* cl) { switch (srcPlayer->GetCodecType()) { case vct_silk: { - if (nDataLength > MAX_SILK_DATA_LEN || srcPlayer->GetVoiceRate() > MAX_SILK_VOICE_RATE) - return; + //if (nDataLength > MAX_SILK_DATA_LEN || srcPlayer->GetVoiceRate() > MAX_SILK_VOICE_RATE) + // return; silkData = chReceived; silkDataLen = nDataLength; speexData = transcodedBuf; @@ -110,8 +95,8 @@ void SV_ParseVoiceData_emu(IGameClient* cl) { } case vct_speex: - if (nDataLength > MAX_SPEEX_DATA_LEN || srcPlayer->GetVoiceRate() > MAX_SPEEX_VOICE_RATE) - return; + //if (nDataLength > MAX_SPEEX_DATA_LEN || srcPlayer->GetVoiceRate() > MAX_SPEEX_VOICE_RATE) + // return; speexData = chReceived; speexDataLen = nDataLength; silkData = transcodedBuf; @@ -176,14 +161,14 @@ void Rehlds_HandleNetCommand(IRehldsHook_HandleNetCommand* chain, IGameClient* c } qboolean mm_ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char szRejectReason[128]) { + CRevoicePlayer* plr = GetPlayerByEdict(pEntity); - if (plr->GetProtocol() == 48) { - g_engfuncs.pfnQueryClientCvarValue2(pEntity, "sv_version", 0); - } + plr->OnConnected(); RETURN_META_VALUE(MRES_IGNORED, TRUE); } void SV_WriteVoiceCodec_hooked(IRehldsHook_SV_WriteVoiceCodec* chain, sizebuf_t* sb) { + IGameClient* cl = g_RehldsFuncs->GetHostClient(); CRevoicePlayer* plr = GetPlayerByClientPtr(cl); @@ -207,7 +192,6 @@ void SV_WriteVoiceCodec_hooked(IRehldsHook_SV_WriteVoiceCodec* chain, sizebuf_t* } bool Revoice_Main_Init() { - g_RehldsHookchains->ClientConnected()->registerHook(Rehlds_ClientConnected_Hook); g_RehldsHookchains->SV_DropClient()->registerHook(SV_DropClient_hook); g_RehldsHookchains->HandleNetCommand()->registerHook(Rehlds_HandleNetCommand); g_RehldsHookchains->SV_WriteVoiceCodec()->registerHook(SV_WriteVoiceCodec_hooked); diff --git a/revoice/src/revoice_player.cpp b/revoice/src/revoice_player.cpp index ff69969..066d90a 100644 --- a/revoice/src/revoice_player.cpp +++ b/revoice/src/revoice_player.cpp @@ -1,4 +1,3 @@ - #include "precompiled.h" CRevoicePlayer g_Players[MAX_PLAYERS]; @@ -13,31 +12,53 @@ CRevoicePlayer::CRevoicePlayer() { m_RehldsClient = NULL; m_Protocol = 0; + m_Connected = false; } void CRevoicePlayer::Initialize(IGameClient* cl) { m_RehldsClient = cl; } -void CRevoicePlayer::InitVoice(revoice_codec_type codecType) { - m_CodecType = codecType; +void CRevoicePlayer::OnConnected() +{ + // already connected, suppose now there is a change of level? + if (m_Connected) { + return; + } + + int protocol = g_ReunionApi->GetClientProtocol(m_RehldsClient->GetId()); + + if (protocol != 47 && protocol != 48) { + return; + } + + // reset codec state m_SilkCodec->ResetState(); m_SpeexCodec->ResetState(); -} -void CRevoicePlayer::OnConnected(int protocol) { - m_Protocol = protocol; - m_CodecType = vct_none; + // default codec + m_CodecType = vct_speex; m_VoiceRate = 0; + m_Connected = true; + m_RequestId = MAKE_REQUESTID(PLID); + m_Protocol = protocol; + + if (m_Protocol == 48) { + g_engfuncs.pfnQueryClientCvarValue2(m_RehldsClient->GetEdict(), "sv_version", m_RequestId); + } } -void CRevoicePlayer::OnDisconnected() { +void CRevoicePlayer::OnDisconnected() +{ + m_Connected = false; m_Protocol = 0; m_CodecType = vct_none; m_VoiceRate = 0; + m_RequestId = 0; } -void Revoice_Init_Players() { +void Revoice_Init_Players() +{ int maxclients = g_RehldsSvs->GetMaxClients(); for (int i = 0; i < maxclients; i++) { @@ -49,7 +70,8 @@ CRevoicePlayer* GetPlayerByClientPtr(IGameClient* cl) { return &g_Players[cl->GetId()]; } -CRevoicePlayer* GetPlayerByEdict(const edict_t* ed) { +CRevoicePlayer* GetPlayerByEdict(const edict_t* ed) +{ int clientId = g_engfuncs.pfnIndexOfEdict(ed) - 1; if (clientId < 0 || clientId >= g_RehldsSvs->GetMaxClients()) { util_syserror("Invalid player edict id=%d\n", clientId); @@ -77,9 +99,8 @@ void CRevoicePlayer::UpdateVoiceRate(double delta) case vct_speex: m_VoiceRate -= int(delta * MAX_SPEEX_VOICE_RATE) + MAX_SPEEX_DATA_LEN; break; - default: - ; + break; } if (m_VoiceRate < 0) @@ -91,8 +112,3 @@ void CRevoicePlayer::IncreaseVoiceRate(int dataLength) { m_VoiceRate += dataLength; } - -int CRevoicePlayer::GetVoiceRate() -{ - return m_VoiceRate; -} \ No newline at end of file diff --git a/revoice/src/revoice_player.h b/revoice/src/revoice_player.h index b3262ea..2790618 100644 --- a/revoice/src/revoice_player.h +++ b/revoice/src/revoice_player.h @@ -6,7 +6,6 @@ #include "VoiceEncoder_Speex.h" #include "voice_codec_frame.h" - class CRevoicePlayer { private: IGameClient* m_RehldsClient; @@ -15,19 +14,24 @@ private: VoiceCodec_Frame* m_SpeexCodec; int m_Protocol; int m_VoiceRate; + int m_RequestId; + bool m_Connected; public: CRevoicePlayer(); void Initialize(IGameClient* cl); - void OnConnected(int protocol); + void OnConnected(); void OnDisconnected(); - void InitVoice(revoice_codec_type codecType); + void SetLastVoiceTime(double time); void UpdateVoiceRate(double delta); void IncreaseVoiceRate(int dataLength); - int GetVoiceRate(); - - int GetProtocol() const { return m_Protocol; } + + int GetVoiceRate() const { return m_VoiceRate; } + int GetRequestId() const { return m_RequestId; } + bool IsConnected() const { return m_Connected; } + + void SetCodecType(revoice_codec_type codecType) { m_CodecType = codecType; }; revoice_codec_type GetCodecType() const { return m_CodecType; } CSteamP2PCodec* GetSilkCodec() const { return m_SilkCodec; } VoiceCodec_Frame* GetSpeexCodec() const { return m_SpeexCodec; }