Connection to a remote network host. More...

#include <Urho3D/Network/Connection.h>

Inheritance diagram for Urho3D::Connection:
[legend]
Collaboration diagram for Urho3D::Connection:
[legend]

Public Member Functions

 Connection (Context *context, bool isClient, const SLNet::AddressOrGUID &address, SLNet::RakPeerInterface *peer)
 Construct with context, RakNet connection address and Raknet peer pointer.
 
 ~Connection () override
 Destruct.
 
PacketType GetPacketType (bool reliable, bool inOrder)
 Get packet type based on the message parameters.
 
void SendMessage (int msgID, bool reliable, bool inOrder, const VectorBuffer &msg, unsigned contentID=0)
 Send a message.
 
void SendMessage (int msgID, bool reliable, bool inOrder, const unsigned char *data, unsigned numBytes, unsigned contentID=0)
 Send a message.
 
void SendRemoteEvent (StringHash eventType, bool inOrder, const VariantMap &eventData=Variant::emptyVariantMap)
 Send a remote event.
 
void SendRemoteEvent (Node *node, StringHash eventType, bool inOrder, const VariantMap &eventData=Variant::emptyVariantMap)
 Send a remote event with the specified node as sender.
 
void SetScene (Scene *newScene)
 
void SetIdentity (const VariantMap &identity)
 Assign identity. Called by Network.
 
void SetControls (const Controls &newControls)
 Set new controls.
 
void SetPosition (const Vector3 &position)
 
void SetRotation (const Quaternion &rotation)
 
void SetConnectPending (bool connectPending)
 Set the connection pending status. Called by Network.
 
void SetLogStatistics (bool enable)
 
void Disconnect (int waitMSec=0)
 Disconnect. If wait time is non-zero, will block while waiting for disconnect to finish.
 
void SendServerUpdate ()
 Send scene update messages. Called by Network.
 
void SendClientUpdate ()
 Send latest controls from the client. Called by Network.
 
void SendRemoteEvents ()
 Send queued remote events. Called by Network.
 
void SendPackages ()
 Send package files to client. Called by network.
 
void SendBuffer (PacketType type)
 Send out buffered messages by their type.
 
void SendAllBuffers ()
 Send out all buffered messages.
 
void ProcessPendingLatestData ()
 Process pending latest data for nodes and components.
 
bool ProcessMessage (int msgID, MemoryBuffer &buffer)
 Process a message from the server or client. Called by Network.
 
void Ban ()
 Ban this connections IP address.
 
const SLNet::AddressOrGUID & GetAddressOrGUID () const
 Return the RakNet address/guid.
 
void SetAddressOrGUID (const SLNet::AddressOrGUID &addr)
 Set the the RakNet address/guid.
 
VariantMapGetIdentity ()
 Return client identity.
 
SceneGetScene () const
 
const ControlsGetControls () const
 Return the client controls of this connection.
 
unsigned char GetTimeStamp () const
 Return the controls timestamp, sent from client to server along each control update.
 
const Vector3GetPosition () const
 
const QuaternionGetRotation () const
 
bool IsClient () const
 
bool IsConnected () const
 
bool IsConnectPending () const
 
bool IsSceneLoaded () const
 
bool GetLogStatistics () const
 
String GetAddress () const
 
unsigned short GetPort () const
 
float GetRoundTripTime () const
 
unsigned GetLastHeardTime () const
 
float GetBytesInPerSec () const
 
float GetBytesOutPerSec () const
 
int GetPacketsInPerSec () const
 
int GetPacketsOutPerSec () const
 
String ToString () const
 Return an address:port string.
 
unsigned GetNumDownloads () const
 
const StringGetDownloadName () const
 
float GetDownloadProgress () const
 
void SendPackageToClient (PackageFile *package)
 Trigger client connection to download a package file from the server. Can be used to download additional resource packages when client is already joined in a scene. The package must have been added as a requirement to the scene the client is joined in, or else the eventual download will fail.
 
void ConfigureNetworkSimulator (int latencyMs, float packetLoss)
 Set network simulation parameters. Called by Network.
 
void SetPacketSizeLimit (int limit)
 Buffered packet size limit, when reached, packet is sent out immediately.
 
- Public Member Functions inherited from Urho3D::Object
 Object (Context *context)
 Construct.
 
 ~Object () override
 Destruct. Clean up self from event sender & receiver structures.
 
virtual StringHash GetType () const =0
 
virtual const StringGetTypeName () const =0
 
