| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include "Controls.h"
- #include "HashSet.h"
- #include "Object.h"
- #include "ReplicationState.h"
- #include "VectorBuffer.h"
- #include <kNetFwd.h>
- #include <kNet/SharedPtr.h>
- #ifdef SendMessage
- #undef SendMessage
- #endif
- class MemoryBuffer;
- class Node;
- class Scene;
- class Serializable;
- /// Queued remote event
- struct RemoteEvent
- {
- /// Receiver node ID (0 if not a remote node event)
- unsigned receiverID_;
- /// Event type
- StringHash eventType_;
- /// Event data
- VariantMap eventData_;
- /// In order -flag
- bool inOrder_;
- };
- /// Connection in a networked scene
- class Connection : public Object
- {
- OBJECT(Connection);
-
- public:
- /// Construct with context and kNet message connection pointers
- Connection(Context* context, bool isClient, kNet::SharedPtr<kNet::MessageConnection> connection);
- /// Destruct
- ~Connection();
-
- /// Send a message
- void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
- /// Send a message
- void SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
- /// Send a message with content ID
- void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
- /// Send a message with content ID
- void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg);
- /// Send a remote event
- void SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
- /// Send a remote node event
- void SendRemoteEvent(Node* receiver, StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
- /// Assign scene. On the server, this will cause the client to load it
- void SetScene(Scene* newScene);
- /// Assign identity. Called by Network
- void SetIdentity(const VariantMap& identity);
- /// Set new controls. Moves the current controls as previous
- void SetControls(const Controls& newControls);
- /// Set the connection pending status. Called by Network
- void SetConnectPending(bool connectPending);
- /// Disconnect. If wait time is non-zero, will block while waiting for disconnect to finish
- void Disconnect(int waitMSec = 0);
- /// Send scene update messages. Called by Network
- void SendServerUpdate();
- /// Send latest controls from the client. Called by Network
- void SendClientUpdate();
- /// Send queued remote events. Called by Network
- void SendQueuedRemoteEvents();
- /// Process pending latest data for nodes and components
- void ProcessPendingLatestData();
- /// Process a LoadScene message from the server. Called by Network
- void ProcessLoadScene(int msgID, MemoryBuffer& msg);
- /// Process a SceneChecksumError message from the server. Called by Network
- void ProcessSceneChecksumError(int msgID, MemoryBuffer& msg);
- /// Process a scene update message from the server. Called by Network
- void ProcessSceneUpdate(int msgID, MemoryBuffer& msg);
- /// Process an Identity message from the client. Called by Network
- void ProcessIdentity(int msgID, MemoryBuffer& msg);
- /// Process a Controls message from the client. Called by Network
- void ProcessControls(int msgID, MemoryBuffer& msg);
- /// Process a SceneLoaded message from the client. Called by Network
- void ProcessSceneLoaded(int msgID, MemoryBuffer& msg);
- /// Process a remote event message from the client or server. Called by Network
- void ProcessRemoteEvent(int msgID, MemoryBuffer& msg);
-
- /// Return the kNet message connection
- kNet::MessageConnection* GetMessageConnection() const;
- /// Return client identity
- const VariantMap& GetIdentity() const { return identity_; }
- /// Return the scene used by this connection
- Scene* GetScene() const;
- /// Return the client controls of this connection
- const Controls& GetControls() const { return controls_; }
- /// Return the previous client controls of this connection
- const Controls& GetPreviousControls() const { return previousControls_; }
- /// Return whether is a client connection
- bool IsClient() const { return isClient_; }
- /// Return whether is fully connected
- bool IsConnected() const;
- /// Return whether connection is pending
- bool IsConnectPending() const { return connectPending_; }
- /// Return whether the scene is loaded and ready to receive updates from network
- bool IsSceneLoaded() const { return sceneLoaded_; }
- /// Return remote address
- String GetAddress() const;
- /// Return remote port
- unsigned short GetPort() const;
- /// Return an address:port string
- String ToString() const;
-
- private:
- /// Handle scene loaded event
- void HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData);
- /// Process a node for sending a network update. Recurses to process depended on node(s) first
- void ProcessNode(Node* node);
- /// Process a node that the client had not yet received
- void ProcessNewNode(Node* node);
- /// Process a node that the client has already received
- void ProcessExistingNode(Node* node);
-
- /// kNet message connection
- kNet::SharedPtr<kNet::MessageConnection> connection_;
- /// Identity map
- VariantMap identity_;
- /// Scene
- WeakPtr<Scene> scene_;
- /// Scene replication state (as last sent to the client)
- Map<unsigned, NodeReplicationState> sceneState_;
- /// Pending latest data for not yet received nodes
- Map<unsigned, PODVector<unsigned char> > nodeLatestData_;
- /// Pending latest data for not yet received components
- Map<unsigned, PODVector<unsigned char> > componentLatestData_;
- /// Queued remote events
- Vector<RemoteEvent> remoteEvents_;
- /// Delta update bits
- PODVector<unsigned char> deltaUpdateBits_;
- /// Node's changed user variables
- HashSet<ShortStringHash> changedVars_;
- /// Already processed nodes during a replication update
- HashSet<Node*> processedNodes_;
- /// Preallocated variants of correct type per networked object class
- Map<ShortStringHash, Vector<Variant> > classCurrentState_;
- /// Reused message buffer
- VectorBuffer msg_;
- /// Current controls
- Controls controls_;
- /// Previous controls
- Controls previousControls_;
- /// Update frame number
- unsigned frameNumber_;
- /// Client flag
- bool isClient_;
- /// Connection pending flag
- bool connectPending_;
- /// Scene loaded flag
- bool sceneLoaded_;
- };
|