/// <summary> /// 读取一个结构体变量 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T ReadStruct <T>() { Type anytype = typeof(T); int rawsize = Marshal.SizeOf(anytype); if ((m_Index + rawsize) > m_Size) { return(default(T)); } var buff = DogBuffer.GetFromPool4K(); System.Buffer.BlockCopy(m_Data, (int)m_Index, buff.Bytes, 0, rawsize); m_Index += rawsize; GCHandle hObject = GCHandle.Alloc(buff.Bytes, GCHandleType.Pinned); IntPtr pObject = hObject.AddrOfPinnedObject(); try { return((T)Marshal.PtrToStructure(pObject, anytype)); } finally { if (hObject.IsAllocated) { hObject.Free(); } buff.Release(); } }
public void TestSize() { var _4Kbuf = DogBuffer.GetFromPool4K(); Assert.IsNotNull(_4Kbuf); Assert.AreEqual(_4Kbuf.Bytes.Length, 1024 * 4); Assert.AreEqual(_4Kbuf.Length, 0); var _32kbuf = DogBuffer.GetFromPool32K(); Assert.IsNotNull(_32kbuf); Assert.AreEqual(_32kbuf.Bytes.Length, 1024 * 32); Assert.AreEqual(_32kbuf.Length, 0); }
public void TestRelease4K() { var firstFree = Get4KbufferFree(); var buf = DogBuffer.GetFromPool4K(); var 分配后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree - 1, 分配后的数量); buf.Release(); var 释放缓冲区后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree, 释放缓冲区后的数量); // 测试重复释放会不会有问题 buf.Release(); 释放缓冲区后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree, 释放缓冲区后的数量); }
public void TestUpdateCapacity() { var buf = DogBuffer.GetFromPool4K(); Assert.AreEqual(buf.Bytes.Length, 1024 * 4); Assert.AreEqual(buf.Length, 0); buf.Bytes[0] = 1; buf.Bytes[1] = 2; buf.Bytes[1024 * 4 - 1] = 255; var b = buf.Bytes; buf.UpdateCapacity(); // 扩容后对象引用会不一样了 Assert.AreNotSame(buf.Bytes, b); Assert.AreEqual(buf.Bytes.Length, 1024 * 8); // 验证数据扩容后是否正确 Assert.AreEqual(buf.Bytes[0], 1); Assert.AreEqual(buf.Bytes[1], 2); Assert.AreEqual(buf.Bytes[1024 * 4 - 1], 255); }
public void Test重复引用对象() { var firstFree = Get4KbufferFree(); var buf = DogBuffer.GetFromPool4K(); var 分配后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree - 1, 分配后的数量); buf.Use(); buf.Release(); // 只释放一个引用 var 释放缓冲区后的数量 = Get4KbufferFree(); Assert.AreEqual(分配后的数量, 释放缓冲区后的数量); buf.Release(); 释放缓冲区后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree, 释放缓冲区后的数量); buf.Release(); 释放缓冲区后的数量 = Get4KbufferFree(); Assert.AreEqual(firstFree, 释放缓冲区后的数量); }
/// <summary> /// 收到Socket数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnSocketRecv(object sender, SocketRecvEventArgs<Session> e) { lock (sessions) { var session = e.Session.Data; DogBuffer buf; if (e.Buffer.Length > 1024*4) { buf = DogBuffer.GetFromPool32K(); } else { buf = DogBuffer.GetFromPool4K(); } Array.Copy(e.Buffer.Bytes, buf.Bytes, e.Buffer.Length); buf.Length = e.Buffer.Length; session.Client.SendPackage(buf); buf.Release(); recvPackageCount++; recvPackageLength += e.Buffer.Length; } }
/// <summary> /// 收到网络消息包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnSocketRecv(object sender, SocketRecvEventArgs <NetState> e) { var netState = e.Session.Data; netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length); if (netState.ReceiveBuffer.Length > MaxPackageSize) { // 缓冲区过多,一定发送了某种异常情况 Logs.Error("client recv buff is full."); netState.NetSocket.CloseSocket(); return; } while (netState.ReceiveBuffer.Length > 2) // 大于包头长度才具备解析的需求 { var len = netState.ReceiveBuffer.GetPacketLength(); if (len == 0) { Logs.Error("get package len is zero."); netState.NetSocket.CloseSocket(); return; } if (len < 4) { Logs.Error("get package len is min 4."); netState.NetSocket.CloseSocket(); return; } if (len > MaxPackageSize) { Logs.Error("get package len is error. size:{0}", len); netState.NetSocket.CloseSocket(); return; } if (len <= netState.ReceiveBuffer.Length) { DogBuffer readBuffer; if (len < 1024 * 4) { readBuffer = DogBuffer.GetFromPool4K(); } else { readBuffer = DogBuffer.GetFromPool32K(); } var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len); if (get == len) { readBuffer.Length = len; var packageReader = PacketReader.AcquireContent(readBuffer); var packageId = packageReader.GetPacketID(); //Debug.Write("msgId = " + packageId.ToString()); var packetHandler = PacketHandlersManger.GetHandler(packageId); if (packetHandler != null) { // 加入网络消息处理 if (_useManyTaskThread) { switch (packetHandler.TaskType) { case TaskType.Low: lowTask.AppendTask(netState, packetHandler, packageReader); break; case TaskType.Assist: Logs.Debug("assist task."); assistTask.AppendTask(netState, packetHandler, packageReader); break; default: mainTask.AppendTask(netState, packetHandler, packageReader); break; } } else { mainTask.AppendTask(netState, packetHandler, packageReader); } } else { Logs.Error("unknow packetid. code={0}", packageId); netState.ErrorCount++; if (netState.ErrorCount >= 10) { // 错误达到极大值,则关闭连接 Logs.Error("ip {0} error count max.", netState.GetIP()); netState.NetSocket.CloseSocket(); } } } continue; } break; } }
void NetSocket_SocketRecv(object sender, SocketRecvEventArgs <NetState> e) { var netState = e.Session.Data; netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length); if (netState.ReceiveBuffer.Length > MaxPackageSize) { // 缓冲区过多,一定发送了某种异常情况 Logs.Error("client recv buff is full."); netState.NetSocket.CloseSocket(); return; } while (netState.ReceiveBuffer.Length > 2) { var len = netState.ReceiveBuffer.GetPacketLength(); if (len == 0) { Logs.Error("get package len is zero."); netState.NetSocket.CloseSocket(); return; } if (len < 4) { Logs.Error("package is min 4."); netState.NetSocket.CloseSocket(); return; } if (len > MaxPackageSize) { Logs.Error("get package len is error. size:{0}", len); netState.NetSocket.CloseSocket(); return; } IsWaitCheckOnline = false; if (len <= netState.ReceiveBuffer.Length) { DogBuffer readBuffer; if (len < 1024 * 4) { readBuffer = DogBuffer.GetFromPool4K(); } else { readBuffer = DogBuffer.GetFromPool32K(); } if (len >= readBuffer.Bytes.Length) { readBuffer.UpdateCapacity(len); } var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len); if (get == len) { readBuffer.Length = len; var packageReader = PacketReader.AcquireContent(readBuffer); ushort id = packageReader.GetPacketID(); Logs.Debug("msgid= {0}", id); var packetHandler = PacketHandlersManger.GetHandler(id); if (packetHandler != null) { // 加入网络消息处理 TaskManager.AppendTask(netState, packetHandler, packageReader); } else { Logs.Error("unknow packetid. code={0}", id); } } continue; } break; } }
/// <summary> /// 收到网络消息包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnSocketRecv(object sender, SocketRecvEventArgs <NetState> e) { var netState = e.Session.Data; netState.ReceiveBuffer.Enqueue(e.Buffer.Bytes, 0, e.Buffer.Length); var len = netState.ReceiveBuffer.GetPacketLength(); do { if (len <= netState.ReceiveBuffer.Length) { DogBuffer readBuffer; if (len < 1024 * 4) { readBuffer = DogBuffer.GetFromPool4K(); } else { readBuffer = DogBuffer.GetFromPool32K(); } var get = netState.ReceiveBuffer.Dequeue(readBuffer.Bytes, 0, len); if (get == len) { readBuffer.Length = len; var packageReader = new PacketReader(readBuffer); var packageId = packageReader.GetPacketID(); var packetHandler = PacketHandlersManger.GetHandler(packageId); if (packetHandler != null) { // 加入网络消息处理 if (_useManyTaskThread) { switch (packetHandler.TaskType) { case TaskType.Low: lowTask.AppendTask(netState, packetHandler, packageReader); break; case TaskType.Assist: Logs.Debug("assist task."); assistTask.AppendTask(netState, packetHandler, packageReader); break; default: mainTask.AppendTask(netState, packetHandler, packageReader); break; } } else { mainTask.AppendTask(netState, packetHandler, packageReader); } } else { Logs.Error("unknow packetid. code={0}", packageId); } } } // 一次网络消息可能会对应多个消息包,因此这里用循环获得消息包 len = netState.ReceiveBuffer.GetPacketLength(); } while (len > 0); }