virtual const TypeInfoGetTypeInfo () const =0
 Return type info.
 
virtual void OnEvent (Object *sender, StringHash eventType, VariantMap &eventData)
 Handle event.
 
bool IsInstanceOf (StringHash type) const
 Check current instance is type of specified type.
 
bool IsInstanceOf (const TypeInfo *typeInfo) const
 Check current instance is type of specified type.
 
template<typename T >
bool IsInstanceOf () const
 Check current instance is type of specified class.
 
template<typename T >
T * Cast ()
 Cast the object to specified most derived class.
 
template<typename T >
const T * Cast () const
 Cast the object to specified most derived class.
 
void SubscribeToEvent (StringHash eventType, EventHandler *handler)
 Subscribe to an event that can be sent by any sender.
 
void SubscribeToEvent (Object *sender, StringHash eventType, EventHandler *handler)
 Subscribe to a specific sender's event.
 
void SubscribeToEvent (StringHash eventType, const std::function< void(StringHash, VariantMap &)> &function, void *userData=nullptr)
 Subscribe to an event that can be sent by any sender.
 
void SubscribeToEvent (Object *sender, StringHash eventType, const std::function< void(StringHash, VariantMap &)> &function, void *userData=nullptr)
 Subscribe to a specific sender's event.
 
void UnsubscribeFromEvent (StringHash eventType)
 Unsubscribe from an event.
 
void UnsubscribeFromEvent (Object *sender, StringHash eventType)
 Unsubscribe from a specific sender's event.
 
void UnsubscribeFromEvents (Object *sender)
 Unsubscribe from a specific sender's events.
 
void UnsubscribeFromAllEvents ()
 Unsubscribe from all events.
 
void UnsubscribeFromAllEventsExcept (const PODVector< StringHash > &exceptions, bool onlyUserData)
 Unsubscribe from all events except those listed, and optionally only those with userdata (script registered events).
 
void SendEvent (StringHash eventType)
 Send event to all subscribers.
 
void SendEvent (StringHash eventType, VariantMap &eventData)
 Send event with parameters to all subscribers.
 
VariantMapGetEventDataMap () const
 Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
 
template<typename... Args>
void SendEvent (StringHash eventType, Args... args)
 Send event with variadic parameter pairs to all subscribers. The parameter pairs is a list of paramID and paramValue separated by comma, one pair after another.
 
ContextGetContext () const
 Return execution context.
 
const VariantGetGlobalVar (StringHash key) const
 
const VariantMapGetGlobalVars () const
 
void SetGlobalVar (StringHash key, const Variant &value)
 
ObjectGetSubsystem (StringHash type) const
 Return subsystem by type.
 
ObjectGetEventSender () const
 Return active event sender. Null outside event handling.
 
EventHandlerGetEventHandler () const
 Return active event handler. Null outside event handling.
 
bool HasSubscribedToEvent (StringHash eventType) const
 Return whether has subscribed to an event without specific sender.
 
bool HasSubscribedToEvent (Object *sender, StringHash eventType) const
 Return whether has subscribed to a specific sender's event.
 
bool HasEventHandlers () const
 Return whether has subscribed to any event.
 
template<class T >
T * GetSubsystem () const
 Template version of returning a subsystem.
 
const StringGetCategory () const
 
void SetBlockEvents (bool block)
 Block object from sending and receiving events.
 
bool GetBlockEvents () const
 Return sending and receiving events blocking status.
 
- Public Member Functions inherited from Urho3D::RefCounted
 RefCounted ()
 Construct. Allocate the reference count structure and set an initial self weak reference.
 
virtual ~RefCounted ()
 Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
 
 RefCounted (const RefCounted &rhs)=delete
 Prevent copy construction.
 
RefCountedoperator= (const RefCounted &rhs)=delete
 Prevent assignment.
 
void AddRef ()
 Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
 
void ReleaseRef ()
 Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
 
int Refs () const
 
int WeakRefs () const
 
RefCountRefCountPtr ()
 Return pointer to the reference count structure.
 

Public Attributes

Controls controls_
 Current controls.
 
unsigned char timeStamp_
 Controls timestamp. Incremented after each sent update.
 
VariantMap identity_
 Identity map.
 

Private Member Functions

 URHO3D_OBJECT (Connection, Object)
 
void HandleAsyncLoadFinished (StringHash eventType, VariantMap &eventData)
 Handle scene loaded event.
 
