diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-13 16:01:56 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-13 16:01:56 +0200 |
commit | 1563ae5be6bc130a9b3a23464f7e28fdb1e87da3 (patch) | |
tree | b1f65a03827494fa78e320b134f4cc7df54754bb /code/Packet.cpp | |
parent | 2017-05-12 (diff) | |
download | AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar.gz AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar.bz2 AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar.lz AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar.xz AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.tar.zst AltCraft-1563ae5be6bc130a9b3a23464f7e28fdb1e87da3.zip |
Diffstat (limited to 'code/Packet.cpp')
-rw-r--r-- | code/Packet.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/code/Packet.cpp b/code/Packet.cpp new file mode 100644 index 0000000..695e371 --- /dev/null +++ b/code/Packet.cpp @@ -0,0 +1,100 @@ +#include "Packet.hpp" + +Packet::Packet(int id) { + Field fLen; + fLen.SetVarInt(0); + m_fields.push_back(fLen); + Field fId; + fId.SetVarInt(id); + m_fields.push_back(fId); +} + +Packet Packet::operator=(Packet other) { + other.swap(*this); + return *this; +} + +void Packet::swap(Packet &other) { + std::swap(m_fields, other.m_fields); + std::swap(m_data, other.m_data); + std::swap(m_parsePtr, other.m_parsePtr); + std::swap(m_dataLength, other.m_dataLength); +} + +void Packet::CopyToBuff(byte *ptr) { + m_fields[0].SetVarInt(GetLength() - m_fields[0].GetLength()); + for (int i = 0; i < m_fields.size(); i++) { + m_fields[i].CopyToBuff(ptr); + ptr += m_fields[i].GetLength(); + } +} + +void Packet::ParseField(FieldType type, size_t len) { + if (type == ByteArray && len == 0) + throw 118; + Field f = FieldParser::Parse(type, m_parsePtr, len); + m_fields.push_back(f); + m_parsePtr += f.GetLength(); + if (m_parsePtr == m_data + m_dataLength) { + delete[] m_data; + m_data = nullptr; + m_dataLength = 0; + m_parsePtr = nullptr; + } +} + +Packet::Packet(byte *data) { + Field fLen = FieldParser::Parse(VarInt, data); + data += fLen.GetLength(); + Field fId = FieldParser::Parse(VarInt, data); + data += fId.GetLength(); + m_dataLength = fLen.GetVarInt() - fId.GetLength(); + m_data = new byte[m_dataLength]; + std::copy(data,data+m_dataLength,m_data); + m_parsePtr = m_data; + m_fields.push_back(fLen); + m_fields.push_back(fId); +} + +Field &Packet::GetField(int id) { + if (id < -2 || id >= m_fields.size() - 2) + throw 111; + return m_fields[id + 2]; +} + +size_t Packet::GetLength() { + size_t len = 0; + for (int i = 0; i < m_fields.size(); i++) + len += m_fields[i].GetLength(); + return len + m_dataLength; +} + +void Packet::AddField(Field field) { + m_fields.push_back(field); +} + +int Packet::GetId() { + return m_fields[1].GetVarInt(); +} + +Packet::Packet(const Packet &other) { + if (other.m_dataLength > 0) { + m_dataLength = other.m_dataLength; + m_data = new byte[m_dataLength]; + m_parsePtr = m_data + (other.m_data - other.m_parsePtr); + std::copy(other.m_data, other.m_data + m_dataLength, m_data); + } + m_fields = other.m_fields; +} + +void Packet::ParseFieldArray(Field &field, FieldType type, size_t len) { + Field f = FieldParser::Parse(type, m_parsePtr, len); + field.Attach(f); + m_parsePtr += f.GetLength(); + if (m_parsePtr == m_data + m_dataLength) { + delete[] m_data; + m_data = nullptr; + m_dataLength = 0; + m_parsePtr = nullptr; + } +} |