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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (c) 2016 DWANGO Co., Ltd. All Rights Reserved.
// See the LICENSE file at the top-level directory of this distribution.

use futures::{Async, Future, Poll, Stream};
use mio;
use mio::net::{TcpListener as MioTcpListener, TcpStream as MioTcpStream};
use std::fmt;
use std::io;
use std::mem;
use std::net::SocketAddr;
use std::sync::Arc;

use super::{into_io_error, Bind};
use fiber::{self, Context};
use io::poll::{EventedHandle, Interest, Register};
use sync::oneshot::Monitor;

/// A structure representing a socket server.
///
/// # Examples
///
/// ```
/// // See also: fibers/examples/tcp_example.rs
/// # extern crate fibers;
/// # extern crate futures;
/// use fibers::{Executor, InPlaceExecutor, Spawn};
/// use fibers::net::{TcpListener, TcpStream};
/// use fibers::sync::oneshot;
/// use futures::{Future, Stream};
///
/// let mut executor = InPlaceExecutor::new().unwrap();
/// let (addr_tx, addr_rx) = oneshot::channel();
///
/// // Spawns TCP listener
/// executor.spawn(TcpListener::bind("127.0.0.1:0".parse().unwrap())
///     .and_then(|listener| {
///         let addr = listener.local_addr().unwrap();
///         println!("# Start listening: {}", addr);
///         addr_tx.send(addr).unwrap();
///         listener.incoming()
///             .for_each(move |(_client, addr)| {
///                 println!("# Accepted: {}", addr);
///                 Ok(())
///             })
///     })
///     .map_err(|e| panic!("{:?}", e)));
///
/// // Spawns TCP client
/// let mut monitor = executor.spawn_monitor(addr_rx.map_err(|e| panic!("{:?}", e))
///     .and_then(|server_addr| {
///         TcpStream::connect(server_addr).and_then(move |_stream| {
///             println!("# Connected: {}", server_addr);
///             Ok(())
///         })
///     }));
///
/// // Runs until the TCP client exits
/// while monitor.poll().unwrap().is_not_ready() {
///     executor.run_once().unwrap();
/// }
/// println!("# Succeeded");
/// ```
pub struct TcpListener {
    handle: Arc<EventedHandle<MioTcpListener>>,
    monitor: Option<Monitor<(), io::Error>>,
}
impl TcpListener {
    /// Makes a future to create a new `TcpListener` which will be bound to the specified address.
    pub fn bind(addr: SocketAddr) -> TcpListenerBind {
        TcpListenerBind(Bind::Bind(addr, MioTcpListener::bind))
    }

    /// Makes a stream of the connections which will be accepted by this listener.
    pub fn incoming(self) -> Incoming {
        Incoming(self)
    }

    /// Returns the local socket address of this listener.
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        self.handle.inner().local_addr()
    }

    /// Get the value of the `SO_ERROR` option on this socket.
    ///
    /// This will retrieve the stored error in the underlying socket,
    /// clearing the field in the process.
    /// This can be useful for checking errors between calls.
    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
        self.handle.inner().take_error()
    }

    /// Calls `f` with the reference to the inner socket.
    pub fn with_inner<F, T>(&self, f: F) -> T
    where
        F: FnOnce(&MioTcpListener) -> T,
    {
        f(&*self.handle.inner())
    }
}
impl fmt::Debug for TcpListener {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TcpListener {{ ")?;
        if let Ok(addr) = self.local_addr() {
            write!(f, "local_addr:{:?}, ", addr)?;
        }
        write!(f, ".. }}")?;
        Ok(())
    }
}

/// A future which will create a new `TcpListener` which will be bound to the specified address.
///
/// This is created by calling `TcpListener::bind` function.
/// It is permitted to move the future across fibers.
///
/// # Panics
///
/// If the future is polled on the outside of a fiber, it may crash.
#[derive(Debug)]
pub struct TcpListenerBind(Bind<fn(&SocketAddr) -> io::Result<MioTcpListener>, MioTcpListener>);
impl Future for TcpListenerBind {
    type Item = TcpListener;
    type Error = io::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        Ok(self.0.poll()?.map(|handle| TcpListener {
            handle,
            monitor: None,
        }))
    }
}

/// An infinite stream of the connections which will be accepted by the listener.
///
/// This is created by calling `TcpListener::incoming` method.
/// It is permitted to move the future across fibers.
///
/// # Panics
///
/// If the stream is polled on the outside of a fiber, it may crash.
#[derive(Debug)]
pub struct Incoming(TcpListener);
impl Stream for Incoming {
    type Item = (Connected, SocketAddr);
    type Error = io::Error;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        loop {
            if let Some(mut monitor) = self.0.monitor.take() {
                if let Async::NotReady = monitor.poll().map_err(into_io_error)? {
                    self.0.monitor = Some(monitor);
                    return Ok(Async::NotReady);
                }
            } else {
                match self.0.handle.inner().accept() {
                    Ok((stream, addr)) => {
                        let register = |mut c: Context| c.poller().register(stream);
                        let future = assert_some!(fiber::with_current_context(register));
                        let stream = Connected(Some(future));
                        return Ok(Async::Ready(Some((stream, addr))));
                    }
                    Err(e) => {
                        if e.kind() == io::ErrorKind::WouldBlock {
                            self.0.monitor = Some(self.0.handle.monitor(Interest::Read));
                        } else {
                            return Err(e);
                        }
                    }
                }
            }
        }
    }
}

