You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System.Collections.Generic;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Sog
|
|
{
|
|
class BufferManager
|
|
{
|
|
int capacity;
|
|
byte[] bufferBlock;
|
|
Stack<int> freeIndexPool;
|
|
int currentIndex;
|
|
int saeaSize;
|
|
|
|
public BufferManager(int capacity, int saeaSize)
|
|
{
|
|
this.capacity = capacity;
|
|
this.saeaSize = saeaSize;
|
|
this.freeIndexPool = new Stack<int>();
|
|
}
|
|
|
|
internal void InitBuffer()
|
|
{
|
|
this.bufferBlock = new byte[capacity];
|
|
}
|
|
|
|
internal bool SetBuffer(SocketAsyncEventArgs args)
|
|
{
|
|
if (this.freeIndexPool.Count > 0)
|
|
{
|
|
args.SetBuffer(this.bufferBlock, this.freeIndexPool.Pop(), this.saeaSize);
|
|
}
|
|
else
|
|
{
|
|
if ((capacity - this.saeaSize) < this.currentIndex)
|
|
{
|
|
return false;
|
|
}
|
|
args.SetBuffer(this.bufferBlock, this.currentIndex, this.saeaSize);
|
|
this.currentIndex += this.saeaSize;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
internal void FreeBuffer(SocketAsyncEventArgs args)
|
|
{
|
|
this.freeIndexPool.Push(args.Offset);
|
|
args.SetBuffer(null, 0, 0);
|
|
}
|
|
}
|
|
}
|