Trait rotor_stream::Protocol [] [src]

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>) -> Request<Self>;
    fn bytes_read(self, transport: &mut Transport<Self::Socket>, end: usize, scope: &mut Scope<Self::Context>) -> Request<Self>;
    fn bytes_flushed(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>;
    fn timeout(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>;
    fn wakeup(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>;

    fn exception(self, _transport: &mut Transport<Self::Socket>, _reason: Exception, _scope: &mut Scope<Self::Context>) -> Request<Self> { ... }
}

Associated Types

Required Methods

fn create(seed: Self::Seed, sock: &mut Self::Socket, scope: &mut Scope<Self::Context>) -> Request<Self>

Starting the protocol (e.g. accepted a socket)

fn bytes_read(self, transport: &mut Transport<Self::Socket>, end: usize, scope: &mut Scope<Self::Context>) -> Request<Self>

The action WaitBytes or WaitDelimiter is complete

Note you don't have to consume input buffer. The data is in the transport, but you are free to ignore it. This may be useful for example to yield Bytes(4) to read the header size and then yield bigger value to read the whole header at once. But be careful, if you don't consume bytes you will repeatedly receive them again.

fn bytes_flushed(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>

The action Flush is complete

fn timeout(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>

Timeout happened, which means either deadline reached in Bytes, Delimiter, Flush. Or Sleep has passed.

fn wakeup(self, transport: &mut Transport<Self::Socket>, scope: &mut Scope<Self::Context>) -> Request<Self>

Message received (from the main loop)

Provided Methods

fn exception(self, _transport: &mut Transport<Self::Socket>, _reason: Exception, _scope: &mut Scope<Self::Context>) -> Request<Self>

The method is called when too much bytes are read but no delimiter is found within the number of bytes specified.

The usual case is to just close the connection (because it's probably DoS attack is going on or the protocol mismatch), but sometimes you want to send error code, like 413 Entity Too Large for HTTP.

Note it's your responsibility to wait for the buffer to be flushed. If you write to the buffer and then return None immediately, your data will be silently discarded.

Implementors