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); //从数据池中移除这组数据