void ProcessLoadScene (int msgID, MemoryBuffer &msg)
 Process a LoadScene message from the server. Called by Network.
 
void ProcessSceneChecksumError (int msgID, MemoryBuffer &msg)
 Process a SceneChecksumError message from the server. Called by Network.
 
void ProcessSceneUpdate (int msgID, MemoryBuffer &msg)
 Process a scene update message from the server. Called by Network. More...
 
void ProcessPackageDownload (int msgID, MemoryBuffer &msg)
 Process package download related messages. Called by Network.
 
void ProcessIdentity (int msgID, MemoryBuffer &msg)
 Process an Identity message from the client. Called by Network.
 
void ProcessControls (int msgID, MemoryBuffer &msg)
 Process a Controls message from the client. Called by Network.
 
void ProcessSceneLoaded (int msgID, MemoryBuffer &msg)
 Process a SceneLoaded message from the client. Called by Network.
 
void ProcessRemoteEvent (int msgID, MemoryBuffer &msg)
 Process a remote event message from the client or server. Called by Network.
 
void ProcessNode (unsigned nodeID)
 Process a node for sending a network update. Recurses to process depended on node(s) first.
 
void ProcessNewNode (Node *node)
 Process a node that the client has not yet received.
 
void ProcessExistingNode (Node *node, NodeReplicationState &nodeState)
 Process a node that the client has already received. More...
 
void ProcessPackageInfo (int msgID, MemoryBuffer &msg)
 Process a SyncPackagesInfo message from server.
 
void ProcessUnknownMessage (int msgID, MemoryBuffer &msg)
 Process unknown message. All unknown messages are forwarded as an events.
 
bool RequestNeededPackages (unsigned numPackages, MemoryBuffer &msg)
 Check a package list received from server and initiate package downloads as necessary. Return true on success, or false if failed to initialze downloads (cache dir not set).
 
void RequestPackage (const String &name, unsigned fileSize, unsigned checksum)
 Initiate a package download.
 
void SendPackageError (const String &name)
 Send an error reply for a package download.
 
void OnSceneLoadFailed ()
 Handle scene load failure on the server or client.
 
void OnPackageDownloadFailed (const String &name)
 Handle a package download failure on the client.
 
void OnPackagesReady ()
 Handle all packages loaded successfully. Also called directly on MSG_LOADSCENE if there are none.
 

Private Attributes

WeakPtr< Scenescene_
 Scene.
 
SceneReplicationState sceneState_
 Network replication state of the scene.
 
HashMap< StringHash, PackageDownloaddownloads_
 Waiting or ongoing package file receive transfers.
 
HashMap< StringHash, PackageUploaduploads_
 Ongoing package send transfers.
 
HashMap< unsigned, PODVector< unsigned char > > nodeLatestData_
 Pending latest data for not yet received nodes.
 
HashMap< unsigned, PODVector< unsigned char > > componentLatestData_
 Pending latest data for not yet received components.
 
HashSet< unsigned > nodesToProcess_
 Node ID's to process during a replication update.
 
VectorBuffer msg_
 Reusable message buffer.
 
Vector< RemoteEventremoteEvents_
 Queued remote events.
 
String sceneFileName_
 Scene file to load once all packages (if any) have been downloaded.
 
Timer statsTimer_
 Statistics timer.
 
unsigned short port_
 Remote endpoint port.
 
Vector3 position_
 Observer position for interest management.
 
Quaternion rotation_
 Observer rotation for interest management.
 
ObserverPositionSendMode sendMode_
 Send mode for the observer position & rotation.
 
bool isClient_
 Client connection flag.
 
bool connectPending_
 Connection pending flag.
 
bool sceneLoaded_
 Scene loaded flag.
 
bool logStatistics_
 Show statistics flag.
 
SLNet::AddressOrGUID * address_
 Address of this connection.
 
SLNet::RakPeerInterface * peer_
 Raknet peer object.
 
IntVector2 tempPacketCounter_
 Temporary variable to hold packet count in the next second, x - packets in, y - packets out.
 
IntVector2 packetCounter_
 Packet count in the last second, x - packets in, y - packets out.
 
Timer packetCounterTimer_
 Packet count timer which resets every 1s.
 
Timer lastHeardTimer_
 Last heard timer, resets when new packet is incoming.
 
HashMap< int, VectorBufferoutgoingBuffer_
 Outgoing packet buffer which can contain multiple messages.
 
int packedMessageLimit_
 Outgoing packet size limit.
 

Additional Inherited Members

