nng-c

Crates.ionng-c
lib.rsnng-c
version
sourcesrc
created_at2024-05-31 13:08:12.177686
updated_at2024-12-08 06:50:44.447856
descriptionHigh level bindings nng C library
homepage
repositoryhttps://github.com/DoumanAsh/nng-c
max_upload_size
id1258008
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Douman (DoumanAsh)

documentation

https://docs.rs/crate/nng-c

README

nng-c

Actions Status Crates.io Documentation

High level bindings to nng.

Version corresponds to C library

Features

  • http - Enables http transport;
  • tls - Enables TLS transport;
  • websocket - Enables websocket transport. Implies http feature;
  • log - Enables logging via log crate;
  • tracing - Enables logging via tracing crate.

Usage

Basic example of client and server communication

use nng_c::{options, Socket, Message, ErrorCode};

use core::time;

//Feel free to append zero char to avoid unnecessary allocations
const ADDR: &str = "ipc://nng-c-example\0";
const REQ_TIMEOUT: options::Req = options::Req {
     resend_time: Some(time::Duration::from_millis(50)),
     resend_tick: Some(time::Duration::from_millis(1)),
};

fn server() -> Result<(), ErrorCode> {
    let server = Socket::rep0()?;
    server.listen(ADDR.into()).expect("listen");

    loop {
        let msg = server.recv_msg()?;
        let body = msg.body();
        let msg = core::str::from_utf8(body).expect("utf-8 bytes");
        match msg {
            "quit" => break Ok(()),
            other => {
                println!("Received bytes(len={})={:?}", other.len(), other);
            }
        }
    }
}

let server = std::thread::spawn(server);

//Wait for thread to spin
std::thread::sleep(time::Duration::from_millis(10));

let client = Socket::req0().expect("Create client");
client.set_opt(REQ_TIMEOUT).expect("Set options");

client.connect(ADDR.into()).expect("connect");

let mut msg = Message::new().expect("create message");
msg.append("ping".as_bytes()).expect("Input bytes");
client.send_msg(msg).expect("send message");

let mut msg = Message::new().expect("create message");
msg.append("quit".as_bytes()).expect("Input bytes");
client.send_msg(msg).expect("send quit");

server.join().expect("Finish server successfully");

Commit count: 20

cargo fmt