Exemple #1
0
        /// <summary>
        /// Deserialize a networkviewid from the networkmessage
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        internal static NetworkViewId Deserialize(NetIncomingMessage message)
        {
            var id = new NetworkViewId();

            id.OnDeserialize(message);
            return(id);
        }
Exemple #2
0
        /// <summary>
        /// find a networkview based on a networkviewid that was serialized into an rpc
        /// </summary>
        /// <param name="message">uses deserialize, so the read location does advance</param>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool Find(ref NetIncomingMessage message, out NetworkView view)
        {
            var id = new NetworkViewId();
            id.OnDeserialize(message);

            return Find(id, out view);
        }
        /// <summary>
        /// find a networkview based on a networkviewid that was serialized into an rpc
        /// </summary>
        /// <param name="message">uses deserialize, so the read location does advance</param>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool Find(ref NetIncomingMessage message, out NetworkView view)
        {
            var id = new NetworkViewId();

            id.OnDeserialize(message);

            return(Find(id, out view));
        }
Exemple #4
0
 /// <summary>
 /// find a network view based on the given NetworkViewId
 /// </summary>
 /// <param name="viewId"></param>
 /// <returns></returns>
 public static NetworkView Find(NetworkViewId viewId)
 {
     if (!UnityEngineHook.ValidInstance)
         return null;
     
     NetworkView view;
     UnityEngineHook.Instance.Manager.TryGetView(viewId, out view);
     return view;
 }
Exemple #5
0
        private void FinishedInstantiate(NetworkViewId netId)
        {
            var msg = Peer.CreateMessage(3);

            msg.Write(RPCUtils.FinishedInstantiate);
            netId.OnSerialize(msg);

            Peer.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, Channels.STATIC_UTILS);
            Debug.Log(this, "Finished instantiation, sending ack");
        }
Exemple #6
0
 /// <summary>
 /// find a networkview based on a networkviewid that was serialized into an rpc
 /// </summary>
 /// <param name="message">uses deserialize, so the read location does advance</param>
 /// <param name="view"></param>
 /// <returns></returns>
 public static bool Find(ref NetIncomingMessage message, out NetworkView view)
 {
     var viewId = new NetworkViewId();
     viewId.OnDeserialize(message);
     if (UnityEngineHook.ValidInstance)
         return UnityEngineHook.Instance.Manager.TryGetView(viewId, out view);
     
     view = null;
     return false;
 }
        internal NetworkView Create(NetworkViewId viewId, ushort ownerId)
        {
            viewId.IsMine = Net.PlayerId == ownerId;

            var newView = GetNetworkView();

            newView.ViewID  = viewId;
            newView.OwnerId = ownerId;
            RegisterView(newView, viewId);
            return(newView);
        }
 public void CreateTest()
 {
     var viewId = new NetworkViewId{guid =15};
     ushort ownerId = 14;
     
     _net.TestablePlayerID = 12;
     NetworkView actual = _target.Create(viewId, ownerId);
     
     
     //test regular creation with an empty pool
     Assert.AreEqual(viewId, actual.ViewID);
     Assert.AreEqual(ownerId, actual.OwnerId);
     Assert.IsFalse(actual.IsMine);
 }
 private void RegisterView(NetworkView view, NetworkViewId viewId)
 {
     _allViews.Add(viewId.guid, view);
 }
Exemple #10
0
 /// <summary>
 /// find a network view based on the given NetworkViewId
 /// </summary>
 /// <param name="viewId"></param>
 /// <returns></returns>
 public NetworkView Find(NetworkViewId viewId)
 {
     return _allViews[viewId.guid];
 }
