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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::cell::RefCell;
use std::io;
use std::net::{SocketAddr, ToSocketAddrs, TcpListener, TcpStream};
use std::fmt;
use IntoInner;
use socket::Socket;
use sys::c;
pub struct TcpBuilder {
socket: RefCell<Option<Socket>>,
}
impl TcpBuilder {
pub fn new_v4() -> io::Result<TcpBuilder> {
Socket::new(c::AF_INET, c::SOCK_STREAM).map(::FromInner::from_inner)
}
pub fn new_v6() -> io::Result<TcpBuilder> {
Socket::new(c::AF_INET6, c::SOCK_STREAM).map(::FromInner::from_inner)
}
pub fn bind<T>(&self, addr: T) -> io::Result<&TcpBuilder>
where T: ToSocketAddrs
{
self.with_socket(|sock| {
let addr = try!(::one_addr(addr));
sock.bind(&addr)
}).map(|()| self)
}
pub fn listen(&self, backlog: i32) -> io::Result<TcpListener> {
self.with_socket(|sock| {
sock.listen(backlog)
}).and_then(|()| {
self.to_tcp_listener()
})
}
pub fn connect<T>(&self, addr: T) -> io::Result<TcpStream>
where T: ToSocketAddrs
{
self.with_socket(|sock| {
let err = io::Error::new(io::ErrorKind::Other,
"no socket addresses resolved");
try!(addr.to_socket_addrs()).fold(Err(err), |prev, addr| {
prev.or_else(|_| sock.connect(&addr))
})
}).and_then(|()| {
self.to_tcp_stream()
})
}
pub fn to_tcp_stream(&self) -> io::Result<TcpStream> {
self.socket.borrow_mut().take().map(|s| s.into_inner().into_tcp_stream())
.ok_or(io::Error::new(io::ErrorKind::Other,
"socket has already been consumed"))
}
pub fn to_tcp_listener(&self) -> io::Result<TcpListener> {
self.socket.borrow_mut().take()
.map(|s| s.into_inner().into_tcp_listener())
.ok_or(io::Error::new(io::ErrorKind::Other,
"socket has already been consumed"))
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
match *self.socket.borrow() {
Some(ref s) => s.getsockname(),
None => Err(io::Error::new(io::ErrorKind::Other,
"builder has already finished its socket")),
}
}
fn with_socket<F>(&self, f: F) -> io::Result<()>
where F: FnOnce(&Socket) -> io::Result<()>
{
match *self.socket.borrow() {
Some(ref s) => f(s),
None => Err(io::Error::new(io::ErrorKind::Other,
"builder has already finished its socket")),
}
}
}
impl fmt::Debug for TcpBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TcpBuilder {{ socket: {:?} }}",
self.socket.borrow().as_ref().unwrap())
}
}
impl ::AsInner for TcpBuilder {
type Inner = RefCell<Option<Socket>>;
fn as_inner(&self) -> &RefCell<Option<Socket>> { &self.socket }
}
impl ::FromInner for TcpBuilder {
type Inner = Socket;
fn from_inner(sock: Socket) -> TcpBuilder {
TcpBuilder { socket: RefCell::new(Some(sock)) }
}
}