Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
423 changes: 423 additions & 0 deletions Game.Server.Tests/VoiceGameMgrTests.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Game.Server/GameInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,8 @@ public TimeUnit_t ProcessUsercmds(Edict player, bf_read buf, int numCmds, int to

public void GMOD_SentClientStringTables(int userID) => Game.Server.GarrysMod.GModDataPack.DataPack().SendFileRequestRequest(userID);

public void GMOD_OnReceivedVoicePacket(Edict? player) => GetVoiceGameMgr().OnReceivedVoicePacket(BaseEntity.Instance(player));

public void GMOD_ReceiveClientMessage(int userID, Edict player, bf_read msg, int bits) {
int type = msg.ReadByte();
if (bits <= 7 || type > 5) {
Expand Down
33 changes: 33 additions & 0 deletions Game.Server/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,39 @@ public virtual void ForceDropOfCarriedPhysObjects(BaseEntity? ground) { }

internal int GetUserID() => engine.GetPlayerUserId(Edict());

#if GMOD_DLL
static readonly ConVar sv_maxvoicepacketsperframe = new("sv_maxvoicepacketsperframe", "48", 0, "How many voice packets the server will accept from a single client per frame/tick, before the client is kicked. 0 to disable. Higher values will make the server more vulnerable to DoS attacks.");

public double LastVoicePacketTime;
ushort VoicePacketsThisFrame;
public bool Kicked;

public void OnVoicePacketReceived() {
double curTime = gpGlobals.CurTime;
ushort packets;
if (LastVoicePacketTime == curTime)
packets = (ushort)(VoicePacketsThisFrame + 1);
else {
packets = 1;
VoicePacketsThisFrame = 0;
}

LastVoicePacketTime = curTime;
VoicePacketsThisFrame = packets;

int maxPackets = sv_maxvoicepacketsperframe.GetInt();
if (maxPackets <= 0)
return;

if ((VoicePacketsThisFrame > 65000 || maxPackets < VoicePacketsThisFrame) && !Kicked) {
Span<char> cmd = stackalloc char[600];
sprintf(cmd, "kickid %d \"Too many voice packets (%i) in a single frame!\"\n").D(engine.GetPlayerUserId(Edict())).I(VoicePacketsThisFrame);
engine.ServerCommand(cmd.SliceNullTerminatedString());
Kicked = true;
}
}
#endif

public bool GetSteamID(out Steamworks.CSteamID id) {
Steamworks.CSteamID? steamID = engine.GetClientSteamID(Edict());
id = steamID ?? default;
Expand Down
133 changes: 131 additions & 2 deletions Game.Server/VoiceGameMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public static class VoiceGameMgrGlobals

// Set game rules to allow all clients to talk to each other.
// Muted players still can't talk to each other.
#if GMOD_DLL
public static readonly ConVar sv_alltalk = new("sv_alltalk", "0", FCvar.Notify, "Players can hear all other players, no team restrictions");
#else
public static readonly ConVar sv_alltalk = new("sv_alltalk", "0", FCvar.Notify | FCvar.Replicated, "Players can hear all other players, no team restrictions");
#endif

public static readonly VoiceGameMgr g_VoiceGameMgr = new();

Expand All @@ -67,9 +71,15 @@ public static void VoiceServerDebug(ReadOnlySpan<char> msg) {
// VoiceGameMgr manages which clients can hear which other clients.
public class VoiceGameMgr
{
#if GMOD_DLL
double LastVoicePacketTime;
#endif
IVoiceGameMgrHelper? Helper;
int MaxPlayers;
double UpdateInterval; // How long since the last update.
#if GMOD_DLL
double FullUpdateInterval;
#endif
int ProximityDistance;

public VoiceGameMgr() {
Expand All @@ -94,17 +104,67 @@ public void SetHelper(IVoiceGameMgrHelper? helper) {
// If gameplay mode is teamplay, then only players on the same team can hear each other.
// Player masks are always applied.
public void Update(double frametime) {
#if GMOD_DLL
UpdateInterval += frametime;
FullUpdateInterval += frametime;
if (UpdateInterval < UPDATE_INTERVAL)
return;

if (gpGlobals.CurTime - LastVoicePacketTime > UPDATE_INTERVAL && UpdateInterval < 5.0)
return;

if (UpdateInterval > 3.0 || FullUpdateInterval > 5.0) {
FullUpdateInterval = 0;
UpdatePlayers(true, null);
}
else
UpdatePlayers(false, null);
#else
// Only update periodically.
UpdateInterval += frametime;
if (UpdateInterval < UPDATE_INTERVAL)
return;

UpdateMasks();
#endif
}

#if GMOD_DLL
public void OnReceivedVoicePacket(BaseEntity? entity) {
if (entity == null || !entity.IsPlayer())
return;

BasePlayer player = (BasePlayer)entity;
bool startedTalking = gpGlobals.CurTime - player.LastVoicePacketTime > UPDATE_INTERVAL;
player.OnVoicePacketReceived();
LastVoicePacketTime = gpGlobals.CurTime;

if (startedTalking)
UpdatePlayers(false, player);
}

void UpdatePlayers(bool full, BasePlayer? talker) {
UpdateInterval = 0;

for (int iClient = 0; iClient < MaxPlayers; iClient++) {
BaseEntity? ent = Util.PlayerByIndex(iClient + 1);
if (ent != null && ent.IsPlayer())
UpdatePlayer((BasePlayer)ent, full, talker);
}
}
#endif

// Called when a new client connects (unsquelches its entity for everyone).
public void ClientConnected(Edict edict) {
int index = edict.EdictIndex - 1;
public void ClientConnected(Edict? edict) {
#if GMOD_DLL
if (edict == null)
return;
#endif
int index = edict!.EdictIndex - 1;
#if GMOD_DLL
if ((uint)index >= VOICE_MAX_PLAYERS)
return;
#endif

// Clear out everything we use for deltas on this guy.
g_bWantModEnable.Set(index, true);
Expand Down Expand Up @@ -164,6 +224,74 @@ public bool IsPlayerIgnoringPlayer(int talker, int listener) {
return g_BanMasks[listener - 1].IsBitSet(talker - 1);
}

#if GMOD_DLL
void UpdatePlayer(BasePlayer player, bool full, BasePlayer? talker) {
int playerIndex = player.EntIndex();
int iClient = playerIndex - 1;

SingleUserRecipientFilter user = new(player);

// Request the state of their "VModEnable" cvar
if (g_bWantModEnable.IsBitSet(iClient)) {
UserMessageBegin(user, "RequestState");
MessageEnd();
// Only send it once
g_bWantModEnable.Set(iClient, false);
}

PlayerBitSet gameRulesMask = default;
PlayerBitSet proximityMask = default;
if (g_PlayerModEnable.IsBitSet(iClient)) {
bool proximity = false;

// Build a mask of who they can hear based on the game rules
for (int iOtherClient = 0; iOtherClient < MaxPlayers; iOtherClient++) {
BaseEntity? otherEnt = Util.PlayerByIndex(iOtherClient + 1);
if (otherEnt == null || !otherEnt.IsPlayer())
continue;

BasePlayer other = (BasePlayer)otherEnt;
bool canHear;
if (full || other == talker || gpGlobals.CurTime - other.LastVoicePacketTime < 1.0)
canHear = Helper!.CanPlayerHearPlayer(player, other, ref proximity);
else {
canHear = g_pVoiceServer.GetClientListening(playerIndex, iOtherClient + 1);
proximity = g_pVoiceServer.GetClientProximity(playerIndex, iOtherClient + 1);
}

if (canHear) {
gameRulesMask.Set(iOtherClient, true);
proximityMask.Set(iOtherClient, proximity);
}
}
}

// If this is different from what the client has, send an update
if (gameRulesMask != g_SentGameRulesMasks[iClient] ||
g_BanMasks[iClient] != g_SentBanMasks[iClient]) {
g_SentGameRulesMasks[iClient] = gameRulesMask;
g_SentBanMasks[iClient] = g_BanMasks[iClient];

UserMessageBegin(user, "VoiceMask");
int dw;
for (dw = 0; dw < VOICE_MAX_PLAYERS_DW; dw++) {
MessageWriteLong((int)gameRulesMask.GetWord(dw));
MessageWriteLong((int)g_BanMasks[iClient].GetWord(dw));
}
MessageWriteByte(g_PlayerModEnable.IsBitSet(iClient) ? 1 : 0);
MessageEnd();
}

// Tell the engine.
for (int iOtherClient = 0; iOtherClient < MaxPlayers; iOtherClient++) {
bool canHear = gameRulesMask.IsBitSet(iOtherClient) && !g_BanMasks[iClient].IsBitSet(iOtherClient);
g_pVoiceServer.SetClientListening(playerIndex, iOtherClient + 1, canHear);

if (canHear)
g_pVoiceServer.SetClientProximity(playerIndex, iOtherClient + 1, proximityMask.IsBitSet(iOtherClient));
}
}
#else
// Force it to update the client masks.
void UpdateMasks() {
UpdateInterval = 0;
Expand Down Expand Up @@ -228,4 +356,5 @@ void UpdateMasks() {
}
}
}
#endif
}
1 change: 1 addition & 0 deletions Source.Common/Server/IServerGameDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public interface IServerGameClients
void GMOD_ReceiveClientMessage(int userID, Edict player, bf_read msg, int bits);
void GMOD_ClientConnected(int userID);
void GMOD_SentClientStringTables(int userID);
void GMOD_OnReceivedVoicePacket(Edict? player);
}

public interface IServerGCLobby
Expand Down
3 changes: 3 additions & 0 deletions Source.Common/Server/IVoiceServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public interface IVoiceServer
bool GetClientListening(int receiver, int sender);
bool SetClientListening(int receiver, int sender, bool listen);
bool SetClientProximity(int receiver, int sender, bool useProximity);
#if GMOD_DLL
bool GetClientProximity(int receiver, int sender);
#endif
}
12 changes: 12 additions & 0 deletions Source.Engine/Server/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ protected override bool ProcessMove(CLC_Move m) {
}

protected override bool ProcessVoiceData(CLC_VoiceData msg) {
#if GMOD_DLL
if ((uint)(msg.Length - 9) >= 0x3FF8) {
Disconnect($"Invalid voice packet length {msg.Length}.");
return false;
}

if (!IsActive())
return false;

serverGameClients.GMOD_OnReceivedVoicePacket(Edict);
#endif

byte[] voiceDataBuffer = new byte[4096];
int bitsRead = (int)msg.DataIn.ReadBitsClamped(voiceDataBuffer, (uint)msg.Length);

Expand Down
12 changes: 12 additions & 0 deletions Source.Engine/Server/VoiceServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public bool SetClientProximity(int receiver, int sender, bool useProximity) {

return true;
}

#if GMOD_DLL
public bool GetClientProximity(int receiver, int sender) {
--receiver;
--sender;

if (receiver < 0 || receiver >= sv.GetClientCount() || sender < 0 || sender >= sv.GetClientCount())
return false;

return sv.Client(sender).VoiceProximity.Get(receiver) != 0;
}
#endif
}
Loading