コード例 #1
0
ファイル: PNet.cs プロジェクト: kangtachan/PNet
 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);
     }
 }
コード例 #2
0
ファイル: PNet.cs プロジェクト: kangtachan/PNet
        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);
            }
        }