parsing msgs
This commit is contained in:
parent
81562f6db6
commit
a224e014b0
59
src/main.rs
59
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<Cell<u32>>,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
start()
|
||||
}
|
||||
|
||||
79
src/net.rs
79
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<Vec<u8>, RpcError> {
|
||||
// specify rpc message as the resulting type
|
||||
match from_slice::<RpcMessage>(&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<u8> {
|
||||
let level_two = Cryp::new()
|
||||
@ -13,4 +77,17 @@ pub fn generate() -> Vec<u8> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user