mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-18 16:38:42 +01:00
Core/Packets: Implemented BitStream helper class
- added Static BitConverter class - upgraded the ByteBuffer class for easier usage
This commit is contained in:
89
src/server/shared/Packets/ByteBuffer.cpp
Normal file
89
src/server/shared/Packets/ByteBuffer.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
|
||||
void BitStream::Clear()
|
||||
{
|
||||
_data.clear();
|
||||
_rpos = _wpos = 0;
|
||||
}
|
||||
|
||||
uint8 BitStream::GetBit(uint32 bit)
|
||||
{
|
||||
ASSERT(_data.size() > bit);
|
||||
return _data[bit];
|
||||
}
|
||||
|
||||
uint8 BitStream::ReadBit()
|
||||
{
|
||||
ASSERT(_data.size() < _rpos);
|
||||
uint8 b = _data[_rpos];
|
||||
++_rpos;
|
||||
return b;
|
||||
}
|
||||
|
||||
void BitStream::WriteBit(uint32 bit)
|
||||
{
|
||||
_data.push_back(bit ? uint8(1) : uint8(0));
|
||||
++_wpos;
|
||||
}
|
||||
|
||||
template <typename T> void BitStream::WriteBits(T value, size_t bits)
|
||||
{
|
||||
for (int32 i = bits-1; i >= 0; --i)
|
||||
WriteBit((value >> i) & 1);
|
||||
}
|
||||
|
||||
bool BitStream::Emtpy ()
|
||||
{
|
||||
return _data.empty();
|
||||
}
|
||||
|
||||
void BitStream::Reverse()
|
||||
{
|
||||
uint32 len = GetLenght();
|
||||
std::vector<uint8> b = _data;
|
||||
Clear();
|
||||
|
||||
for(uint32 i = len; i > 0; --i)
|
||||
WriteBit(b[i-1]);
|
||||
}
|
||||
|
||||
void BitStream::Print()
|
||||
{
|
||||
if (!sLog->IsOutDebug())
|
||||
return;
|
||||
std::stringstream ss;
|
||||
ss << "BitStream: ";
|
||||
for (uint32 i = 0; i < GetLenght(); ++i)
|
||||
ss << uint32(GetBit(i)) << " ";
|
||||
|
||||
sLog->outDebug(LOG_FILTER_NETWORKIO, ss.str().c_str());
|
||||
}
|
||||
|
||||
ByteBuffer::ByteBuffer(size_t res, bool init): _rpos(0), _wpos(0), _bitpos(8), _curbitval(0)
|
||||
{
|
||||
if (init)
|
||||
{
|
||||
for (size_t i = 0; i < res; ++i)
|
||||
{
|
||||
*this << uint8(0);
|
||||
}
|
||||
} else
|
||||
_storage.reserve(res);
|
||||
}
|
||||
@@ -24,6 +24,8 @@
|
||||
#include "Logging/Log.h"
|
||||
#include "Utilities/ByteConverter.h"
|
||||
|
||||
|
||||
|
||||
class ByteBufferException
|
||||
{
|
||||
public:
|
||||
@@ -45,6 +47,45 @@ class ByteBufferException
|
||||
size_t size;
|
||||
};
|
||||
|
||||
class BitStream
|
||||
{
|
||||
public:
|
||||
BitStream(): _rpos(0), _wpos(0) {}
|
||||
|
||||
BitStream(uint32 val, size_t len): _rpos(0), _wpos(0)
|
||||
{
|
||||
WriteBits(val, len);
|
||||
}
|
||||
|
||||
void Clear();
|
||||
uint8 GetBit(uint32 bit);
|
||||
uint8 ReadBit();
|
||||
void WriteBit(uint32 bit);
|
||||
template <typename T> void WriteBits(T value, size_t bits);
|
||||
bool Emtpy ();
|
||||
void Reverse();
|
||||
void Print();
|
||||
|
||||
size_t GetLenght () { return _data.size();}
|
||||
uint32 GetReadPosition() { return _rpos; }
|
||||
uint32 GetWritePosition() { return _wpos; }
|
||||
void SetReadPos (uint32 pos) { _rpos = pos; }
|
||||
|
||||
uint8 operator[](uint32 pos) const
|
||||
{
|
||||
return _data[pos];
|
||||
}
|
||||
|
||||
uint8& operator[] (const uint32 pos)
|
||||
{
|
||||
return _data[pos];
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<uint8> _data;
|
||||
uint32 _rpos, _wpos;
|
||||
};
|
||||
|
||||
class ByteBuffer
|
||||
{
|
||||
public:
|
||||
@@ -57,10 +98,7 @@ class ByteBuffer
|
||||
}
|
||||
|
||||
// constructor
|
||||
ByteBuffer(size_t res): _rpos(0), _wpos(0), _bitpos(8), _curbitval(0)
|
||||
{
|
||||
_storage.reserve(res);
|
||||
}
|
||||
ByteBuffer(size_t res, bool init = false);
|
||||
|
||||
// copy constructor
|
||||
ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage), _bitpos(buf._bitpos), _curbitval(buf._curbitval) { }
|
||||
@@ -135,6 +173,14 @@ class ByteBuffer
|
||||
return value;
|
||||
}
|
||||
|
||||
BitStream* ReadBitStream(uint32 len)
|
||||
{
|
||||
BitStream* b = new BitStream();
|
||||
for (uint32 i = 0; i < len; ++i)
|
||||
b->WriteBit(readBit());
|
||||
return b;
|
||||
}
|
||||
|
||||
template <typename T> void put(size_t pos, T value)
|
||||
{
|
||||
EndianConvert(value);
|
||||
@@ -302,6 +348,13 @@ class ByteBuffer
|
||||
return read<uint8>(pos);
|
||||
}
|
||||
|
||||
uint8& operator[] (const size_t pos)
|
||||
{
|
||||
if (pos >= size())
|
||||
throw ByteBufferException(false, pos, 1, size());
|
||||
return _storage[pos];
|
||||
}
|
||||
|
||||
size_t rpos() const { return _rpos; }
|
||||
|
||||
size_t rpos(size_t rpos_)
|
||||
@@ -381,6 +434,83 @@ class ByteBuffer
|
||||
}
|
||||
}
|
||||
|
||||
uint8 ReadUInt8()
|
||||
{
|
||||
uint8 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint16 ReadUInt16()
|
||||
{
|
||||
uint16 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint32 ReadUInt32()
|
||||
{
|
||||
uint32 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint64 ReadUInt64()
|
||||
{
|
||||
uint64 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
int8 ReadInt8()
|
||||
{
|
||||
int8 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
int16 ReadInt16()
|
||||
{
|
||||
int16 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
int32 ReadInt32()
|
||||
{
|
||||
uint32 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
int64 ReadInt64()
|
||||
{
|
||||
int64 u = 0;
|
||||
(*this) >> u;
|
||||
return u;
|
||||
}
|
||||
|
||||
std::string ReadString()
|
||||
{
|
||||
std::string s = 0;
|
||||
(*this) >> s;
|
||||
return s;
|
||||
}
|
||||
|
||||
bool ReadBoolean()
|
||||
{
|
||||
uint8 b = 0;
|
||||
(*this) >> b;
|
||||
return b > 0 ? true : false;
|
||||
}
|
||||
|
||||
float ReadSingle()
|
||||
{
|
||||
float f = 0;
|
||||
(*this) >> f;
|
||||
return f;
|
||||
}
|
||||
|
||||
const uint8 *contents() const { return &_storage[0]; }
|
||||
|
||||
size_t size() const { return _storage.size(); }
|
||||
@@ -657,5 +787,46 @@ inline void ByteBuffer::read_skip<std::string>()
|
||||
{
|
||||
read_skip<char*>();
|
||||
}
|
||||
|
||||
class BitConverter
|
||||
{
|
||||
public:
|
||||
|
||||
static uint8 ToUInt8(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<uint8>(start);
|
||||
}
|
||||
|
||||
static uint16 ToUInt16(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<uint16>(start);
|
||||
}
|
||||
|
||||
static uint32 ToUInt32(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<uint32>(start);
|
||||
}
|
||||
|
||||
static uint64 ToUInt64(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<uint64>(start);
|
||||
}
|
||||
|
||||
static int16 ToInt16(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<int16>(start);
|
||||
}
|
||||
|
||||
static int32 ToInt32(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<int32>(start);
|
||||
}
|
||||
|
||||
static int64 ToInt64(ByteBuffer buff, size_t start = 0)
|
||||
{
|
||||
return buff.read<int64>(start);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user