引入ArrayPool<T>,优化协议内存拷贝
This commit is contained in:
parent
665ef09bf7
commit
c1a3650a95
@ -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)
|
||||
{
|
||||
|
35
NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs
Normal file
35
NetLib/HaoYueNet.ServerNetwork/NetWork/ArrayPoolManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
//从数据池中移除这组数据
|
||||
|
Loading…
Reference in New Issue
Block a user