public void Update(float deltaTime) { if (Transport == null) { return; } if (!configuration.UseMultithreading) { LoopDealwithReceiveData(); Send2TransportProcess(); } List <NetMsgProcessPluginBase> pluginList = Configuration.GetNetMsgProcessPlugins(); foreach (var item in pluginList) { item.Update(deltaTime); } while (netEventDatas.Count > 0) { try { OnHandleEvent(); } catch (Exception e) { NetDebug.LogError(e.ToString()); } } try { OnHandleMsgPackests(); } catch (Exception e) { NetDebug.LogError(e.ToString()); } OnUpdate(deltaTime); }
private void OnHandleMsgPackests() { if (netMsgPackests.Count == 0) { return; } MsgPackest packest; while (netMsgPackests.TryDequeue(out packest)) { Session session = packest.session; INetMsgSerializer serializer = configuration.GetMsgSerializer(); object msgType = null; object msgData = null; try { msgData = serializer.Deserialize(packest.contents, out msgType); } catch (Exception e) { NetDebug.LogError("消息序列化失败:" + e); return; } IMessageHandler messageHander = configuration.GetMessageHander(); if (messageHander != null) { try { messageHander.DispatchMessage(session, msgType, msgData); } catch (Exception e) { NetDebug.LogError("DispatchMessage error:" + e); } } } }
private void ClientHeardBeatUpdate(int deltaTime) { if (isConnect) { if (msgQueue.Count > 0) { //NetDebug.Log("重置心跳包:"); ResetFlag(); return; } if (tempTime <= 0) { lostCount++; tempTime = heatBeatTime + lostCount * 2; if (lostCount > 4) { NetDebug.Log("ClientHeardBeatUpdate Disconnect! lostCount:" + lostCount); networkCommon.Configuration.Transport.Disconnect(this.session.ConnectionId, EDisconnectReason.Timeout); isConnect = false; lostCount = 0; return; } else { //NetDebug.Log("发送心跳:lostCount:"+ lostCount); session.StatisticSendPackets((byte)NetProperty.HeartBeatClinetSend, sendBytes.Length); networkCommon.Sendbytes(session, sendBytes); } } else { tempTime -= deltaTime; } } }
public override bool Disconnect(long connectionId, EDisconnectReason disconnectReason) { try { if (!IsServer) { client.Disconnect(); m_disconnectInfo = new EDisconnectInfo() { Reason = disconnectReason }; } else { server.Disconnect((int)connectionId); } } catch (Exception e) { NetDebug.LogError(e.ToString()); return(false); } return(true); }
protected bool SendData <T>(Session session, object msgType, T messageData) { //if (LogInfo) //{ //NetDebug.Log("Send Msg =>::" + typeof(T).Name + "-->" + JsonUtility.ToJson(messageData)); //} if (Transport == null) { return(false); } INetMsgSerializer serializer = configuration.GetMsgSerializer(); if (serializer == null) { NetDebug.LogError("No INetMsgSerializer!"); return(false); } if (msgType == null) { msgType = serializer.GetMsgType(messageData); } byte[] datas = null; try { datas = serializer.Serialize(msgType, messageData); } catch (Exception e) { NetDebug.LogError("INetMsgSerializer error:" + e); return(false); } sendMsgLoop.Enqueue(new SendMsgTempData(session, datas)); return(true); }
public override byte[] SendProcess(Session session, byte msgProperty, byte[] datas) { ByteOrder byteOrder = networkCommon.Configuration.byteOrder; byte compressType = 0; byte isEncryption = 0; byte isCompress = 0; if (bitConverter == null || bitConverter.byteOrder != byteOrder) { bitConverter = EndianBitConverter.GetBitConverter(byteOrder); } byte[] pBytes = bitConverter.GetBytes(session.AddSendCounter()); byte[] allDatas = new byte[pBytes.Length + datas.Length]; pBytes.CopyTo(allDatas, 0); datas.CopyTo(allDatas, pBytes.Length); datas = allDatas; MsgCompressBase compress = networkCommon.Configuration.GetSendCompressFunction(); if (compress != null) { isCompress = 1; try { //NetDebug.Log("压缩消息:" + datas.Length); datas = compress.Compress(datas); //NetDebug.Log("压缩后消息:" + datas.Length); } catch (Exception e) { NetDebug.LogError("压缩错误:" + e); return(null); } compressType = compress.CompressType; } else { isCompress = 0; } MsgEncryptionBase encryption = networkCommon.Configuration.GetMsgEncryption(); if (networkCommon.Configuration.IsEncryption && encryption != null) { isEncryption = 1; try { datas = encryption.Encryption(session, datas); } catch (Exception e) { NetDebug.LogError("加密错误:" + e); return(null); } } else { isEncryption = 0; } byte[] res = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, isEncryption, isCompress, compressType, msgProperty, datas); return(res); }
public override void ReceveProcess(MsgPackest packest) { Session session = packest.session; if (packest.isEncryption == 1) { MsgEncryptionBase encryption = networkCommon.Configuration.GetMsgEncryption(); if (encryption == null) { NetDebug.LogError("不支持消息解密:" + packest); return; } else { try { packest.contents = encryption.Decryption(packest.session, packest.contents); } catch (System.Exception e) { NetDebug.LogError("消息解密错误:" + packest + " \n" + e); return; } } } if (packest.isCompress == 1) { MsgCompressBase compress = networkCommon.Configuration.GetCompressFunction(packest.compressType); if (compress == null) { NetDebug.LogError("不支持的压缩方式:" + packest.compressType); return; } else { packest.contents = compress.Decompress(packest.contents); //NetDebug.Log("解压缩:"+ packest.contents.Length); } } if (bitConverter == null || bitConverter.byteOrder != packest.byteOrder) { bitConverter = EndianBitConverter.GetBitConverter(packest.byteOrder); } //收发消息计数器(标明消息序列号) uint counter = bitConverter.ToUInt32(packest.contents, 0); //if (session.CheckReceiveMsgCounter(counter)) //{ // session.SetReceiveCounter(counter); //} //else //{ // NetDebug.LogError("packest.counter error:" + counter + " session.ReceiveMsgCounter:" + (session.ReceiveMsgCounter + 1)); // return; //} byte[] dataArray = new byte[packest.contents.Length - 4]; //NetDebug.Log("packest.contents.Length:" + packest.contents.Length + " dataArray:" + dataArray.Length) ; Array.Copy(packest.contents, 4, dataArray, 0, dataArray.Length); packest.contents = dataArray; networkCommon.ReceiveMsgPackest(packest); }
private void LoopDealwithReceiveData() { if (Transport == null) { return; } TransportEventData eventData; while (Transport.Receive(out eventData)) { Session session = null; sessions.TryGetValue(eventData.connectionId, out session); if (eventData.type == ENetworkEvent.DataEvent) { if (session == null) { NetDebug.LogError("session is null, connectionId :" + session + " is not Connnected"); return; } MsgPackest packest; try { packest = new MsgPackest(configuration.byteOrder, session, eventData.data); } catch (Exception e) { NetDebug.LogError("Parse MsgPackest Error :" + e); continue; } if (eventData.data != null) { session.StatisticReceivePackets(packest.msgProperty, eventData.data.Length); } //NetDebug.Log("接收到消息 connectionId:" + packest.connectionId); NetMsgProcessPluginBase plugin = configuration.GetPlugin(packest.msgProperty); if (plugin == null) { NetDebug.LogError("No NetMsgProcessPluginBase By msgProperty:" + packest.msgProperty); } else { plugin.ReceveProcess(packest); } } else { if (eventData.type == ENetworkEvent.ConnectEvent) { session = new Session(eventData.connectionId); session.OpenNetStatistics(Configuration.UseStatistics); session.SetConnectTimeInStatistic(); sessions.Add(session.ConnectionId, session); foreach (var plugin in configuration.GetNetMsgProcessPlugins()) { plugin.PeerConnectedEvent(session); } } else if (eventData.type == ENetworkEvent.DisconnectEvent) { if (session != null) { session.SetDisconnectTimeInStatistic(); } sessions.Remove(eventData.connectionId); foreach (var plugin in configuration.GetNetMsgProcessPlugins()) { plugin.DisconnectedEvent(session, eventData.disconnectInfo); } } eventData.session = session; //NetDebug.Log("循环接收事件:" + eventData.type); AddNetEvent(eventData); } } }