From c1a3650a95375f6b8984ad462a3b110609c0c280 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Thu, 27 Jun 2024 02:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5ArrayPool=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8D=8F=E8=AE=AE=E5=86=85=E5=AD=98=E6=8B=B7?= =?UTF-8?q?=E8=B4=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetLib/HaoYueNet.ServerNetwork/BaseData.cs | 15 ++++++-- .../NetWork/ArrayPoolManager.cs | 35 +++++++++++++++++++ .../NetWork/TcpSaeaServer.cs | 21 +++++++---- 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs diff --git a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs index ce0fd3d..e7615a7 100644 --- a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs +++ b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs @@ -45,6 +45,7 @@ namespace HaoYueNet.ServerNetwork int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; byte[] BufferData = new byte[AllLenght]; + /* //包长度 writeInt(BufferData, 0, AllLenght); @@ -53,9 +54,17 @@ namespace HaoYueNet.ServerNetwork //Error writeUInt16(BufferData, 4 + 2, Error); + */ - //DATA - Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); + + //包长度 + Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int)); + //CMDID + Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16)); + //CMDID + Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16)); + //DATA + Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); return BufferData; } @@ -83,7 +92,7 @@ namespace HaoYueNet.ServerNetwork public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data) { myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data)); - } + } public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data) { diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs new file mode 100644 index 0000000..eb14139 --- /dev/null +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs @@ -0,0 +1,35 @@ +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYueNet.ServerNetwork.NetWork +{ + public static class ArrayPoolManager + { + static ArrayPool instance = ArrayPool.Shared; + + /// + /// 租用指定大小byte数组 + /// + /// + /// + public static byte[] RentByteArr(int lenght) + { + return instance.Rent(lenght); + } + + + /// + /// 将数组归还给池 + /// + /// + /// + public static void ReturnByteArr(byte[] byteArr) + { + instance.Return(byteArr); + } + } +} diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs index 183a01e..e89b9f8 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs @@ -1,4 +1,5 @@ //using HunterProtobufCore; +using HaoYueNet.ServerNetwork.NetWork; using System.IO; using System.Net; using System.Net.Sockets; @@ -402,22 +403,28 @@ namespace HaoYueNet.ServerNetwork //} long FristBeginPos = token.memoryStream.Position; - byte[] lenBytes = new byte[4]; - token.memoryStream.Seek(0, SeekOrigin.Begin); + //byte[] lenBytes = new byte[4]; + byte[] lenBytes = ArrayPoolManager.RentByteArr(4); + + token.memoryStream.Seek(0, SeekOrigin.Begin); token.memoryStream.Read(lenBytes, 0, 4); int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; - if (packageLen > token.memoryStream.Length - 4) + ArrayPoolManager.ReturnByteArr(lenBytes); + + + if (packageLen > token.memoryStream.Length - 4) { token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); //长度不够时,退出循环,让程序继续接收 break; } - ////包够长时,则提取出来,交给后面的程序去处理 - //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); + ////包够长时,则提取出来,交给后面的程序去处理 + //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); - byte[] rev = new byte[packageLen]; - token.memoryStream.Seek(4, SeekOrigin.Begin); + byte[] rev = new byte[packageLen]; + + token.memoryStream.Seek(4, SeekOrigin.Begin); token.memoryStream.Read(rev, 0, packageLen); //从数据池中移除这组数据