/// A future which represents a `TcpStream` connected to a `TcpListener`.
///
/// This is produced by `Incoming` stream.
/// It is permitted to move the future across fibers.
///
/// # Panics
///
/// If the future is polled on the outside of a fiber, it may crash.
#[derive(Debug)]
pub struct Connected(Option<Register<MioTcpStream>>);
impl Future for Connected {
    type Item = TcpStream;
    type Error = io::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let mut future = self.0.take().expect("Cannot poll Connected twice");
        if let Async::Ready(handle) = future.poll().map_err(into_io_error)? {
            Ok(Async::Ready(TcpStream::new(handle)))
        } else {
            self.0 = Some(future);
            Ok(Async::NotReady)
        }
    }
}

/// A structure which represents a TCP stream between a local socket and a remote socket.
///
/// The socket will be closed when the value is dropped.
///
/// # Note
///
/// Non blocking mode is always enabled on this socket.
/// Roughly speaking, if an operation (read or write) for a socket would block,
/// it returns the `std::io::ErrorKind::WouldBlock` error and
/// current fiber is suspended until the socket becomes available.
/// If the fiber has multiple sockets (or other objects which may block),
/// it will be suspended only the case that all of them are unavailable.
///
/// To handle read/write operations over TCP streams in
/// [futures](https://github.com/alexcrichton/futures-rs) style,
/// it is preferred to use external crate like [`handy_async`](https://github.com/sile/handy_async).
///
/// # Examples
///
/// ```
/// // See also: fibers/examples/tcp_example.rs
/// # extern crate fibers;
/// # extern crate futures;
/// use fibers::{Executor, InPlaceExecutor, Spawn};
/// use fibers::net::{TcpListener, TcpStream};
/// use fibers::sync::oneshot;
/// use futures::{Future, Stream};
///
/// let mut executor = InPlaceExecutor::new().unwrap();
/// let (addr_tx, addr_rx) = oneshot::channel();
///
/// // Spawns TCP listener
/// executor.spawn(TcpListener::bind("127.0.0.1:0".parse().unwrap())
///     .and_then(|listener| {
///         let addr = listener.local_addr().unwrap();
///         println!("# Start listening: {}", addr);
///         addr_tx.send(addr).unwrap();
///         listener.incoming()
///             .for_each(move |(_client, addr)| {
///                 println!("# Accepted: {}", addr);
///                 Ok(())
///             })
///     })
///     .map_err(|e| panic!("{:?}", e)));
///
/// // Spawns TCP client
/// let mut monitor = executor.spawn_monitor(addr_rx.map_err(|e| panic!("{:?}", e))
///     .and_then(|server_addr| {
///         TcpStream::connect(server_addr).and_then(move |mut stream| {
///             use std::io::Write;
///             println!("# Connected: {}", server_addr);
///             stream.write(b"Hello World!"); // This may return `WouldBlock` error
///             Ok(())
///         })
///     }));
///
/// // Runs until the TCP client exits
/// while monitor.poll().unwrap().is_not_ready() {
///     executor.run_once().unwrap();
/// }
/// println!("# Succeeded");
/// ```
pub struct TcpStream {
    handle: Arc<EventedHandle<MioTcpStream>>,
    read_monitor: Option<Monitor<(), io::Error>>,
    write_monitor: Option<Monitor<(), io::Error>>,
}
impl Clone for TcpStream {
    fn clone(&self) -> Self {
        TcpStream {
            handle: self.handle.clone(),
            read_monitor: None,
            write_monitor: None,
        }
    }
}
impl TcpStream {
    fn new(handle: Arc<EventedHandle<MioTcpStream>>) -> Self {
        TcpStream {
            handle,
            read_monitor: None,
            write_monitor: None,
        }
    }

    /// Makes a future to open a TCP connection to a remote host.
    pub fn connect(addr: SocketAddr) -> Connect {
        Connect(ConnectInner::Connect(addr))
    }

