Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
Signed-off-by: trivernis <trivernis@protonmail.com>
  • Loading branch information
Trivernis committed Sep 9, 2020
1 parent 49dd76c commit f25e96c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# msg-rpc
An rpc server implementation using msgpack

## Usage

Add the crate to the dependencies

```toml
[dependencies]
msgrpc = {git = "https://github.com/flotte-goes-smart/msg-rpc/tree/main"}
```


```rust
pub fn main() {
let mut server = RPCServer::new("127.0.0.1:".to_string());
let mut receiver = Arc::clone(&server.receiver);
thread::spawn(move || {
server.start();
});
for handler in receiver {
// handle the message and return a response
}
}
```
13 changes: 12 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io;
use byteorder::{BigEndian, ByteOrder};
use crate::message::Message;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::mpsc::{Receiver, Sender, channel};
use crossbeam_utils::sync::WaitGroup;
use std::mem;

Expand Down Expand Up @@ -32,6 +32,17 @@ pub struct RpcServer {
}

impl RpcServer {

/// Creates a new RPC Server
pub fn new(address: String) -> Self {
let (tx, rx) = channel();
Self {
address,
sender: tx,
receiver: Arc::new(Mutex::new(rx))
}
}

/// Starts the RPC server
pub fn start(&mut self) -> io::Result<()> {
let listener = TcpListener::bind(&self.address)?;
Expand Down

0 comments on commit f25e96c

Please sign in to comment.