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.
|
|
|
|
using HuizhongLibrary.Log;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
|
|
|
|
namespace HuizhongLibrary.Network
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pools SocketAsyncEventArgs objects to avoid repeated allocations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SocketArgsPool : IDisposable
|
|
|
|
|
{
|
|
|
|
|
Queue<SocketAsyncEventArgs> argsPool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SocketArgsPool(Int32 capacity)
|
|
|
|
|
{
|
|
|
|
|
argsPool = new Queue<SocketAsyncEventArgs>(capacity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SocketArgsPool()
|
|
|
|
|
{
|
|
|
|
|
argsPool = new Queue<SocketAsyncEventArgs>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 新增
|
|
|
|
|
public void CheckIn(SocketAsyncEventArgs item)
|
|
|
|
|
{
|
|
|
|
|
lock (argsPool)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
argsPool.Enqueue(item);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region 移除并返回开始对象
|
|
|
|
|
public SocketAsyncEventArgs 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 int Available
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (argsPool)
|
|
|
|
|
{
|
|
|
|
|
return argsPool.Count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region 移除
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
lock (argsPool)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
argsPool.Clear();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
private Boolean disposed = false;
|
|
|
|
|
|
|
|
|
|
~SocketArgsPool()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!this.disposed)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
foreach (SocketAsyncEventArgs args in argsPool)
|
|
|
|
|
{
|
|
|
|
|
args.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|