nng_c/
lib.rs

1//!High level bindings to the lib nng
2//!
3//!Version corresponds to C library
4//!
5//!## Features
6//!
7//!- `http` - Enables http transport;
8//!- `tls` - Enables TLS transpor;
9//!- `websocket` - Enables websocket transport. Implies `http` feature;
10//!- `log` - Enables logging via [log](https://crates.io/crates/log) crate;
11//!- `tracing` - Enables logging via [tracing](https://crates.io/crates/tracing) crate.
12//!
13//!## Usage
14//!
15//!Basic example of client and server communication
16//!
17//!```rust
18//!use nng_c::{options, Socket, Message, ErrorCode};
19//!
20//!use core::time;
21//!
22//!//Feel free to append zero char to avoid unnecessary allocations
23//!const ADDR: &str = "ipc://nng-c-example\0";
24//!const REQ_TIMEOUT: options::Req = options::Req {
25//!     resend_time: Some(time::Duration::from_millis(50)),
26//!     resend_tick: Some(time::Duration::from_millis(1)),
27//!};
28//!
29//!fn server() -> Result<(), ErrorCode> {
30//!    let server = Socket::rep0()?;
31//!    server.listen(ADDR.into()).expect("listen");
32//!
33//!    loop {
34//!        let msg = server.recv_msg()?;
35//!        let body = msg.body();
36//!        let msg = core::str::from_utf8(body).expect("utf-8 bytes");
37//!        match msg {
38//!            "quit" => break Ok(()),
39//!            other => {
40//!                println!("Received bytes(len={})={:?}", other.len(), other);
41//!            }
42//!        }
43//!    }
44//!}
45//!
46//!let server = std::thread::spawn(server);
47//!
48//!//Wait for thread to spin
49//!std::thread::sleep(time::Duration::from_millis(10));
50//!
51//!let client = Socket::req0().expect("Create client");
52//!client.set_opt(REQ_TIMEOUT).expect("Set options");
53//!
54//!client.connect(ADDR.into()).expect("connect");
55//!
56//!let mut msg = Message::new().expect("create message");
57//!msg.append("ping".as_bytes()).expect("Input bytes");
58//!client.send_msg(msg).expect("send message");
59//!
60//!let mut msg = Message::new().expect("create message");
61//!msg.append("quit".as_bytes()).expect("Input bytes");
62//!client.send_msg(msg).expect("send quit");
63//!
64//!server.join().expect("Finish server successfully");
65//!```
66
67#![no_std]
68#![warn(missing_docs)]
69//Imagine enabling this shit by default
70#![allow(clippy::deprecated_clippy_cfg_attr)]
71#![allow(clippy::style)]
72
73extern crate alloc;
74
75mod defs;
76mod aio;
77pub mod str;
78pub use nng_c_sys as sys;
79mod msg;
80pub use msg::Message;
81mod error;
82pub use error::{ErrorCode, NngError};
83pub mod options;
84pub mod socket;
85pub use socket::Socket;
86pub mod tls;
87pub mod utils;