23#ifndef NETWORK_STRING_HPP
24#define NETWORK_STRING_HPP
27#include "utils/leak_check.hpp"
29#include "utils/vec3.hpp"
31#include "LinearMath/btQuaternion.h"
42typedef unsigned char uchar;
80 throw std::out_of_range(
"getString out of range.");
91 for (
unsigned int i = 0; i < value.size(); i++)
92 m_buffer.push_back((uint8_t)(value[i]));
98 template<
typename T,
size_t n>
150 uint16_t max_len = 65535);
152 int decodeString16(irr::core::stringw* out,
153 uint16_t max_len = 65535);
159 std::string
getLogMessage(
const std::string &indent=
"")
const;
220 m_buffer.push_back((uint8_t)(value));
227 m_buffer.push_back((value >> 8) & 0xff);
236 uint32_t combined = (uint32_t)value & 0xffffff;
237 m_buffer.push_back((combined >> 16) & 0xff);
238 m_buffer.push_back((combined >> 8) & 0xff);
239 m_buffer.push_back(combined & 0xff);
247 m_buffer.push_back((value >> 24) & 0xff);
248 m_buffer.push_back((value >> 16) & 0xff);
249 m_buffer.push_back((value >> 8) & 0xff);
258 m_buffer.push_back((value >> 56) & 0xff);
259 m_buffer.push_back((value >> 48) & 0xff);
260 m_buffer.push_back((value >> 40) & 0xff);
261 m_buffer.push_back((value >> 32) & 0xff);
262 m_buffer.push_back((value >> 24) & 0xff);
263 m_buffer.push_back((value >> 16) & 0xff);
264 m_buffer.push_back((value >> 8) & 0xff);
273 uint32_t *p = (uint32_t*)&value;
321 inline uint64_t
getUInt64()
const {
return get<uint64_t, 8>(); }
324 inline uint32_t
getUInt32()
const {
return get<uint32_t, 4>(); }
329 uint32_t combined = get<uint32_t, 3>();
330 if (combined & 0x800000)
331 return (0x1000000 - (
int)combined) * -1;
333 return (
int)combined;
337 inline uint32_t
getTime()
const {
return get<uint32_t, 4>(); }
340 inline uint16_t
getUInt16()
const {
return get<uint16_t, 2>(); }
343 inline int16_t
getInt16()
const {
return get<int16_t, 2>(); }
374 memcpy(&f, &u,
sizeof(
float));
466 m_buffer[0] &= ~PROTOCOL_SYNCHRONOUS;
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
BareNetworkString & add(float f)
Adds a floating point number.
Definition: network_string.hpp:290
uint64_t getUInt64() const
Returns a unsigned 64 bit integer.
Definition: network_string.hpp:321
btQuaternion getQuat() const
Gets a bullet quaternion.
Definition: network_string.hpp:391
uint8_t getUInt8() const
Returns an unsigned 8-bit integer.
Definition: network_string.hpp:346
unsigned int size() const
Returns the remaining length of the network string.
Definition: network_string.hpp:191
BareNetworkString(int capacity=16)
Constructor, sets the protocol type of this message.
Definition: network_string.hpp:124
BareNetworkString & encodeString16(const irr::core::stringw &value, uint16_t max_len=65535)
Encode string with max length of 16bit and utf32, used in motd or chat.
Definition: network_string.cpp:131
int decodeString(std::string *out) const
Returns a string at the given position.
Definition: network_string.cpp:104
int m_current_offset
To avoid copying the buffer when bytes are deleted (which only happens at the front),...
Definition: network_string.hpp:68
const char * getData() const
Returns a byte pointer to the content of the network string.
Definition: network_string.hpp:170
BareNetworkString & addUInt8(const uint8_t value)
Add 8 bit unsigned int.
Definition: network_string.hpp:210
std::vector< uint8_t > & getBuffer()
Returns the internal buffer of the network string.
Definition: network_string.hpp:162
BareNetworkString & encodeString(const std::string &value)
Adds one byte for the length of the string, and then (up to 255 of) the characters of the given strin...
Definition: network_string.cpp:79
BareNetworkString & addInt24(const int value)
Adds signed 24 bit integer.
Definition: network_string.hpp:234
std::string getString(int len) const
Returns a part of the network string as a std::string.
Definition: network_string.hpp:76
char * getCurrentData()
Returns a byte pointer to the unread remaining content of the network string.
Definition: network_string.hpp:175
BareNetworkString & addUInt64(const uint64_t &value)
Adds unsigned 64 bit integer.
Definition: network_string.hpp:256
BareNetworkString & addFloat(const float value)
Adds a 4 byte floating point value.
Definition: network_string.hpp:271
void skip(int n)
Skips the specified number of bytes when reading.
Definition: network_string.hpp:195
std::vector< uint8_t > m_buffer
The actual buffer.
Definition: network_string.hpp:60
int getInt24() const
Returns a signed 24 bit integer.
Definition: network_string.hpp:327
BareNetworkString & add(const btQuaternion &quat)
Adds the four components of a quaternion.
Definition: network_string.hpp:303
BareNetworkString(const char *data, int len)
Initialises the string with a sequence of characters.
Definition: network_string.hpp:138
BareNetworkString & add(const Vec3 &xyz)
Adds the xyz components of a Vec3 to the string.
Definition: network_string.hpp:296
float getFloat() const
Gets a 4 byte floating point value.
Definition: network_string.hpp:358
Vec3 getVec3() const
Gets a Vec3.
Definition: network_string.hpp:380
int decodeStringW(irr::core::stringw *out) const
Returns an irrlicht wide string from the utf8 encoded string at the given position.
Definition: network_string.cpp:120
BareNetworkString & addChar(const char value)
Adds a single character to the string.
Definition: network_string.hpp:218
unsigned int getTotalSize() const
Returns the send size, which is the full length of the buffer.
Definition: network_string.hpp:206
BareNetworkString & addTime(int ticks)
Adds a function to add a time ticks value.
Definition: network_string.hpp:313
void reset()
Allows one to read a buffer from the beginning again.
Definition: network_string.hpp:147
const char * getCurrentData() const
Returns a byte pointer to the unread remaining content of the network string.
Definition: network_string.hpp:183
BareNetworkString & operator+=(BareNetworkString const &value)
Adds the content of another network string.
Definition: network_string.hpp:280
BareNetworkString & addUInt32(const uint32_t &value)
Adds unsigned 32 bit integer.
Definition: network_string.hpp:245
BareNetworkString & addUInt16(const uint16_t value)
Adds 16 bit unsigned int.
Definition: network_string.hpp:225
T get() const
Another function for n == 1 to surpress warnings in clang.
Definition: network_string.hpp:116
uint32_t getUInt32() const
Returns a unsigned 32 bit integer.
Definition: network_string.hpp:324
char * getData()
Returns a byte pointer to the content of the network string.
Definition: network_string.hpp:166
T get() const
Template to get n bytes from a buffer into a single data type.
Definition: network_string.hpp:99
int16_t getInt16() const
Returns an unsigned 16 bit integer.
Definition: network_string.hpp:343
uint16_t getUInt16() const
Returns an unsigned 16 bit integer.
Definition: network_string.hpp:340
uint32_t getTime() const
Returns a unsigned 32 bit integer.
Definition: network_string.hpp:337
std::string getLogMessage(const std::string &indent="") const
Returns a string representing this message suitable to be printed to stdout or via the Log mechanism.
Definition: network_string.cpp:202
BareNetworkString & addString(const std::string &value)
Adds a std::string.
Definition: network_string.hpp:89
int8_t getInt8() const
Returns an unsigned 8-bit integer.
Definition: network_string.hpp:352
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
static void unitTesting()
Unit testing function.
Definition: network_string.cpp:30
NetworkString(const uint8_t *data, int len)
Constructor for a received message.
Definition: network_string.hpp:439
NetworkString(ProtocolType type, int capacity=16)
Constructor for a message to be sent.
Definition: network_string.hpp:429
void clear()
Empties the string, but does not reset the pre-allocated size.
Definition: network_string.hpp:447
void setSynchronous(bool b)
Sets if this message is to be sent synchronous or asynchronous.
Definition: network_string.hpp:461
ProtocolType getProtocolType() const
Returns the protocol type of this message.
Definition: network_string.hpp:454
bool isSynchronous() const
Returns if this message is synchronous or not.
Definition: network_string.hpp:470
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
Generic protocols declarations.
ProtocolType
The types that protocols can have.
Definition: protocol.hpp:43
@ PROTOCOL_SYNCHRONOUS
Flag, indicates synchronous delivery.
Definition: protocol.hpp:51
Declares the general types that are used by the network.