Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SendableSystem Class Reference

Sends updated entities from server to clients. More...

#include <Systems.h>

Public Member Functions

void Run (GameWorld &world, std::unordered_map< ObjectID, Sendable * > &index)
 

Protected Member Functions

DLLEXPORT void HandleNode (ObjectID id, Sendable &obj, GameWorld &world)
 

Detailed Description

Sends updated entities from server to clients.

Todo:
Change this to take distance into account don't send as many updates to clients far away

Definition at line 142 of file Systems.h.

Member Function Documentation

◆ HandleNode()

DLLEXPORT void SendableSystem::HandleNode ( ObjectID  id,
Sendable obj,
GameWorld world 
)
protected
Todo:
Something should be done with the required state allocation in this method

Definition at line 126 of file Systems.cpp.

127 {
128  const bool isServer = world.GetNetworkSettings().IsAuthoritative;
129 
130  const auto& players = world.GetConnectedPlayers();
131  const auto& serverConnection = world.GetServerForLocalControl();
132 
133  // Create current state here as one or more connections should require it //
134  // TODO: this could be removed entirely if we don't allow delta compression within a single
135  // component. Other possible approach is to recycle old captured state objects
136  auto curState = std::make_shared<EntityState>();
137 
138  world.CaptureEntityState(id, *curState);
139 
140  if(!curState) {
141 
142  LOG_ERROR(
143  "SendableSystem: created invalid state for entity, id: " + Convert::ToString(id));
144  return;
145  }
146 
147  // Detect successful packets and closed connections
148  for(auto iter = obj.UpdateReceivers.begin(); iter != obj.UpdateReceivers.end();) {
149  // This is closed if the connection is invalid
150  if(!iter->CorrespondingConnection->IsValidForSend()) {
151  iter = obj.UpdateReceivers.erase(iter);
152  continue;
153  }
154 
155  bool exists = false;
156 
157  if(isServer) {
158  // Or if the player has been removed from the world
159 
160  for(const auto& player : players) {
161 
162  if(iter->CorrespondingConnection == player->GetConnection()) {
163  exists = true;
164  break;
165  }
166  }
167  } else {
168 
169  if(iter->CorrespondingConnection == serverConnection)
170  exists = true;
171  }
172 
173  if(!exists) {
174  iter = obj.UpdateReceivers.erase(iter);
175  continue;
176  }
177 
178  // Check the most recent received state
179  iter->CheckReceivedPackets();
180 
181  // Still valid, move to next
182  ++iter;
183  }
184 
185  // Handle sending updates
186  if(isServer) {
187  for(const auto& player : players) {
188 
189  const auto& connection = player->GetConnection();
190 
191  SendableHandleHelper(id, obj, world, connection, curState, isServer);
192  }
193  } else {
194 
195  if(serverConnection && serverConnection->IsValidForSend()) {
196 
197  SendableHandleHelper(id, obj, world, serverConnection, curState, isServer);
198  }
199  }
200 }
std::vector< ActiveConnection > UpdateReceivers
Clients we have already sent a state to.
Definition: Components.h:162
#define LOG_ERROR(x)
Definition: Define.h:94
DLLEXPORT const auto & GetNetworkSettings() const
Definition: GameWorld.h:340
DLLEXPORT const auto & GetServerForLocalControl() const
Definition: GameWorld.h:414
static std::string ToString(const T &val)
Definition: Convert.h:72
virtual DLLEXPORT void CaptureEntityState(ObjectID id, EntityState &curstate) const
Captures the current state of an entity.
Definition: GameWorld.cpp:665
const auto & GetConnectedPlayers() const
This is used by Sendable system to loop all players.
Definition: GameWorld.h:374
void SendableHandleHelper(ObjectID id, Sendable &obj, GameWorld &world, const std::shared_ptr< Connection > &connection, const std::shared_ptr< EntityState > &curstate, bool server)
Helper for SendableSystem::HandleNode to not have as much code duplication for client and server code...
Definition: Systems.cpp:43

◆ Run()

void Leviathan::SendableSystem::Run ( GameWorld world,
std::unordered_map< ObjectID, Sendable * > &  index 
)
inline
Precondition
Final states for entities have been created for current tick

Definition at line 145 of file Systems.h.

146  {
147  for(auto iter = index.begin(); iter != index.end(); ++iter) {
148 
149  auto& node = *iter->second;
150 
151  if(!node.Marked)
152  continue;
153 
154  HandleNode(iter->first, node, world);
155 
156  node.Marked = false;
157  }
158  }
DLLEXPORT void HandleNode(ObjectID id, Sendable &obj, GameWorld &world)
Definition: Systems.cpp:126

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