Skip to content

Commit

Permalink
1.8.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed May 31, 2024
1 parent 09ae217 commit 9fafdff
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
run: cargo check

- name: Test
run: cargo test --features http,tls
run: cargo test --features http,tls --release
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nng-c"
version = "1.8.0"
version = "1.8.0-beta.1"
authors = ["Douman <douman@gmx.se>"]
edition = "2018"
keywords = ["nng", "nanomsg"]
Expand Down
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,58 @@ 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](https://crates.io/crates/log) crate

## Usage

Basic example of client and server communication

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

use core::time;

//Feel free to append zero char to avoid unnecessary allocations
const ADDR: &str = "inproc://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);

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");

```
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
//!High level bindings to the lib 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](https://crates.io/crates/log) crate
//!
//!## Usage
//!
//!Basic example of client and server communication
//!
//!```rust
//!use nng_c::{options, Socket, Message, ErrorCode};
//!
//!use core::time;
//!
//!//Feel free to append zero char to avoid unnecessary allocations
//!const ADDR: &str = "inproc://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);
//!
//!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");
//!```
#![no_std]
#![warn(missing_docs)]
Expand Down

0 comments on commit 9fafdff

Please sign in to comment.