新增TCP扩展
This commit is contained in:
1 parent
514d6f5255
commit
f119665d29
26 files changed
+2457
-34
No files matched your search
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user