-use std::io::{ TcpListener, TcpStream, Acceptor, Listener, IoError, IoResult };
+use std::io::{ TcpListener, TcpStream, Acceptor, Listener, IoError, IoResult, EndOfFile };
use std::io::net::tcp::{ TcpAcceptor };
-use command::{ Command, Packet, Error };
+use packet::{ Packet, IOReadError };
pub struct Server {
acceptor: TcpAcceptor
self.acceptor.close_accept()
}
- fn handle_client(mut stream: TcpStream) {
-
+ fn handle_client(mut stream: TcpStream) {
let mut current_timestamp = 0u64;
- match Packet::read(&mut stream) {
- Ok(Command(packet)) => println!("CommandPacket"),
- Ok(Error(packet)) => println!("AnswerPacket"),
- Err(io_error) => println!("IO Error")
+ loop {
+ // stream.set_timeout(Some(1000000000));
+ match Packet::read(&mut stream) {
+ Ok(packet) => {
+ if packet.timestamp <= current_timestamp {
+ println!("Error, timestamp mismatch, current timestamp: {}, packet received: {}", current_timestamp, packet);
+ } else {
+ current_timestamp += 1;
+ println!("Receive packet: {}", packet);
+ }
+ },
+ // Socket has been closed.
+ Err(IOReadError(IoError { kind: EndOfFile, .. })) => {
+ return;
+ },
+ Err(e) => {
+ println!("Read error: {}", e)
+ }
+ }
}
}
}
\ No newline at end of file