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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use io::{Readiness, Ready};
use reactor::{self, Source};
use mio::would_block;
use mio::tcp as mio;
use std::io::{self, Read, Write};
use std::net::SocketAddr;
pub struct TcpListener {
mio: mio::TcpListener,
source: Source,
}
impl TcpListener {
pub fn bind(addr: &SocketAddr) -> io::Result<TcpListener> {
let mio = try!(mio::TcpListener::bind(addr));
TcpListener::watch(mio)
}
pub fn watch(mio: mio::TcpListener) -> io::Result<TcpListener> {
let source = try!(reactor::register_source(&mio, Ready::readable()));
Ok(TcpListener {
mio: mio,
source: source,
})
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.mio.local_addr()
}
pub fn accept(&self) -> io::Result<Option<mio::TcpStream>> {
if !self.source.is_readable() {
return Ok(None);
}
match self.mio.accept() {
Ok((socket, _)) => {
self.source.advance();
Ok(Some(socket))
}
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
self.source.unset_readable();
} else {
self.source.advance();
}
Err(e)
}
}
}
}
impl Readiness for TcpListener {
fn is_readable(&self) -> bool {
self.source.is_readable()
}
fn is_writable(&self) -> bool {
false
}
}
pub struct TcpStream {
mio: mio::TcpStream,
source: Source,
}
impl TcpStream {
pub fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
let mio = try!(mio::TcpStream::connect(addr));
TcpStream::watch(mio)
}
pub fn watch(mio: mio::TcpStream) -> io::Result<TcpStream> {
let source = try!(reactor::register_source(&mio, Ready::all()));
Ok(TcpStream {
mio: mio,
source: source,
})
}
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
if !self.source.is_readable() {
return Err(would_block());
}
match (&self.mio).read(buf) {
Ok(n) => {
self.source.advance();
Ok(n)
}
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
self.source.unset_readable();
} else {
self.source.advance();
}
Err(e)
}
}
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
if !self.source.is_writable() {
return Err(would_block());
}
match (&self.mio).write(buf) {
Ok(n) => {
self.source.advance();
Ok(n)
}
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
self.source.unset_writable();
} else {
self.source.advance();
}
Err(e)
}
}
}
}
impl Readiness for TcpStream {
fn is_readable(&self) -> bool {
self.source.is_readable()
}
fn is_writable(&self) -> bool {
self.source.is_writable()
}
}
impl io::Read for TcpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
TcpStream::read(&*self, buf)
}
}
impl io::Write for TcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
TcpStream::write(&*self, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<'a> io::Read for &'a TcpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
TcpStream::read(self, buf)
}
}
impl<'a> io::Write for &'a TcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
TcpStream::write(self, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}