Exemple #11
0
 private void Consume(NetIncomingMessage msg)
 {
     try
     {
         //faster than switch, as this is in most to least common order
         if (msg.SequenceChannel == Channels.UNRELIABLE_STREAM)
         {
             if (msg.DeliveryMethod == NetDeliveryMethod.ReliableUnordered)
             {
                 HandleStaticRpc(msg);
             }
             else
             {
                 var         actorId = NetworkViewId.Deserialize(msg);
                 NetworkView find;
                 if (NetworkViewManager.Find(actorId, out find))
                 {
                     find.DoOnDeserializeStream(msg);
                 }
             }
         }
         else if (msg.SequenceChannel == Channels.RELIABLE_STREAM)
         {
             var         actorId = NetworkViewId.Deserialize(msg);
             NetworkView find;
             if (NetworkViewManager.Find(actorId, out find))
             {
                 find.DoOnDeserializeStream(msg);
             }
         }
         else if (msg.SequenceChannel >= Channels.BEGIN_RPCMODES && msg.SequenceChannel <= Channels.OWNER_RPC)
         {
             //rpc...
             var         viewId = NetworkViewId.Deserialize(msg);
             var         rpcId  = msg.ReadByte();
             NetworkView find;
             if (NetworkViewManager.Find(viewId, out find))
             {
                 find.CallRPC(rpcId, msg);
             }
             else
             {
                 Debug.LogWarning(this, "couldn't find view {0} to send rpc {1}", viewId, rpcId);
             }
         }
         else if (msg.SequenceChannel == Channels.SYNCHED_FIELD)
         {
             var         viewId  = NetworkViewId.Deserialize(msg);
             var         fieldId = msg.ReadByte();
             NetworkView find;
             if (NetworkViewManager.Find(viewId, out find))
             {
                 find.SetSynchronizedField(fieldId, msg);
             }
             else
             {
                 Debug.LogWarning(this, "couldn't find view " + viewId + " to set field " + fieldId);
             }
         }
         else if (msg.SequenceChannel == Channels.OBJECT_RPC)
         {
             var viewId = msg.ReadUInt16();
             var rpcId  = msg.ReadByte();
             NetworkedSceneObject.CallRPC(viewId, rpcId, msg);
         }
         else if (msg.SequenceChannel == Channels.STATIC_RPC)
         {
             HandleStaticRpc(msg);
         }
         else if (msg.SequenceChannel == Channels.STATIC_UTILS)
         {
             ProcessUtils(msg);
         }
         else
         {
             Debug.LogWarning(this, "{1} bytes received over unhandled channel {0}, delivery {2}", msg.SequenceChannel, msg.LengthBytes, msg.DeliveryMethod);
         }
     }
     catch (Exception er)
     {
         Debug.LogError(this, "[Net.Consume] {0}", er);
     }
 }
