Ejemplo n.º 1
0
        void Update()
        {
            delatT = Time.deltaTime;

            if (SSSocketManager.Instance.IsConnceted)
            {
                timeFromLastTick += Time.deltaTime;
            }

            while (ISMessageDataQueue.Count > 0)
            {
                lock (ISMessageDataQueue)
                {
                    ISSocketModel tmpNetMessageData = ISMessageDataQueue.Dequeue();
                    if (ISMessage_HandlerList.ContainsKey(tmpNetMessageData.Command))
                    {
                        ISMessage_HandlerList[tmpNetMessageData.Command](tmpNetMessageData);
                    }
                }
            }


            //计算延迟
            if (SSMessageDataQueue.Count <= 0)
            {
                ping += delatT;
            }
            else
            {
                NetManager.Instance.latency = ping;
                ping = 0f;
            }


            while (SSMessageDataQueue.Count > 0)
            {
                lock (SSMessageDataQueue)
                {
                    SSSocketModel tmpSSMessageData = SSMessageDataQueue.Dequeue();
                    currentTick = tmpSSMessageData.CurrentTick;


                    ServerTransformCall?.Invoke(tmpSSMessageData);


                    if (tickFrameUpdater != null)
                    {
                        timeFromLastTick = 0;

                        SSSocketModel snd = tickFrameUpdater(tmpSSMessageData);
                        snd.CurrentTick = currentTick;

                        ClientTransformCall?.Invoke(ref snd);

                        byte[] rawData = SocketUtil.SSSerial(snd);
                        SSSocketManager.Instance.SendMsgBase(rawData);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 internal static SSSocketModel SSDeSerial(byte[] msg)//将字节数组转化成我们的消息类型SocketModel
 {
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(msg, 0, msg.Length);
         ms.Position = 0;
         SSSocketModel socketModel = Serializer.Deserialize <SSSocketModel>(ms);
         return(socketModel);
     }
 }
Ejemplo n.º 3
0
 internal static byte[] SSSerial(SSSocketModel socketModel)//将SocketModel转化成字节数组
 {
     using (MemoryStream ms = new MemoryStream())
     {
         Serializer.Serialize(ms, socketModel);
         byte[] data = new byte[ms.Length];
         ms.Position = 0;
         ms.Read(data, 0, data.Length);
         return(data);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 接受网络数据,将接受到的放入消息队列
        /// </summary>
        private void _onReceiveSocket()
        {
            while (true)
            {
                if (!clientSocket.Connected)
                {
                    _isConnected = false;
                    break;
                }
                try
                {
                    int receiveLength = clientSocket.Receive(_tmpReceiveBuff);
                    if (receiveLength > 0)
                    {
                        //将收到的数据添加到缓存器中
                        _databuffer.AddBuffer(_tmpReceiveBuff, receiveLength);
                        SGNFDebug.HEXLog("rcv", _tmpReceiveBuff, receiveLength);
                        //取出一条完整数据
                        while (_databuffer.GetData(out _socketData))
                        {
                            SSSocketModel DeData = SocketUtil.SSDeSerial(_socketData);

                            //如果数据属于内部协议
                            if (Enum.IsDefined(typeof(SocketUtil.InternalCommand), DeData.Command))
                            {
                                if (DeData.Command == (int)SocketUtil.InternalCommand.NULL)
                                {
                                    SGNFDebug.Log("Got NULL from server");
                                }
                                if (DeData.Command == (int)SocketUtil.InternalCommand.PING)
                                {
                                    //Client.RcvPingStr = DeData.Message[0];
                                }
                                if (DeData.Command == (int)SocketUtil.InternalCommand.TICK)
                                {
                                    if (DeData.Message.Count >= 1)
                                    {
                                        NetManager.Instance.tick = DeData.Message[0];
                                        SGNFDebug.Log("Got Tick!Which is " + DeData.Message[0]);
                                    }
                                    else
                                    {
                                        SGNFDebug.ExceptionCaught(new Exception("Recieved nothing from TICK package!"));;
                                    }
                                }
                            }
                            else
                            {
                                lock (SSRecorder.Instance.RecieveDataBuffer)
                                {
                                    SSRecorder.Instance.RecieveDataBuffer.Add(DeData.CurrentTick, DeData);
                                }
                                //锁死消息中心消息队列,并添加数据
                                lock (MessageCenter.Instance.SSMessageDataQueue)
                                {
                                    MessageCenter.Instance.SSMessageDataQueue.Enqueue(DeData);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    clientSocket.Disconnect(true);
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                    SGNFDebug.ExceptionCaught(e);
                    break;
                }
            }
        }