You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Mesnac.Communication
|
|
{
|
|
public sealed class BufferManager : IDisposable
|
|
{
|
|
private Byte[] buffer;
|
|
private Int32 bufferSize;
|
|
private Int32 numSize;
|
|
private Int32 currentIndex;
|
|
private Stack<Int32> freeIndexPool;
|
|
|
|
public BufferManager(Int32 numSize, Int32 bufferSize)
|
|
{
|
|
this.bufferSize = bufferSize;
|
|
this.numSize = numSize;
|
|
this.currentIndex = 0;
|
|
this.freeIndexPool = new Stack<Int32>();
|
|
}
|
|
|
|
public void FreeBuffer(SocketAsyncEventArgs args)
|
|
{
|
|
this.freeIndexPool.Push(args.Offset);
|
|
args.SetBuffer(null, 0, 0);
|
|
}
|
|
|
|
public void InitBuffer()
|
|
{
|
|
this.buffer = new Byte[this.numSize];
|
|
}
|
|
|
|
public Boolean SetBuffer(SocketAsyncEventArgs args)
|
|
{
|
|
if (this.freeIndexPool.Count > 0)
|
|
{
|
|
args.SetBuffer(this.buffer, this.freeIndexPool.Pop(), this.bufferSize);
|
|
}
|
|
else
|
|
{
|
|
if ((this.numSize - this.bufferSize) < this.currentIndex)
|
|
{
|
|
return false;
|
|
}
|
|
args.SetBuffer(this.buffer, this.currentIndex, this.bufferSize);
|
|
this.currentIndex += this.bufferSize;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
buffer = null;
|
|
freeIndexPool = null;
|
|
}
|
|
}
|
|
}
|