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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::framed_io::ReaderWriter;
use crate::id_sequence::IdSequence;
use bytesize::ByteSize;
use failure::Error;
use toku_protocol::frames::{Error as ErrorFrame, TokuFrame, Push, Request, Response};
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use tokio::net::TcpStream;

/// Specific types of toku frames that are delegated to a connection handler.  The rest of the
/// frames will be handled by the connection itself.
#[derive(Debug)]
pub enum DelegatedFrame {
    Push(Push),
    Request(Request),
    Response(Response),
    Error(ErrorFrame),
}

/// Settings negotiated from handshake.
#[derive(Debug)]
pub struct Ready {
    pub ping_interval: Duration,
    pub encoding: &'static str,
}

/// A trait that handles the specific functionality of a connection. The client and server each
/// implement this.
pub trait Handler: Send + Sync + 'static {
    /// Events specific to the implementing connection handler. They will be passed through to the
    /// handle_internal_event callback.
    type InternalEvent: Send;
    // Whether or not the connection should send a GoAway frame on close.
    const SEND_GO_AWAY: bool;

    /// The maximum payload size this connection can handle.
    fn max_payload_size(&self) -> ByteSize;
    /// Takes a tcp stream and completes an HTTP upgrade.
    fn upgrade(
        &self,
        tcp_stream: TcpStream,
    ) -> Pin<Box<dyn Future<Output = Result<TcpStream, Error>> + Send>>;
    /// Hello/HelloAck handshake.
    fn handshake(
        &mut self,
        reader_writer: ReaderWriter,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<(Ready, ReaderWriter), (Error, Option<ReaderWriter>)>>
                + Send,
        >,
    >;
    /// Handle a single delegated frame. Optionally returns a future that resolves to a
    /// Response. The Response will be sent back through the socket to the other side.
    fn handle_frame(
        &mut self,
        frame: DelegatedFrame,
        encoding: &'static str,
    ) -> Option<Pin<Box<dyn Future<Output = Result<Response, (Error, u32)>> + Send>>>;
    /// Handle internal events for this connection. Completely opaque to the connection. Optionally
    /// return a `TokuFrame` that will be sent back through the socket to the other side.
    fn handle_internal_event(
        &mut self,
        event: Self::InternalEvent,
        id_sequence: &mut IdSequence,
    ) -> Option<TokuFrame>;
    /// Periodic callback that fires whenever a ping fires.
    fn on_ping_received(&mut self);
}

impl From<Push> for DelegatedFrame {
    fn from(push: Push) -> DelegatedFrame {
        DelegatedFrame::Push(push)
    }
}

impl From<Request> for DelegatedFrame {
    fn from(request: Request) -> DelegatedFrame {
        DelegatedFrame::Request(request)
    }
}

impl From<Response> for DelegatedFrame {
    fn from(response: Response) -> DelegatedFrame {
        DelegatedFrame::Response(response)
    }
}

impl From<ErrorFrame> for DelegatedFrame {
    fn from(error: ErrorFrame) -> DelegatedFrame {
        DelegatedFrame::Error(error)
    }
}