SocketAsyncEventArgs.SetBuffer SocketAsyncEventArgs.SetBuffer SocketAsyncEventArgs.SetBuffer SocketAsyncEventArgs.SetBuffer Method

定义

初始化要用于异步套接字方法的数据缓冲区。Initializes the data buffer to use with an asynchronous socket method.

重载

SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>)
SetBuffer(Int32, Int32) SetBuffer(Int32, Int32) SetBuffer(Int32, Int32) SetBuffer(Int32, Int32)

设置要用于异步套接字方法的数据缓冲区。Sets the data buffer to use with an asynchronous socket method.

SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32)

设置要用于异步套接字方法的数据缓冲区。Sets the data buffer to use with an asynchronous socket method.

注解

此方法设置Buffer属性设置为 null,CountOffset属性为零。This method sets the Buffer property to null and the Count and Offset properties to zero.

SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>) SetBuffer(Memory<Byte>)

public:
 void SetBuffer(Memory<System::Byte> buffer);
public void SetBuffer (Memory<byte> buffer);
member this.SetBuffer : Memory<byte> -> unit
Public Sub SetBuffer (buffer As Memory(Of Byte))

参数

buffer
Memory<Byte>

SetBuffer(Int32, Int32) SetBuffer(Int32, Int32) SetBuffer(Int32, Int32) SetBuffer(Int32, Int32)

设置要用于异步套接字方法的数据缓冲区。Sets the data buffer to use with an asynchronous socket method.

public:
 void SetBuffer(int offset, int count);
public void SetBuffer (int offset, int count);
member this.SetBuffer : int * int -> unit
Public Sub SetBuffer (offset As Integer, count As Integer)

参数

offset
Int32 Int32 Int32 Int32

数据缓冲区中操作开始位置处的偏移量,以字节为单位。The offset, in bytes, in the data buffer where the operation starts.

count
Int32 Int32 Int32 Int32

可在缓冲区中发送或接收的最大数据量(以字节为单位)。The maximum amount of data, in bytes, to send or receive in the buffer.

异常

参数超出范围。An argument was out of range. 如果 offset 参数小于零或大于 Buffer 属性中的数组长度,将发生此异常。This exception occurs if the offset parameter is less than zero or greater than the length of the array in the Buffer property. 如果 count 参数小于零或大于 Buffer 属性中的数组长度减去 offset 参数的值,也会发生此异常。This exception also occurs if the count parameter is less than zero or greater than the length of the array in the Buffer property minus the offset parameter.

注解

offsetcount参数不能为负数。The offset and count parameters can't be negative numbers. 组合offsetcount参数必须是中的缓冲区数组的界限内Buffer属性。The combination of the offset and count parameters must be in bounds of the buffer array in the Buffer property.

此方法设置Count属性设置为count参数和Offset属性设置为offset参数。This method sets the Count property to the count parameter and the Offset property to the offset parameter. 如果Buffer属性为 null,则此方法将忽略offsetcount参数和集OffsetCount属性设置为 0。If the Buffer property is null, this method ignores the offset and count parameters and sets the Offset and Count properties to 0.

此方法不会更改Buffer属性。This method does not change the Buffer property.

另请参阅

SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32) SetBuffer(Byte[], Int32, Int32)

设置要用于异步套接字方法的数据缓冲区。Sets the data buffer to use with an asynchronous socket method.

public:
 void SetBuffer(cli::array <System::Byte> ^ buffer, int offset, int count);
public void SetBuffer (byte[] buffer, int offset, int count);
member this.SetBuffer : byte[] * int * int -> unit
Public Sub SetBuffer (buffer As Byte(), offset As Integer, count As Integer)

参数

buffer
Byte[]

要用于异步套接字方法的数据缓冲区。The data buffer to use with an asynchronous socket method.

offset
Int32 Int32 Int32 Int32

数据缓冲区中操作开始位置处的偏移量,以字节为单位。The offset, in bytes, in the data buffer where the operation starts.

count
Int32 Int32 Int32 Int32

可在缓冲区中发送或接收的最大数据量(以字节为单位)。The maximum amount of data, in bytes, to send or receive in the buffer.

异常

指定的缓冲区不明确。There are ambiguous buffers specified. 如果 Buffer 属性不为 null,BufferList 属性也不为 null,将发生此异常。This exception occurs if the Buffer property is also not null and the BufferList property is also not null.

参数超出范围。An argument was out of range. 如果 offset 参数小于零或大于 Buffer 属性中的数组长度,将发生此异常。This exception occurs if the offset parameter is less than zero or greater than the length of the array in the Buffer property. 如果 count 参数小于零或大于 Buffer 属性中的数组长度减去 offset 参数的值,也会发生此异常。This exception also occurs if the count parameter is less than zero or greater than the length of the array in the Buffer property minus the offset parameter.

示例

下面的代码示例创建一个大型缓冲区可以划分和分配给 [SetBuffer 方法 (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs > 对象以用于每个套接字 I/O 操作。The following code example creates a single large buffer which can be divided up and assigned to [SetBuffer Method (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs> objects for use with each socket I/O operation. 这使缓冲区可以轻松地重复使用,防止出现堆内存碎片化。This enables buffers to be easily reused and guards against fragmenting heap memory.

// 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);
    }

}

注解

offsetcount参数不能为负数。The offset and count parameters can't be negative numbers. 组合offsetcount参数必须是中的数据数组的界限内buffer参数。The combination of the offset and count parameters must be in bounds of the data array in the buffer parameter.

此方法设置 [SetBuffer 方法 (字节<xref:System.Net.Sockets.SocketAsyncEventArgs.Buffer%2A > 属性设置为buffer参数,[SetBuffer 方法 (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs.Count%2A > 属性设置为count参数,和 [SetBuffer 方法 (字节<xref:System.Net.Sockets.SocketAsyncEventArgs.Offset%2A > 属性设置为offset参数。This method sets the [SetBuffer Method (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs.Buffer%2A> property to the buffer parameter, the [SetBuffer Method (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs.Count%2A> property to the count parameter, and the [SetBuffer Method (Byte<xref:System.Net.Sockets.SocketAsyncEventArgs.Offset%2A> property to the offset parameter.

另请参阅

适用于