Bunch more work on fixing up DB and packets

This commit is contained in:
Michael Cook (mackal)
2018-07-02 22:01:17 -04:00
parent 1b8736188d
commit 0ea82b5d88
8 changed files with 360 additions and 162 deletions
+2
View File
@@ -61,6 +61,7 @@ SET(common_sources
rdtsc.cpp
rulesys.cpp
say_link.cpp
serialize_buffer.cpp
serverinfo.cpp
shareddb.cpp
skills.cpp
@@ -192,6 +193,7 @@ SET(common_headers
ruletypes.h
say_link.h
seperator.h
serialize_buffer.h
serverinfo.h
servertalk.h
shareddb.h
+2
View File
@@ -63,6 +63,8 @@ public:
void WriteFloat(float value) { *(float *)(pBuffer + _wpos) = value; _wpos += sizeof(float); }
void WriteDouble(double value) { *(double *)(pBuffer + _wpos) = value; _wpos += sizeof(double); }
void WriteString(const char * str) { uint32 len = static_cast<uint32>(strlen(str)) + 1; memcpy(pBuffer + _wpos, str, len); _wpos += len; }
// this is used in task system a lot, it is NOT null-termed
void WriteLengthString(uint32 len, const char *str) { *(uint32 *)(pBuffer + _wpos) = len; _wpos += sizeof(uint32); memcpy(pBuffer + _wpos, str, len); _wpos += len; }
void WriteData(const void *ptr, size_t n) { memcpy(pBuffer + _wpos, ptr, n); _wpos += n; }
uint8 ReadUInt8() { uint8 value = *(uint8 *)(pBuffer + _rpos); _rpos += sizeof(uint8); return value; }
+1
View File
@@ -20,6 +20,7 @@
#include "base_packet.h"
#include "platform.h"
#include "serialize_buffer.h"
#include <iostream>
#ifdef STATIC_OPCODE
+23
View File
@@ -0,0 +1,23 @@
#include "serialize_buffer.h"
void SerializeBuffer::Grow(size_t new_size)
{
assert(new_size > m_capacity);
auto new_buffer = new unsigned char[new_size * 2];
memset(new_buffer, 0, new_size * 2);
if (m_buffer)
memcpy(new_buffer, m_buffer, m_capacity);
m_capacity = new_size * 2;
delete[] m_buffer;
m_buffer = new_buffer;
}
void SerializeBuffer::Reset()
{
delete[] m_buffer;
m_buffer = nullptr;
m_capacity = 0;
m_pos = 0;
}
+186
View File
@@ -0,0 +1,186 @@
#ifndef SERIALIZE_BUFFER_H
#define SERIALIZE_BUFFER_H
#include <cstring>
#include <cassert>
#include <cstdint>
#include <string>
class SerializeBuffer
{
public:
SerializeBuffer() : m_buffer(nullptr), m_capacity(0), m_pos(0) {}
explicit SerializeBuffer(size_t size) : m_capacity(size), m_pos(0)
{
m_buffer = new unsigned char[size];
memset(m_buffer, 0, size);
}
SerializeBuffer(const SerializeBuffer &rhs)
: m_buffer(new unsigned char[rhs.m_capacity]), m_capacity(rhs.m_capacity), m_pos(rhs.m_pos)
{
memcpy(m_buffer, rhs.m_buffer, rhs.m_capacity);
}
SerializeBuffer &operator=(const SerializeBuffer &rhs)
{
if (this != &rhs) {
delete[] m_buffer;
m_buffer = new unsigned char[rhs.m_capacity];
m_capacity = rhs.m_capacity;
m_pos = rhs.m_pos;
memcpy(m_buffer, rhs.m_buffer, m_capacity);
}
return *this;
}
SerializeBuffer(SerializeBuffer &&rhs) : m_buffer(rhs.m_buffer), m_capacity(rhs.m_capacity), m_pos(rhs.m_pos)
{
rhs.m_buffer = nullptr;
rhs.m_capacity = 0;
rhs.m_pos = 0;
}
SerializeBuffer &operator=(SerializeBuffer &&rhs)
{
if (this != &rhs) {
delete[] m_buffer;
m_buffer = rhs.m_buffer;
m_capacity = rhs.m_capacity;
m_pos = rhs.m_pos;
rhs.m_buffer = nullptr;
rhs.m_capacity = 0;
rhs.m_pos = 0;
}
return *this;
}
~SerializeBuffer() { delete[] m_buffer; }
void WriteUInt8(uint8_t v)
{
if (m_pos + sizeof(uint8_t) > m_capacity)
Grow(m_capacity + sizeof(uint8_t));
*(uint8_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(uint8_t);
}
void WriteUInt16(uint16_t v)
{
if (m_pos + sizeof(uint16_t) > m_capacity)
Grow(m_capacity + sizeof(uint16_t));
*(uint16_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(uint16_t);
}
void WriteUInt32(uint32_t v)
{
if (m_pos + sizeof(uint32_t) > m_capacity)
Grow(m_capacity + sizeof(uint32_t));
*(uint32_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(uint32_t);
}
void WriteUInt64(uint64_t v)
{
if (m_pos + sizeof(uint64_t) > m_capacity)
Grow(m_capacity + sizeof(uint64_t));
*(uint64_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(uint64_t);
}
void WriteInt8(int8_t v)
{
if (m_pos + sizeof(int8_t) > m_capacity)
Grow(m_capacity + sizeof(int8_t));
*(int8_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(int8_t);
}
void WriteInt16(int16_t v)
{
if (m_pos + sizeof(int16_t) > m_capacity)
Grow(m_capacity + sizeof(int16_t));
*(int16_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(int16_t);
}
void WriteInt32(int32_t v)
{
if (m_pos + sizeof(int32_t) > m_capacity)
Grow(m_capacity + sizeof(int32_t));
*(int32_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(int32_t);
}
void WriteInt64(int64_t v)
{
if (m_pos + sizeof(int64_t) > m_capacity)
Grow(m_capacity + sizeof(int64_t));
*(int64_t *)(m_buffer + m_pos) = v;
m_pos += sizeof(int64_t);
}
void WriteFloat(float v)
{
if (m_pos + sizeof(float) > m_capacity)
Grow(m_capacity + sizeof(float));
*(float *)(m_buffer + m_pos) = v;
m_pos += sizeof(float);
}
void WriteDouble(double v)
{
if (m_pos + sizeof(double) > m_capacity)
Grow(m_capacity + sizeof(double));
*(double *)(m_buffer + m_pos) = v;
m_pos += sizeof(double);
}
void WriteString(const char *str)
{
assert(str != nullptr);
auto len = strlen(str) + 1;
if (m_pos + len > m_capacity)
Grow(m_capacity + len);
memcpy(m_buffer + m_pos, str, len);
m_pos += len;
}
void WriteString(const std::string &str)
{
auto len = str.length() + 1;
if (m_pos + len > m_capacity)
Grow(m_capacity + len);
memcpy(m_buffer + m_pos, str.c_str(), len);
m_pos += len;
}
void WriteLengthString(uint32_t len, const char *str)
{
assert(str != nullptr);
if (m_pos + len + sizeof(uint32_t) > m_capacity)
Grow(m_capacity + len + sizeof(uint32_t));
*(uint32_t *)(m_buffer + m_pos) = len;
m_pos += sizeof(uint32_t);
memcpy(m_buffer + m_pos, str, len);
m_pos += len;
}
size_t size() const { return m_pos; }
size_t length() const { return size(); }
size_t capacity() const { return m_capacity; }
const unsigned char *buffer() const { return m_buffer; }
private:
void Grow(size_t new_size);
void Reset();
unsigned char *m_buffer;
size_t m_capacity;
size_t m_pos;
};
#endif /* !SERIALIZE_BUFFER_H */