From 7c1bf68bb411b5f20fc0fd93306127032340ef60 Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Sun, 25 Dec 2022 14:29:53 -0500 Subject: [PATCH] bsd: Use std::span for read payloads Allows the use of HLERequestContext::ReadBufferSpan --- src/core/hle/service/sockets/bsd.cpp | 29 +++++++++++----------- src/core/hle/service/sockets/bsd.h | 23 +++++++++-------- src/core/internal_network/network.cpp | 4 +-- src/core/internal_network/socket_proxy.cpp | 4 +-- src/core/internal_network/socket_proxy.h | 5 ++-- src/core/internal_network/sockets.h | 9 ++++--- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index 9e94a462f..119d6ba5b 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -186,7 +186,7 @@ void BSD::Poll(Kernel::HLERequestContext& ctx) { ExecuteWork(ctx, PollWork{ .nfds = nfds, .timeout = timeout, - .read_buffer = ctx.ReadBuffer(), + .read_buffer = ctx.ReadBufferSpan(), .write_buffer = std::vector(ctx.GetWriteBufferSize()), }); } @@ -208,8 +208,7 @@ void BSD::Bind(Kernel::HLERequestContext& ctx) { const s32 fd = rp.Pop(); LOG_DEBUG(Service, "called. fd={} addrlen={}", fd, ctx.GetReadBufferSize()); - - BuildErrnoResponse(ctx, BindImpl(fd, ctx.ReadBuffer())); + BuildErrnoResponse(ctx, BindImpl(fd, ctx.ReadBufferSpan())); } void BSD::Connect(Kernel::HLERequestContext& ctx) { @@ -220,7 +219,7 @@ void BSD::Connect(Kernel::HLERequestContext& ctx) { ExecuteWork(ctx, ConnectWork{ .fd = fd, - .addr = ctx.ReadBuffer(), + .addr = ctx.ReadBufferSpan(), }); } @@ -312,7 +311,7 @@ void BSD::SetSockOpt(Kernel::HLERequestContext& ctx) { const u32 level = rp.Pop(); const OptName optname = static_cast(rp.Pop()); - const std::vector buffer = ctx.ReadBuffer(); + const auto buffer = ctx.ReadBufferSpan(); const u8* optval = buffer.empty() ? nullptr : buffer.data(); size_t optlen = buffer.size(); @@ -383,7 +382,7 @@ void BSD::Send(Kernel::HLERequestContext& ctx) { ExecuteWork(ctx, SendWork{ .fd = fd, .flags = flags, - .message = ctx.ReadBuffer(), + .message = ctx.ReadBufferSpan(), }); } @@ -398,8 +397,8 @@ void BSD::SendTo(Kernel::HLERequestContext& ctx) { ExecuteWork(ctx, SendToWork{ .fd = fd, .flags = flags, - .message = ctx.ReadBuffer(0), - .addr = ctx.ReadBuffer(1), + .message = ctx.ReadBufferSpan(0), + .addr = ctx.ReadBufferSpan(1), }); } @@ -412,7 +411,7 @@ void BSD::Write(Kernel::HLERequestContext& ctx) { ExecuteWork(ctx, SendWork{ .fd = fd, .flags = 0, - .message = ctx.ReadBuffer(), + .message = ctx.ReadBufferSpan(), }); } @@ -489,7 +488,7 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco return {fd, Errno::SUCCESS}; } -std::pair BSD::PollImpl(std::vector& write_buffer, std::vector read_buffer, +std::pair BSD::PollImpl(std::vector& write_buffer, std::span read_buffer, s32 nfds, s32 timeout) { if (write_buffer.size() < nfds * sizeof(PollFD)) { return {-1, Errno::INVAL}; @@ -584,7 +583,7 @@ std::pair BSD::AcceptImpl(s32 fd, std::vector& write_buffer) { return {new_fd, Errno::SUCCESS}; } -Errno BSD::BindImpl(s32 fd, const std::vector& addr) { +Errno BSD::BindImpl(s32 fd, std::span addr) { if (!IsFileDescriptorValid(fd)) { return Errno::BADF; } @@ -595,7 +594,7 @@ Errno BSD::BindImpl(s32 fd, const std::vector& addr) { return Translate(file_descriptors[fd]->socket->Bind(Translate(addr_in))); } -Errno BSD::ConnectImpl(s32 fd, const std::vector& addr) { +Errno BSD::ConnectImpl(s32 fd, std::span addr) { if (!IsFileDescriptorValid(fd)) { return Errno::BADF; } @@ -800,15 +799,15 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess return {ret, bsd_errno}; } -std::pair BSD::SendImpl(s32 fd, u32 flags, const std::vector& message) { +std::pair BSD::SendImpl(s32 fd, u32 flags, std::span message) { if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } return Translate(file_descriptors[fd]->socket->Send(message, flags)); } -std::pair BSD::SendToImpl(s32 fd, u32 flags, const std::vector& message, - const std::vector& addr) { +std::pair BSD::SendToImpl(s32 fd, u32 flags, std::span message, + std::span addr) { if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } diff --git a/src/core/hle/service/sockets/bsd.h b/src/core/hle/service/sockets/bsd.h index 81e855e0f..56bb3f8b1 100644 --- a/src/core/hle/service/sockets/bsd.h +++ b/src/core/hle/service/sockets/bsd.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include "common/common_types.h" @@ -44,7 +45,7 @@ private: s32 nfds; s32 timeout; - std::vector read_buffer; + std::span read_buffer; std::vector write_buffer; s32 ret{}; Errno bsd_errno{}; @@ -65,7 +66,7 @@ private: void Response(Kernel::HLERequestContext& ctx); s32 fd; - std::vector addr; + std::span addr; Errno bsd_errno{}; }; @@ -98,7 +99,7 @@ private: s32 fd; u32 flags; - std::vector message; + std::span message; s32 ret{}; Errno bsd_errno{}; }; @@ -109,8 +110,8 @@ private: s32 fd; u32 flags; - std::vector message; - std::vector addr; + std::span message; + std::span addr; s32 ret{}; Errno bsd_errno{}; }; @@ -143,11 +144,11 @@ private: void ExecuteWork(Kernel::HLERequestContext& ctx, Work work); std::pair SocketImpl(Domain domain, Type type, Protocol protocol); - std::pair PollImpl(std::vector& write_buffer, std::vector read_buffer, + std::pair PollImpl(std::vector& write_buffer, std::span read_buffer, s32 nfds, s32 timeout); std::pair AcceptImpl(s32 fd, std::vector& write_buffer); - Errno BindImpl(s32 fd, const std::vector& addr); - Errno ConnectImpl(s32 fd, const std::vector& addr); + Errno BindImpl(s32 fd, std::span addr); + Errno ConnectImpl(s32 fd, std::span addr); Errno GetPeerNameImpl(s32 fd, std::vector& write_buffer); Errno GetSockNameImpl(s32 fd, std::vector& write_buffer); Errno ListenImpl(s32 fd, s32 backlog); @@ -157,9 +158,9 @@ private: std::pair RecvImpl(s32 fd, u32 flags, std::vector& message); std::pair RecvFromImpl(s32 fd, u32 flags, std::vector& message, std::vector& addr); - std::pair SendImpl(s32 fd, u32 flags, const std::vector& message); - std::pair SendToImpl(s32 fd, u32 flags, const std::vector& message, - const std::vector& addr); + std::pair SendImpl(s32 fd, u32 flags, std::span message); + std::pair SendToImpl(s32 fd, u32 flags, std::span message, + std::span addr); Errno CloseImpl(s32 fd); s32 FindFreeFileDescriptorHandle() noexcept; diff --git a/src/core/internal_network/network.cpp b/src/core/internal_network/network.cpp index 447fbffaa..452212964 100644 --- a/src/core/internal_network/network.cpp +++ b/src/core/internal_network/network.cpp @@ -546,7 +546,7 @@ std::pair Socket::RecvFrom(int flags, std::vector& message, Sock return {-1, GetAndLogLastError()}; } -std::pair Socket::Send(const std::vector& message, int flags) { +std::pair Socket::Send(std::span message, int flags) { ASSERT(message.size() < static_cast(std::numeric_limits::max())); ASSERT(flags == 0); @@ -559,7 +559,7 @@ std::pair Socket::Send(const std::vector& message, int flags) { return {-1, GetAndLogLastError()}; } -std::pair Socket::SendTo(u32 flags, const std::vector& message, +std::pair Socket::SendTo(u32 flags, std::span message, const SockAddrIn* addr) { ASSERT(flags == 0); diff --git a/src/core/internal_network/socket_proxy.cpp b/src/core/internal_network/socket_proxy.cpp index 1e1c42cea..7a77171c2 100644 --- a/src/core/internal_network/socket_proxy.cpp +++ b/src/core/internal_network/socket_proxy.cpp @@ -182,7 +182,7 @@ std::pair ProxySocket::ReceivePacket(int flags, std::vector& mes return {static_cast(read_bytes), Errno::SUCCESS}; } -std::pair ProxySocket::Send(const std::vector& message, int flags) { +std::pair ProxySocket::Send(std::span message, int flags) { LOG_WARNING(Network, "(STUBBED) called"); ASSERT(message.size() < static_cast(std::numeric_limits::max())); ASSERT(flags == 0); @@ -200,7 +200,7 @@ void ProxySocket::SendPacket(ProxyPacket& packet) { } } -std::pair ProxySocket::SendTo(u32 flags, const std::vector& message, +std::pair ProxySocket::SendTo(u32 flags, std::span message, const SockAddrIn* addr) { ASSERT(flags == 0); diff --git a/src/core/internal_network/socket_proxy.h b/src/core/internal_network/socket_proxy.h index f12b5f567..9421492bc 100644 --- a/src/core/internal_network/socket_proxy.h +++ b/src/core/internal_network/socket_proxy.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -48,11 +49,11 @@ public: std::pair ReceivePacket(int flags, std::vector& message, SockAddrIn* addr, std::size_t max_length); - std::pair Send(const std::vector& message, int flags) override; + std::pair Send(std::span message, int flags) override; void SendPacket(ProxyPacket& packet); - std::pair SendTo(u32 flags, const std::vector& message, + std::pair SendTo(u32 flags, std::span message, const SockAddrIn* addr) override; Errno SetLinger(bool enable, u32 linger) override; diff --git a/src/core/internal_network/sockets.h b/src/core/internal_network/sockets.h index 2e328c645..4c7489258 100644 --- a/src/core/internal_network/sockets.h +++ b/src/core/internal_network/sockets.h @@ -5,6 +5,7 @@ #include #include +#include #include #if defined(_WIN32) @@ -66,9 +67,9 @@ public: virtual std::pair RecvFrom(int flags, std::vector& message, SockAddrIn* addr) = 0; - virtual std::pair Send(const std::vector& message, int flags) = 0; + virtual std::pair Send(std::span message, int flags) = 0; - virtual std::pair SendTo(u32 flags, const std::vector& message, + virtual std::pair SendTo(u32 flags, std::span message, const SockAddrIn* addr) = 0; virtual Errno SetLinger(bool enable, u32 linger) = 0; @@ -138,9 +139,9 @@ public: std::pair RecvFrom(int flags, std::vector& message, SockAddrIn* addr) override; - std::pair Send(const std::vector& message, int flags) override; + std::pair Send(std::span message, int flags) override; - std::pair SendTo(u32 flags, const std::vector& message, + std::pair SendTo(u32 flags, std::span message, const SockAddrIn* addr) override; Errno SetLinger(bool enable, u32 linger) override;