1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::io;
use tokio_core::io::{Codec, EasyBuf};
use types::{GopherRequest, GopherResponse};
pub struct Server;
impl Codec for Server {
type In = GopherRequest;
type Out = GopherResponse;
fn decode(&mut self, buf: &mut EasyBuf) -> io::Result<Option<GopherRequest>> {
let line = match buf.as_slice().windows(2).position(|w| w == b"\r\n") {
Some(i) => buf.drain_to(i),
None => return Ok(None)
};
buf.drain_to(2);
Ok(Some(GopherRequest::decode(line)))
}
fn encode(&mut self, msg: GopherResponse, buf: &mut Vec<u8>) -> io::Result<()> {
msg.encode(buf)
}
}