diff options
| author | jackpoz <giacomopoz@gmail.com> | 2016-08-19 22:29:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-19 22:29:00 +0200 |
| commit | 4073cc510a5c73ede8a38687e92ec9b3ed0ee4e5 (patch) | |
| tree | 4d198d8d591563dcd900f97de5ea39f8d02ddea1 /src/server/game/Server/Packets | |
| parent | 628792f960e035ca1ce10dcfbea2297a20c1e00c (diff) | |
Core/Packets: Port packet handling rework from 6.x branch (#17777)
* Core/Packets: Port packet handling rework from 6.x branch
Port packet handling reword from 6.x branch. Old handlers are still used at the moment, these changes allow to port old handlers to new packet-specifc ones.
Diffstat (limited to 'src/server/game/Server/Packets')
| -rw-r--r-- | src/server/game/Server/Packets/AllPackets.h | 21 | ||||
| -rw-r--r-- | src/server/game/Server/Packets/PacketUtilities.h | 231 |
2 files changed, 252 insertions, 0 deletions
diff --git a/src/server/game/Server/Packets/AllPackets.h b/src/server/game/Server/Packets/AllPackets.h new file mode 100644 index 00000000000..b4f63e44c84 --- /dev/null +++ b/src/server/game/Server/Packets/AllPackets.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2008-2016 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/>. + */ + +#ifndef AllPackets_h__ +#define AllPackets_h__ + +#endif // AllPackets_h__ diff --git a/src/server/game/Server/Packets/PacketUtilities.h b/src/server/game/Server/Packets/PacketUtilities.h new file mode 100644 index 00000000000..60c8994afc5 --- /dev/null +++ b/src/server/game/Server/Packets/PacketUtilities.h @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2008-2016 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/>. + */ + +#ifndef PacketUtilities_h__ +#define PacketUtilities_h__ + +#include "ByteBuffer.h" +#include <G3D/Vector2.h> +#include <G3D/Vector3.h> +#include <sstream> +#include <array> + +inline ByteBuffer& operator<<(ByteBuffer& data, G3D::Vector2 const& v) +{ + data << v.x << v.y; + return data; +} + +inline ByteBuffer& operator>>(ByteBuffer& data, G3D::Vector2& v) +{ + data >> v.x >> v.y; + return data; +} + +inline ByteBuffer& operator<<(ByteBuffer& data, G3D::Vector3 const& v) +{ + data << v.x << v.y << v.z; + return data; +} + +inline ByteBuffer& operator>>(ByteBuffer& data, G3D::Vector3& v) +{ + data >> v.x >> v.y >> v.z; + return data; +} + +namespace WorldPackets +{ + class PacketArrayMaxCapacityException : public ByteBufferException + { + public: + PacketArrayMaxCapacityException(std::size_t requestedSize, std::size_t sizeLimit) + { + std::ostringstream builder; + builder << "Attempted to read more array elements from packet " << requestedSize << " than allowed " << sizeLimit; + message().assign(builder.str()); + } + }; + + /** + * Utility class for automated prevention of loop counter spoofing in client packets + */ + template<typename T, std::size_t N = 1000 /*select a sane default limit*/> + class Array + { + typedef std::vector<T> storage_type; + + typedef typename storage_type::value_type value_type; + typedef typename storage_type::size_type size_type; + typedef typename storage_type::reference reference; + typedef typename storage_type::const_reference const_reference; + typedef typename storage_type::iterator iterator; + typedef typename storage_type::const_iterator const_iterator; + + public: + Array() : _limit(N) { } + Array(size_type limit) : _limit(limit) { } + + iterator begin() { return _storage.begin(); } + const_iterator begin() const { return _storage.begin(); } + + iterator end() { return _storage.end(); } + const_iterator end() const { return _storage.end(); } + + size_type size() const { return _storage.size(); } + bool empty() const { return _storage.empty(); } + + reference operator[](size_type i) { return _storage[i]; } + const_reference operator[](size_type i) const { return _storage[i]; } + + void resize(size_type newSize) + { + if (newSize > _limit) + throw PacketArrayMaxCapacityException(newSize, _limit); + + _storage.resize(newSize); + } + + void reserve(size_type newSize) + { + if (newSize > _limit) + throw PacketArrayMaxCapacityException(newSize, _limit); + + _storage.reserve(newSize); + } + + void push_back(value_type const& value) + { + if (_storage.size() >= _limit) + throw PacketArrayMaxCapacityException(_storage.size() + 1, _limit); + + _storage.push_back(value); + } + + void push_back(value_type&& value) + { + if (_storage.size() >= _limit) + throw PacketArrayMaxCapacityException(_storage.size() + 1, _limit); + + _storage.push_back(std::forward<value_type>(value)); + } + + private: + storage_type _storage; + size_type _limit; + }; + + template <typename T> + class CompactArray + { + public: + CompactArray() : _mask(0) { } + + CompactArray(CompactArray const& right) + : _mask(right._mask), _contents(right._contents) { } + + CompactArray(CompactArray&& right) + : _mask(right._mask), _contents(std::move(right._contents)) + { + right._mask = 0; + } + + CompactArray& operator= (CompactArray const& right) + { + _mask = right._mask; + _contents = right._contents; + return *this; + } + + CompactArray& operator= (CompactArray&& right) + { + _mask = right._mask; + right._mask = 0; + _contents = std::move(right._contents); + return *this; + } + + uint32 GetMask() const { return _mask; } + T const& operator[](size_t index) const { return _contents.at(index); } + size_t GetSize() const { return _contents.size(); } + + void Insert(size_t index, T const& value) + { + ASSERT(index < 0x20); + + _mask |= 1 << index; + if (_contents.size() <= index) + _contents.resize(index + 1); + _contents[index] = value; + } + + void Clear() + { + _mask = 0; + _contents.clear(); + } + + bool operator==(CompactArray const& r) const + { + if (_mask != r._mask) + return false; + + return _contents == r._contents; + } + + bool operator!=(CompactArray const& r) const { return !(*this == r); } + + private: + uint32 _mask; + std::vector<T> _contents; + }; + + template <typename T> + ByteBuffer& operator<<(ByteBuffer& data, CompactArray<T> const& v) + { + uint32 mask = v.GetMask(); + data << uint32(mask); + for (size_t i = 0; i < v.GetSize(); ++i) + { + if (mask & (1 << i)) + data << v[i]; + } + + return data; + } + + template <typename T> + ByteBuffer& operator>>(ByteBuffer& data, CompactArray<T>& v) + { + uint32 mask; + data >> mask; + + for (size_t index = 0; mask != 0; mask >>= 1, ++index) + { + if ((mask & 1) != 0) + { + T value; + data >> value; + v.Insert(index, value); + } + } + + return data; + } +} + +#endif // PacketUtilities_h__ |
