Skip to content

Commit

Permalink
Use separate packer with larger buffer only for Teehistorian
Browse files Browse the repository at this point in the history
Revert buffer size for regular `CPacker` to 2 KiB to avoid stack overflows on systems with only 1 MiB of stack memory (e.g. Windows with MSVC and Android).

Add separate `CTeehistorianPacker` with a larger buffer to be used only for Teehistorian chunks.

To avoid duplicate code the existing `CPacker` is renamed to `CAbstractPacker` and the buffer and its size are supplied by the concrete subclasses `CPacker` and `CTeehistorianPacker`.
  • Loading branch information
Robyt3 committed Nov 21, 2024
1 parent 3dab25b commit 7f34f07
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 93 deletions.
20 changes: 13 additions & 7 deletions src/engine/shared/packer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
#include "compression.h"
#include "packer.h"

void CPacker::Reset()
CAbstractPacker::CAbstractPacker(unsigned char *pBuffer, size_t Size) :
m_pBuffer(pBuffer),
m_BufferSize(Size)
{
}

void CAbstractPacker::Reset()
{
m_Error = false;
m_pCurrent = m_aBuffer;
m_pEnd = m_pCurrent + PACKER_BUFFER_SIZE;
m_pCurrent = m_pBuffer;
m_pEnd = m_pCurrent + m_BufferSize;
}

void CPacker::AddInt(int i)
void CAbstractPacker::AddInt(int i)
{
if(m_Error)
return;
Expand All @@ -26,14 +32,14 @@ void CPacker::AddInt(int i)
m_pCurrent = pNext;
}

