diff --git a/src/engine/shared/packer.cpp b/src/engine/shared/packer.cpp index 3593929ea68..d9649f7e5a6 100644 --- a/src/engine/shared/packer.cpp +++ b/src/engine/shared/packer.cpp @@ -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; @@ -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) { @@ -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; diff --git a/src/engine/shared/packer.h b/src/engine/shared/packer.h index 3ee622f6fd9..69fe8ab6837 100644 --- a/src/engine/shared/packer.h +++ b/src/engine/shared/packer.h @@ -3,31 +3,53 @@ #ifndef ENGINE_SHARED_PACKER_H #define ENGINE_SHARED_PACKER_H -class CPacker -{ -public: - enum - { - PACKER_BUFFER_SIZE = 1024 * 64 - }; +#include +/** + * 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; diff --git a/src/game/server/teehistorian.cpp b/src/game/server/teehistorian.cpp index 16c43b29086..c2e67c5f0e7 100644 --- a/src/game/server/teehistorian.cpp +++ b/src/game/server/teehistorian.cpp @@ -1,12 +1,26 @@ #include "teehistorian.h" #include -#include + #include #include +#include #include + #include +class CTeehistorianPacker : public CAbstractPacker +{ +public: + CTeehistorianPacker() : + CAbstractPacker(m_aBuffer, sizeof(m_aBuffer)) + { + } + +private: + unsigned char m_aBuffer[1024 * 64]; +}; + static const char TEEHISTORIAN_NAME[] = "teehistorian@ddnet.tw"; static const CUuid TEEHISTORIAN_UUID = CalculateUuid(TEEHISTORIAN_NAME); static const char TEEHISTORIAN_VERSION[] = "2"; @@ -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)); @@ -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) { @@ -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); @@ -341,7 +355,7 @@ void CTeeHistorian::RecordPlayerTeam(int ClientId, int Team) EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(ClientId); Buffer.AddInt(Team); @@ -363,7 +377,7 @@ void CTeeHistorian::RecordTeamPractice(int Team, bool Practice) EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(Team); Buffer.AddInt(Practice); @@ -392,7 +406,7 @@ void CTeeHistorian::EnsureTickWritten() void CTeeHistorian::WriteTick() { - CPacker TickPacker; + CTeehistorianPacker TickPacker; TickPacker.Reset(); int dt = m_Tick - m_LastWrittenTick - 1; @@ -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; @@ -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); @@ -499,7 +513,7 @@ void CTeeHistorian::RecordPlayerJoin(int ClientId, int Protocol) EnsureTickWritten(); { - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(ClientId); if(m_Debug) @@ -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); @@ -527,7 +541,7 @@ void CTeeHistorian::RecordPlayerRejoin(int ClientId) { EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(ClientId); @@ -543,7 +557,7 @@ void CTeeHistorian::RecordPlayerReady(int ClientId) { EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(ClientId); @@ -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); @@ -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); @@ -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); @@ -628,7 +642,7 @@ void CTeeHistorian::RecordPlayerSwap(int ClientId1, int ClientId2) { EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(ClientId1); Buffer.AddInt(ClientId2); @@ -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)); @@ -660,7 +674,7 @@ void CTeeHistorian::RecordTeamSaveFailure(int Team) { EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(Team); @@ -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)); @@ -696,7 +710,7 @@ void CTeeHistorian::RecordTeamLoadFailure(int Team) { EnsureTickWritten(); - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(Team); @@ -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); @@ -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)); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -852,7 +866,7 @@ void CTeeHistorian::Finish() EndTick(); } - CPacker Buffer; + CTeehistorianPacker Buffer; Buffer.Reset(); Buffer.AddInt(-TEEHISTORIAN_FINISH); diff --git a/src/test/packer.cpp b/src/test/packer.cpp index 6b4e133698f..bc20fe8fbdc 100644 --- a/src/test/packer.cpp +++ b/src/test/packer.cpp @@ -243,56 +243,62 @@ TEST(Packer, AddStringBroken) ExpectAddString5("\x80\x80", 6, 0); } -TEST(Packer, Error) +TEST(Packer, Error1) { char aData[CPacker::PACKER_BUFFER_SIZE]; mem_zero(aData, sizeof(aData)); - { - CPacker Packer; - Packer.Reset(); - EXPECT_EQ(Packer.Error(), false); - Packer.AddRaw(aData, sizeof(aData) - 1); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData) - 1); - Packer.AddInt(1); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData)); - Packer.AddInt(2); - EXPECT_EQ(Packer.Error(), true); - Packer.AddInt(3); - EXPECT_EQ(Packer.Error(), true); - } + CPacker Packer; + Packer.Reset(); + EXPECT_EQ(Packer.Error(), false); + Packer.AddRaw(aData, sizeof(aData) - 1); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData) - 1); + Packer.AddInt(1); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData)); + Packer.AddInt(2); + EXPECT_EQ(Packer.Error(), true); + Packer.AddInt(3); + EXPECT_EQ(Packer.Error(), true); +} - { - CPacker Packer; - Packer.Reset(); - EXPECT_EQ(Packer.Error(), false); - Packer.AddRaw(aData, sizeof(aData) - 1); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData) - 1); - Packer.AddRaw(aData, 1); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData)); - Packer.AddRaw(aData, 1); - EXPECT_EQ(Packer.Error(), true); - Packer.AddRaw(aData, 1); - EXPECT_EQ(Packer.Error(), true); - } +TEST(Packer, Error2) +{ + char aData[CPacker::PACKER_BUFFER_SIZE]; + mem_zero(aData, sizeof(aData)); - { - CPacker Packer; - Packer.Reset(); - EXPECT_EQ(Packer.Error(), false); - Packer.AddRaw(aData, sizeof(aData) - 5); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData) - 5); - Packer.AddString("test"); - EXPECT_EQ(Packer.Error(), false); - EXPECT_EQ(Packer.Size(), sizeof(aData)); - Packer.AddString("test"); - EXPECT_EQ(Packer.Error(), true); - Packer.AddString("test"); - EXPECT_EQ(Packer.Error(), true); - } + CPacker Packer; + Packer.Reset(); + EXPECT_EQ(Packer.Error(), false); + Packer.AddRaw(aData, sizeof(aData) - 1); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData) - 1); + Packer.AddRaw(aData, 1); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData)); + Packer.AddRaw(aData, 1); + EXPECT_EQ(Packer.Error(), true); + Packer.AddRaw(aData, 1); + EXPECT_EQ(Packer.Error(), true); +} + +TEST(Packer, Error3) +{ + char aData[CPacker::PACKER_BUFFER_SIZE]; + mem_zero(aData, sizeof(aData)); + + CPacker Packer; + Packer.Reset(); + EXPECT_EQ(Packer.Error(), false); + Packer.AddRaw(aData, sizeof(aData) - 5); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData) - 5); + Packer.AddString("test"); + EXPECT_EQ(Packer.Error(), false); + EXPECT_EQ(Packer.Size(), sizeof(aData)); + Packer.AddString("test"); + EXPECT_EQ(Packer.Error(), true); + Packer.AddString("test"); + EXPECT_EQ(Packer.Error(), true); }