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;
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
pub enum Socket {
Udp(UdpSock),
Tcp(TcpSock),
}
impl Socket {
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),
}
}
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),
}
}
pub fn local_addr(&self) -> ::Res<SocketAddr> {
match *self {
Socket::Udp(ref sock) => sock.local_addr(),
Socket::Tcp(ref sock) => sock.local_addr(),
}
}
pub fn peer_addr(&self) -> ::Res<SocketAddr> {
match *self {
Socket::Udp(ref sock) => sock.peer_addr(),
Socket::Tcp(ref sock) => sock.peer_addr(),
}
}
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),
}
}
pub fn ttl(&self) -> ::Res<u32> {
match *self {
Socket::Udp(ref sock) => sock.ttl(),
Socket::Tcp(ref sock) => sock.ttl(),
}
}
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(),
}
}
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(),
}
}
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),
}
}
pub fn set_linger(&self, dur: Option<Duration>) -> ::Res<()> {
match *self {
Socket::Udp(_) => Ok(()),
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)
}
}