From f25e96c2ae3fd5b177ec3eec176ccf0ae381984c Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 9 Sep 2020 12:12:39 +0200 Subject: [PATCH] Update README Signed-off-by: trivernis --- README.md | 23 +++++++++++++++++++++++ src/server.rs | 13 ++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5169d2c..52584e2 100644 --- a/README.md +++ b/README.md @@ -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 + } +} +``` \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index 310166e..79315a9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -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; @@ -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)?;