Question

I'm using std::vector to represent a buffer in my per IO data structure:

struct PerIoData 
{
    WSAOVERLAPPED m_overlapped;
    SOCKET m_socket;
    WSABUF m_wsaBuf;
    std::vector<BYTE> m_vecBuffer;
    DWORD m_dwFlags;
    DWORD m_dwNumberOfBytesSent;
    DWORD m_dwNumberOfBytesToSend;
    eOperationType m_operationType;
};

...
...
if (WSARecv(pPerIoData->m_socket, &(pPerIoData->m_wsaBuf), 1, &dwNumberOfBytesReceived, &(pPerIoData->m_dwFlags), &(pPerIoData->m_overlapped), NULL) == 0)
    continue;

I want to specify the maximum size and perform "shrink to fit" on m_vecBuffer after calling WSARecv().

I've had a look at this page but been unable to find the information I'm looking for.

Was it helpful?

Solution

The WSABUF struct that you pass to WSARecv() specifies a pointer to a buffer that WSARecv() reads the bytes into, and the maximum number of bytes that can be read into that buffer. When WSARecv() completes its work, it reports how many bytes were actually read into the buffer. If you use the vector as the buffer, you have to pre-allocate the vector to whatever max size you want, then set WSABUF::buf to point at the vector's internal memory, set WSABUF::len to the vector's allocated size, and then resize the vector to the new value that WSARecv() reports.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top