Skip to content

Instantly share code, notes, and snippets.

@Langerz82
Created July 18, 2018 03:18
Show Gist options
  • Save Langerz82/e3f4bf3d642ccfce6b5cf47ef7baf91e to your computer and use it in GitHub Desktop.
Save Langerz82/e3f4bf3d642ccfce6b5cf47ef7baf91e to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2008-2018 TrinityCore <https://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* 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 "ByteBufferNew.h"
#include "Errors.h"
#include "MessageBuffer.h"
#include "Common.h"
#include "Log.h"
#include "Util.h"
#include <sstream>
#include <ctime>
ByteBufferNew::ByteBufferNew(MessageBuffer&& buffer) : _rpos(0), _wpos(buffer.GetActiveSize()), _storage(nullptr), maxSize(0)
{
if (buffer.GetActiveSize() == 0)
{
maxSize = DEFAULT_SIZE;
_storage = new uint8[DEFAULT_SIZE]();
return;
}
maxSize = buffer.GetBufferSize();
_storage = new uint8[maxSize]();
std::memcpy(&_storage[0], &(buffer.Move())[0], buffer.GetActiveSize());
}
ByteBufferNewPositionException::ByteBufferNewPositionException(bool add, size_t pos,
size_t size, size_t valueSize)
{
std::ostringstream ss;
ss << "Attempted to " << (add ? "put" : "get") << " value with size: "
<< valueSize << " in ByteBuffer (pos: " << pos << " size: " << size
<< ")";
message().assign(ss.str());
}
ByteBufferNewSourceException::ByteBufferNewSourceException(size_t pos, size_t size,
size_t valueSize)
{
std::ostringstream ss;
ss << "Attempted to put a "
<< (valueSize > 0 ? "NULL-pointer" : "zero-sized value")
<< " in ByteBuffer (pos: " << pos << " size: " << size << ")";
message().assign(ss.str());
}
ByteBufferNew& ByteBufferNew::operator>>(float& value)
{
value = read<float>();
if (!std::isfinite(value))
throw ByteBufferNewException();
return *this;
}
ByteBufferNew& ByteBufferNew::operator>>(double& value)
{
value = read<double>();
if (!std::isfinite(value))
throw ByteBufferNewException();
return *this;
}
uint32 ByteBufferNew::ReadPackedTime()
{
uint32 packedDate = read<uint32>();
tm lt = tm();
lt.tm_min = packedDate & 0x3F;
lt.tm_hour = (packedDate >> 6) & 0x1F;
//lt.tm_wday = (packedDate >> 11) & 7;
lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1;
lt.tm_mon = (packedDate >> 20) & 0xF;
lt.tm_year = ((packedDate >> 24) & 0x1F) + 100;
return uint32(mktime(&lt));
}
void ByteBufferNew::append(uint8 const* src, size_t cnt)
{
ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
ASSERT(size() < 10000000);
size_t const newsize = _wpos + cnt;
if (newsize >= maxSize)
{
resizeMem(round((newsize / DEFAULT_SIZE + 1) * DEFAULT_SIZE * 1.5f));
}
std::memcpy(&_storage[_wpos], src, cnt);
_wpos = newsize;
}
void ByteBufferNew::AppendPackedTime(time_t time)
{
tm lt;
localtime_r(&time, &lt);
append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min);
}
void ByteBufferNew::put(size_t pos, uint8 const* src, size_t cnt)
{
//ASSERT(pos + cnt <= maxSize, "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size());
ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
size_t const newsize = pos + cnt;
if (newsize >= maxSize)
{
resizeMem(round((newsize / DEFAULT_SIZE + 1) * DEFAULT_SIZE * 1.5f));
_wpos = newsize;
}
std::memcpy(&_storage[pos], src, cnt);
}
void ByteBufferNew::print_storage() const
{
if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
return;
std::ostringstream o;
o << "STORAGE_SIZE: " << size();
for (uint32 i = 0; i < size(); ++i)
o << read<uint8>(i) << " - ";
o << " ";
TC_LOG_TRACE("network", "%s", o.str().c_str());
}
void ByteBufferNew::textlike() const
{
if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
return;
std::ostringstream o;
o << "STORAGE_SIZE: " << size();
for (uint32 i = 0; i < size(); ++i)
{
char buf[2];
snprintf(buf, 2, "%c", read<uint8>(i));
o << buf;
}
o << " ";
TC_LOG_TRACE("network", "%s", o.str().c_str());
}
void ByteBufferNew::hexlike() const
{
if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
return;
uint32 j = 1, k = 1;
std::ostringstream o;
o << "STORAGE_SIZE: " << size();
for (uint32 i = 0; i < size(); ++i)
{
char buf[3];
snprintf(buf, 3, "%2X", read<uint8>(i));
if ((i == (j * 8)) && ((i != (k * 16))))
{
o << "| ";
++j;
}
else if (i == (k * 16))
{
o << "\n";
++k;
++j;
}
o << buf;
}
o << " ";
TC_LOG_TRACE("network", "%s", o.str().c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment