SocketAsyncEventArgs.SetBuffer 메서드

정의

비동기 소켓 메서드에 사용할 데이터 버퍼를 초기화합니다.

오버로드

SetBuffer(Memory<Byte>)

비동기 소켓 메서드를 사용하여 버퍼로 사용할 메모리 영역을 설정합니다.

SetBuffer(Int32, Int32)

비동기 소켓 메서드에 사용할 데이터 버퍼를 설정합니다.

SetBuffer(Byte[], Int32, Int32)

비동기 소켓 메서드에 사용할 데이터 버퍼를 설정합니다.

설명

이 메서드는 Buffer 속성을 null Count 로 설정하고 및 Offset 속성을 0으로 설정합니다.

SetBuffer(Memory<Byte>)

Source:
SocketAsyncEventArgs.cs
Source:
SocketAsyncEventArgs.cs
Source:
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 속성을 0으로 설정합니다.

적용 대상

SetBuffer(Int32, Int32)

Source:
SocketAsyncEventArgs.cs
Source:
SocketAsyncEventArgs.cs
Source:
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 매개 변수가 0보다 작거나 Buffer 속성에 지정된 배열 길이보다 크면 이 예외가 발생합니다. 또한 count 매개 변수가 0보다 작거나, Buffer 속성에 지정된 배열 길이에서 offset 매개 변수를 뺀 값보다 큰 경우에도 이 예외가 발생합니다.

설명

count 매개 변수는 offset 음수일 수 없습니다. 및 count 매개 변수의 offset 조합은 속성의 버퍼 배열 경계에 Buffer 있어야 합니다.

이 메서드는 Count 속성을 매개 변수로 count 설정하고 속성을 매개 Offset 변수로 offset 설정합니다. 속성이 Buffer null이면 이 메서드는 및 count 매개 변수를 offset 무시하고 및 Count 속성을 0으로 설정합니다Offset.

이 메서드는 속성을 변경하지 Buffer 않습니다.

추가 정보

적용 대상

SetBuffer(Byte[], Int32, Int32)

Source:
SocketAsyncEventArgs.cs
Source:
SocketAsyncEventArgs.cs
Source:
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 속성도 null이 아니고 BufferList 속성도 null이 아니면 이 예외가 발생합니다.

인수가 범위를 벗어난 경우. offset 매개 변수가 0보다 작거나 Buffer 속성에 지정된 배열 길이보다 크면 이 예외가 발생합니다. 또한 count 매개 변수가 0보다 작거나, Buffer 속성에 지정된 배열 길이에서 offset 매개 변수를 뺀 값보다 큰 경우에도 이 예외가 발생합니다.

예제

다음 코드 예제에서는 각 소켓 I/O 작업에 사용할 개체에 SocketAsyncEventArgs 분할 및 할당할 수 있는 단일 큰 버퍼를 만듭니다. 이렇게 하면 버퍼를 쉽게 재사용하고 조각화된 힙 메모리를 방어할 수 있습니다.

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

설명

count 매개 변수는 offset 음수일 수 없습니다. 및 count 매개 변수의 offset 조합은 매개 변수의 데이터 배열 buffer 범위에 있어야 합니다.

이 메서드는 Buffer 속성을 buffer 매개 변수로 Count , 속성을 매개 변수로 count , Offset 속성을 매개 변수로 offset 설정합니다.

추가 정보

적용 대상