HRESULT Read(SOCKET socket, MemoryBuffer& buffer) { const size_t position = buffer.GetPosition(); const size_t size = buffer.GetSize(); int length = static_cast<int>(size - position); char* data = reinterpret_cast<char*>(buffer.GetData()) + position; const int status = recv(socket, data, length, 0); if (status <= 0) { const DWORD code = WSAGetLastError(); const HRESULT result = (WSAEWOULDBLOCK == code) ? S_FALSE : E_FAIL; return result; } length -= status; buffer.SetPosition(position + status); if (length > 0) { return S_FALSE; } return S_OK; }
HRESULT Write(SOCKET socket, MemoryBuffer& buffer) { const size_t position = buffer.GetPosition(); const size_t size = buffer.GetSize(); int length = static_cast<int>(size - position); const char* data = reinterpret_cast<const char*>(buffer.GetData()) + position; const int status = send(socket, data, length, 0); if (status < 0) { return E_FAIL; } length -= status; buffer.SetPosition(position + status); if (length > 0) { return S_FALSE; } return S_OK; }