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
use crypto::{DecryptContext, EncryptContext};
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use std::io;
use std::net::SocketAddr;
use std::time::Duration;
use tcp_sock::TcpSock;
use udp::UdpSock;
use Priority;

/// Protocol agnostic socket that wraps [`UdpSock`] and [`TcpSock`].
///
/// [`UdpSock`]: struct.UdpSock.html
/// [`TcpSock`]: struct.TcpSock.html
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
pub enum Socket {
    Udp(UdpSock),
    Tcp(TcpSock),
}

impl Socket {
    /// Specify data encryption context which will determine how outgoing data is encrypted.
    pub fn set_encrypt_ctx(&mut self, enc_ctx: EncryptContext) -> ::Res<()> {
        match *self {
            Socket::Udp(ref mut sock) => sock.set_encrypt_ctx(enc_ctx),
            Socket::Tcp(ref mut sock) => sock.set_encrypt_ctx(enc_ctx),
        }
    }

    /// Specify data decryption context which will determine how incoming data is decrypted.
    pub fn set_decrypt_ctx(&mut self, dec_ctx: DecryptContext) -> ::Res<()> {
        match *self {
            Socket::Udp(ref mut sock) => sock.set_decrypt_ctx(dec_ctx),
            Socket::Tcp(ref mut sock) => sock.set_decrypt_ctx(dec_ctx),
        }
    }

    /// Get the local address socket is bound to.
    pub fn local_addr(&self) -> ::Res<SocketAddr> {
        match *self {
            Socket::Udp(ref sock) => sock.local_addr(),
            Socket::Tcp(ref sock) => sock.local_addr(),
        }
    }

    /// Get the address socket was connected to.
    pub fn peer_addr(&self) -> ::Res<SocketAddr> {
        match *self {
            Socket::Udp(ref sock) => sock.peer_addr(),
            Socket::Tcp(ref sock) => sock.peer_addr(),
        }
    }

    /// Set Time To Live value for the underlying socket.
    pub fn set_ttl(&self, ttl: u32) -> ::Res<()> {
        match *self {
            Socket::Udp(ref sock) => sock.set_ttl(ttl),
            Socket::Tcp(ref sock) => sock.set_ttl(ttl),
        }
    }

    /// Retrieve Time To Live value.
    pub fn ttl(&self) -> ::Res<u32> {
        match *self {
            Socket::Udp(ref sock) => sock.ttl(),
            Socket::Tcp(ref sock) => sock.ttl(),
        }
    }

    /// Retrieve last socket error, if one exists.
    pub fn take_error(&self) -> ::Res<Option<io::Error>> {
        match *self {
            Socket::Udp(ref sock) => sock.take_error(),
            Socket::Tcp(ref sock) => sock.take_error(),
        }
    }

    /// Read message from the connected socket. Call this method once socket becomes readable.
    ///
    /// # Returns:
    ///
    ///   - Ok(Some(data)): data has been successfully read from the socket
    ///   - Ok(None):       there is not enough data in the socket. Call `read()`
    ///                     again in the next invocation of the `ready` handler.
    ///   - Err(error):     there was an error reading from the socket.
    pub fn read<T: DeserializeOwned + Serialize>(&mut self) -> ::Res<Option<T>> {
        match *self {
            Socket::Udp(ref mut sock) => sock.read(),
            Socket::Tcp(ref mut sock) => sock.read(),
        }
    }

    /// Write a message to the connected socket.
    ///
    /// # Returns:
    ///
    ///   - Ok(true):   the message has been successfully written.
    ///   - Ok(false):  the message has been queued, but not yet fully written.
    ///                 will be attempted in the next write schedule.
    ///   - Err(error): there was an error while writing to the socket.
    pub fn write<T: Serialize>(&mut self, msg: Option<(T, Priority)>) -> ::Res<bool> {
        match *self {
            Socket::Udp(ref mut sock) => sock.write(msg),
            Socket::Tcp(ref mut sock) => sock.write(msg),
        }
    }

    /// Sets linger time for connection based protocols.
    pub fn set_linger(&self, dur: Option<Duration>) -> ::Res<()> {
        match *self {
            Socket::Udp(_) => Ok(()), // do nothing for UDP socket
            Socket::Tcp(ref sock) => sock.set_linger(dur),
        }
    }
}

impl From<UdpSock> for Socket {
    fn from(sock: UdpSock) -> Socket {
        Socket::Udp(sock)
    }
}

impl From<TcpSock> for Socket {
    fn from(sock: TcpSock) -> Socket {
        Socket::Tcp(sock)
    }
}