提交 6e9a9a75 编写于 作者: T tanghai

Packet中MemoryStream存在一个问题,SetLength会将内容清空成0,这个操作没有必要,消息内容如果很长会影响性能。使用Recyclabl...

Packet中MemoryStream存在一个问题,SetLength会将内容清空成0,这个操作没有必要,消息内容如果很长会影响性能。使用RecyclableMemoryStream代替MemoryStream
上级 08003826
......@@ -119,6 +119,7 @@ namespace ETModel
this.kcp = IntPtr.Zero;
}
this.socket = null;
this.packet.Dispose();
}
public override MemoryStream Stream
......
using System;
using System.IO;
using Microsoft.IO;
namespace ETModel
{
......@@ -9,7 +10,7 @@ namespace ETModel
PacketBody
}
public class Packet
public class Packet: IDisposable
{
public const int SizeLength = 2;
public const int MinSize = 3;
......@@ -17,6 +18,8 @@ namespace ETModel
public const int FlagIndex = 0;
public const int OpcodeIndex = 1;
public const int MessageIndex = 3;
public static RecyclableMemoryStreamManager memoryStreamManager = new RecyclableMemoryStreamManager();
/// <summary>
/// 只读,不允许修改
......@@ -29,25 +32,31 @@ namespace ETModel
}
}
public MemoryStream Stream { get; }
public MemoryStream Stream { get; private set; }
public Packet(int length)
{
this.Stream = new MemoryStream(length);
this.Stream = memoryStreamManager.GetStream("Packet", length);
}
public Packet(byte[] bytes)
public void Dispose()
{
this.Stream = new MemoryStream(bytes);
if (this.Stream == null)
{
return;
}
this.Stream.Close();
this.Stream = null;
}
}
public class PacketParser
public class PacketParser: IDisposable
{
private readonly CircularBuffer buffer;
private ushort packetSize;
private ParserState state;
public readonly Packet packet = new Packet(ushort.MaxValue);
public Packet packet = new Packet(ushort.MaxValue);
private bool isOK;
public PacketParser(CircularBuffer buffer)
......@@ -109,5 +118,15 @@ namespace ETModel
this.isOK = false;
return this.packet;
}
public void Dispose()
{
if (this.packet == null)
{
return;
}
this.packet?.Dispose();
this.packet = null;
}
}
}
\ No newline at end of file
......@@ -71,6 +71,7 @@ namespace ETModel
this.socket.Close();
this.innArgs.Dispose();
this.outArgs.Dispose();
this.parser.Dispose();
this.innArgs = null;
this.outArgs = null;
this.socket = null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册