mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-22 13:16:23 +00:00
feat(Core/Packets): Port packet handling from TrinityCore (#5617)
* feat(Core/Packets): Port packet handling from TrinityCore * 1 * 2 * 3 * 1 * 2 * #3670 * 3 * 1 * codestyle * fix msvc warnings
This commit is contained in:
128
src/common/Utilities/MessageBuffer.h
Normal file
128
src/common/Utilities/MessageBuffer.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef __MESSAGEBUFFER_H_
|
||||
#define __MESSAGEBUFFER_H_
|
||||
|
||||
#include "Define.h"
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
class MessageBuffer
|
||||
{
|
||||
using size_type = std::vector<uint8>::size_type;
|
||||
|
||||
public:
|
||||
MessageBuffer() : _wpos(0), _rpos(0), _storage()
|
||||
{
|
||||
_storage.resize(4096);
|
||||
}
|
||||
|
||||
explicit MessageBuffer(std::size_t initialSize) : _wpos(0), _rpos(0), _storage()
|
||||
{
|
||||
_storage.resize(initialSize);
|
||||
}
|
||||
|
||||
MessageBuffer(MessageBuffer const& right) :
|
||||
_wpos(right._wpos), _rpos(right._rpos), _storage(right._storage) { }
|
||||
|
||||
MessageBuffer(MessageBuffer&& right) noexcept :
|
||||
_wpos(right._wpos), _rpos(right._rpos), _storage(right.Move()) { }
|
||||
|
||||
void Reset()
|
||||
{
|
||||
_wpos = 0;
|
||||
_rpos = 0;
|
||||
}
|
||||
|
||||
void Resize(size_type bytes)
|
||||
{
|
||||
_storage.resize(bytes);
|
||||
}
|
||||
|
||||
uint8* GetBasePointer() { return _storage.data(); }
|
||||
uint8* GetReadPointer() { return GetBasePointer() + _rpos; }
|
||||
uint8* GetWritePointer() { return GetBasePointer() + _wpos; }
|
||||
|
||||
void ReadCompleted(size_type bytes) { _rpos += bytes; }
|
||||
void WriteCompleted(size_type bytes) { _wpos += bytes; }
|
||||
|
||||
[[nodiscard]] size_type GetActiveSize() const { return _wpos - _rpos; }
|
||||
[[nodiscard]] size_type GetRemainingSpace() const { return _storage.size() - _wpos; }
|
||||
[[nodiscard]] size_type GetBufferSize() const { return _storage.size(); }
|
||||
|
||||
// Discards inactive data
|
||||
void Normalize()
|
||||
{
|
||||
if (_rpos)
|
||||
{
|
||||
if (_rpos != _wpos)
|
||||
{
|
||||
memmove(GetBasePointer(), GetReadPointer(), GetActiveSize());
|
||||
}
|
||||
|
||||
_wpos -= _rpos;
|
||||
_rpos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensures there's "some" free space, make sure to call Normalize() before this
|
||||
void EnsureFreeSpace()
|
||||
{
|
||||
// resize buffer if it's already full
|
||||
if (GetRemainingSpace() == 0)
|
||||
{
|
||||
_storage.resize(_storage.size() * 3 / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void Write(void const* data, std::size_t size)
|
||||
{
|
||||
if (size)
|
||||
{
|
||||
memcpy(GetWritePointer(), data, size);
|
||||
WriteCompleted(size);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8>&& Move()
|
||||
{
|
||||
_wpos = 0;
|
||||
_rpos = 0;
|
||||
|
||||
return std::move(_storage);
|
||||
}
|
||||
|
||||
MessageBuffer& operator=(MessageBuffer const& right)
|
||||
{
|
||||
if (this != &right)
|
||||
{
|
||||
_wpos = right._wpos;
|
||||
_rpos = right._rpos;
|
||||
_storage = right._storage;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
MessageBuffer& operator=(MessageBuffer&& right) noexcept
|
||||
{
|
||||
if (this != &right)
|
||||
{
|
||||
_wpos = right._wpos;
|
||||
_rpos = right._rpos;
|
||||
_storage = right.Move();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
size_type _wpos;
|
||||
size_type _rpos;
|
||||
std::vector<uint8> _storage;
|
||||
};
|
||||
|
||||
#endif /* __MESSAGEBUFFER_H_ */
|
||||
53
src/common/Utilities/Tuples.h
Normal file
53
src/common/Utilities/Tuples.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef Tuples_h__
|
||||
#define Tuples_h__
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace acore
|
||||
{
|
||||
template <typename T, typename Tuple>
|
||||
struct has_type;
|
||||
|
||||
template <typename T, typename... Us>
|
||||
struct has_type<T, std::tuple<Us...>> : std::disjunction<std::is_same<T, Us>...>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T, typename... Us>
|
||||
constexpr bool has_type_v = has_type<T, Us...>::value;
|
||||
|
||||
template<typename>
|
||||
struct is_tuple : std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
struct is_tuple<std::tuple<Ts...>> : std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
constexpr bool is_tuple_v = is_tuple<Ts...>::value;
|
||||
|
||||
namespace Impl
|
||||
{
|
||||
template <class T, class Tuple, size_t... I>
|
||||
T* new_from_tuple(Tuple&& args, std::index_sequence<I...>)
|
||||
{
|
||||
return new T(std::get<I>(std::forward<Tuple>(args))...);
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class Tuple>
|
||||
[[nodiscard]] T* new_from_tuple(Tuple&& args)
|
||||
{
|
||||
return Impl::new_from_tuple<T>(std::forward<Tuple>(args), std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
|
||||
}
|
||||
}
|
||||
|
||||
#endif // Tuples_h__
|
||||
Reference in New Issue
Block a user