Leviathan  0.8.0.0
Leviathan game engine
Leviathan::EntitySerializer Class Reference

Base class for all entity serializer classes. More...

#include <EntitySerializer.h>

Public Member Functions

 EntitySerializer ()
 Creates a serializer which is guaranteed to be able to serialize the type. More...
 
virtual ~EntitySerializer ()
 
virtual DLLEXPORT bool CreatePacketForConnection (GameWorld *world, Lock &worldlock, ObjectID id, Sendable &sendable, sf::Packet &packet, Connection &connection)
 Serializes an entity entirely into a packet. More...
 
virtual DLLEXPORT bool DeserializeWholeEntityFromPacket (GameWorld *world, Lock &worldlock, ObjectID id, sf::Packet &packet)
 Deserializes a whole object from a packet if the Type in the packet is the same as Type of this. More...
 
virtual DLLEXPORT bool ApplyUpdateFromPacket (GameWorld *world, Lock &worldlock, ObjectID targetobject, int ticknumber, int referencetick, sf::Packet &packet)
 Deserializes and applies an update from a packet. More...
 

Protected Member Functions

DLLEXPORT bool VerifyAndFillReceivedState (Received *received, int ticknumber, int referencetick, std::shared_ptr< ComponentState > receivedstate)
 
 EntitySerializer (const EntitySerializer &other)=delete
 
EntitySerializeroperator= (const EntitySerializer &other)=delete
 

Detailed Description

Base class for all entity serializer classes.

Note
All possible types should be defined in this file here

Definition at line 15 of file EntitySerializer.h.

Constructor & Destructor Documentation

◆ EntitySerializer() [1/2]

Leviathan::EntitySerializer::EntitySerializer ( )
inline

Creates a serializer which is guaranteed to be able to serialize the type.

Definition at line 19 of file EntitySerializer.h.

19  {
20  }

◆ ~EntitySerializer()

virtual Leviathan::EntitySerializer::~EntitySerializer ( )
inlinevirtual

Definition at line 22 of file EntitySerializer.h.

22 { }

◆ EntitySerializer() [2/2]

Leviathan::EntitySerializer::EntitySerializer ( const EntitySerializer other)
protecteddelete

Member Function Documentation

◆ ApplyUpdateFromPacket()

DLLEXPORT bool EntitySerializer::ApplyUpdateFromPacket ( GameWorld world,
Lock worldlock,
ObjectID  targetobject,
int  ticknumber,
int  referencetick,
sf::Packet &  packet 
)
virtual

Deserializes and applies an update from a packet.

Note
The targetobject is found by the caller based on the ID that it inserts before CreatePacketForConnection
Returns
True when the type of packet is correct even if the data is invalid

Definition at line 113 of file EntitySerializer.cpp.

116 {
117 
118  Received* received;
119 
120  try{
121  received = &world->GetComponent<Received>(targetobject);
122  } catch(...){
123 
124  // targetobject is invalid type for us //
125  Logger::Get()->Error("EntitySerializer: target object has no "
126  "Received component");
127  return false;
128  }
129 
130  // TODO: load sub update states per component
131 
132 
133  // if(!VerifyAndFillReceivedState(received, ticknumber, referencetick, state)){
134 
135  // // Should only get here if it isn't older than any //
136  // DEBUG_BREAK;
137  // return true;
138  // }
139 
140  // Interpolations can only happen if more than one state is received
141  if(received->ClientStateBuffer.size() > 1)
142  received->Marked = true;
143 
144  // Send an empty packet on next tick //
145  //world->MarkForNotifyReceivedStates();
146 
147  return true;
148 }
Entity is received from a server.
Definition: Components.h:161
boost::circular_buffer< StoredState > ClientStateBuffer
Client side buffer of past states.
Definition: Components.h:192
TComponent & GetComponent(ObjectID id)
Definition: GameWorld.h:199
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

◆ CreatePacketForConnection()

DLLEXPORT bool EntitySerializer::CreatePacketForConnection ( GameWorld world,
Lock worldlock,
ObjectID  id,
Sendable sendable,
sf::Packet &  packet,
Connection connection 
)
virtual