void CPacker::AddString(const char *pStr, int Limit)
void CAbstractPacker::AddString(const char *pStr, int Limit)
{
if(m_Error)
return;

if(Limit <= 0)
{
Limit = PACKER_BUFFER_SIZE;
Limit = m_BufferSize;
}
while(*pStr && Limit != 0)
{
Expand Down Expand Up @@ -61,7 +67,7 @@ void CPacker::AddString(const char *pStr, int Limit)
*m_pCurrent++ = 0;
}

void CPacker::AddRaw(const void *pData, int Size)
void CAbstractPacker::AddRaw(const void *pData, int Size)
{
if(m_Error)
return;
Expand Down
44 changes: 33 additions & 11 deletions src/engine/shared/packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,53 @@
#ifndef ENGINE_SHARED_PACKER_H
#define ENGINE_SHARED_PACKER_H

class CPacker
{
public:
enum
{
PACKER_BUFFER_SIZE = 1024 * 64
};
#include <cstddef>

/**
* Abstract packer implementation. Subclasses must supply the buffer.
*/
class CAbstractPacker
{
private:
unsigned char m_aBuffer[PACKER_BUFFER_SIZE];
unsigned char *const m_pBuffer;
const size_t m_BufferSize;
unsigned char *m_pCurrent;
unsigned char *m_pEnd;
bool m_Error;

protected:
CAbstractPacker(unsigned char *pBuffer, size_t Size);

public:
void Reset();
void AddInt(int i);
void AddString(const char *pStr, int Limit = PACKER_BUFFER_SIZE);
void AddString(const char *pStr, int Limit = 0);
void AddRaw(const void *pData, int Size);

int Size() const { return (int)(m_pCurrent - m_aBuffer); }
const unsigned char *Data() const { return m_aBuffer; }
int Size() const { return (int)(m_pCurrent - m_pBuffer); }
const unsigned char *Data() const { return m_pBuffer; }
bool Error() const { return m_Error; }
};

/**
* Default packer with buffer for networking.
*/
class CPacker : public CAbstractPacker
{
public:
enum
{
PACKER_BUFFER_SIZE = 1024 * 2
};
CPacker() :
CAbstractPacker(m_aBuffer, sizeof(m_aBuffer))
{
}

private:
unsigned char m_aBuffer[PACKER_BUFFER_SIZE];
};

class CUnpacker
{
const unsigned char *m_pStart;
Expand Down
72 changes: 43 additions & 29 deletions src/game/server/teehistorian.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#include "teehistorian.h"

#include <base/system.h>
#include <engine/external/json-parser/json.h>

#include <engine/shared/config.h>
#include <engine/shared/json.h>
#include <engine/shared/packer.h>
#include <engine/shared/snapshot.h>

#include <game/gamecore.h>

class CTeehistorianPacker : public CAbstractPacker
{
public:
CTeehistorianPacker() :
CAbstractPacker(m_aBuffer, sizeof(m_aBuffer))
{
}

private:
unsigned char m_aBuffer[1024 * 64];
};

static const char TEEHISTORIAN_NAME[] = "[email protected]";
static const CUuid TEEHISTORIAN_UUID = CalculateUuid(TEEHISTORIAN_NAME);
static const char TEEHISTORIAN_VERSION[] = "2";
Expand Down Expand Up @@ -215,7 +229,7 @@ void CTeeHistorian::WriteExtra(CUuid Uuid, const void *pData, int DataSize)
{
EnsureTickWritten();

CPacker Ex;
CTeehistorianPacker Ex;
Ex.Reset();
Ex.AddInt(-TEEHISTORIAN_EX);
Ex.AddRaw(&Uuid, sizeof(Uuid));
Expand Down Expand Up @@ -277,7 +291,7 @@ void CTeeHistorian::RecordPlayer(int ClientId, const CNetObj_CharacterCore *pCha
{
EnsureTickWrittenPlayerData(ClientId);

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
if(pPrev->m_Alive)
{
Expand Down Expand Up @@ -320,7 +334,7 @@ void CTeeHistorian::RecordDeadPlayer(int ClientId)
{
EnsureTickWrittenPlayerData(ClientId);

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_PLAYER_OLD);
Buffer.AddInt(ClientId);
Expand All @@ -341,7 +355,7 @@ void CTeeHistorian::RecordPlayerTeam(int ClientId, int Team)

EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddInt(Team);
Expand All @@ -363,7 +377,7 @@ void CTeeHistorian::RecordTeamPractice(int Team, bool Practice)

EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(Team);
Buffer.AddInt(Practice);
Expand Down Expand Up @@ -392,7 +406,7 @@ void CTeeHistorian::EnsureTickWritten()

void CTeeHistorian::WriteTick()
{
CPacker TickPacker;
CTeehistorianPacker TickPacker;
TickPacker.Reset();

int dt = m_Tick - m_LastWrittenTick - 1;
Expand Down Expand Up @@ -424,7 +438,7 @@ void CTeeHistorian::BeginInputs()

void CTeeHistorian::RecordPlayerInput(int ClientId, uint32_t UniqueClientId, const CNetObj_PlayerInput *pInput)
{
CPacker Buffer;
CTeehistorianPacker Buffer;

CTeehistorianPlayer *pPrev = &m_aPrevPlayers[ClientId];
CNetObj_PlayerInput DiffInput;
Expand Down Expand Up @@ -473,7 +487,7 @@ void CTeeHistorian::RecordPlayerMessage(int ClientId, const void *pMsg, int MsgS
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_MESSAGE);
Buffer.AddInt(ClientId);
Expand All @@ -499,7 +513,7 @@ void CTeeHistorian::RecordPlayerJoin(int ClientId, int Protocol)
EnsureTickWritten();

{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
if(m_Debug)
Expand All @@ -510,7 +524,7 @@ void CTeeHistorian::RecordPlayerJoin(int ClientId, int Protocol)
WriteExtra(Uuid, Buffer.Data(), Buffer.Size());
}

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_JOIN);
Buffer.AddInt(ClientId);
Expand All @@ -527,7 +541,7 @@ void CTeeHistorian::RecordPlayerRejoin(int ClientId)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);

Expand All @@ -543,7 +557,7 @@ void CTeeHistorian::RecordPlayerReady(int ClientId)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);

Expand All @@ -559,7 +573,7 @@ void CTeeHistorian::RecordPlayerDrop(int ClientId, const char *pReason)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_DROP);
Buffer.AddInt(ClientId);
Expand All @@ -577,7 +591,7 @@ void CTeeHistorian::RecordPlayerName(int ClientId, const char *pName)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddString(pName, 0);
Expand All @@ -594,7 +608,7 @@ void CTeeHistorian::RecordConsoleCommand(int ClientId, int FlagMask, const char
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_CONSOLE_COMMAND);
Buffer.AddInt(ClientId);
Expand Down Expand Up @@ -628,7 +642,7 @@ void CTeeHistorian::RecordPlayerSwap(int ClientId1, int ClientId2)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId1);
Buffer.AddInt(ClientId2);
Expand All @@ -640,7 +654,7 @@ void CTeeHistorian::RecordTeamSaveSuccess(int Team, CUuid SaveId, const char *pT
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(Team);
Buffer.AddRaw(&SaveId, sizeof(SaveId));
Expand All @@ -660,7 +674,7 @@ void CTeeHistorian::RecordTeamSaveFailure(int Team)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(Team);

Expand All @@ -676,7 +690,7 @@ void CTeeHistorian::RecordTeamLoadSuccess(int Team, CUuid SaveId, const char *pT
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(Team);
Buffer.AddRaw(&SaveId, sizeof(SaveId));
Expand All @@ -696,7 +710,7 @@ void CTeeHistorian::RecordTeamLoadFailure(int Team)
{
EnsureTickWritten();

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(Team);

Expand All @@ -723,7 +737,7 @@ void CTeeHistorian::EndTick()

void CTeeHistorian::RecordDDNetVersionOld(int ClientId, int DDNetVersion)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddInt(DDNetVersion);
Expand All @@ -738,7 +752,7 @@ void CTeeHistorian::RecordDDNetVersionOld(int ClientId, int DDNetVersion)

void CTeeHistorian::RecordDDNetVersion(int ClientId, CUuid ConnectionId, int DDNetVersion, const char *pDDNetVersionStr)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddRaw(&ConnectionId, sizeof(ConnectionId));
Expand All @@ -757,7 +771,7 @@ void CTeeHistorian::RecordDDNetVersion(int ClientId, CUuid ConnectionId, int DDN

void CTeeHistorian::RecordAuthInitial(int ClientId, int Level, const char *pAuthName)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddInt(Level);
Expand All @@ -773,7 +787,7 @@ void CTeeHistorian::RecordAuthInitial(int ClientId, int Level, const char *pAuth

void CTeeHistorian::RecordAuthLogin(int ClientId, int Level, const char *pAuthName)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddInt(Level);
Expand All @@ -789,7 +803,7 @@ void CTeeHistorian::RecordAuthLogin(int ClientId, int Level, const char *pAuthNa

void CTeeHistorian::RecordAuthLogout(int ClientId)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);

Expand All @@ -813,7 +827,7 @@ void CTeeHistorian::RecordAntibot(const void *pData, int DataSize)

void CTeeHistorian::RecordPlayerFinish(int ClientId, int TimeTicks)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(ClientId);
Buffer.AddInt(TimeTicks);
Expand All @@ -827,7 +841,7 @@ void CTeeHistorian::RecordPlayerFinish(int ClientId, int TimeTicks)

void CTeeHistorian::RecordTeamFinish(int TeamId, int TimeTicks)
{
CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(TeamId);
Buffer.AddInt(TimeTicks);
Expand All @@ -852,7 +866,7 @@ void CTeeHistorian::Finish()
EndTick();
}

CPacker Buffer;
CTeehistorianPacker Buffer;
Buffer.Reset();
Buffer.AddInt(-TEEHISTORIAN_FINISH);

Expand Down
Loading

0 comments on commit 7f34f07

Please sign in to comment.