引入ArrayPool<T>,优化协议内存拷贝

This commit is contained in:
sin365 2024-06-27 02:04:12 +08:00
parent 665ef09bf7
commit c1a3650a95
3 changed files with 61 additions and 10 deletions

View File

@ -45,6 +45,7 @@ namespace HaoYueNet.ServerNetwork
int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
byte[] BufferData = new byte[AllLenght]; byte[] BufferData = new byte[AllLenght];
/*
//包长度 //包长度
writeInt(BufferData, 0, AllLenght); writeInt(BufferData, 0, AllLenght);
@ -53,7 +54,15 @@ namespace HaoYueNet.ServerNetwork
//Error //Error
writeUInt16(BufferData, 4 + 2, Error); writeUInt16(BufferData, 4 + 2, Error);
*/
//包长度
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 //DATA
Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length);

View File

@ -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<byte> instance = ArrayPool<byte>.Shared;
/// <summary>
/// 租用指定大小byte数组
/// </summary>
/// <param name="lenght"></param>
/// <returns></returns>
public static byte[] RentByteArr(int lenght)
{
return instance.Rent(lenght);
}
/// <summary>
/// 将数组归还给池
/// </summary>
/// <param name="lenght"></param>
/// <returns></returns>
public static void ReturnByteArr(byte[] byteArr)
{
instance.Return(byteArr);
}
}
}

View File

@ -1,4 +1,5 @@
//using HunterProtobufCore; //using HunterProtobufCore;
using HaoYueNet.ServerNetwork.NetWork;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -402,10 +403,15 @@ namespace HaoYueNet.ServerNetwork
//} //}
long FristBeginPos = token.memoryStream.Position; long FristBeginPos = token.memoryStream.Position;
byte[] lenBytes = new byte[4]; //byte[] lenBytes = new byte[4];
byte[] lenBytes = ArrayPoolManager.RentByteArr(4);
token.memoryStream.Seek(0, SeekOrigin.Begin); token.memoryStream.Seek(0, SeekOrigin.Begin);
token.memoryStream.Read(lenBytes, 0, 4); token.memoryStream.Read(lenBytes, 0, 4);
int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4;
ArrayPoolManager.ReturnByteArr(lenBytes);
if (packageLen > token.memoryStream.Length - 4) if (packageLen > token.memoryStream.Length - 4)
{ {
token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin);
@ -417,6 +423,7 @@ namespace HaoYueNet.ServerNetwork
//byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray();
byte[] rev = new byte[packageLen]; byte[] rev = new byte[packageLen];
token.memoryStream.Seek(4, SeekOrigin.Begin); token.memoryStream.Seek(4, SeekOrigin.Begin);
token.memoryStream.Read(rev, 0, packageLen); token.memoryStream.Read(rev, 0, packageLen);