Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1#pragma once
2
3// TCP, UDP, and OS-local socket abstraction.
4
5#include <atomic>
6#include <string>
7#include <string_view>
8#include <vector>
9#include <optional>
10#include <cstddef>
11#include <cstdint>
12#include <chrono>
13
14namespace pulp::runtime {
15
16enum class SocketType { TCP, UDP, Local };
17
21 std::uint64_t user_id = 0;
22 std::uint64_t group_id = 0;
23 std::int64_t process_id = 0;
27 std::uint64_t process_generation_id = 0;
28};
29
30class Socket {
31public:
32 Socket() = default;
34
36 bool create(SocketType type);
37
39 bool bind(std::string_view address, uint16_t port);
40
43 bool bind_local(std::string_view path);
44
46 bool listen(int backlog = 5);
47
50 std::optional<Socket> accept();
51
54 std::optional<Socket> accept(std::chrono::milliseconds timeout);
55
58 bool connect(std::string_view address, uint16_t port,
59 std::chrono::milliseconds timeout = {});
60
63 bool connect_local(std::string_view path,
64 std::chrono::milliseconds timeout = {});
65
68 std::optional<LocalPeerCredentials> local_peer_credentials() const;
69
72 bool duplicate_local_handle(std::intptr_t handle);
73
75 int send(const uint8_t* data, size_t length);
76 int send(std::string_view data);
77
79 int send_to(const uint8_t* data, size_t length,
80 std::string_view address, uint16_t port);
81
83 int receive(uint8_t* buffer, size_t buffer_size);
84
87 bool set_write_timeout(std::chrono::milliseconds timeout);
88
91 bool set_read_timeout(std::chrono::milliseconds timeout);
92
94 int receive_from(uint8_t* buffer, size_t buffer_size,
95 std::string& from_address, uint16_t& from_port);
96
98 void close();
99
102 void shutdown();
103
105 bool is_open() const;
106
108 uint16_t local_port() const;
109
112 std::string local_address() const;
113
114 // No copy, move OK
115 Socket(const Socket&) = delete;
116 Socket& operator=(const Socket&) = delete;
117 Socket(Socket&& other) noexcept;
119
120private:
121#ifdef _WIN32
122 std::uintptr_t fd_ = ~std::uintptr_t{0};
123#else
124 int fd_ = -1;
125#endif
127 std::string bound_local_path_;
128 std::uint64_t bound_local_device_ = 0;
129 std::uint64_t bound_local_inode_ = 0;
130#ifdef _WIN32
131 // Winsock does not wake a recv() blocked in another thread when the socket
132 // is shut down locally, so receive() waits for readiness in short slices,
133 // checks this flag between them, and enforces the read timeout itself.
134 std::atomic<bool> shutdown_requested_{false};
135 std::atomic<std::int64_t> read_timeout_ms_{0};
136#endif
137
138 bool connect_address(const void* address, std::size_t address_size,
139 std::chrono::milliseconds timeout);
140};
141
142} // namespace pulp::runtime
bool bind(std::string_view address, uint16_t port)
Bind to a local address and port.
bool connect(std::string_view address, uint16_t port, std::chrono::milliseconds timeout={})
Connect to a remote address (TCP).
Socket & operator=(const Socket &)=delete
Socket(const Socket &)=delete
Socket & operator=(Socket &&other) noexcept
int receive(uint8_t *buffer, size_t buffer_size)
Receive data. Returns bytes received, or -1 on error. 0 = connection closed.
void shutdown()
Interrupt blocking stream operations without closing the socket handle.
int send_to(const uint8_t *data, size_t length, std::string_view address, uint16_t port)
Send UDP datagram to specific address.
bool create(SocketType type)
Create a socket.
int send(std::string_view data)
bool duplicate_local_handle(std::intptr_t handle)
Duplicate an already-connected OS-local stream handle and own the duplicate.
void close()
Close the socket.
bool bind_local(std::string_view path)
Bind an OS-local stream endpoint.
std::optional< Socket > accept()
Accept an incoming TCP or OS-local connection, blocking until one is available.
int send(const uint8_t *data, size_t length)
Send data. Returns bytes sent, or -1 on error.
bool listen(int backlog=5)
Listen for incoming TCP or OS-local stream connections.
bool connect_local(std::string_view path, std::chrono::milliseconds timeout={})
Connect to an OS-local stream endpoint with the same bounded-connect semantics as TCP.
int receive_from(uint8_t *buffer, size_t buffer_size, std::string &from_address, uint16_t &from_port)
Receive UDP datagram. Returns bytes received and source address.
std::optional< LocalPeerCredentials > local_peer_credentials() const
Kernel-observed credentials for the connected OS-local peer.
bool is_open() const
Whether the socket is open.
std::optional< Socket > accept(std::chrono::milliseconds timeout)
Wait up to timeout for and accept an incoming connection.
bool set_write_timeout(std::chrono::milliseconds timeout)
Bound blocking send operations.
std::string local_address() const
Bound local IPv4 address in dotted form, or empty when unavailable/unbound.
uint16_t local_port() const
Bound local port, or 0 when unavailable/unbound.
Socket(Socket &&other) noexcept
bool set_read_timeout(std::chrono::milliseconds timeout)
Bound blocking receive operations.
Definition generated_event_source.hpp:33
@ other
Definition udev_monitor.hpp:26
SocketType
Definition socket.hpp:16
@ Local
Definition socket.hpp:16
@ TCP
Definition socket.hpp:16
@ UDP
Definition socket.hpp:16
STL namespace.
Credentials reported by the kernel for the process on the other end of an OS-local stream.
Definition socket.hpp:20
std::uint64_t group_id
Definition socket.hpp:22
std::uint64_t process_generation_id
Kernel process-generation discriminator when the platform exposes one.
Definition socket.hpp:27
std::int64_t process_id
Definition socket.hpp:23
std::uint64_t user_id
Definition socket.hpp:21