Exemple #12
0
        private void ProcessUtils(NetIncomingMessage msg)
        {
            var utilId = msg.ReadByte();

            if (utilId == RPCUtils.TimeUpdate)
            {
                throw new NotImplementedException("RPCUtils.TimeUpdate");
            }
            else if (utilId == RPCUtils.Instantiate)
            {
                //read the path...
                var resourcePath = msg.ReadString();
                var viewId       = NetworkViewId.Deserialize(msg);
                var ownerId      = msg.ReadUInt16();

                var position = new Vector3();
                position.OnDeserialize(msg);
                var rotation = new Quaternion();
                rotation.OnDeserialize(msg);

                var view = NetworkViewManager.Create(viewId, ownerId);

                try
                {
                    EngineHook.Instantiate(resourcePath, view, position, rotation);
                }
                catch (Exception e)
                {
                    Debug.LogError(this, "[EngineHook.Instantiate] {0}", e);
                }

                Debug.Log(this, "Created {0}", view);

                view.DoOnFinishedCreation();

                FinishedInstantiate(viewId);
            }
            else if (utilId == RPCUtils.Remove)
            {
                var viewId = new NetworkViewId();
                viewId.OnDeserialize(msg);
                byte reasonCode;
                if (!msg.ReadByte(out reasonCode))
                {
                    reasonCode = 0;
                }

                NetworkView find;
                if (NetworkViewManager.Find(viewId, out find))
                {
                    find.DoOnRemove(reasonCode);
                }
                else
                {
                    Debug.LogError(this, "Attempted to remove {0}, but it could not be found", viewId);
                }
            }
            else if (utilId == RPCUtils.ChangeRoom)
            {
                var newRoom = msg.ReadString();

                Debug.LogInfo(this, "Changing to room {0}", newRoom);

                if (OnRoomChange != null)
                {
                    try
                    {
                        OnRoomChange(newRoom);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(this, "[OnChangeRoom] {0}", e);
                    }
                }

                if (Configuration.DeleteNetworkInstantiatesOnRoomChange)
                {
                    NetworkViewManager.DestroyAllViews();
                }
            }
            else if (utilId == RPCUtils.AddView)
            {
                var    addToId = NetworkViewId.Deserialize(msg);
                var    idToAdd = NetworkViewId.Deserialize(msg);
                string customFunction;
                msg.ReadString(out customFunction);


                NetworkView view;
                if (NetworkViewManager.Find(addToId, out view))
                {
                    var newView = NetworkViewManager.Create(idToAdd, view.OwnerId);

                    try
                    {
                        EngineHook.AddNetworkView(view, newView, customFunction);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(this, "[EngineHook.AddNetworkView] {0}", e);
                    }
                }
                else
                {
                    Debug.LogError(this, "Attempted to add a network view to id {0}, but it could not be found");
                }
            }
            else if (utilId == RPCUtils.SetPlayerId)
            {
                var playerId = msg.ReadUInt16();
                PlayerId = playerId;
                Debug.LogInfo(this, "Setting player id to " + playerId);
            }
        }
Exemple #13
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="id"></param>
 /// <param name="view"></param>
 /// <returns></returns>
 public bool Find(NetworkViewId id, out NetworkView view)
 {
     return _allViews.TryGetValue(id.guid, out view);
 }
Exemple #14
0
        private void ProcessUtils(NetIncomingMessage msg)
        {
            var utilId = msg.ReadByte();

            if (utilId == RPCUtils.TimeUpdate)
            {
                throw new NotImplementedException("RPCUtils.TimeUpdate");
            }
            else if (utilId == RPCUtils.Instantiate)
            {
                //read the path...
                var resourcePath = msg.ReadString();
                var viewId = NetworkViewId.Deserialize(msg);
                var ownerId = msg.ReadUInt16();

                var position = new Vector3();
                position.OnDeserialize(msg);
                var rotation = new Quaternion();
                rotation.OnDeserialize(msg);

                var view = NetworkViewManager.Create(viewId, ownerId);

                try
                {
                    EngineHook.Instantiate(resourcePath, view, position, rotation);
                }
                catch(Exception e)
                {
                    Debug.LogError(this, "[EngineHook.Instantiate] {0}", e);
                }

                Debug.Log(this, "Created {0}", view);

                view.DoOnFinishedCreation();
                
                FinishedInstantiate(viewId);
            }
            else if (utilId == RPCUtils.Remove)
            {

                var viewId = new NetworkViewId();
                viewId.OnDeserialize(msg);
                byte reasonCode;
                if (!msg.ReadByte(out reasonCode))
                    reasonCode = 0;

                NetworkView find;
                if (NetworkViewManager.Find(viewId, out find))
                {
                    find.DoOnRemove(reasonCode);
                }
                else
                {
                    Debug.LogError(this, "Attempted to remove {0}, but it could not be found", viewId);
                }
            }
            else if (utilId == RPCUtils.ChangeRoom)
            {
                var newRoom = msg.ReadString();

                Debug.LogInfo(this, "Changing to room {0}", newRoom);

                if (OnRoomChange != null)
                {
                    try
                    {
                        OnRoomChange(newRoom);
                    }
                    catch(Exception e)
                    {
                        Debug.LogError(this, "[OnChangeRoom] {0}", e);
                    }
                }

                if (Configuration.DeleteNetworkInstantiatesOnRoomChange)
                {
                    NetworkViewManager.DestroyAllViews();
                }
            }
            else if (utilId == RPCUtils.AddView)
            {
                var addToId = NetworkViewId.Deserialize(msg);
                var idToAdd = NetworkViewId.Deserialize(msg);
                string customFunction;
                msg.ReadString(out customFunction);


                NetworkView view;
                if (NetworkViewManager.Find(addToId, out view))
                {
                    var newView = NetworkViewManager.Create(idToAdd, view.OwnerId);

                    try
                    {
                        EngineHook.AddNetworkView(view, newView, customFunction);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(this, "[EngineHook.AddNetworkView] {0}", e);
                    }
                }
                else
                {
                    Debug.LogError(this, "Attempted to add a network view to id {0}, but it could not be found");
                }
            }
            else if (utilId == RPCUtils.SetPlayerId)
            {
                var playerId = msg.ReadUInt16();
                PlayerId = playerId;
                Debug.LogInfo(this, "Setting player id to " + playerId);
            }
        }
