using HuizhongLibrary.Log; using System; using System.Collections.Generic; using System.Net.Sockets; namespace HuizhongLibrary.Network { /// /// Pools SocketAsyncEventArgs objects to avoid repeated allocations. /// public class SocketArgsPool : IDisposable { Queue argsPool; public SocketArgsPool(Int32 capacity) { argsPool = new Queue(capacity); } public SocketArgsPool() { argsPool = new Queue(); } #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 } }