[−][src]Crate nng
A safe Rust wrapper for NNG
What Is NNG
From the NNG Github Repository:
NNG, like its predecessors nanomsg (and to some extent ZeroMQ), is a lightweight, broker-less library, offering a simple API to solve common recurring messaging problems, such as publish/subscribe, RPC-style request/reply, or service discovery. The API frees the programmer from worrying about details like connection management, retries, and other common considerations, so that they can focus on the application instead of the plumbing.
Nng-rs
This crate provides a safe wrapper around the NNG library, seeking to maintain an API that is similar to the original library. As such, the majority of examples available online should be easy to apply to this crate.
Examples
The following example uses the intra-process transport to set up a request/reply socket pair. The "client" sends a String to the "server" which responds with a nice phrase.
use nng::*; // Set up the server and listen for connections on the specified address. let address = "inproc://nng/lib.rs"; let server = Socket::new(Protocol::Rep0).unwrap(); server.listen(address).unwrap(); // Set up the client and connect to the specified address let mut client = Socket::new(Protocol::Req0).unwrap(); client.dial(address).unwrap(); // Send the request from the client to the server. let request = b"Ferris"[..].into(); client.send(request).unwrap(); // Receive the message on the server and send back the reply let request = { let req = server.recv().unwrap(); String::from_utf8(req.to_vec()).unwrap() }; assert_eq!(request, "Ferris"); let reply = format!("Hello, {}!", request).as_bytes().into(); server.send(reply).unwrap(); // Get the response on the client side. let reply = { let rep = client.recv().unwrap(); String::from_utf8(rep.to_vec()).unwrap() }; assert_eq!(reply, "Hello, Ferris!");
Additional examples are in the examples
directory.
Modules
options | Options available to configure |
Structs
Aio | An asynchronous I/O context. |
Body | The body of a |
Context | A socket context. |
Dialer | A constructed and running dialer. |
DialerOptions | Configuration utility for nanomsg-next-generation dialers. |
Header | The header of a |
Listener | A constructed and running listener. |
ListenerOptions | Configuration utility for nanomsg-next-generation listeners. |
Message | An |
Pipe | A nanomsg-next-generation pipe. |
Socket | A nanomsg-next-generation socket. |
Enums
AioResult | The result of an AIO operation. |
Error | Errors potentially returned by NNG operations. |
PipeEvent | An event that happens on a Pipe instance. |
Protocol | Protocols available for use by sockets. |
SocketAddr | Represents the addresses used by the underlying transports. |
Type Definitions
Result | Specialized |