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); } } } }
/// <summary> /// 发送 /// </summary> /// <param name="_protocalType"></param> /// <param name="data"></param> public static void SendISMsg(ISSocketModel data) { if (!IsConnected) { return; } byte[] rawData = SocketUtil.ISSerial(data); ISSocketManager.Instance.SendMsgBase(rawData); }
internal static ISSocketModel ISDeSerial(byte[] msg)//将字节数组转化成我们的消息类型SocketModel { using (MemoryStream ms = new MemoryStream()) { ms.Write(msg, 0, msg.Length); ms.Position = 0; ISSocketModel socketModel = Serializer.Deserialize <ISSocketModel>(ms); return(socketModel); } }
internal static byte[] ISSerial(ISSocketModel 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); } }
/// <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)) { ISSocketModel DeData = SocketUtil.ISDeSerial(_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.SSINFO) { int num = Convert.ToInt32(DeData.Message[0]); if (num > 0) { NetManager.Instance.allSSInfo.Clear(); for (int i = 0; i < num; i++) { NetManager.Instance.allSSInfo.Add(new SocketUtil.SSInfo() { Tag = DeData.Message[i * 3 + 1], IP = DeData.Message[i * 3 + 2], Port = Convert.ToInt32(DeData.Message[i * 3 + 3]), }); } } SGNFDebug.ListLog("Got " + num + " row of SS from server", NetManager.Instance.allSSInfo); } } else { //锁死消息中心消息队列,并添加数据 lock (MessageCenter.Instance.ISMessageDataQueue) { MessageCenter.Instance.ISMessageDataQueue.Enqueue(DeData); } } } } } catch (Exception e) { clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); SGNFDebug.ExceptionCaught(e); break; } } }