pub struct Socket { /* private fields */ }
Implementations§
Source§impl Socket
impl Socket
pub fn from_raw(socket: *mut c_void) -> Socket
Sourcepub fn close(&mut self) -> Result<(), Error>
pub fn close(&mut self) -> Result<(), Error>
Close 0MQ socket
Binding of int zmq_close (void *s);
It’s not mandatory to call this function since socket can be closed automatically on dropping The function will destroy this socket. Any outstanding messages physically received from the network but not yet received by the application with recv() shall be discarded. The behaviour for discarding messages sent by the application with send() but not yet physically transferred to the network depends on the value of the ZMQ_LINGER socket option for the specified socket.
Examples found in repository?
4fn main() {
5 println!("Connecting to hello world server...");
6 let mut context = zmq::Context::new().unwrap();
7 let mut requester = context.socket(zmq::REQ).unwrap();
8 requester.connect("tcp://localhost:5555").unwrap();
9
10 for request_nbr in 0..10 {
11 let mut buffer: Vec<u8> = Vec::with_capacity(10);
12 println!("Sending Hello {}...", request_nbr);
13 requester.send_bytes("Hello".as_bytes(), 0).unwrap();
14 requester.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received World {}", request_nbr);
16 }
17 requester.close().unwrap();
18 context.shutdown().unwrap();
19}
Sourcepub fn bind(&mut self, endpoint: &str) -> Result<(), Error>
pub fn bind(&mut self, endpoint: &str) -> Result<(), Error>
Accept incoming connections on a socket
Binding of int zmq_bind (void *socket, const char *endpoint);
The function binds the socket to a local endpoint and then accepts incoming connections on that endpoint.
Examples found in repository?
7fn main() {
8 let mut context = zmq::Context::new().unwrap();
9 let mut responder = context.socket(zmq::REP).unwrap();
10 responder.bind("tcp://*:5555").unwrap();
11
12 loop {
13 let mut buffer: Vec<u8> = Vec::with_capacity(10);
14 responder.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received Hello");
16 sleep(Duration::from_secs(1));
17 responder.send_bytes("World".as_bytes(), 0).unwrap();
18 }
19}
Sourcepub fn connect(&mut self, endpoint: &str) -> Result<(), Error>
pub fn connect(&mut self, endpoint: &str) -> Result<(), Error>
Create outgoing connection from socket
Binding of int zmq_connect (void *socket, const char *endpoint);
The function connects the socket to an endpoint and then accepts incoming connections on that endpoint.
Examples found in repository?
4fn main() {
5 println!("Connecting to hello world server...");
6 let mut context = zmq::Context::new().unwrap();
7 let mut requester = context.socket(zmq::REQ).unwrap();
8 requester.connect("tcp://localhost:5555").unwrap();
9
10 for request_nbr in 0..10 {
11 let mut buffer: Vec<u8> = Vec::with_capacity(10);
12 println!("Sending Hello {}...", request_nbr);
13 requester.send_bytes("Hello".as_bytes(), 0).unwrap();
14 requester.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received World {}", request_nbr);
16 }
17 requester.close().unwrap();
18 context.shutdown().unwrap();
19}
Sourcepub fn unbind(&mut self, endpoint: &str) -> Result<(), Error>
pub fn unbind(&mut self, endpoint: &str) -> Result<(), Error>
Stop accepting connections on a socket
Binding of int zmq_unbind (void *socket, const char *endpoint);
The function will unbind a socket specified by the socket argument from the endpoint specified by the endpoint argument.
Sourcepub fn disconnect(&mut self, endpoint: &str) -> Result<(), Error>
pub fn disconnect(&mut self, endpoint: &str) -> Result<(), Error>
Disconnect a socket
Binding of int zmq_disconnect (void *socket, const char *endpoint);
The function will disconnect socket from the endpoint specified by the endpoint argument. Any outstanding messages physically received from the network but not yet received by the application with recv() will be discarded. The behaviour for discarding messages sent by the application with send() but not yet physically transferred to the network depends on the value of the ZMQ_LINGER socket option for the socket.
Sourcepub fn send_msg(
&mut self,
msg: Message,
flags: SocketFlag,
) -> Result<i32, Error>
pub fn send_msg( &mut self, msg: Message, flags: SocketFlag, ) -> Result<i32, Error>
Send a message part on a socket
Binding of int zmq_msg_send (zmq_msg_t *msg, void *socket, int flags);
Sourcepub fn recv_into_msg(
&mut self,
msg: &mut Message,
flags: SocketFlag,
) -> Result<i32, Error>
pub fn recv_into_msg( &mut self, msg: &mut Message, flags: SocketFlag, ) -> Result<i32, Error>
Receive a message part from a socket
Binding of int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);
Sourcepub fn recv_msg(&mut self, flags: SocketFlag) -> Result<Message, Error>
pub fn recv_msg(&mut self, flags: SocketFlag) -> Result<Message, Error>
Receive a message part from a socket
Binding of int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);
Sourcepub fn send_bytes(
&mut self,
data: &[u8],
flags: SocketFlag,
) -> Result<i32, Error>
pub fn send_bytes( &mut self, data: &[u8], flags: SocketFlag, ) -> Result<i32, Error>
Send bytes on a socket
Data will be copied into a Message object in order to be sent.
Examples found in repository?
7fn main() {
8 let mut context = zmq::Context::new().unwrap();
9 let mut responder = context.socket(zmq::REP).unwrap();
10 responder.bind("tcp://*:5555").unwrap();
11
12 loop {
13 let mut buffer: Vec<u8> = Vec::with_capacity(10);
14 responder.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received Hello");
16 sleep(Duration::from_secs(1));
17 responder.send_bytes("World".as_bytes(), 0).unwrap();
18 }
19}
More examples
4fn main() {
5 println!("Connecting to hello world server...");
6 let mut context = zmq::Context::new().unwrap();
7 let mut requester = context.socket(zmq::REQ).unwrap();
8 requester.connect("tcp://localhost:5555").unwrap();
9
10 for request_nbr in 0..10 {
11 let mut buffer: Vec<u8> = Vec::with_capacity(10);
12 println!("Sending Hello {}...", request_nbr);
13 requester.send_bytes("Hello".as_bytes(), 0).unwrap();
14 requester.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received World {}", request_nbr);
16 }
17 requester.close().unwrap();
18 context.shutdown().unwrap();
19}
Sourcepub fn send_const_bytes(
&mut self,
data: &'static [u8],
flags: SocketFlag,
) -> Result<i32, Error>
pub fn send_const_bytes( &mut self, data: &'static [u8], flags: SocketFlag, ) -> Result<i32, Error>
Send a constant-memory message part on a socket
Binding of ZMQ_EXPORT int zmq_send_const (void *s, const void *buf, size_t len, int flags);
The message buffer is assumed to be constant-memory(static) and will therefore not be copied or deallocated in any way
Sourcepub fn send_str(&mut self, data: &str, flags: SocketFlag) -> Result<i32, Error>
pub fn send_str(&mut self, data: &str, flags: SocketFlag) -> Result<i32, Error>
Send a UTF-8 string on socket
Sourcepub fn recv_bytes(&mut self, flags: SocketFlag) -> Result<Vec<u8>, Error>
pub fn recv_bytes(&mut self, flags: SocketFlag) -> Result<Vec<u8>, Error>
Receive bytes from a socket
Sourcepub fn recv_bytes_into_slice(
&mut self,
buffer: &mut [u8],
flags: SocketFlag,
) -> Result<i32, Error>
pub fn recv_bytes_into_slice( &mut self, buffer: &mut [u8], flags: SocketFlag, ) -> Result<i32, Error>
Receive bytes into a mutable slice
§Caution
Any bytes exceeding the length of buffer will be truncated.
Examples found in repository?
7fn main() {
8 let mut context = zmq::Context::new().unwrap();
9 let mut responder = context.socket(zmq::REP).unwrap();
10 responder.bind("tcp://*:5555").unwrap();
11
12 loop {
13 let mut buffer: Vec<u8> = Vec::with_capacity(10);
14 responder.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received Hello");
16 sleep(Duration::from_secs(1));
17 responder.send_bytes("World".as_bytes(), 0).unwrap();
18 }
19}
More examples
4fn main() {
5 println!("Connecting to hello world server...");
6 let mut context = zmq::Context::new().unwrap();
7 let mut requester = context.socket(zmq::REQ).unwrap();
8 requester.connect("tcp://localhost:5555").unwrap();
9
10 for request_nbr in 0..10 {
11 let mut buffer: Vec<u8> = Vec::with_capacity(10);
12 println!("Sending Hello {}...", request_nbr);
13 requester.send_bytes("Hello".as_bytes(), 0).unwrap();
14 requester.recv_bytes_into_slice(&mut buffer, 0).unwrap();
15 println!("Received World {}", request_nbr);
16 }
17 requester.close().unwrap();
18 context.shutdown().unwrap();
19}
Sourcepub fn recv_string(
&mut self,
flags: SocketFlag,
) -> Result<Result<String, Vec<u8>>, Error>
pub fn recv_string( &mut self, flags: SocketFlag, ) -> Result<Result<String, Vec<u8>>, Error>
Receive a UTF-8 string from socket
Sourcepub fn socket_monitor(
&mut self,
endpoint: &str,
events: &Vec<SocketEvent>,
) -> Result<(), Error>
pub fn socket_monitor( &mut self, endpoint: &str, events: &Vec<SocketEvent>, ) -> Result<(), Error>
Monitor socket events
Binding of int zmq_socket_monitor (void *socket, char *endpoint, int events);
The method lets an application thread track socket events (like connects) on a ZeroMQ socket
Sourcepub fn run_proxy(
frontend: &mut Socket,
backend: &mut Socket,
) -> Result<(), Error>
pub fn run_proxy( frontend: &mut Socket, backend: &mut Socket, ) -> Result<(), Error>
Start built-in 0MQ proxy
Binding of int zmq_proxy (const void *frontend, const void *backend, const void *capture);
The function starts the built-in ØMQ proxy in the current application thread.
Sourcepub fn run_proxy_ex(
frontend: &mut Socket,
backend: &mut Socket,
capture: Option<&mut Socket>,
control: Option<&mut Socket>,
) -> Result<(), Error>
pub fn run_proxy_ex( frontend: &mut Socket, backend: &mut Socket, capture: Option<&mut Socket>, control: Option<&mut Socket>, ) -> Result<(), Error>
Start built-in 0MQ proxy
Binding of int zmq_proxy (const void *frontend, const void *backend, const void *capture);
or
int zmq_proxy_steerable (const void *frontend, const void *backend, const void *capture, const void *control);
The function starts the built-in ØMQ proxy in the current application thread. The proxy will send all messages, received on both frontend and backend, to the capture socket. The capture socket should be a ZMQ_PUB, ZMQ_DEALER, ZMQ_PUSH, or ZMQ_PAIR socket. If the control socket is not None, the proxy supports control flow. If PAUSE is received on this socket, the proxy suspends its activities. If RESUME is received, it goes on. If TERMINATE is received, it terminates smoothly. At start, the proxy runs normally as if run_proxy was used.
Sourcepub fn as_poll_item(&self) -> PollItem
pub fn as_poll_item(&self) -> PollItem
Create a poll item from current socket
§Safty
There is no lifetime guarantee that poll item does not live out socket
Sourcepub fn poll(
items: &mut [PollItem],
nitems: i32,
timeout: i32,
) -> Result<i32, Error>
pub fn poll( items: &mut [PollItem], nitems: i32, timeout: i32, ) -> Result<i32, Error>
input/output multiplexing
Binding of int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout);