using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; namespace HuizhongLibrary.Network { #region 缓存管理BufferPool public class BufferPool { int m_numBytes; // 未知 byte[] m_buffer; // Stack m_freeIndexPool; // int m_currentIndex;       //未知    int m_bufferSize; //缓存大小 public BufferPool(int totalBytes, int bufferSize) { m_numBytes = totalBytes; m_currentIndex = 0; m_bufferSize = bufferSize; m_freeIndexPool = new Stack(); } public void InitBuffer() { m_buffer = new byte[m_numBytes]; } public bool SetBuffer(SocketAsyncEventArgs args) { if (m_freeIndexPool.Count > 0) { args.SetBuffer(m_buffer, m_freeIndexPool.Pop(), m_bufferSize); } else { if ((m_numBytes - m_bufferSize) < m_currentIndex) { return false; } args.SetBuffer(m_buffer, m_currentIndex, m_bufferSize); m_currentIndex += m_bufferSize; } return true; } public void FreeBuffer(SocketAsyncEventArgs args) { m_freeIndexPool.Push(args.Offset); args.SetBuffer(null, 0, 0); } } #endregion }