Skip to content

Commit

Permalink
net/SocketDescriptor: move code to Receive(), Send()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 27, 2023
1 parent 09a2da8 commit 8ca6606
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/net/SocketDescriptor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,30 @@ SocketDescriptor::GetPeerAddress() const noexcept
}

ssize_t
SocketDescriptor::Read(void *buffer, std::size_t length) const noexcept
SocketDescriptor::Receive(std::span<std::byte> dest, int flags) const noexcept
{
int flags = 0;
#ifndef _WIN32
flags |= MSG_DONTWAIT;
return ::recv(Get(), (char *)dest.data(), dest.size(), flags);
}

ssize_t
SocketDescriptor::Send(std::span<const std::byte> src, int flags) const noexcept
{
#ifdef __linux__
flags |= MSG_NOSIGNAL;
#endif

return ::recv(Get(), (char *)buffer, length, flags);
return ::send(Get(), (const char *)src.data(), src.size(), flags);
}

ssize_t
SocketDescriptor::Write(const void *buffer, std::size_t length) const noexcept
SocketDescriptor::Read(void *buffer, std::size_t length) const noexcept
{
int flags = 0;
#ifdef __linux__
flags |= MSG_NOSIGNAL;
#ifndef _WIN32
flags |= MSG_DONTWAIT;
#endif

return ::send(Get(), (const char *)buffer, length, flags);
return Receive({static_cast<std::byte *>(buffer), length}, flags);
}

#ifdef _WIN32
Expand Down
19 changes: 18 additions & 1 deletion src/net/SocketDescriptor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "io/FileDescriptor.hxx"
#endif

#include <cstddef>
#include <span>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -260,8 +262,23 @@ public:
[[gnu::pure]]
StaticSocketAddress GetPeerAddress() const noexcept;

/**
* Wrapper for recv().
*/
ssize_t Receive(std::span<std::byte> dest, int flags=0) const noexcept;

/**
* Wrapper for send().
*
* MSG_NOSIGNAL is implicitly added (if available).
*/
ssize_t Send(std::span<const std::byte> src, int flags=0) const noexcept;

ssize_t Read(void *buffer, std::size_t length) const noexcept;
ssize_t Write(const void *buffer, std::size_t length) const noexcept;

ssize_t Write(const void *buffer, std::size_t length) const noexcept {
return Send({static_cast<const std::byte *>(buffer), length});
}

#ifdef _WIN32
int WaitReadable(int timeout_ms) const noexcept;
Expand Down

0 comments on commit 8ca6606

Please sign in to comment.