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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::io as std_io;
use std::fmt::Debug;

use futures::Poll;
use bytes::buf::{Buf, BufMut};
use tokio::net::TcpStream;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_tls::TlsStream;

/// Abstraction over Tcp, TcpTls (and Mock)
///
/// Allows treating both `TcpStream` and
/// `TlsStream<TcpStream>` the same.
///
/// # Features
/// ## `mock_support`
///
/// if enabled this abstracts not only over `TcpStream` and
/// `TlsStream<TcpStream` but also `Box<MockStream+Send>`
///
#[derive(Debug)]
pub enum Socket {
    Secure(TlsStream<TcpStream>),
    Insecure(TcpStream),
    #[cfg(feature="mock-support")]
    Mock(Box<MockStream + Send>)
}

impl Socket {

    /// true if it's a `TlsStream` (or if mock says so)
    pub fn is_secure(&self) -> bool {
        match *self {
            Socket::Secure(_) => true,
            Socket::Insecure(_) => false,
            #[cfg(feature="mock-support")]
            Socket::Mock(ref mock) => mock.is_secure()
        }
    }
}

macro_rules! socket_mux {
    ($self:ident, |$socket:ident| $block:block) => ({
        match *$self {
            Socket::Secure(ref mut $socket) => $block,
            Socket::Insecure(ref mut $socket) => $block,
            #[cfg(feature="mock-support")]
            Socket::Mock(ref mut $socket) => $block
        }
    });
}

impl std_io::Read for Socket {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, std_io::Error> {
        socket_mux! {self, |socket| {
            socket.read(buf)
        }}
    }
}

impl std_io::Write for Socket {
    fn write(&mut self, buf: &[u8]) -> Result<usize, std_io::Error> {
        socket_mux! {self, |socket| {
            socket.write(buf)
        }}
    }

    fn flush(&mut self) -> Result<(), std_io::Error> {
        socket_mux! {self, |socket| {
            socket.flush()
        }}
    }
}

impl AsyncRead for Socket {
    #[inline]
    unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
        match *self {
            Socket::Secure(ref socket) => socket.prepare_uninitialized_buffer(buf),
            Socket::Insecure(ref socket) => socket.prepare_uninitialized_buffer(buf),
            #[cfg(feature="mock-support")]
            Socket::Mock(ref socket) => socket.prepare_uninitialized_buffer(buf)
        }
    }

    #[inline]
    fn poll_read(&mut self, buf: &mut [u8]) -> Poll<usize, std_io::Error> {
        socket_mux! {self, |socket| {
            socket.poll_read(buf)
        }}
    }

    #[inline]
    fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, std_io::Error>
        where Self: Sized,
    {
        socket_mux! {self, |socket| {
            socket.read_buf(buf)
        }}
    }
}

impl AsyncWrite for Socket {
    fn poll_write(&mut self, buf: &[u8]) -> Poll<usize, std_io::Error> {
        socket_mux! {self, |socket| {
            AsyncWrite::poll_write(socket, buf)
        }}
    }

    fn poll_flush(&mut self) -> Poll<(), std_io::Error> {
        socket_mux! {self, |socket| {
            AsyncWrite::poll_flush(socket)
        }}
    }

    fn shutdown(&mut self) -> Poll<(), std_io::Error> {
        socket_mux! {self, |socket| {
            AsyncWrite::shutdown(socket)
        }}
    }

    fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, std_io::Error>
        where Self: Sized,
    {
        socket_mux! {self, |socket| {
            AsyncWrite::write_buf(socket, buf)
        }}
    }
}

/// trait representing a mock stream
pub trait MockStream: Debug + AsyncRead + AsyncWrite + 'static {
    fn is_secure(&self) -> bool {
        false
    }
    fn set_is_secure(&mut self, secure: bool);
}