pub struct StreamSocket { /* private fields */ }Expand description
STREAM socket - raw TCP bridging without ZMTP handshake.
Accepts plain TCP connections and multiplexes them through a ZeroMQ-style routing-ID interface. Each accepted connection is assigned a unique 8-byte routing ID; all subsequent sends and receives for that connection use the same ID to route messages.
Unlike other socket types, StreamSocket performs no ZMTP handshake -
it speaks plain TCP bytes, making it suitable for bridging to HTTP servers,
legacy services, and command-line tools such as nc and curl.
Implementations§
Source§impl StreamSocket
impl StreamSocket
Sourcepub async fn bind(addr: impl ToSocketAddrsAsync) -> Result<StreamSocket, Error>
pub async fn bind(addr: impl ToSocketAddrsAsync) -> Result<StreamSocket, Error>
Bind a STREAM socket to a TCP address.
The returned socket is ready to accept raw (non-ZMTP) TCP connections
via accept_raw().
§Errors
Returns an error if the address cannot be bound (e.g., port in use).
Sourcepub async fn accept_raw(&mut self) -> Result<Bytes, Error>
pub async fn accept_raw(&mut self) -> Result<Bytes, Error>
Accept the next raw TCP connection and register it as a new peer.
Spawns background reader and writer tasks for the connection. Returns
the routing ID assigned to this peer; the same ID is used to address
messages to this peer via send().
The caller will also receive a connection notification from
recv(): [routing_id, "", ""] with an empty data frame.
§Errors
Returns an error if the accept() system call fails.
Sourcepub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>, Error>
pub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>, Error>
Receive the next message from any connected peer.
Returns [routing_id, empty, data]. An empty data frame signals a
connection event (connect or disconnect) for routing_id.
Returns Ok(None) only if the socket’s inbound channel has been
closed (i.e., the StreamSocket itself is being dropped).
§Errors
Returns an error if the underlying channel has an unexpected failure.
Sourcepub async fn send(&mut self, msg: Vec<Bytes>) -> Result<(), Error>
pub async fn send(&mut self, msg: Vec<Bytes>) -> Result<(), Error>
Send raw bytes to a specific peer.
msg must contain at least one frame (the routing ID). A 3-frame
layout is expected: [routing_id, empty, data]. The routing ID
selects the destination; remaining frames are flattened and written as
raw bytes to the TCP stream.
If the peer is not found (e.g., already disconnected), the message is silently dropped.
§Errors
Returns an error if the message has no frames or if the peer’s send channel has disconnected.
Returns WouldBlock when the peer’s send queue has reached the
configured send_hwm limit.
Sourcepub fn close_peer(&mut self, routing_id: &Bytes) -> bool
pub fn close_peer(&mut self, routing_id: &Bytes) -> bool
Close a single peer, removing it from the routing table and cancelling its background reader and writer tasks.
Dropping the peer’s internal PeerHandle closes both the outbound and the
shutdown channels, so the writer exits and the reader’s shutdown branch
fires. Both tasks then release their TCP halves and file descriptors,
rather than the reader lingering until the remote closes the connection.
After this call, messages addressed to routing_id are silently dropped.
Returns true if a peer was present and removed.
Sourcepub fn disconnect(&mut self, routing_id: &Bytes)
pub fn disconnect(&mut self, routing_id: &Bytes)
Disconnect a peer explicitly. Alias for close_peer.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Number of currently tracked (connected) peers.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
The local address this socket is bound to.
Sourcepub const fn options(&self) -> &SocketOptions
pub const fn options(&self) -> &SocketOptions
Get a reference to the socket options.
Sourcepub fn options_mut(&mut self) -> &mut SocketOptions
pub fn options_mut(&mut self) -> &mut SocketOptions
Get a mutable reference to the socket options.
Auto Trait Implementations§
impl !Freeze for StreamSocket
impl !RefUnwindSafe for StreamSocket
impl !Send for StreamSocket
impl !Sync for StreamSocket
impl Unpin for StreamSocket
impl UnsafeUnpin for StreamSocket
impl UnwindSafe for StreamSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more