SocketAsyncEventArgs.SetBuffer Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes the data buffer to use with an asynchronous socket method.
Overloads
SetBuffer(Memory<Byte>) |
Sets the region of memory to use as a buffer with an asynchronous socket method. |
SetBuffer(Int32, Int32) |
Sets the data buffer to use with an asynchronous socket method. |
SetBuffer(Byte[], Int32, Int32) |
Sets the data buffer to use with an asynchronous socket method. |
Remarks
This method sets the Buffer property to null and the Count and Offset properties to zero.
SetBuffer(Memory<Byte>)
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
Sets the region of memory to use as a buffer with an asynchronous socket method.
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))
Parameters
Remarks
This method sets the MemoryBuffer property to the buffer
parameter, the Count property to the buffer
length, and the Offset property to zero.
Applies to
SetBuffer(Int32, Int32)
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
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)
Parameters
- offset
- Int32
The offset, in bytes, in the data buffer where the operation starts.
- count
- Int32
The maximum amount of data, in bytes, to send or receive in the buffer.
Exceptions
An argument was out of range. This exception occurs if the offset
parameter is less than zero or greater than the length of the array in the Buffer property. 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.
Remarks
The offset
and count
parameters can't be negative numbers. The combination of the offset
and count
parameters must be in bounds of the buffer array in the Buffer property.
This method sets the Count property to the count
parameter and the Offset property to the offset
parameter. If the Buffer property is null, this method ignores the offset
and count
parameters and sets the Offset and Count properties to 0.
This method does not change the Buffer property.
See also
Applies to
SetBuffer(Byte[], Int32, Int32)
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
- Source:
- SocketAsyncEventArgs.cs
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);
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)
Parameters
- buffer
- Byte[]
The data buffer to use with an asynchronous socket method.
- offset
- Int32
The offset, in bytes, in the data buffer where the operation starts.
- count
- Int32
The maximum amount of data, in bytes, to send or receive in the buffer.
Exceptions
There are ambiguous buffers specified. 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. This exception occurs if the offset
parameter is less than zero or greater than the length of the array in the Buffer property. 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.
Examples
The following code example 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 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);
}
}
Remarks
The offset
and count
parameters can't be negative numbers. The combination of the offset
and count
parameters must be in bounds of the data array in the buffer
parameter.
This method sets the Buffer property to the buffer
parameter, the Count property to the count
parameter, and the Offset property to the offset
parameter.