SocketAsyncEventArgs.SetBuffer 方法

定義

初始化要和非同步通訊端方法一起使用的資料緩衝區。

多載

SetBuffer(Memory<Byte>)

設定使用非同步通訊端方法作為緩衝區使用的記憶體區域。

SetBuffer(Int32, Int32)

設定要和非同步通訊端方法一起使用的資料緩衝區。

SetBuffer(Byte[], Int32, Int32)

設定要和非同步通訊端方法一起使用的資料緩衝區。

備註

這個方法會將 Buffer 屬性設定為 null, Count 並將 和 Offset 屬性設定為零。

SetBuffer(Memory<Byte>)

來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs

設定使用非同步通訊端方法作為緩衝區使用的記憶體區域。

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>

使用非同步通訊端方法作為緩衝區使用的記憶體區域。

備註

這個方法會將 MemoryBuffer 屬性設定為 buffer 參數、 Count 屬性設定為 buffer 長度,並將 Offset 屬性設定為零。

適用於

SetBuffer(Int32, Int32)

來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs

設定要和非同步通訊端方法一起使用的資料緩衝區。

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

作業開始的資料緩衝區位移 (以位元組為單位)。

count
Int32

緩衝區中要傳送或接收的資料量上限 (以位元組為單位)。

例外狀況

引數超出範圍。 如果 offset 參數小於零或大於 Buffer 屬性中的陣列長度,就會發生這個例外狀況。 如果 count 參數小於零或大於 Buffer 屬性中的陣列長度減去 offset 參數,也會發生這個例外狀況。

備註

offsetcount 參數不能是負數。 和參數的組合offset必須位於 屬性中緩衝區陣列的界限中Buffercount

這個方法會將 Count 屬性設定為 count 參數,並將 Offset 屬性設定為 offset 參數。 Buffer如果屬性為 Null,這個方法會offset忽略 和 count 參數,並將 和 Count 屬性設定Offset為 0。

這個方法不會變更 Buffer 屬性。

另請參閱

適用於

SetBuffer(Byte[], Int32, Int32)

來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs
來源:
SocketAsyncEventArgs.cs

設定要和非同步通訊端方法一起使用的資料緩衝區。

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)

參數

buffer
Byte[]

要和非同步通訊端方法一起使用的資料緩衝區。

offset
Int32

作業開始的資料緩衝區位移 (以位元組為單位)。

count
Int32

緩衝區中要傳送或接收的資料量上限 (以位元組為單位)。

例外狀況

指定了不明確的緩衝區。 如果 Buffer 屬性和 BufferList 屬性都不是 null,就會發生這個例外狀況。

引數超出範圍。 如果 offset 參數小於零或大於 Buffer 屬性中的陣列長度,就會發生這個例外狀況。 如果 count 參數小於零或大於 Buffer 屬性中的陣列長度減去 offset 參數,也會發生這個例外狀況。

範例

下列程式代碼範例會建立單一大型緩衝區,可分割並指派給物件, SocketAsyncEventArgs 以便與每個套接字 I/O 作業搭配使用。 這可讓緩衝區輕鬆重複使用,並防止分散堆積記憶體。

// 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 參數不能是負數。 和參數的組合offset必須位於 參數中數據數位的界限中buffercount

這個方法會將 Buffer 屬性設定為 buffer 參數、 Count 將 屬性 count 設定為 參數,並將 Offset 屬性設定為 offset 參數。

另請參閱

適用於