Skip to content

Commit

Permalink
Rethink authorize a client
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Feb 4, 2016
1 parent fcc2d4f commit a5667b7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 61 deletions.
60 changes: 22 additions & 38 deletions revoice/src/revoice_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if (buildNumber > 4554) {
plr->InitVoice(vct_silk);
} else {
plr->InitVoice(vct_speex);
}
CRevoicePlayer* plr = GetPlayerByEdict(pEnt);

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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
50 changes: 33 additions & 17 deletions revoice/src/revoice_player.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "precompiled.h"

CRevoicePlayer g_Players[MAX_PLAYERS];
Expand All @@ -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++) {
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -91,8 +112,3 @@ void CRevoicePlayer::IncreaseVoiceRate(int dataLength)
{
m_VoiceRate += dataLength;
}

int CRevoicePlayer::GetVoiceRate()
{
return m_VoiceRate;
}
16 changes: 10 additions & 6 deletions revoice/src/revoice_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "VoiceEncoder_Speex.h"
#include "voice_codec_frame.h"


class CRevoicePlayer {
private:
IGameClient* m_RehldsClient;
Expand All @@ -15,19 +14,24 @@ class CRevoicePlayer {
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; }
Expand Down

0 comments on commit a5667b7

Please sign in to comment.