7 #ifndef CAFU_NETWORK_STATE_HPP_INCLUDED
8 #define CAFU_NETWORK_STATE_HPP_INCLUDED
10 #include "Templates/Array.hpp"
16 #define WIN32_LEAN_AND_MEAN
21 #include <netinet/in.h>
24 #if defined(_WIN32) && _MSC_VER<1600
35 uint64_t htonll(uint64_t value);
36 uint64_t ntohll(uint64_t value);
87 : m_Data(State.m_Data)
111 m_Data.PushBackEmpty(2);
112 *(
unsigned short*)&m_Data[m_Data.
Size()-2]=htons(w);
119 m_Data.PushBackEmpty(4);
120 *(int32_t*)&m_Data[m_Data.
Size()-4]=htonl(i);
127 m_Data.PushBackEmpty(4);
128 *(uint32_t*)&m_Data[m_Data.
Size()-4]=htonl(ui);
135 assert(ntohll(htonll(ui)) == ui);
136 assert(htonll(ntohll(ui)) == ui);
138 m_Data.PushBackEmpty(8);
139 *(uint64_t*)&m_Data[m_Data.
Size()-8]=htonll(ui);
146 assert(
sizeof(f) ==
sizeof(uint32_t));
147 *
this << *(uint32_t*)&f;
154 assert(
sizeof(d) ==
sizeof(uint64_t));
155 *
this << *(uint64_t*)&d;
162 m_Data.PushBack(b ? 1 : 0);
171 const unsigned long Start =m_Data.
Size();
172 const unsigned long Length=(
unsigned long)strlen(s)+1;
174 m_Data.PushBackEmpty(Length);
175 memcpy(&m_Data[Start], s, Length);
182 *
this << str.c_str();
187 template <
class T>
OutStreamT& operator << (const ArrayT<T>& A)
189 *
this << uint32_t(A.Size());
191 for (
unsigned long i=0; i<A.Size(); i++)
213 : m_Data(State.m_Data),
230 if (m_ReadPos+1 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
232 b = m_Data[m_ReadPos];
240 if (m_ReadPos+1 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
242 b = m_Data[m_ReadPos];
250 if (m_ReadPos+2 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
252 w = ntohs(*(
unsigned short*)&m_Data[m_ReadPos]);
260 if (m_ReadPos+4 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
262 i = ntohl(*(int32_t*)&m_Data[m_ReadPos]);
270 if (m_ReadPos+4 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
272 ui = ntohl(*(uint32_t*)&m_Data[m_ReadPos]);
280 if (m_ReadPos+8 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
282 ui = ntohll(*(uint64_t*)&m_Data[m_ReadPos]);
290 assert(
sizeof(f) ==
sizeof(uint32_t));
301 assert(
sizeof(d) ==
sizeof(uint64_t));
312 if (m_ReadPos+1 > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
314 b = (m_Data[m_ReadPos] != 0);
322 const unsigned long Start=m_ReadPos;
324 while (m_ReadPos < m_Data.
Size() && m_Data[m_ReadPos] != 0)
330 if (m_ReadPos > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
332 memcpy(s, &m_Data[Start], m_ReadPos-Start);
339 const unsigned long Start=m_ReadPos;
341 while (m_ReadPos < m_Data.
Size() && m_Data[m_ReadPos] != 0)
347 if (m_ReadPos > m_Data.
Size()) { m_ReadOfl=
true;
return *
this; }
349 s=std::string((
char*)&m_Data[Start], (m_ReadPos-1)-Start);
360 A.PushBackEmptyExact(Size);
362 for (uint32_t i=0; i<Size; i++)
373 unsigned long m_ReadPos;
ArrayT< uint8_t > GetDeltaMessage(const StateT &Other, bool Compress=true) const
Creates a delta message from this state.
Definition: State.cpp:146
OutStreamT(StateT &State, bool Overwrite=true)
Creates a stream for writing data into the given state.
Definition: State.hpp:86
static bool IsDeltaMessageEmpty(const ArrayT< uint8_t > &DeltaMessage)
Returns whether a delta message created by GetDeltaMessage() is empty (no change to the state)...
Definition: State.cpp:211
InStreamT & operator>>(char &b)
Reads a data item of the given type from the stream.
Definition: State.hpp:228
unsigned long Size() const
Get size of array.
Definition: Array.hpp:138
This class is used for reading data from a StateT instance (deserialization).
Definition: State.hpp:207
OutStreamT & operator<<(char b)
Writes a data item of the given type into the stream.
Definition: State.hpp:95
void Restart()
Re-initializes reading from the stream.
Definition: State.hpp:220
InStreamT(const StateT &State)
Creates a stream for reading data from the given state.
Definition: State.hpp:212
This class holds the serialized state of another object (typically a game entity).
Definition: State.hpp:46
This class is used for writing data into a StateT instance (serialization).
Definition: State.hpp:81
void Overwrite()
Clear array (but reuse memory)
Definition: Array.hpp:154
StateT()
Constructor for creating an empty StateT instance.
Definition: State.hpp:53