protected override void OnInit() { if (networkCommon.Configuration.UseMultithreading) { heardBeatThread = new Thread(HeardBeatThreadFun); heardBeatThread.Start(); heardBeatThread.IsBackground = true; } sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, (byte)NetProperty.HeartBeatClinetSend, new byte[0]); }
private void ClientPingUpdate(object obj) { int tempTime = pingDelayTime; while (true) { if (enable && isConnect) { if (msgQueue.Count > 0) { MsgPackest eventData = msgQueue.Dequeue(); long lastTime = bitConverter.ToInt64(eventData.contents, 0); //NetDebug.Log("接收到时间:" + lastTime + " eventData.contents"+eventData.contents.Length); ping = (int)((DateTime.Now.Ticks - lastTime) / 20000); msgQueue.Clear(); } if (tempTime <= 0) { tempTime = pingDelayTime; //Debug.Log("发送时间:" + DateTime.Now.Ticks); byte[] contents = bitConverter.GetBytes(DateTime.Now.Ticks); //long temp = bitConverter.ToInt64(contents, 0); //NetDebug.Log("发送Ping:" + temp); byte[] sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, property, contents); session.StatisticSendPackets(property, sendBytes.Length); networkCommon.Sendbytes(session, sendBytes); } else { tempTime -= 50; } } Thread.Sleep(50); } }
protected override void OnInit() { sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, (byte)NetProperty.HeartBeatServerSend, new byte[0]); }
public override void ReceveProcess(MsgPackest packest) { byte[] sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, (byte)NetProperty.Pong, packest.contents); networkCommon.Sendbytes(packest.session, sendBytes); }
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); }