Skip to main content

RepSocket

Struct RepSocket 

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

A REP socket for synchronous reply patterns.

REP sockets enforce strict alternation between receive and send:

  • Must call recv() to get a request
  • Must call send() to reply before next recv()
  • Automatically handles routing envelopes

They’re used for:

  • Synchronous RPC servers
  • Request-reply protocols
  • Service endpoints

§ZeroMQ Compatibility

Compatible with zmq::REQ and zmq::REP sockets from libzmq.

§Example

use monocoque::zmq::RepSocket;
use monocoque_core::rt::TcpListener;
use bytes::Bytes;

// Bind and accept
let listener = TcpListener::bind("127.0.0.1:5555").await?;
let (stream, _) = listener.accept().await?;
let mut socket = RepSocket::from_tcp(stream).await?;

loop {
    // Receive request
    if let Ok(Some(request)) = socket.recv().await {
        println!("Got request: {:?}", request);
         
        // Send reply
        socket.send(vec![Bytes::from("REPLY")]).await?;
    }
}

Implementations§

Source§

impl RepSocket

Source

pub async fn bind(addr: impl ToSocketAddrs) -> Result<(TcpListener, Self)>

Bind to addr, accept one connection, and return a ready REP socket.

Returns the TcpListener so the caller can accept further connections.

§Example
use monocoque::zmq::RepSocket;
use bytes::Bytes;

let (_listener, mut socket) = RepSocket::bind("127.0.0.1:5555").await?;
if let Ok(Some(req)) = socket.recv().await {
    socket.send(vec![Bytes::from("PONG")]).await?;
}
Source

pub async fn bind_with_options( addr: impl ToSocketAddrs, options: SocketOptions, ) -> Result<(TcpListener, Self)>

Bind with custom socket options.

Source

pub async fn from_tcp(stream: TcpStream) -> Result<Self>

Create a REP socket from an existing TCP stream.

Create a REP socket from a TCP stream with TCP_NODELAY enabled.

Source

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

Create a REP socket from a TCP stream with custom options.

Source

pub async fn with_options<Stream>( stream: Stream, options: SocketOptions, ) -> Result<RepSocket<Stream>>
where Stream: AsyncRead + AsyncWrite + Unpin,

Create a REP socket from any stream with custom options.

Source§

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

Source

pub fn monitor(&mut self) -> SocketMonitor

Enable monitoring for this socket.

Returns a receiver for socket lifecycle events.

Source

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

Receive a request message.

This blocks until a request is received. The routing envelope is automatically extracted and stored for the subsequent send() call.

§Returns
  • Some(msg) - Received a request (content only, envelope stripped)
  • None - Connection closed gracefully or error occurred
§Example
use monocoque::zmq::RepSocket;

if let Ok(Some(request)) = socket.recv().await {
    for (i, frame) in request.iter().enumerate() {
        println!("Frame {}: {:?}", i, frame);
    }
}
Source

pub const fn socket_type() -> 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. \n /// # 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

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

Send a reply message.

This must be called after recv() and automatically uses the stored routing envelope from the request.

§Arguments
  • msg - Vector of message frames (parts) to send as reply
§Errors

Returns an error if:

  • Called without first calling recv()
  • The underlying connection is closed
§Example
use monocoque::zmq::RepSocket;
use bytes::Bytes;

// Send single-part reply
socket.send(vec![Bytes::from("OK")]).await?;

// Send multi-part reply
socket.send(vec![
    Bytes::from("Status: OK"),
    Bytes::from("Data: ..."),
]).await?;
Source

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

Get a mutable reference to this socket’s options.

Source§

impl RepSocket<UnixStream>

Source

pub async fn from_unix_stream(stream: UnixStream) -> Result<Self>

Create a REP socket from an existing Unix domain socket stream (IPC).

Source

pub async fn from_unix_stream_with_options( stream: UnixStream, options: SocketOptions, ) -> Result<Self>

Create a REP socket from an existing Unix stream with custom options.

This method provides full control over socket behavior through SocketOptions.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<S> Unpin for RepSocket<S>

§

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

§

impl<S> UnwindSafe for RepSocket<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