46 std::string_view name,
48 std::chrono::milliseconds timeout = {});
93 return max_message_bytes_.load(std::memory_order_relaxed);
120 (void)data; (void)size;
151 std::shared_ptr<Impl> impl_;
153 std::atomic<bool> running_{
false};
154 std::atomic<std::uint64_t> connection_generation_{0};
155 std::atomic<std::uint64_t> read_generation_{0};
156 std::atomic<std::size_t> max_message_bytes_{64u * 1024u * 1024u};
157 std::atomic<std::int64_t> write_timeout_ms_{0};
158 std::atomic<std::int64_t> frame_read_timeout_ms_{0};
159 std::atomic<bool> secure_receive_buffer_{
false};
160 std::atomic<bool> write_poisoned_{
false};
161 std::atomic<bool> defer_first_dispatch_until_callback_{
false};
162 std::shared_ptr<std::atomic<bool>> first_dispatch_gate_;
163 mutable std::mutex callback_mutex_;
164 runtime::AliveToken alive_;
166 void disconnect_impl(
bool destroying);
167 void release_first_dispatch_gate();
168 void start_read_thread(
bool allow_active_disconnect_owner =
false,
169 std::uint64_t expected_connection_generation = 0);
170 void read_loop(std::uint64_t generation);
216 std::atomic<bool> running_{
false};
217 std::atomic<std::size_t> max_message_bytes_{64u * 1024u * 1024u};
218 std::atomic<std::int64_t> write_timeout_ms_{0};
219 std::atomic<std::int64_t> frame_read_timeout_ms_{0};
220 std::thread accept_thread_;
221 std::vector<std::unique_ptr<InterprocessConnection>> clients_;
223 std::unique_ptr<ServerImpl> server_impl_;
void set_write_timeout(std::chrono::milliseconds timeout)
void stop()
Stop listening and disconnect all clients.
InterprocessConnectionServer & operator=(const InterprocessConnectionServer &)=delete
InterprocessConnectionServer()
virtual ~InterprocessConnectionServer()
void set_max_message_bytes(std::size_t bytes)
Apply a framing ceiling to every subsequently accepted connection.
std::function< void(std::unique_ptr< InterprocessConnection >)> on_client_connected
Lambda callback alternative.
Definition interprocess_connection.hpp:209
bool start(std::string_view name, IpcTransport transport=IpcTransport::Socket)
Start listening on the given name.
std::string bound_address() const
Actual bound IPv4 address after binding (e.g.
std::uint16_t bound_port() const
Actual TCP port after binding, including an OS-assigned port requested with host:0.
InterprocessConnectionServer(const InterprocessConnectionServer &)=delete
virtual void client_connected(std::unique_ptr< InterprocessConnection > connection)
Called when a new client connects.
bool is_running() const
Whether the server is running.
Definition interprocess_connection.hpp:194
void set_frame_read_timeout(std::chrono::milliseconds timeout)
void set_frame_read_timeout(std::chrono::milliseconds timeout)
Bound a frame once its first byte arrives.
void set_on_text_message(std::function< void(std::string_view)> callback)
virtual void connection_made()
Called when a connection is established.
Definition interprocess_connection.hpp:113
InterprocessConnection(const InterprocessConnection &)=delete
virtual void message_received(std::string_view message)
Convenience: called with string view for text messages.
Definition interprocess_connection.hpp:124
void disconnect()
Disconnect and clean up.
virtual ~InterprocessConnection()
void set_max_message_bytes(std::size_t bytes)
Override the framing ceiling before connecting or accepting work.
std::size_t max_message_bytes() const
Definition interprocess_connection.hpp:92
bool send_message(const void *data, size_t size)
Send a message (length-prefixed).
std::function< void(const void *, size_t)> on_message
Definition interprocess_connection.hpp:135
IpcState state() const
Current state.
Definition interprocess_connection.hpp:72
void set_write_timeout(std::chrono::milliseconds timeout)
Apply a blocking write deadline for socket transports.
std::function< void()> on_connected
Lambda-based callbacks (alternative to overriding).
Definition interprocess_connection.hpp:133
std::string server_bind_address() const
Address the most recent socket create_server() bound its listener to (e.g.
friend class InterprocessConnectionServer
Definition interprocess_connection.hpp:147
bool connect(std::string_view name, IpcTransport transport=IpcTransport::NamedPipe, std::chrono::milliseconds timeout={})
Connect as client to a named pipe, TCP socket, or OS-local socket.
std::optional< runtime::LocalPeerCredentials > local_peer_credentials() const
Kernel-observed credentials for the process on the other end of a connected local socket.
void set_on_message(std::function< void(const void *, size_t)> callback)
void set_on_disconnected(std::function< void()> callback)
bool send_message(std::string_view message)
Send a string message.
void set_secure_receive_buffer(bool enabled)
Securely clear each received frame buffer after callbacks return.
bool attach_inherited_local_socket(std::intptr_t handle)
Attach to a duplicate of an already-connected OS-local socket handle.
bool create_server(std::string_view name, IpcTransport transport=IpcTransport::NamedPipe, int timeout_ms=0)
Create a server that listens for one client connection.
std::function< void(std::string_view)> on_text_message
Definition interprocess_connection.hpp:136
bool is_connected() const
Whether currently connected.
Definition interprocess_connection.hpp:69
std::function< void()> on_disconnected
Definition interprocess_connection.hpp:134
InterprocessConnection & operator=(const InterprocessConnection &)=delete
virtual void connection_lost()
Called when the connection is lost.
Definition interprocess_connection.hpp:116
virtual void message_received(const void *data, size_t size)
Called when a message is received. Called on the background read thread.
Definition interprocess_connection.hpp:119
void set_on_connected(std::function< void()> callback)
Definition child_process_manager.hpp:18
IpcState
Connection state.
Definition interprocess_connection.hpp:29
@ Connected
Definition interprocess_connection.hpp:29
@ Error
Definition interprocess_connection.hpp:29
@ Connecting
Definition interprocess_connection.hpp:29
@ Disconnected
Definition interprocess_connection.hpp:29
IpcTransport
Transport type for IPC.
Definition interprocess_connection.hpp:26
@ NamedPipe
Definition interprocess_connection.hpp:26
@ Socket
Definition interprocess_connection.hpp:26
@ LocalSocket
Definition interprocess_connection.hpp:26