Skip to main content

StreamSocket

Struct StreamSocket 

Source
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

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn disconnect(&mut self, routing_id: &Bytes)

Disconnect a peer explicitly. Alias for close_peer.

Source

pub fn peer_count(&self) -> usize

Number of currently tracked (connected) peers.

Source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

The local address this socket is bound to.

Source

pub const fn options(&self) -> &SocketOptions

Get a reference to the socket options.

Source

pub fn options_mut(&mut self) -> &mut SocketOptions

Get a mutable reference to the socket options.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more