Trait Protocol

Source
pub trait Protocol: Sized {
    type Context;
    type Socket: StreamSocket;
    type Seed;

    // Required methods
    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<dyn Error>>;
    fn wakeup(
        self,
        transport: &mut Transport<'_, Self::Socket>,
        scope: &mut Scope<'_, Self::Context>,
    ) -> Intent<Self>;
}

Required Associated Types§

Required Methods§

Source

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

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

Source

fn bytes_read( self, transport: &mut Transport<'_, Self::Socket>, end: usize, scope: &mut Scope<'_, Self::Context>, ) -> Intent<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.

Source

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

The action Flush is complete

Source

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

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

Source

fn exception( self, _transport: &mut Transport<'_, Self::Socket>, reason: Exception, _scope: &mut Scope<'_, Self::Context>, ) -> Intent<Self>

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

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 Intent::done() immediately, your data will be silently discarded.

The WriteError and ConnectError are never passed here but passed into fatal handler instead.

Source

fn fatal( self, reason: Exception, scope: &mut Scope<'_, Self::Context>, ) -> Option<Box<dyn Error>>

This method is called on fatal errors of the connection

Connection can’t proceed after this method is called

Note: we use shared Exception type for both exception and fatal exceptions. This method receives WriteError and ConnectError options only.

Source

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

Message received (from the main loop)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§