Skip to main content

PairSocket

Struct PairSocket 

Source
pub struct PairSocket<S = TcpStream>
where S: AsyncRead + AsyncWrite + Unpin,
{ /* private fields */ }
Expand description

PAIR socket for exclusive peer-to-peer communication.

PAIR sockets connect exactly two endpoints and provide bidirectional message passing without any routing or filtering logic.

Implementations§

Source§

impl<S> PairSocket<S>
where S: AsyncRead + AsyncWrite + Unpin,

Source

pub async fn new(stream: S) -> Result<PairSocket<S>, Error>

Create a new PAIR socket from a stream with default buffer configuration.

Source

pub async fn with_options( stream: S, options: SocketOptions, ) -> Result<PairSocket<S>, Error>

Create a new PAIR socket with custom buffer configuration and socket options.

Source

pub async fn send(&mut self, msg: Vec<Bytes>) -> Result<(), Error>

Send a message to the paired socket.

§Errors

Returns an error if the socket is poisoned, disconnected, or if the write fails.

Source

pub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>, Error>

Receive a message from the paired socket.

Returns Ok(Some(msg)) if a message was received, Ok(None) if the connection was closed, or an error.

Source

pub async fn close(self) -> Result<(), Error>

Close the socket gracefully by shutting down the underlying stream.

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.

Source

pub fn set_options(&mut self, options: SocketOptions)

Set socket options (builder-style).

Source

pub const fn socket_type(&self) -> SocketType

Get the socket type.

§ZeroMQ Compatibility

Corresponds to ZMQ_TYPE (16) option.

Source

pub fn last_endpoint(&self) -> Option<&Endpoint>

Get the endpoint this socket is connected/bound to, if available.

Returns None if the socket was created from a raw stream.

§ZeroMQ Compatibility

Corresponds to ZMQ_LAST_ENDPOINT (32) option.

Source

pub fn has_more(&self) -> bool

Check if the last received message has more frames coming.

Returns true if there are more frames in the current multipart message.

§ZeroMQ Compatibility

Corresponds to ZMQ_RCVMORE (13) option.

Source

pub fn events(&self) -> u32

Get the event state of the socket.

Returns a bitmask indicating ready-to-receive and ready-to-send states.

§Returns
  • 1 (POLLIN) - Socket is ready to receive
  • 2 (POLLOUT) - Socket is ready to send
  • 3 (POLLIN | POLLOUT) - Socket is ready for both
§ZeroMQ Compatibility

Corresponds to ZMQ_EVENTS (15) option.

Source§

impl PairSocket

Source

pub async fn bind( addr: impl ToSocketAddrsAsync, ) -> Result<(TcpListener, PairSocket), Error>

Bind to an address and accept the first connection.

PAIR sockets form an exclusive pair with exactly one peer.

§Returns

A tuple of (listener, socket) where:

  • listener can be used to accept additional connections if needed
  • socket is ready to send/receive with the first peer
§Example
use monocoque_zmtp::pair::PairSocket;

let (listener, mut socket) = PairSocket::bind("127.0.0.1:5555").await?;
Source

pub async fn connect(addr: impl ToSocketAddrsAsync) -> Result<PairSocket, Error>

Connect to a remote PAIR socket, storing the endpoint for automatic reconnection.

§Example
use monocoque_zmtp::pair::PairSocket;

let mut socket = PairSocket::connect("127.0.0.1:5555").await?;
Source

pub async fn connect_with_options( addr: impl ToSocketAddrsAsync, options: SocketOptions, ) -> Result<PairSocket, Error>

Connect with custom options, storing the endpoint for reconnection.

Source

pub async fn from_tcp(stream: TcpStream) -> Result<PairSocket, Error>

Create a new PAIR socket from a TCP stream with TCP_NODELAY enabled.

Source

pub async fn from_tcp_with_options( stream: TcpStream, options: SocketOptions, ) -> Result<PairSocket, Error>

Create a new PAIR socket from a TCP stream with TCP_NODELAY and custom options.

Source

pub fn is_connected(&self) -> bool

Check if the socket is currently connected.