Exemple #15
0
        private void FinishedInstantiate(NetworkViewId netId)
        {
            var msg = Peer.CreateMessage(3);
            msg.Write(RPCUtils.FinishedInstantiate);
            netId.OnSerialize(msg);

            Peer.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, Channels.STATIC_UTILS);
            Debug.Log(this, "Finished instantiation, sending ack");
        }
Exemple #16
0
 /// <summary>
 /// Whether or not the this viewid refers to the same id
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public bool Equals(NetworkViewId other)
 {
     return other.guid == guid;
 }
 internal bool TryGetView(NetworkViewId viewId, out NetworkView view)
 {
     return _networkViews.TryGetValue(viewId, out view);
 }
Exemple #18
0
 private void RegisterView(NetworkView view, NetworkViewId viewId)
 {
     _allViews.Add(viewId.guid, view);
 }
Exemple #19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="id"></param>
 /// <param name="view"></param>
 /// <returns></returns>
 public bool Find(NetworkViewId id, out NetworkView view)
 {
     return(_allViews.TryGetValue(id.guid, out view));
 }
Exemple #20
0
        internal NetworkView Create(NetworkViewId viewId, ushort ownerId)
        {
            viewId.IsMine = Net.PlayerId == ownerId;

            var newView = GetNetworkView();
            newView.ViewID = viewId;
            newView.OwnerId = ownerId;
            RegisterView(newView, viewId);
            return newView;
        }
Exemple #21
0
 /// <summary>
 /// find a network view based on the given NetworkViewId
 /// </summary>
 /// <param name="viewId"></param>
 /// <returns></returns>
 public NetworkView Find(NetworkViewId viewId)
 {
     return(_allViews[viewId.guid]);
 }
Exemple #22
0
 /// <summary>
 /// Whether or not the this viewid refers to the same id
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public bool Equals(NetworkViewId other)
 {
     return(other.guid == guid);
 }
        public void RecycleTest()
        {
            var vid1 = new NetworkViewId{guid =15};
            var vid2 = new NetworkViewId{guid =16};
            var vid3 = new NetworkViewId{guid =17};

            ushort owner1 = 1;
            ushort owner2 = 2;
            ushort owner3 = 3;

            var first = _target.Create(vid1, owner1);
            var second = _target.Create(vid2, owner2);

            _target.RemoveView(first);
            first = null;

            var third = _target.Create(vid3, owner3);

            Assert.AreNotEqual(vid1, third.ViewID.guid);
            Assert.AreNotEqual(owner1, third.OwnerId);
        }
 internal bool Remove(NetworkViewId viewId)
 {
     return _networkViews.Remove(viewId);
 }
Exemple #25
0
 /// <summary>
 /// Deserialize a networkviewid from the networkmessage
 /// </summary>
 /// <param name="message"></param>
 /// <returns></returns>
 internal static NetworkViewId Deserialize(NetIncomingMessage message)
 {
     var id = new NetworkViewId();
     id.OnDeserialize(message);
     return id;
 }