|
|
@@ -1,12 +1,10 @@
|
|
|
#include <cassert>
|
|
|
#include <cstring>
|
|
|
|
|
|
-#include "MathUtils.h"
|
|
|
#include "Message.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
-
|
|
|
namespace network
|
|
|
{
|
|
|
|
|
|
@@ -15,11 +13,15 @@ Message::Message() : w_data(NULL), r_data(NULL), max_size(0), cur_size(0), write
|
|
|
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
Message::~Message()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
uint8_t* Message::get_byte_space(int32_t len)
|
|
|
{
|
|
|
uint8_t *ptr;
|
|
|
@@ -42,6 +44,8 @@ uint8_t* Message::get_byte_space(int32_t len)
|
|
|
return ptr;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
bool Message::check_overflow(int32_t num_bits)
|
|
|
{
|
|
|
assert( num_bits >= 0 );
|
|
|
@@ -60,6 +64,8 @@ bool Message::check_overflow(int32_t num_bits)
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::init(uint8_t *data, int32_t len)
|
|
|
{
|
|
|
w_data = data;
|
|
|
@@ -67,6 +73,8 @@ void Message::init(uint8_t *data, int32_t len)
|
|
|
max_size = len;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::init(const uint8_t *data, int32_t len)
|
|
|
{
|
|
|
w_data = NULL;
|
|
|
@@ -74,32 +82,43 @@ void Message::init(const uint8_t *data, int32_t len)
|
|
|
max_size = len;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
uint8_t* Message::get_data()
|
|
|
{
|
|
|
return w_data;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
const uint8_t* Message::get_data() const
|
|
|
{
|
|
|
return r_data;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_max_size() const
|
|
|
{
|
|
|
return max_size;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
bool Message::is_overflowed()
|
|
|
{
|
|
|
return overflowed;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
|
|
|
int32_t Message::get_size() const
|
|
|
{
|
|
|
return cur_size;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::set_size(int32_t size)
|
|
|
{
|
|
|
if (size > max_size)
|
|
|
@@ -112,11 +131,15 @@ void Message::set_size(int32_t size)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_write_bit() const
|
|
|
{
|
|
|
return write_bit;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::set_write_bit(int32_t bit)
|
|
|
{
|
|
|
write_bit = bit & 7;
|
|
|
@@ -126,22 +149,30 @@ void Message::set_write_bit(int32_t bit)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_num_bits_written() const
|
|
|
{
|
|
|
return ((cur_size << 3) - ((8 - write_bit) & 7));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_remaining_write_bits() const
|
|
|
{
|
|
|
return (max_size << 3) - get_num_bits_written();
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::save_write_state(int32_t& s,int32_t& b) const
|
|
|
{
|
|
|
s = cur_size;
|
|
|
b = write_bit;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::restore_write_state(int32_t s,int32_t b)
|
|
|
{
|
|
|
cur_size = s;
|
|
|
@@ -153,48 +184,66 @@ void Message::restore_write_state(int32_t s,int32_t b)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_read_count() const
|
|
|
{
|
|
|
return read_count;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::set_read_count(int32_t bytes)
|
|
|
{
|
|
|
read_count = bytes;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_read_bit() const
|
|
|
{
|
|
|
return read_bit;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::set_read_bit(int32_t bit)
|
|
|
{
|
|
|
read_bit = bit & 7;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_num_bits_read() const
|
|
|
{
|
|
|
return ((read_count << 3) - ((8 - read_bit) & 7));
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_remaining_read_bits() const
|
|
|
{
|
|
|
return (cur_size << 3) - get_num_bits_read();
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::save_read_state(int32_t& c, int32_t& b) const
|
|
|
{
|
|
|
c = read_count;
|
|
|
b = read_bit;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::restore_read_state(int32_t c, int32_t b)
|
|
|
{
|
|
|
read_count = c;
|
|
|
read_bit = b & 7;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::begin_writing()
|
|
|
{
|
|
|
cur_size = 0;
|
|
|
@@ -202,16 +251,22 @@ void Message::begin_writing()
|
|
|
overflowed = false;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_remaining_space() const
|
|
|
{
|
|
|
return max_size - cur_size;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_byte_align()
|
|
|
{
|
|
|
write_bit = 0;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_bits(int32_t value, int32_t num_bits)
|
|
|
{
|
|
|
int32_t put;
|
|
|
@@ -292,41 +347,57 @@ void Message::write_bits(int32_t value, int32_t num_bits)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_int8(int32_t c)
|
|
|
{
|
|
|
write_bits(c, -8);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_uint8(int32_t c)
|
|
|
{
|
|
|
write_bits(c, 8);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_int16(int32_t c)
|
|
|
{
|
|
|
write_bits(c, -16);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_uint16(int32_t c)
|
|
|
{
|
|
|
write_bits(c, 16);
|
|
|
}
|
|
|
|
|
|
-void Message::write_int64(int32_t c)
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
+void Message::write_int32(int32_t c)
|
|
|
{
|
|
|
write_bits(c, 32);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_real(real f)
|
|
|
{
|
|
|
write_bits(*reinterpret_cast<int32_t *>(&f), 32);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_vec3(const Vec3& v, int32_t num_bits)
|
|
|
{
|
|
|
write_bits(vec3_to_bits(v, num_bits), num_bits);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_string(const char* s, int32_t max_len, bool make_7_bit)
|
|
|
{
|
|
|
if (!s)
|
|
|
@@ -376,11 +447,15 @@ void Message::write_string(const char* s, int32_t max_len, bool make_7_bit)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_data(const void* data, int32_t length)
|
|
|
{
|
|
|
memcpy(get_byte_space(length), data, length);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::write_ipv4addr(const os::IPv4Address addr)
|
|
|
{
|
|
|
uint8_t* ptr;
|
|
|
@@ -390,22 +465,30 @@ void Message::write_ipv4addr(const os::IPv4Address addr)
|
|
|
write_uint16(addr.port);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::begin_reading() const
|
|
|
{
|
|
|
read_count = 0;
|
|
|
read_bit = 0;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::get_remaing_data() const
|
|
|
{
|
|
|
cur_size - read_count;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::read_byte_align() const
|
|
|
{
|
|
|
read_bit = 0;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_bits(int32_t num_bits) const
|
|
|
{
|
|
|
int32_t value;
|
|
|
@@ -479,32 +562,44 @@ int32_t Message::read_bits(int32_t num_bits) const
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_int8() const
|
|
|
{
|
|
|
return (int32_t)read_bits(-8);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_uint8() const
|
|
|
{
|
|
|
return (int32_t)read_bits(8);
|
|
|
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_int16() const
|
|
|
{
|
|
|
return (int32_t)read_bits(-16);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_uint16() const
|
|
|
{
|
|
|
return (int32_t)read_bits(16);
|
|
|
}
|
|
|
|
|
|
-int32_t Message::read_int64() const
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
+int32_t Message::read_int32() const
|
|
|
{
|
|
|
return (int32_t)read_bits(32);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
real Message::read_real() const
|
|
|
{
|
|
|
float value;
|
|
|
@@ -512,16 +607,20 @@ real Message::read_real() const
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
Vec3 Message::read_vec3(int32_t num_bits) const
|
|
|
{
|
|
|
return bits_to_vec3(read_bits(num_bits), num_bits);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_string(char* buffer, int32_t buffer_size) const
|
|
|
{
|
|
|
int l = 0;
|
|
|
int c;
|
|
|
-
|
|
|
+
|
|
|
read_byte_align();
|
|
|
|
|
|
while(1)
|
|
|
@@ -551,6 +650,8 @@ int32_t Message::read_string(char* buffer, int32_t buffer_size) const
|
|
|
return l;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::read_data(void* data, int32_t length) const
|
|
|
{
|
|
|
int count;
|
|
|
@@ -579,6 +680,8 @@ int32_t Message::read_data(void* data, int32_t length) const
|
|
|
return (read_count - count);
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
void Message::read_ipv4addr(os::IPv4Address* addr) const
|
|
|
{
|
|
|
|
|
|
@@ -590,6 +693,8 @@ void Message::read_ipv4addr(os::IPv4Address* addr) const
|
|
|
addr->port = read_uint16();
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
|
|
|
{
|
|
|
assert(num_bits >= 6 && num_bits <= 32);
|
|
|
@@ -613,6 +718,8 @@ int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
|
|
|
return bits;
|
|
|
}
|
|
|
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
Vec3 Message::bits_to_vec3(int32_t bits, int32_t num_bits)
|
|
|
{
|
|
|
assert(num_bits >= 6 && num_bits <= 32);
|
|
|
@@ -634,5 +741,7 @@ Vec3 Message::bits_to_vec3(int32_t bits, int32_t num_bits)
|
|
|
return v;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
-}
|
|
|
+//---------------------------------------------------------------------------------------------
|
|
|
+
|
|
|
+} //namespace network
|
|
|
+} //namespace crown
|