- Static Public Member Functions inherited from Urho3D::Object
static const TypeInfoGetTypeInfoStatic ()
 Return type info static.
 
- Protected Attributes inherited from Urho3D::Object
Contextcontext_
 Execution context.
 

Detailed Description

Connection to a remote network host.

Member Function Documentation

◆ GetAddress()

String Urho3D::Connection::GetAddress ( ) const

Return remote address.

Here is the caller graph for this function:

◆ GetBytesInPerSec()

float Urho3D::Connection::GetBytesInPerSec ( ) const

Return bytes received per second.

Here is the caller graph for this function:

◆ GetBytesOutPerSec()

float Urho3D::Connection::GetBytesOutPerSec ( ) const

Return bytes sent per second.

Here is the caller graph for this function:

◆ GetDownloadName()

const String & Urho3D::Connection::GetDownloadName ( ) const

Return name of current package download, or empty if no downloads.

◆ GetDownloadProgress()

float Urho3D::Connection::GetDownloadProgress ( ) const

Return progress of current package download, or 1.0 if no downloads.

◆ GetLastHeardTime()

unsigned Urho3D::Connection::GetLastHeardTime ( ) const

Return the time since last received data from the remote host in milliseconds.

Here is the caller graph for this function:

◆ GetLogStatistics()

bool Urho3D::Connection::GetLogStatistics ( ) const
inline

Return whether to log data in/out statistics.

◆ GetNumDownloads()

unsigned Urho3D::Connection::GetNumDownloads ( ) const

Return number of package downloads remaining.

◆ GetPacketsInPerSec()

int Urho3D::Connection::GetPacketsInPerSec ( ) const

Return packets received per second.

Here is the caller graph for this function:

◆ GetPacketsOutPerSec()

int Urho3D::Connection::GetPacketsOutPerSec ( ) const

Return packets sent per second.

Here is the caller graph for this function:

◆ GetPort()

unsigned short Urho3D::Connection::GetPort ( ) const
inline

Return remote port.

Here is the caller graph for this function:

◆ GetPosition()

const Vector3& Urho3D::Connection::GetPosition ( ) const
inline

Return the observer position sent by the client for interest management.

◆ GetRotation()

const Quaternion& Urho3D::Connection::GetRotation ( ) const
inline

Return the observer rotation sent by the client for interest management.

◆ GetRoundTripTime()

float Urho3D::Connection::GetRoundTripTime ( ) const

Return the connection's round trip time in milliseconds.

Here is the caller graph for this function:

◆ GetScene()

Scene * Urho3D::Connection::GetScene ( ) const

Return the scene used by this connection.

◆ IsClient()

bool Urho3D::Connection::IsClient ( ) const
inline

Return whether is a client connection.

Here is the caller graph for this function:

◆ IsConnected()

bool Urho3D::Connection::IsConnected ( ) const

Return whether is fully connected.

◆ IsConnectPending()

bool Urho3D::Connection::IsConnectPending ( ) const
inline

Return whether connection is pending.

◆ IsSceneLoaded()

bool Urho3D::Connection::IsSceneLoaded ( ) const
inline

Return whether the scene is loaded and ready to receive server updates.

◆ ProcessExistingNode()

void Urho3D::Connection::ProcessExistingNode ( Node node,
NodeReplicationState nodeState 
)
private

Process a node that the client has already received.

Todo:
Searching for the component is a potential CPU hotspot. It should be cached
Here is the caller graph for this function:

◆ ProcessSceneUpdate()

void Urho3D::Connection::ProcessSceneUpdate ( int  msgID,
MemoryBuffer msg 
)
private

Process a scene update message from the server. Called by Network.

Todo:
On mobile devices processing this message may potentially cause a crash if it attempts to load new GPU resources while the application is minimized
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetLogStatistics()

void Urho3D::Connection::SetLogStatistics ( bool  enable)

Set whether to log data in/out statistics.

◆ SetPosition()

void Urho3D::Connection::SetPosition ( const Vector3 position)

Set the observer position for interest management, to be sent to the server.

◆ SetRotation()

void Urho3D::Connection::SetRotation ( const Quaternion rotation)

Set the observer rotation for interest management, to be sent to the server. Note: not used by the NetworkPriority component.

◆ SetScene()

void Urho3D::Connection::SetScene ( Scene newScene)

Assign scene. On the server, this will cause the client to load it.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files:
  • Source/Urho3D/Network/Connection.h
  • Source/Urho3D/Network/Connection.cpp