mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-02 11:11:36 +00:00
Add an Error associated type to transports (#835)
* Add an Error associated type to transports * Improve raw swarm a bit * Rename map_other to map * Use source() instead of cause() * RawSwarmIncErr -> IncomingError
This commit is contained in:
@ -50,10 +50,10 @@ extern crate tokio_tcp;
|
||||
use futures::{future, future::FutureResult, prelude::*, Async, Poll};
|
||||
use multiaddr::{Protocol, Multiaddr, ToMultiaddr};
|
||||
use std::fmt;
|
||||
use std::io::{Error as IoError, Read, Write};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
use swarm::Transport;
|
||||
use swarm::{Transport, transport::TransportError};
|
||||
use tk_listen::{ListenExt, SleepOnError};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use tokio_tcp::{ConnectFuture, Incoming, TcpListener, TcpStream};
|
||||
@ -130,11 +130,12 @@ impl TcpConfig {
|
||||
|
||||
impl Transport for TcpConfig {
|
||||
type Output = TcpTransStream;
|
||||
type Error = io::Error;
|
||||
type Listener = TcpListenStream;
|
||||
type ListenerUpgrade = FutureResult<Self::Output, IoError>;
|
||||
type ListenerUpgrade = FutureResult<Self::Output, io::Error>;
|
||||
type Dial = TcpDialFut;
|
||||
|
||||
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
||||
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), TransportError<Self::Error>> {
|
||||
if let Ok(socket_addr) = multiaddr_to_socketaddr(&addr) {
|
||||
let listener = TcpListener::bind(&socket_addr);
|
||||
// We need to build the `Multiaddr` to return from this function. If an error happened,
|
||||
@ -154,21 +155,23 @@ impl Transport for TcpConfig {
|
||||
debug!("Now listening on {}", new_addr);
|
||||
let sleep_on_error = self.sleep_on_error;
|
||||
let inner = listener
|
||||
.map_err(Some)
|
||||
.map(move |l| l.incoming().sleep_on_error(sleep_on_error));
|
||||
.map_err(TransportError::Other)?
|
||||
.incoming()
|
||||
.sleep_on_error(sleep_on_error);
|
||||
|
||||
Ok((
|
||||
TcpListenStream {
|
||||
inner,
|
||||
inner: Ok(inner),
|
||||
config: self,
|
||||
},
|
||||
new_addr,
|
||||
))
|
||||
} else {
|
||||
Err((self, addr))
|
||||
Err(TransportError::MultiaddrNotSupported(addr))
|
||||
}
|
||||
}
|
||||
|
||||
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
|
||||
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
|
||||
if let Ok(socket_addr) = multiaddr_to_socketaddr(&addr) {
|
||||
// As an optimization, we check that the address is not of the form `0.0.0.0`.
|
||||
// If so, we instantly refuse dialing instead of going through the kernel.
|
||||
@ -180,10 +183,10 @@ impl Transport for TcpConfig {
|
||||
})
|
||||
} else {
|
||||
debug!("Instantly refusing dialing {}, as it is invalid", addr);
|
||||
Err((self, addr))
|
||||
Err(TransportError::Other(io::ErrorKind::ConnectionRefused.into()))
|
||||
}
|
||||
} else {
|
||||
Err((self, addr))
|
||||
Err(TransportError::MultiaddrNotSupported(addr))
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,7 +227,7 @@ fn multiaddr_to_socketaddr(addr: &Multiaddr) -> Result<SocketAddr, ()> {
|
||||
}
|
||||
|
||||
/// Applies the socket configuration parameters to a socket.
|
||||
fn apply_config(config: &TcpConfig, socket: &TcpStream) -> Result<(), IoError> {
|
||||
fn apply_config(config: &TcpConfig, socket: &TcpStream) -> Result<(), io::Error> {
|
||||
if let Some(recv_buffer_size) = config.recv_buffer_size {
|
||||
socket.set_recv_buffer_size(recv_buffer_size)?;
|
||||
}
|
||||
@ -259,9 +262,9 @@ pub struct TcpDialFut {
|
||||
|
||||
impl Future for TcpDialFut {
|
||||
type Item = TcpTransStream;
|
||||
type Error = IoError;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<TcpTransStream, IoError> {
|
||||
fn poll(&mut self) -> Poll<TcpTransStream, io::Error> {
|
||||
match self.inner.poll() {
|
||||
Ok(Async::Ready(stream)) => {
|
||||
apply_config(&self.config, &stream)?;
|
||||
@ -278,20 +281,20 @@ impl Future for TcpDialFut {
|
||||
|
||||
/// Stream that listens on an TCP/IP address.
|
||||
pub struct TcpListenStream {
|
||||
inner: Result<SleepOnError<Incoming>, Option<IoError>>,
|
||||
inner: Result<SleepOnError<Incoming>, Option<io::Error>>,
|
||||
/// Original configuration.
|
||||
config: TcpConfig,
|
||||
}
|
||||
|
||||
impl Stream for TcpListenStream {
|
||||
type Item = (FutureResult<TcpTransStream, IoError>, Multiaddr);
|
||||
type Error = IoError;
|
||||
type Item = (FutureResult<TcpTransStream, io::Error>, Multiaddr);
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
Option<(FutureResult<TcpTransStream, IoError>, Multiaddr)>,
|
||||
IoError,
|
||||
Option<(FutureResult<TcpTransStream, io::Error>, Multiaddr)>,
|
||||
io::Error,
|
||||
> {
|
||||
let inner = match self.inner {
|
||||
Ok(ref mut inc) => inc,
|
||||
@ -352,7 +355,7 @@ pub struct TcpTransStream {
|
||||
|
||||
impl Read for TcpTransStream {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
|
||||
self.inner.read(buf)
|
||||
}
|
||||
}
|
||||
@ -361,19 +364,19 @@ impl AsyncRead for TcpTransStream {}
|
||||
|
||||
impl Write for TcpTransStream {
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
||||
self.inner.write(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<(), IoError> {
|
||||
fn flush(&mut self) -> Result<(), io::Error> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for TcpTransStream {
|
||||
#[inline]
|
||||
fn shutdown(&mut self) -> Poll<(), IoError> {
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
AsyncWrite::shutdown(&mut self.inner)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user