Serializes an entity entirely into a packet.

Note
This will also link the entity to the connection so that it will automatically send updates
Parameters
connectionptrPointer to the connection to be used (doesn't have to be verified to be valid)
Returns
Returns true when the type of object is correct. Which should be when IsObjectTypeCorrect returns true
Note
The caller will have added the ID to the packet so that needs to be skipped (or rather should be) But the Type variable should be included by this object (as an int32_t)

Definition at line 9 of file EntitySerializer.cpp.

12 {
13  DEBUG_BREAK;
14 
15  // TODO: send all the components
16 
17  return true;
18 }

◆ DeserializeWholeEntityFromPacket()

DLLEXPORT bool EntitySerializer::DeserializeWholeEntityFromPacket ( GameWorld world,
Lock worldlock,
ObjectID  id,
sf::Packet &  packet 
)
virtual

Deserializes a whole object from a packet if the Type in the packet is the same as Type of this.

Note
This should do the exact opposite of CreatePacketForConnection
Returns
True when the type of packet is correct even if the data is invalid
Todo:
Allow reporting invalid data
Parameters
serializetypeThe type that was included in the packet by a CreatePacketForConnection
worldThe world into which the object is created. Has to be locked before this call

Definition at line 20 of file EntitySerializer.cpp.

22 {
23  DEBUG_BREAK;
24 
25  // TODO: load all components
26 
27 
28  return true;
29 }

◆ operator=()

EntitySerializer& Leviathan::EntitySerializer::operator= ( const EntitySerializer other)
protecteddelete

◆ VerifyAndFillReceivedState()

DLLEXPORT bool EntitySerializer::VerifyAndFillReceivedState ( Received received,
int  ticknumber,
int  referencetick,
std::shared_ptr< ComponentState receivedstate 
)
protected

Definition at line 31 of file EntitySerializer.cpp.

33 {
34  if(!receivedstate){
35 
36  Logger::Get()->Error("EntitySerializer: invalid packet, failed to create "
37  "state object");
38 
39  return false;
40  }
41 
42  // Store for interpolation //
43  // Skip if not newer than any //
44  if(received->ClientStateBuffer.size() != 0){
45 
46  bool newer = false;
47  bool filled = false;
48 
49  for(auto& obj : received->ClientStateBuffer){
50 
51  // Fill data from the reference tick to make this update packet as complete as
52  // possible
53  if(obj.Tick == referencetick){
54 
55  // Add missing values //
56  DEBUG_BREAK;
57  //receivedstate->FillMissingData(*obj.DeltaData);
58 
59  filled = true;
60 
61  if(newer)
62  break;
63  }
64 
65  if(obj.Tick < ticknumber){
66 
67  newer = true;
68 
69  if(filled)
70  break;
71  }
72  }
73 
74  if(!newer)
75  return false;
76 
77  // If it isn't filled that tells that our buffer is too small //
78  // referencetick is invalid when it is -1 and is ignored in that case //
79  if(!filled && referencetick != -1){
80 
81  bool olderexist = false;
82 
83  // TODO: make sure that this doesn't mess with interpolation too badly
84  // under reasonable network stress, also this probably should never be missing
85  // under normal conditions
86 
87  // Or that we have missed a single packet //
88  for(auto& obj : received->ClientStateBuffer){
89 
90  if(obj.Tick < referencetick){
91 
92  olderexist = true;
93  break;
94  }
95  }
96 
97  if(!olderexist){
98 
99  Logger::Get()->Warning("Entity old state "+Convert::ToString(referencetick)+
100  " is no longer in memory, too small buffer");
101  }
102  }
103 
104  } else {
105 
106  // No stored states, must be newer //
107  // Also no need to fill missing data as only the updated values should be in the packet //
108  }
109 
110  return true;
111 }
boost::circular_buffer< StoredState > ClientStateBuffer
Client side buffer of past states.
Definition: Components.h:192
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

The documentation for this class was generated from the following files: