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 136 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 125 of file Systems.cpp.

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

◆ 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 139 of file Systems.h.

140  {
141  for(auto iter = index.begin(); iter != index.end(); ++iter) {
142 
143  auto& node = *iter->second;
144 
145  if(!node.Marked)
146  continue;
147 
148  HandleNode(iter->first, node, world);
149 
150  node.Marked = false;
151  }
152  }
DLLEXPORT void HandleNode(ObjectID id, Sendable &obj, GameWorld &world)
Definition: Systems.cpp:125

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