    /// Returns the local socket address of this listener.
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        self.handle.inner().local_addr()
    }

    /// Returns the socket address of the remote peer of this TCP connection.
    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        self.handle.inner().peer_addr()
    }

    /// Get the value of the `SO_ERROR` option on this socket.
    ///
    /// This will retrieve the stored error in the underlying socket,
    /// clearing the field in the process.
    /// This can be useful for checking errors between calls.
    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
        self.handle.inner().take_error()
    }

    /// Gets the value of the `TCP_NODELAY` option on this socket.
    pub fn nodelay(&self) -> io::Result<bool> {
        self.handle.inner().nodelay()
    }

    /// Sets the value of the `TCP_NODELAY `option on this socket.
    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
        self.handle.inner().set_nodelay(nodelay)
    }

    /// Calls `f` with the reference to the inner socket.
    pub fn with_inner<F, T>(&self, f: F) -> T
    where
        F: FnOnce(&MioTcpStream) -> T,
    {
        f(&*self.handle.inner())
    }

    fn monitor(&mut self, interest: Interest) -> &mut Option<Monitor<(), io::Error>> {
        if interest == Interest::Read {
            &mut self.read_monitor
        } else {
            &mut self.write_monitor
        }
    }
    fn start_monitor_if_needed(&mut self, interest: Interest) -> Result<bool, io::Error> {
        if self.monitor(interest).is_none() {
            *self.monitor(interest) = Some(self.handle.monitor(interest));
            if let Err(e) = self.monitor(interest).poll() {
                return Err(e.unwrap_or_else(|| {
                    io::Error::new(io::ErrorKind::Other, "Monitor channel disconnected")
                }));
            }
            Ok(true)
        } else {
            Ok(false)
        }
    }
    fn operate<F, T>(&mut self, interest: Interest, mut f: F) -> io::Result<T>
    where
        F: FnMut(&mut MioTcpStream) -> io::Result<T>,
    {
        loop {
            if let Some(mut monitor) = self.monitor(interest).take() {
                if let Async::NotReady = monitor.poll().map_err(into_io_error)? {
                    *self.monitor(interest) = Some(monitor);
                    return Err(mio::would_block());
                }
            } else {
                let result = f(&mut *self.handle.inner());
                match result {
                    Err(e) => {
                        if e.kind() == io::ErrorKind::WouldBlock {
                            *self.monitor(interest) = Some(self.handle.monitor(interest));
                        } else {
                            return Err(e);
                        }
                    }
                    Ok(v) => return Ok(v),
                }
            }
        }
    }
}
impl io::Read for TcpStream {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.operate(Interest::Read, |inner| inner.read(buf))
    }
}
impl io::Write for TcpStream {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.operate(Interest::Write, |inner| inner.write(buf))
    }
    fn flush(&mut self) -> io::Result<()> {
        self.operate(Interest::Write, |inner| inner.flush())
    }
}
impl fmt::Debug for TcpStream {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TcpStream {{ ")?;
        if let Ok(addr) = self.local_addr() {
            write!(f, "local_addr:{:?}, ", addr)?;
        }
        if let Ok(addr) = self.peer_addr() {
            write!(f, "peer_addr:{:?}, ", addr)?;
        }
        write!(f, ".. }}")?;
        Ok(())
    }
}

/// A future which will open a TCP connection to a remote host.
///
/// This is created by calling `TcpStream::connect` function.
/// It is permitted to move the future across fibers.
///
/// # Panics
///
/// If the future is polled on the outside of a fiber, it may crash.
#[derive(Debug)]
pub struct Connect(ConnectInner);
impl Future for Connect {
    type Item = TcpStream;
    type Error = io::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.poll()
    }
}

#[derive(Debug)]
enum ConnectInner {
    Connect(SocketAddr),
    Registering(Register<MioTcpStream>),
    Connecting(TcpStream),
    Polled,
}
impl Future for ConnectInner {
    type Item = TcpStream;
    type Error = io::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match mem::replace(self, ConnectInner::Polled) {
            ConnectInner::Connect(addr) => {
                let stream = MioTcpStream::connect(&addr)?;
                let register = assert_some!(fiber::with_current_context(|mut c| c
                    .poller()
                    .register(stream),));
                *self = ConnectInner::Registering(register);
                self.poll()
            }
            ConnectInner::Registering(mut future) => {
                if let Async::Ready(handle) = future.poll().map_err(into_io_error)? {
                    *self = ConnectInner::Connecting(TcpStream::new(handle));
                    self.poll()
                } else {
                    *self = ConnectInner::Registering(future);
                    Ok(Async::NotReady)
                }
            }
            ConnectInner::Connecting(mut stream) => match stream.peer_addr() {
                Ok(_) => Ok(Async::Ready(stream)),
                Err(e) => {
                    if let Some(e) = stream.take_error()? {
                        return Err(e);
                    }
                    if e.kind() == io::ErrorKind::NotConnected {
                        let retry = stream.start_monitor_if_needed(Interest::Write)?;
                        *self = ConnectInner::Connecting(stream);
                        if retry {
                            self.poll()
                        } else {
                            Ok(Async::NotReady)
                        }
                    } else {
                        Err(e)
                    }
                }
            },
            ConnectInner::Polled => panic!("Cannot poll ConnectInner twice"),
        }
    }
}