From a224e014b04f2ba5ab904174a7ed8927b07b2104 Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 7 Sep 2018 00:02:42 +1000 Subject: [PATCH] parsing msgs --- src/main.rs | 59 ++------------------------------------- src/net.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 57 deletions(-) diff --git a/src/main.rs b/src/main.rs index bdd9ec9e..dea80e75 100755 --- a/src/main.rs +++ b/src/main.rs @@ -14,61 +14,8 @@ mod net; mod combat; mod skill; -use std::rc::Rc; -use std::cell::Cell; -use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error}; -use serde_cbor::{to_value}; - -use net::{generate}; - -struct Server { - out: Sender, - count: Rc>, -} - -impl Handler for Server { - - fn on_open(&mut self, _: Handshake) -> Result<()> { - // We have a new connection, so we increment the connection counter - println!("somebody joined"); - Ok(self.count.set(self.count.get() + 1)) - } - - fn on_message(&mut self, msg: Message) -> Result<()> { - // Tell the user the current count - println!("The number of live connections is {}", self.count.get()); - println!("{:?}", to_value(msg.into_data()).unwrap()); - - let reply = Message::binary(generate()); - - // Echo the message back - self.out.send(reply) - } - - fn on_close(&mut self, code: CloseCode, reason: &str) { - match code { - CloseCode::Normal => println!("The client is done with the connection."), - CloseCode::Away => println!("The client is leaving the site."), - CloseCode::Abnormal => println!( - "Closing handshake failed! Unable to obtain closing status from client."), - _ => println!("The client encountered an error: {}", reason), - } - - // The connection is going down, so we need to decrement the count - self.count.set(self.count.get() - 1) - } - - fn on_error(&mut self, err: Error) { - println!("The server encountered an error: {:?}", err); - } - -} +use net::{start}; fn main() { - // Cell gives us interior mutability so we can increment - // or decrement the count between handlers. - // Rc is a reference-counted box for sharing the count between handlers - // since each handler needs to own its contents. - let count = Rc::new(Cell::new(0)); - listen("127.0.0.1:40000", |out| { Server { out: out, count: count.clone() } }).unwrap(); -} \ No newline at end of file + start() +} diff --git a/src/net.rs b/src/net.rs index 315cb541..949662f6 100755 --- a/src/net.rs +++ b/src/net.rs @@ -1,6 +1,70 @@ use cryp::{Cryp}; use skill::{Skill}; -use serde_cbor::{to_vec}; +use serde_cbor::{to_vec,from_slice,Value}; +use serde_cbor::error::Error as CborError; +use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error}; +use std::result::Result as StdResult; + +#[derive(Debug)] +enum RpcError { + Parse(CborError), + // UnknownMethod, +} + +struct Rpc; +impl Rpc { + pub fn receive(&self, msg: Message) -> StdResult, RpcError> { + // specify rpc message as the resulting type + match from_slice::(&msg.into_data()) { + Ok(v) => { + println!("{:?}", v); + Ok(generate()) + }, + Err(e) => Err(RpcError::Parse(e)), + } + } +} + +#[derive(Debug,Clone,Serialize,Deserialize)] +struct RpcMessage { + method: String, +} + +struct Server { + out: Sender, + rpc: Rpc, +} + +impl Handler for Server { + fn on_open(&mut self, _: Handshake) -> Result<()> { + // We have a new connection, so we increment the connection counter + println!("somebody joined"); + Ok(()) + } + + fn on_message(&mut self, msg: Message) -> Result<()> { + let reply = Message::binary(generate()); + self.out.send(reply) + } + + fn on_close(&mut self, code: CloseCode, reason: &str) { + match code { + CloseCode::Normal => println!("The client is done with the connection."), + CloseCode::Away => println!("The client is leaving the site."), + CloseCode::Abnormal => println!( + "Closing handshake failed! Unable to obtain closing status from client."), + _ => println!("The client encountered an error: {}", reason), + } + } + + fn on_error(&mut self, err: Error) { + println!("The server encountered an error: {:?}", err); + } +} + +pub fn start() { + listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {} } }).unwrap(); +} pub fn generate() -> Vec { let level_two = Cryp::new() @@ -13,4 +77,17 @@ pub fn generate() -> Vec { Ok(v) => v, Err(e) => panic!("couldn't serialize cryp"), } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rpc_parse() { + let rpc = Rpc {}; + let msg = RpcMessage { method: "grep".to_string() }; + let v = to_vec(&msg).unwrap(); + let received = rpc.receive(Message::Binary(v)); + } } \ No newline at end of file