Skip to content

Commit

Permalink
LibCore: Add helper to convert Bytes/ReadonlyBytes to WSABUF
Browse files Browse the repository at this point in the history
  • Loading branch information
ADKaster committed Feb 11, 2025
1 parent 25f00b2 commit 0dd23e4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Libraries/LibCore/SocketWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@

namespace Core {

static WSABUF make_wsa_buf(ReadonlyBytes bytes)
{
WSABUF buffer {};
buffer.buf = reinterpret_cast<CHAR*>(const_cast<u8*>(bytes.data()));
buffer.len = static_cast<ULONG>(bytes.size());
return buffer;
}

ErrorOr<Bytes> PosixSocketHelper::read(Bytes buffer, int flags)
{
if (!is_open())
return Error::from_errno(ENOTCONN);

// FIXME: also take into account if PosixSocketHelper is blocking/non-blocking (see set_blocking)
bool blocking = !(flags & MSG_DONTWAIT);
WSABUF buf = { static_cast<ULONG>(buffer.size()), reinterpret_cast<CHAR*>(buffer.data()) };
WSABUF buf = make_wsa_buf(buffer);
DWORD nread = 0;
DWORD fl = 0;
OVERLAPPED ov = {};
Expand Down Expand Up @@ -66,7 +74,7 @@ ErrorOr<size_t> PosixSocketHelper::write(ReadonlyBytes buffer, int flags)

// FIXME: Implement non-blocking PosixSocketHelper::write
(void)flags;
WSABUF buf = { static_cast<ULONG>(buffer.size()), reinterpret_cast<CHAR*>(const_cast<u8*>(buffer.data())) };
WSABUF buf = make_wsa_buf(buffer);
DWORD nwritten = 0;

if (WSASend(m_fd, &buf, 1, &nwritten, 0, NULL, NULL) == SOCKET_ERROR)
Expand Down

0 comments on commit 0dd23e4

Please sign in to comment.