use std::io;
use std::error::Error;
use rotor::Scope;
use {Transport, Intent, StreamSocket};
quick_error!{
#[derive(Debug)]
pub enum Exception {
EndOfStream {
description("end of stream reached")
}
LimitReached {
description("reached the limit of bytes buffered")
}
ReadError(err: io::Error) {
description("error when reading from stream")
display("read error: {}", err)
}
WriteError(err: io::Error) {
description("error when writing to stream")
display("write error: {}", err)
}
ConnectError(err: io::Error) {
description("error when connecting to an address")
display("connection error: {}", err)
}
}
}
#[derive(Debug)]
pub enum Expectation {
Bytes(usize),
Delimiter(usize, &'static [u8], usize),
Flush(usize),
Sleep,
}
pub trait Protocol: Sized {
type Context;
type Socket: StreamSocket;
type Seed;
fn create(seed: Self::Seed, sock: &mut Self::Socket,
scope: &mut Scope<Self::Context>)
-> Intent<Self>;
fn bytes_read(self, transport: &mut Transport<Self::Socket>,
end: usize, scope: &mut Scope<Self::Context>)
-> Intent<Self>;
fn bytes_flushed(self, transport: &mut Transport<Self::Socket>,
scope: &mut Scope<Self::Context>)
-> Intent<Self>;
fn timeout(self, transport: &mut Transport<Self::Socket>,
scope: &mut Scope<Self::Context>)
-> Intent<Self>;
fn exception(self, _transport: &mut Transport<Self::Socket>,
reason: Exception, _scope: &mut Scope<Self::Context>)
-> Intent<Self>;
fn fatal(self, reason: Exception, scope: &mut Scope<Self::Context>)
-> Option<Box<Error>>;
fn wakeup(self, transport: &mut Transport<Self::Socket>,
scope: &mut Scope<Self::Context>)
-> Intent<Self>;
}