当前位置: 首页>>代码示例>>C#>>正文


C# SocketAsyncEventArgs.SetBuffer方法代码示例

本文整理汇总了C#中System.Net.Sockets.SocketAsyncEventArgs.SetBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs.SetBuffer方法的具体用法?C# SocketAsyncEventArgs.SetBuffer怎么用?C# SocketAsyncEventArgs.SetBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.SocketAsyncEventArgs的用法示例。


在下文中一共展示了SocketAsyncEventArgs.SetBuffer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BufferManager

// This class creates a single large buffer which can be divided up 
// and assigned to SocketAsyncEventArgs objects for use with each 
// socket I/O operation.  
// This enables bufffers to be easily reused and guards against 
// fragmenting heap memory.
// 
// The operations exposed on the BufferManager class are not thread safe.
class BufferManager
{
    int m_numBytes;                 // the total number of bytes controlled by the buffer pool
    byte[] m_buffer;                // the underlying byte array maintained by the Buffer Manager
    Stack<int> m_freeIndexPool;     // 
    int m_currentIndex;
    int m_bufferSize;

    public BufferManager(int totalBytes, int bufferSize)
    {
        m_numBytes = totalBytes;
        m_currentIndex = 0;
        m_bufferSize = bufferSize;
        m_freeIndexPool = new Stack<int>();
    }

    // Allocates buffer space used by the buffer pool
    public void InitBuffer()
    {
        // create one big large buffer and divide that 
        // out to each SocketAsyncEventArg object
        m_buffer = new byte[m_numBytes];
    }

    // Assigns a buffer from the buffer pool to the 
    // specified SocketAsyncEventArgs object
    //
    // <returns>true if the buffer was successfully set, else false</returns>
    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;
    }

    // Removes the buffer from a SocketAsyncEventArg object.  
    // This frees the buffer back to the buffer pool
    public void FreeBuffer(SocketAsyncEventArgs args)
    {
        m_freeIndexPool.Push(args.Offset);
        args.SetBuffer(null, 0, 0);
    }
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:62,代码来源:SocketAsyncEventArgs.SetBuffer


注:本文中的System.Net.Sockets.SocketAsyncEventArgs.SetBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。