pub struct Server { /* private fields */ }
Expand description
A Server
socket is a socket used for advanced request-reply messaging.
A Server
socket talks to a set of Client
sockets. The Client
must
first initiate the conversation, which generates a routing_id
associated
with the connection. Each message received from a Server
will have this
routing_id
. To send messages back to the server, you must associate them
to a RoutingId
either manually using set_routing_id
or via the route
convenience method. If the routing_id
is not specified, or
does not refer to a connected server peer, the send call will fail with
HostUnreachable
.
§Mute State
When a Server
socket enters the mute state due to having reached the high
water mark for all servers, or if there are no servers at
all, then any send
operations on the socket shall block until the mute
state ends or at least one downstream node becomes available for sending;
messages are not discarded.
§Summary of Characteristics
Characteristic | Value |
---|---|
Compatible peer sockets | Server |
Direction | Bidirectional |
Pattern | Unrestricted |
Incoming routing strategy | Fair-queued |
Outgoing routing strategy | See text |
Action in mute state | Block |
§Example
use libzmq::{prelude::*, *};
let addr: TcpAddr = "127.0.0.1:*".try_into()?;
let server = ServerBuilder::new()
.bind(addr)
.build()?;
let bound = server.last_endpoint()?;
let client = ClientBuilder::new()
.connect(bound)
.build()?;
// The client initiates the conversation so it is assigned a `routing_id`.
client.send("request")?;
let msg = server.recv_msg()?;
assert_eq!("request", msg.to_str()?);
let id = msg.routing_id().unwrap();
// Using this `routing_id`, we can now route as many replies as we
// want to the client.
server.route("reply 1", id)?;
server.route("reply 2", id)?;
// The `routing_id` is discarted when the message is sent to the client.
let mut msg = client.recv_msg()?;
assert_eq!("reply 1", msg.to_str()?);
assert!(msg.routing_id().is_none());
client.recv(&mut msg)?;
assert_eq!("reply 2", msg.to_str()?);
assert!(msg.routing_id().is_none());
Implementations§
Source§impl Server
impl Server
Sourcepub fn with_ctx(handle: CtxHandle) -> Result<Server, Error>
pub fn with_ctx(handle: CtxHandle) -> Result<Server, Error>
Create a Server
socket associated with a specific context
from a CtxHandle
.