[−][src]Trait libzmq::prelude::Socket
Methods shared by all thread-safe sockets.
Provided methods
fn connect<I, E>(&self, endpoints: I) -> Result<(), Error<usize>> where
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
Schedules a connection to one or more Endpoints
and then accepts
incoming connections.
Since ØMQ handles all connections behind the curtain, one cannot know
exactly when the connection is truly established a blocking send
or recv
call is made on that connection.
When any of the connection attempt fail, the Error
will contain the position
of the iterator before the failure. This represents the number of
connections that succeeded before the failure.
Usage Contract
- The endpoint's protocol must be supported by the socket.
Returned Errors
InvalidInput
(transport incompatible or not supported)CtxTerminated
Example
use libzmq::{prelude::*, Client, TcpAddr}; use std::convert::TryInto; let addr1: TcpAddr = "127.0.0.1:420".try_into()?; let addr2: TcpAddr = "127.0.0.1:69".try_into()?; let client = Client::new()?; // Connect to multiple endpoints at once. client.connect(&[addr1, addr2])?;
fn bind<I, E>(&self, endpoints: I) -> Result<(), Error<usize>> where
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
Schedules a bind to one or more Endpoints
and then accepts
incoming connections.
As opposed to connect
, the socket will straight await and start
accepting connections.
When any of the connection attempt fail, the Error
will contain the position
of the iterator before the failure. This represents the number of
binds that succeeded before the failure.
Usage Contract
- The transport must be supported by the socket type.
- The endpoint must not be in use.
- The endpoint must be local.
Returned Errors
InvalidInput
(transport incompatible or not supported)AddrInUse
(addr already in use)AddrNotAvailable
(addr not local)CtxTerminated
Example
use libzmq::{prelude::*, Server, TcpAddr}; use std::convert::TryInto; // Use a system-assigned port. let addr: TcpAddr = "127.0.0.1:*".try_into()?; let server = Server::new()?; server.bind(addr)?;
fn disconnect<I, E>(&self, endpoints: I) -> Result<(), Error<usize>> where
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
I: IntoIterator<Item = E>,
E: Into<Endpoint>,
Disconnect the socket from one or more Endpoints
.
The behavior of disconnect
varies depending whether the endpoint
was connected or bound to. Note that, in both cases, the disconnection
is not immediate.
When any of the connection attempt fail, the Error
will contain the position
of the iterator before the failure. This represents the number of
disconnections that succeeded before the failure.
Disconnect from a connected endpoint
The socket stops receiving and sending messages to the remote. The incoming and outgoing queue of the socket associated to the endpoint are discarded. However, the remote server might still have outstanding messages from the socket sent prior to the disconnection in its incoming queue.
Disconnect from a bound endpoint
The socket stops receiving and sending messages to peers connected to the now unbound endpoint. The outgoing queue of the socket associated to the endpoint is discarded, but incoming queue is kept.
Usage Contract
- The endpoint must be currently connected or bound to.
Returned Errors
NotFound
(endpoint was not bound to)CtxTerminated
fn last_endpoint(&self) -> Result<Option<Endpoint>, Error>
Retrieve the last endpoint connected or bound to.
This is the only way to retrieve the assigned value of an
Unspecified
port.
Example
use libzmq::{prelude::*, Server, TcpAddr, addr::Endpoint}; // We create a tcp addr with an unspecified port. // This port will be assigned by the OS when binding. let addr: TcpAddr = "127.0.0.1:*".try_into()?; assert!(addr.host().port().is_unspecified()); let server = Server::new()?; assert!(server.last_endpoint()?.is_none()); server.bind(&addr)?; if let Endpoint::Tcp(tcp) = server.last_endpoint()?.unwrap() { // The port was indeed assigned by the OS. assert!(tcp.host().port().is_specified()); } else { unreachable!(); }
fn mechanism(&self) -> Mechanism
Returns the socket's Mechanism
.
Example
use libzmq::{prelude::*, Server, auth::Mechanism}; let server = Server::new()?; assert_eq!(server.mechanism(), Mechanism::Null);
fn set_mechanism<M>(&self, mechanism: M) -> Result<(), Error> where
M: Into<Mechanism>,
M: Into<Mechanism>,
Set the socket's Mechanism
.
Example
use libzmq::{prelude::*, Client, auth::*}; let client = Client::new()?; assert_eq!(client.mechanism(), Mechanism::Null); let server_cert = CurveCert::new_unique(); // We do not specify a client certificate, so it // will be automatically generated. let creds = CurveClientCreds::new(server_cert.public()); client.set_mechanism(&creds)?; if let Mechanism::CurveClient(creds) = client.mechanism() { assert_eq!(creds.server(), server_cert.public()); assert!(creds.cert().is_some()); } else { unreachable!() }