--- a/gameServer2/src/protocol/messages.rs Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs Sat Jan 14 00:46:52 2017 +0300
@@ -9,6 +9,8 @@
Ping,
Pong,
Quit(Option<&'a str>),
+ Bye(&'a str),
+ LobbyLeft(&'a str),
//Cmd(&'a str, Vec<&'a str>),
Global(&'a str),
Watch(&'a str),
@@ -79,3 +81,31 @@
}
value
}
+
+fn construct_message(msg: & [&str]) -> String {
+ let mut m = String::with_capacity(64);
+
+ for s in msg {
+ m.push_str(s);
+ m.push('\n');
+ }
+ m.push('\n');
+
+ m
+}
+
+impl<'a> HWProtocolMessage<'a> {
+ pub fn to_raw_protocol(&self) -> String {
+ match self {
+ &HWProtocolMessage::Ping
+ => "PING\n\n".to_string(),
+ &HWProtocolMessage::Pong
+ => "PONG\n\n".to_string(),
+ &HWProtocolMessage::Bye(msg)
+ => construct_message(&["BYE", msg]),
+ &HWProtocolMessage::LobbyLeft(msg)
+ => construct_message(&["LOBBY_LEFT", msg]),
+ _ => String::new()
+ }
+ }
+}
--- a/gameServer2/src/protocol/mod.rs Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/protocol/mod.rs Sat Jan 14 00:46:52 2017 +0300
@@ -1,18 +1,21 @@
use netbuf;
use std::io::Read;
use std::io::Result;
+use nom::IResult;
-mod messages;
+pub mod messages;
mod parser;
-pub struct FrameDecoder {
+pub struct ProtocolDecoder {
buf: netbuf::Buf,
+ consumed: usize,
}
-impl FrameDecoder {
- pub fn new() -> FrameDecoder {
- FrameDecoder {
- buf: netbuf::Buf::new()
+impl ProtocolDecoder {
+ pub fn new() -> ProtocolDecoder {
+ ProtocolDecoder {
+ buf: netbuf::Buf::new(),
+ consumed: 0,
}
}
@@ -20,7 +23,20 @@
self.buf.read_from(stream)
}
- pub fn extract_messages(&mut self) -> &[u8] {
- &self.buf[..]
+ pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
+ let parse_result = parser::extract_messages(&self.buf[..]);
+ match parse_result {
+ IResult::Done(tail, msgs) => {
+ self.consumed = self.buf.len() - self.consumed - tail.len();
+ msgs
+ },
+ IResult::Incomplete(_) => unreachable!(),
+ IResult::Error(_) => unreachable!(),
+ }
+ }
+
+ pub fn sweep(&mut self) {
+ self.buf.consume(self.consumed);
+ self.consumed = 0;
}
}
--- a/gameServer2/src/protocol/parser.rs Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/protocol/parser.rs Sat Jan 14 00:46:52 2017 +0300
@@ -99,7 +99,7 @@
(BanNick(n, r, t)))
));
-named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
+named!(message<&[u8], HWProtocolMessage>, terminated!(alt!(
basic_message
| one_param_message
| cmd_message
@@ -107,6 +107,7 @@
), end_of_message
));
+named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message)));
#[test]
fn parse_test() {
@@ -118,4 +119,6 @@
assert_eq!(message(b"QUIT\n\n"), IResult::Done(&b""[..], Quit(None)));
assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo")));
assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me", "bad", 77)));
+
+ assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), IResult::Done(&b"P"[..], vec![Ping, Ping]));
}
--- a/gameServer2/src/server/client.rs Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/server/client.rs Sat Jan 14 00:46:52 2017 +0300
@@ -6,11 +6,13 @@
use netbuf;
use utils;
-use protocol::FrameDecoder;
+use protocol::ProtocolDecoder;
+use protocol::messages;
+use protocol::messages::HWProtocolMessage::*;
pub struct HWClient {
sock: TcpStream,
- decoder: FrameDecoder,
+ decoder: ProtocolDecoder,
buf_out: netbuf::Buf
}
@@ -18,7 +20,7 @@
pub fn new(sock: TcpStream) -> HWClient {
HWClient {
sock: sock,
- decoder: FrameDecoder::new(),
+ decoder: ProtocolDecoder::new(),
buf_out: netbuf::Buf::new(),
}
}
@@ -38,6 +40,10 @@
self.flush();
}
+ fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
+ self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
+ }
+
fn flush(&mut self) {
self.buf_out.write_to(&mut self.sock).unwrap();
self.sock.flush();
@@ -45,8 +51,21 @@
pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
let v = self.decoder.read_from(&mut self.sock)?;
- self.decoder.extract_messages();
println!("Read {} bytes", v);
+ let mut response = Vec::new();
+ {
+ let msgs = self.decoder.extract_messages();
+ for msg in msgs {
+ match msg {
+ Ping => response.push(Pong),
+ _ => println!("Unknown message")
+ }
+ }
+ }
+ for r in response {
+ self.send_msg(r);
+ }
+ self.decoder.sweep();
Ok(())
}
--- a/gameServer2/src/server/server.rs Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/server/server.rs Sat Jan 14 00:46:52 2017 +0300
@@ -3,7 +3,6 @@
use mio::*;
use std::io::Write;
use std::io;
-use netbuf;
use utils;
use server::client::HWClient;