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.

101 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HuizhongLibrary.Log;
namespace HuizhongLibrary.Network
{
public class ServerConnectionPool
{
Queue<ServerConnection> argsPool;
public ServerConnectionPool()
{
argsPool = new Queue<ServerConnection>();
}
#region 新增
public void CheckIn(ServerConnection item)
{
lock (argsPool)
{
try
{
argsPool.Enqueue(item);
}
catch (Exception ex)
{
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
}
}
}
#endregion
#region 移除并返回开始对象
public ServerConnection CheckOut()
{
lock (argsPool)
{
try
{
if (argsPool.Count == 0) return null;
return argsPool.Dequeue();
}
catch (Exception ex)
{
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
}
}
return null;
}
#endregion
#region 返回开始对象,但不移除
public ServerConnection Peek()
{
lock (argsPool)
{
try
{
if (argsPool.Count == 0) return null;
return argsPool.Peek();
}
catch (Exception ex)
{
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
}
}
return null;
}
#endregion
#region 移除
public void Clear()
{
lock (argsPool)
{
try
{
argsPool.Clear();
}
catch (Exception ex)
{
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
}
}
}
#endregion
#region 返回数量
public int Available
{
get
{
lock (argsPool)
{
return argsPool.Count;
}
}
}
#endregion
}
}