[−][src]Trait libzmq::prelude::Socket
Methods shared by all thread-safe sockets.
Here is the list of socket option that differs from the ØMQ defaults:
- All sockets have their linger period set to zero (
ZMQ_BLOCKY
). - All sockets have IPV6 enabled (
ZMQ_IPV6
). - All sockets have
ZMQ_ZAP_ENFORCE_DOMAIN
set to true. - All sockets have
ZMQ_ZAP_DOMAIN
hardcoded to "global".
Provided methods
fn connect<E>(&self, endpoint: E) -> Result<(), Error> where
E: Into<Endpoint>,
E: Into<Endpoint>,
Schedules a connection to a Endpoint
.
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.
Usage Contract
- The endpoint's protocol must be supported by the socket.
Returned Errors
InvalidInput
(transport incompatible or not supported)InvalidCtx
Example
use libzmq::{prelude::*, Client, TcpAddr}; use std::convert::TryInto; let addr: TcpAddr = "127.0.0.1:420".try_into()?; let client = Client::new()?; client.connect(&addr)?;
fn bind<E>(&self, endpoint: E) -> Result<(), Error> where
E: Into<Endpoint>,
E: Into<Endpoint>,
Schedules a bind to a Endpoint
and then accepts
incoming connections.
As opposed to connect
, the socket will straight await and start
accepting connections.
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)InvalidCtx
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<E>(&self, endpoint: E) -> Result<(), Error> where
E: Into<Endpoint>,
E: Into<Endpoint>,
Disconnect the socket from a Endpoint
.
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.
Usage Contract
- The endpoint must be currently connected to.
Returned Errors
NotFound
(endpoint was not connected to)InvalidCtx
fn unbind<I, E>(&self, endpoint: E) -> Result<(), Error> where
E: Into<Endpoint>,
E: Into<Endpoint>,
Unbind the socket from a Endpoint
.
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 the incoming queue is kept.
Usage Contract
- The endpoint must be currently bound to.
Returned Errors
NotFound
(endpoint was not bound to)InvalidCtx
fn last_endpoint(&self) -> Result<Endpoint, Error>
Retrieve the last endpoint connected or bound to.
Returns Error
with ErrorKind::NotFound
if not endpoint
was previously bound or connect to.
This is the only way to retrieve the assigned value of an
Unspecified
port.
Returned Errors
NotFound
(no endpoint to retrieve)InvalidCtx
Example
use libzmq::{prelude::*, Server, TcpAddr, addr::Endpoint, ErrorKind}; // 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()?; // Returns a `NotFound` error since no endpoint was bound // or connected to. let err = server.last_endpoint().unwrap_err(); match err.kind() { ErrorKind::NotFound(_) => (), _ => unreachable!(), } server.bind(&addr)?; // Now we retrieve the endpoint that was previously bound. match server.last_endpoint()? { Endpoint::Tcp(tcp) => assert!(tcp.host().port().is_specified()), _ => 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
.
Feature Flags
Note that Mechanism::CurveClient
and Mechanism::CurveServer
require
the feature flag "curve" to be enabled, and will panic if used otherwise.
Example
#[cfg(feature = "curve")] { 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!() } }