Source

pub async fn try_reconnect(&mut self) -> Result<(), Error>

Try to reconnect to the stored endpoint.

Source

pub async fn recv_with_reconnect(&mut self) -> Result<Option<Vec<Bytes>>, Error>

Receive a message with automatic reconnection on EOF or network error.

If the socket was created with connect() and stores an endpoint, this method loops: on EOF or broken-pipe it clears the stream and calls try_reconnect() (which applies exponential backoff), then retries recv().

Respects max_reconnect_attempts - returns NotConnected when exhausted.

Source

pub async fn send_with_reconnect( &mut self, msg: Vec<Bytes>, ) -> Result<(), Error>

Send a message with automatic reconnection on network error.

On BrokenPipe / ConnectionReset, write_from_buf() already sets stream = None, so the next loop iteration reconnects automatically.

Respects max_reconnect_attempts - returns NotConnected when exhausted.

Source§

impl PairSocket<InprocStream>

Source

pub fn bind_inproc(endpoint: &str) -> Result<PairSocket<InprocStream>, Error>

Bind to an inproc endpoint.

Creates a new inproc endpoint that other sockets can connect to. Inproc endpoints must be bound before they can be connected to.

§Arguments
  • endpoint - Inproc URI (e.g., “inproc://my-endpoint”)
§Example
use monocoque_zmtp::pair::PairSocket;

let socket = PairSocket::bind_inproc("inproc://my-pair")?;
Source

pub fn bind_inproc_with_options( endpoint: &str, options: SocketOptions, ) -> Result<PairSocket<InprocStream>, Error>

Bind to an inproc endpoint with custom configuration and options.

Source

pub fn connect_inproc(endpoint: &str) -> Result<PairSocket<InprocStream>, Error>

Connect to an inproc endpoint.

Connects to a previously bound inproc endpoint.

§Arguments
  • endpoint - Inproc URI (e.g., “inproc://my-endpoint”)
§Example
use monocoque_zmtp::pair::PairSocket;

let socket = PairSocket::connect_inproc("inproc://my-pair")?;
Source

pub fn connect_inproc_with_options( endpoint: &str, options: SocketOptions, ) -> Result<PairSocket<InprocStream>, Error>

Connect to an inproc endpoint with custom configuration and options.

Trait Implementations§

Source§

impl ProxySocket for PairSocket

Source§

fn recv_multipart<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<Bytes>>, Error>> + 'async_trait>>
where 'life0: 'async_trait, PairSocket: 'async_trait,

Receive a multipart message from the socket. Read more
Source§

fn send_multipart<'life0, 'async_trait>( &'life0 mut self, msg: Vec<Bytes>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
where 'life0: 'async_trait, PairSocket: 'async_trait,

Send a multipart message to the socket. Read more
Source§

fn socket_desc(&self) -> &'static str

Get a description of the socket for logging.
Source§

impl<S> Socket for PairSocket<S>
where S: AsyncRead + AsyncWrite + Unpin + 'static,

Source§

fn send<'life0, 'async_trait>( &'life0 mut self, msg: Vec<Bytes>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
where 'life0: 'async_trait, PairSocket<S>: 'async_trait,

Send a multipart message on the socket. Read more
Source§

fn recv<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<Bytes>>, Error>> + 'async_trait>>
where 'life0: 'async_trait, PairSocket<S>: 'async_trait,

Receive a multipart message from the socket. Read more
Source§

fn socket_type(&self) -> SocketType

Get the socket type. Read more
Source§

fn has_more(&self) -> bool

Check if socket has more message frames pending. Read more

Auto Trait Implementations§

§

impl<S = TcpStream> !Freeze for PairSocket<S>

§

impl<S> RefUnwindSafe for PairSocket<S>
where S: RefUnwindSafe,

§

impl<S> Send for PairSocket<S>
where S: Send,

§

impl<S> Sync for PairSocket<S>
where S: Sync,

§

impl<S> Unpin for PairSocket<S>

§

impl<S> UnsafeUnpin for PairSocket<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for PairSocket<S>
where S: UnwindSafe,

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