public NetMessage(byte[] rawData) { int headLength = Marshal.SizeOf(typeof(CommonPackHead)); //分配结构体内存空间 IntPtr structPtr = Marshal.AllocHGlobal(headLength); //将byte数组拷贝到分配好的内存空间 Marshal.Copy(rawData, 0, structPtr, headLength); //将内存空间转换为目标结构体 MessageHead = (CommonPackHead)Marshal.PtrToStructure(structPtr, typeof(CommonPackHead)); //释放内存空间 Marshal.FreeHGlobal(structPtr); /// MsgID = MessageHead.msg_id; UID = (long)MessageHead.uid; byte[] tmpAppend = new byte[MessageHead.option_len]; Array.Copy(rawData, headLength, tmpAppend, 0, MessageHead.option_len); AddAppendData(tmpAppend, MessageHead.option_len); byte[] encryData = new byte[(int)MessageHead.msg_len - headLength - MessageHead.option_len]; Array.Copy(rawData, headLength + MessageHead.option_len, encryData, 0, encryData.Length); encryData = RC4.Decrypt(Encoding.ASCII.GetBytes(encryKey.ToCharArray()), encryData); encryData = Snappy.Sharp.Snappy.Uncompress(encryData); Data = InfibrProtoBuf.Serializer.Deserialize <T>(new MemoryStream(encryData, 0, encryData.Length)); }
public NetMessage(int _msgID, long uid, T _data) { MessageHead = new CommonPackHead(); MsgID = _msgID; UID = uid; Data = InfibrProtoBuf.Serializer.DeepClone <T>(_data); }
/// <summary> /// 接收消息 /// </summary> /// <param name="buff"></param> protected void OnReceivedServerData(CommonPackHead packHead, byte[] buff) { Log.Debug("OnReceivedServerData.dataLength=" + packHead.msg_id); Type type = Service.GetServiceType(packHead.msg_id); var genericType = typeof(NetMessage <>).MakeGenericType(type); Log.Assert(null != type, string.Format("msg_id({0}) receive not setup", packHead.msg_id)); var p = Activator.CreateInstance(genericType, buff); }
/// <summary> /// 使用新进程通知事件回调 /// </summary> /// <param name="buff"></param> private void DoReceiveEvent(CommonPackHead packHead, byte[] buff) { if (ServerDataHandler == null) { return; } //ServerDataHandler(buff); //可直接调用. //但我更喜欢用新的线程,这样不拖延接收新数据. Thread thread = new Thread(new ParameterizedThreadStart((obj) => { ServerDataHandler(packHead, (byte[])obj); })); thread.IsBackground = true; thread.Start(buff); }
protected void UpdateKcp() { while (connected) { long start = GetCurrent(); lock (kcpManager) { List <uint> pp = new List <uint>(kcpManager.Keys); for (var index = 0; index < pp.Count; index++) //foreach (KeyValuePair<uint, KCP> kv in kcpManager) { KeyValuePair <uint, KCP> kv = new KeyValuePair <uint, KCP>(pp[index], kcpManager[pp[index]]); kv.Value.Update((uint)start); int len = kv.Value.Recv(kcpDataCache); if (len > 0) { byte[] data = new byte[len]; Array.Copy(kcpDataCache, data, len); lock (m_buffer) { m_buffer.AddRange(data); } //DoReceiveEvent() do { int headLength = Marshal.SizeOf(typeof(CommonPackHead)); // 判断包头是否满足 if (headLength <= m_buffer.Count) { byte[] lenBytes = m_buffer.GetRange(0, headLength).ToArray(); //分配结构体内存空间 IntPtr structPtr = Marshal.AllocHGlobal(headLength); //将byte数组拷贝到分配好的内存空间 Marshal.Copy(lenBytes, 0, structPtr, headLength); //将内存空间转换为目标结构体 CommonPackHead packHead = (CommonPackHead)Marshal.PtrToStructure(structPtr, typeof(CommonPackHead)); //释放内存空间 Marshal.FreeHGlobal(structPtr); if (packHead.msg_len <= m_buffer.Count) { //包够长时,则提取出来,交给后面的程序去处理 byte[] recv = m_buffer.GetRange(0, (int)packHead.msg_len).ToArray(); lock (m_buffer) { m_buffer.RemoveRange(0, (int)packHead.msg_len); } //将数据包交给前台去处理 DoReceiveEvent(packHead, recv); } else { break; } } else { // if (4 == len) { uint new_conv = 0; KCP.ikcp_decode32u(data, 0, ref new_conv); if (!kcpManager.ContainsKey(new_conv)) { kcpManager.Add(new_conv, new KCP(new_conv, SendData)); lock (m_buffer) { m_buffer.RemoveRange(0, 4); } } usingConv = new_conv; Log.Error("usingConv=" + new_conv); } //长度不够,还得继续接收,需要跳出循环 break; } } while (m_buffer.Count > 4); } } } long after = GetCurrent(); if (after - start < ThreadGap) { Thread.Sleep((int)(ThreadGap + start - after)); } } }