Process update and UdpSocket
Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
1 parent
827c9e150a
commit
7ca0e1b62e
5 files changed
+241
-1
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Net
|
||||
{
|
||||
class UdpSocket final
|
||||
{
|
||||
public:
|
||||
UdpSocket() noexcept = default;
|
||||
~UdpSocket();
|
||||
UdpSocket(UdpSocket&& other) noexcept;
|
||||
UdpSocket& operator=(UdpSocket&& other) noexcept;
|
||||
UdpSocket(const UdpSocket&) = delete;
|
||||
UdpSocket& operator=(const UdpSocket&) = delete;
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<UdpSocket, Void::Result::Error>
|
||||
connect(std::string_view host, std::uint16_t port);
|
||||
[[nodiscard]] bool valid() const noexcept { return handle_ >= 0; }
|
||||
[[nodiscard]] int native_handle() const noexcept { return handle_; }
|
||||
void close() noexcept;
|
||||
void shutdown() noexcept;
|
||||
[[nodiscard]] Void::Result::Result<bool, Void::Result::Error>
|
||||
wait_readable(std::chrono::milliseconds timeout) const;
|
||||
[[nodiscard]] Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
send(std::span<const std::uint8_t> bytes);
|
||||
[[nodiscard]] Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
receive(std::span<std::uint8_t> destination);
|
||||
|
||||
private:
|
||||
explicit UdpSocket(int handle) noexcept : handle_(handle) {}
|
||||
int handle_{-1};
|
||||
};
|
||||
}
|
||||
@@ -63,4 +63,11 @@ namespace Void::Process
|
||||
shell(std::string_view command,
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
||||
|
||||
/// Replace the current process image while preserving its PID and inherited
|
||||
/// environment. Returns only when the platform does not support re-exec or
|
||||
/// exec fails.
|
||||
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
||||
reexec(const std::filesystem::path& executable,
|
||||
const std::vector<std::string>& arguments);
|
||||
|
||||
} // namespace Void::Process
|
||||
@@ -0,0 +1,142 @@
|
||||
#include <Void/Net/Socket.hpp>
|
||||
|
||||
#include <cerrno>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace Void::Net
|
||||
{
|
||||
UdpSocket::~UdpSocket() = default;
|
||||
UdpSocket::UdpSocket(UdpSocket&&) noexcept = default;
|
||||
UdpSocket& UdpSocket::operator=(UdpSocket&&) noexcept = default;
|
||||
Void::Result::Result<UdpSocket, Void::Result::Error>
|
||||
UdpSocket::connect(std::string_view, std::uint16_t)
|
||||
{
|
||||
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
||||
}
|
||||
void UdpSocket::close() noexcept {}
|
||||
void UdpSocket::shutdown() noexcept {}
|
||||
Void::Result::Result<bool, Void::Result::Error>
|
||||
UdpSocket::wait_readable(std::chrono::milliseconds) const
|
||||
{
|
||||
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
||||
}
|
||||
Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
UdpSocket::send(std::span<const std::uint8_t>)
|
||||
{
|
||||
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
||||
}
|
||||
Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
UdpSocket::receive(std::span<std::uint8_t>)
|
||||
{
|
||||
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
||||
}
|
||||
}
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Void::Net
|
||||
{
|
||||
namespace
|
||||
{
|
||||
Void::Result::Failure<Void::Result::Error> socketError(
|
||||
std::string_view operation)
|
||||
{
|
||||
return Void::Result::fail(std::format("{}: {}", operation,
|
||||
std::strerror(errno)));
|
||||
}
|
||||
}
|
||||
|
||||
UdpSocket::~UdpSocket() { close(); }
|
||||
UdpSocket::UdpSocket(UdpSocket&& other) noexcept
|
||||
: handle_(std::exchange(other.handle_, -1))
|
||||
{
|
||||
}
|
||||
UdpSocket& UdpSocket::operator=(UdpSocket&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
close();
|
||||
handle_ = std::exchange(other.handle_, -1);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Void::Result::Result<UdpSocket, Void::Result::Error>
|
||||
UdpSocket::connect(std::string_view host, std::uint16_t port)
|
||||
{
|
||||
addrinfo hints{};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
addrinfo* addresses = nullptr;
|
||||
const auto service = std::to_string(port);
|
||||
const auto hostname = std::string(host);
|
||||
const int resolved = ::getaddrinfo(hostname.c_str(), service.c_str(),
|
||||
&hints, &addresses);
|
||||
if (resolved != 0) return Void::Result::fail(std::format(
|
||||
"getaddrinfo '{}': {}", hostname, gai_strerror(resolved)));
|
||||
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> guard(
|
||||
addresses, &freeaddrinfo);
|
||||
for (auto* address = addresses; address; address = address->ai_next) {
|
||||
const int handle = ::socket(address->ai_family,
|
||||
address->ai_socktype, address->ai_protocol);
|
||||
if (handle < 0) continue;
|
||||
if (::connect(handle, address->ai_addr, address->ai_addrlen) == 0)
|
||||
return UdpSocket(handle);
|
||||
::close(handle);
|
||||
}
|
||||
return socketError("UDP connect");
|
||||
}
|
||||
|
||||
void UdpSocket::close() noexcept
|
||||
{
|
||||
if (handle_ >= 0) {
|
||||
::close(handle_);
|
||||
handle_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void UdpSocket::shutdown() noexcept
|
||||
{
|
||||
if (handle_ >= 0) ::shutdown(handle_, SHUT_RDWR);
|
||||
}
|
||||
|
||||
Void::Result::Result<bool, Void::Result::Error>
|
||||
UdpSocket::wait_readable(std::chrono::milliseconds timeout) const
|
||||
{
|
||||
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
||||
pollfd descriptor{.fd = handle_, .events = POLLIN, .revents = 0};
|
||||
const auto bounded = std::clamp<long long>(timeout.count(), 0,
|
||||
std::numeric_limits<int>::max());
|
||||
const int result = ::poll(&descriptor, 1, static_cast<int>(bounded));
|
||||
if (result < 0 && errno != EINTR) return socketError("UDP poll");
|
||||
return result > 0 && (descriptor.revents & POLLIN) != 0;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
UdpSocket::send(std::span<const std::uint8_t> bytes)
|
||||
{
|
||||
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
||||
const auto result = ::send(handle_, bytes.data(), bytes.size(), 0);
|
||||
if (result < 0) return socketError("UDP send");
|
||||
return static_cast<std::size_t>(result);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::size_t, Void::Result::Error>
|
||||
UdpSocket::receive(std::span<std::uint8_t> destination)
|
||||
{
|
||||
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
||||
const auto result = ::recv(handle_, destination.data(), destination.size(), 0);
|
||||
if (result < 0) return socketError("UDP receive");
|
||||
return static_cast<std::size_t>(result);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+31
-1
@@ -14,7 +14,7 @@ namespace Void::Process
|
||||
std::chrono::milliseconds)
|
||||
{
|
||||
return Void::Result::fail("Process::run: not implemented on Windows");
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
run_streaming(const std::filesystem::path&,
|
||||
@@ -32,6 +32,13 @@ namespace Void::Process
|
||||
{
|
||||
return Void::Result::fail("Process::shell: not implemented on Windows");
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
reexec(const std::filesystem::path&,
|
||||
const std::vector<std::string>&)
|
||||
{
|
||||
return Void::Result::fail("Process::reexec: not implemented on Windows");
|
||||
}
|
||||
} // namespace Void::Process
|
||||
|
||||
#else // POSIX
|
||||
@@ -316,6 +323,29 @@ namespace Void::Process
|
||||
return run("/bin/sh", {"-c", std::string{command}}, {}, timeout);
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
reexec(const std::filesystem::path& executable,
|
||||
const std::vector<std::string>& arguments)
|
||||
{
|
||||
if (executable.empty()) {
|
||||
return Void::Result::fail("Process::reexec: executable is empty");
|
||||
}
|
||||
std::vector<std::string> owned;
|
||||
owned.reserve(arguments.size() + 1);
|
||||
owned.push_back(executable.string());
|
||||
owned.insert(owned.end(), arguments.begin(), arguments.end());
|
||||
std::vector<char*> argv;
|
||||
argv.reserve(owned.size() + 1);
|
||||
for (auto& argument : owned) {
|
||||
argv.push_back(argument.data());
|
||||
}
|
||||
argv.push_back(nullptr);
|
||||
::execv(executable.c_str(), argv.data());
|
||||
return Void::Result::fail(std::format(
|
||||
"Process::reexec: execv '{}' failed: {}", executable.string(),
|
||||
std::strerror(errno)));
|
||||
}
|
||||
|
||||
} // namespace Void::Process
|
||||
|
||||
#endif // _WIN32
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <Void/Net/Socket.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <utility>
|
||||
|
||||
Void::Testing::Test udp_socket_default_is_closed()
|
||||
{
|
||||
Void::Net::UdpSocket socket;
|
||||
Void::Testing::expect(!socket.valid());
|
||||
Void::Testing::expect(!socket.wait_readable(std::chrono::milliseconds{0}));
|
||||
}
|
||||
|
||||
Void::Testing::Test udp_socket_move_preserves_closed_state()
|
||||
{
|
||||
Void::Net::UdpSocket source;
|
||||
Void::Net::UdpSocket destination = std::move(source);
|
||||
Void::Testing::expect(!source.valid());
|
||||
Void::Testing::expect(!destination.valid());
|
||||
}
|
||||
Reference in new issue
Block a user