コード例 #1
0
ファイル: NetWorker.cs プロジェクト: MentalRefinery/Cha-Cha
 private void HandleServerAcceptanceMessage(FrameStream frame)
 {
     Time.SetStartTime(frame.TimeStep);
     Me = new NetworkingPlayer(frame.StreamData.GetBasicType <uint>(), "0.0.0.0", false, null, this);
     Me.AssignPort(Port);
     OnServerAccepted();
 }
コード例 #2
0
        /// <summary>
        /// A wrapper for the messageReceived event call that chindren of this can call
        /// </summary>
        protected void OnMessageReceived(NetworkingPlayer player, FrameStream frame)
        {
            if (frame.GroupId == MessageGroupIds.NETWORK_ID_REQUEST && this is IClient)
            {
                Time.SetStartTime(frame.TimeStep);
                Me = new NetworkingPlayer(frame.StreamData.GetBasicType <uint>(), "0.0.0.0", false, null, this);
                Me.AssignPort(Port);
                OnServerAccepted();
                return;
            }

            if (frame.GroupId == MessageGroupIds.PING || frame.GroupId == MessageGroupIds.PONG)
            {
                long     receivedTimestep = frame.StreamData.GetBasicType <long>();
                DateTime received         = new DateTime(receivedTimestep);
                TimeSpan ms = DateTime.UtcNow - received;

                if (frame.GroupId == MessageGroupIds.PING)
                {
                    Pong(player, received);
                }
                else
                {
                    OnPingRecieved(ms.TotalMilliseconds, player);
                }

                return;
            }

            if (frame is Binary)
            {
                byte routerId = ((Binary)frame).RouterId;
                if (routerId == RouterIds.RPC_ROUTER_ID || routerId == RouterIds.BINARY_DATA_ROUTER_ID || routerId == RouterIds.CREATED_OBJECT_ROUTER_ID)
                {
                    uint          id           = frame.StreamData.GetBasicType <uint>();
                    NetworkObject targetObject = null;

                    lock (NetworkObjects)
                    {
                        NetworkObjects.TryGetValue(id, out targetObject);
                    }

                    if (targetObject == null)
                    {
                        lock (missingObjectBuffer)
                        {
                            if (!missingObjectBuffer.ContainsKey(id))
                            {
                                missingObjectBuffer.Add(id, new List <Action <NetworkObject> >());
                            }

                            missingObjectBuffer[id].Add((networkObject) =>
                            {
                                ExecuteRouterAction(routerId, networkObject, (Binary)frame, player);
                            });
                        }

                        // TODO:  If the server is missing an object, it should have a timed buffer
                        // that way useless messages are not setting around in memory

                        return;
                    }

                    ExecuteRouterAction(routerId, targetObject, (Binary)frame, player);
                }
                else if (routerId == RouterIds.NETWORK_OBJECT_ROUTER_ID)
                {
                    NetworkObject.CreateNetworkObject(this, player, (Binary)frame);
                }
                else if (routerId == RouterIds.ACCEPT_MULTI_ROUTER_ID)
                {
                    NetworkObject.CreateMultiNetworkObject(this, player, (Binary)frame);
                }
                else if (binaryMessageReceived != null)
                {
                    binaryMessageReceived(player, (Binary)frame, this);
                }
            }
            else if (frame is Text && textMessageReceived != null)
            {
                textMessageReceived(player, (Text)frame, this);
            }

            if (messageReceived != null)
            {
                messageReceived(player, frame, this);
            }
        }
コード例 #3
0
        /// <summary>
        /// 这个可以调用的messageReceived事件调用的包装器
        /// A wrapper for the messageReceived event call that chindren of this can call
        /// </summary>
        protected void OnMessageReceived(NetworkingPlayer player, FrameStream frame)
        {
            // 客户端被服务器接收
            if (frame.GroupId == MessageGroupIds.NETWORK_ID_REQUEST && this is IClient)
            {
                Time.SetStartTime(frame.TimeStep);
                // 读取玩家ID, 创建客户端自己的NetworkingPlayer
                Me = new NetworkingPlayer(frame.StreamData.GetBasicType <uint>(), "0.0.0.0", false, null, this);
                Me.AssignPort(Port);
                OnServerAccepted();
                return;
            }

            // 收到ping
            if (frame.GroupId == MessageGroupIds.PING || frame.GroupId == MessageGroupIds.PONG)
            {
                // 发送ping时的 发送者时间
                long     receivedTimestep = frame.StreamData.GetBasicType <long>();
                DateTime received         = new DateTime(receivedTimestep);

                // 现在接收到反馈ping的时间 - 自己发送ping是的时间
                TimeSpan ms = DateTime.UtcNow - received;

                if (frame.GroupId == MessageGroupIds.PING)
                {
                    // 反馈ping, 将发送ping时的 发送者时间 一起发给他
                    Pong(player, received);
                }
                else
                {
                    // 接收都ping的反馈
                    OnPingRecieved(ms.TotalMilliseconds, player);
                }

                return;
            }

            // 二进制消息
            if (frame is Binary)
            {
                if (frame.RoomId == 0)
                {
                    byte routerId = ((Binary)frame).RouterId;
                    if (routerId == RouterIds.RPC_ROUTER_ID || routerId == RouterIds.BINARY_DATA_ROUTER_ID || routerId == RouterIds.CREATED_OBJECT_ROUTER_ID)
                    {
                        uint          id           = frame.StreamData.GetBasicType <uint>();
                        NetworkObject targetObject = null;

                        lock (NetworkObjects)
                        {
                            NetworkObjects.TryGetValue(id, out targetObject);
                        }

                        if (targetObject == null)
                        {
                            // 收到该网络对象的消息包
                            // 但是该玩家机器上还没有创建网络对象
                            // 就将该消息缓存器来,等待网络对象创建完再掉用
                            lock (missingObjectBuffer)
                            {
                                if (!missingObjectBuffer.ContainsKey(id))
                                {
                                    missingObjectBuffer.Add(id, new List <Action <NetworkObject> >());
                                }

                                missingObjectBuffer[id].Add((networkObject) =>
                                {
                                    ExecuteRouterAction(routerId, networkObject, (Binary)frame, player);
                                });
                            }

                            // TODO:  If the server is missing an object, it should have a timed buffer
                            // that way useless messages are not setting around in memory
                            // TODO:如果服务器缺少一个对象,它应该有一个定时缓冲区
                            //这种无用的消息不会在内存中设置

                            return;
                        }

                        ExecuteRouterAction(routerId, targetObject, (Binary)frame, player);
                    }
                    // 创建网络对象
                    else if (routerId == RouterIds.NETWORK_OBJECT_ROUTER_ID)
                    {
                        NetworkObject.CreateNetworkObject(this, player, (Binary)frame);
                    }
                    // 在服务器接受客户端时,将服务器现有的所有网络对象 发给该玩家,让他创建
                    else if (routerId == RouterIds.ACCEPT_MULTI_ROUTER_ID)
                    {
                        NetworkObject.CreateMultiNetworkObject(this, player, (Binary)frame);
                    }
                    else if (binaryMessageReceived != null)
                    {
                        binaryMessageReceived(player, (Binary)frame, this);
                    }
                }
                else if (binaryMessageReceived != null)
                {
                    binaryMessageReceived(player, (Binary)frame, this);
                }
            }
            // 文本消息
            else if (frame is Text && textMessageReceived != null)
            {
                textMessageReceived(player, (Text)frame, this);
            }

            if (messageReceived != null)
            {
                messageReceived(player, frame, this);
            }
        }