|
| 1 | +use crate::internal::{self, TcpListener, TcpStream, TcpTransStream}; |
| 2 | +use futures::{AsyncRead, AsyncWrite}; |
| 3 | +use libp2p_core::{transport::TransportError, Multiaddr, Transport}; |
| 4 | +use std::{ |
| 5 | + io, |
| 6 | + net::SocketAddr, |
| 7 | + pin::Pin, |
| 8 | + task::{Context, Poll}, |
| 9 | +}; |
| 10 | + |
| 11 | +type AsyncStdConfig = internal::TcpConfig<async_std::net::TcpListener>; |
| 12 | + |
| 13 | +/// Represents the configuration for a TCP/IP transport capability for libp2p. |
| 14 | +/// |
| 15 | +/// This implementation uses the network futures from async_std. |
| 16 | +#[derive(Debug, Clone, Default)] |
| 17 | +pub struct TcpConfig { |
| 18 | + inner: AsyncStdConfig, |
| 19 | +} |
| 20 | + |
| 21 | +impl TcpConfig { |
| 22 | + /// Creates a new configuration object for TCP/IP. |
| 23 | + pub fn new() -> Self { |
| 24 | + Self { |
| 25 | + inner: AsyncStdConfig::new(), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Sets the TTL to set for opened sockets. |
| 30 | + pub fn ttl(mut self, value: u32) -> Self { |
| 31 | + self.inner.ttl = Some(value); |
| 32 | + self |
| 33 | + } |
| 34 | + |
| 35 | + /// Sets the `TCP_NODELAY` to set for opened sockets. |
| 36 | + pub fn nodelay(mut self, value: bool) -> Self { |
| 37 | + self.inner.nodelay = Some(value); |
| 38 | + self |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Transport for TcpConfig { |
| 43 | + type Output = <AsyncStdConfig as Transport>::Output; |
| 44 | + type Error = <AsyncStdConfig as Transport>::Error; |
| 45 | + type Listener = <AsyncStdConfig as Transport>::Listener; |
| 46 | + type ListenerUpgrade = <AsyncStdConfig as Transport>::ListenerUpgrade; |
| 47 | + type Dial = <AsyncStdConfig as Transport>::Dial; |
| 48 | + |
| 49 | + fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> { |
| 50 | + self.inner.listen_on(addr) |
| 51 | + } |
| 52 | + |
| 53 | + fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> { |
| 54 | + self.inner.dial(addr) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[async_trait::async_trait] |
| 59 | +impl TcpListener for async_std::net::TcpListener { |
| 60 | + type TcpStream = async_std::net::TcpStream; |
| 61 | + |
| 62 | + async fn bind(addrs: &SocketAddr) -> io::Result<async_std::net::TcpListener> { |
| 63 | + async_std::net::TcpListener::bind(addrs).await |
| 64 | + } |
| 65 | + |
| 66 | + async fn accept(&mut self) -> io::Result<(async_std::net::TcpStream, SocketAddr)> { |
| 67 | + async_std::net::TcpListener::accept(self).await |
| 68 | + } |
| 69 | + |
| 70 | + fn local_addr(&self) -> io::Result<SocketAddr> { |
| 71 | + async_std::net::TcpListener::local_addr(self) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl AsyncRead for TcpTransStream<async_std::net::TcpStream> { |
| 76 | + fn poll_read( |
| 77 | + mut self: Pin<&mut Self>, |
| 78 | + cx: &mut Context, |
| 79 | + buf: &mut [u8], |
| 80 | + ) -> Poll<Result<usize, io::Error>> { |
| 81 | + AsyncRead::poll_read(Pin::new(&mut self.inner), cx, buf) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl AsyncWrite for TcpTransStream<async_std::net::TcpStream> { |
| 86 | + fn poll_write( |
| 87 | + mut self: Pin<&mut Self>, |
| 88 | + cx: &mut Context, |
| 89 | + buf: &[u8], |
| 90 | + ) -> Poll<Result<usize, io::Error>> { |
| 91 | + AsyncWrite::poll_write(Pin::new(&mut self.inner), cx, buf) |
| 92 | + } |
| 93 | + |
| 94 | + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> { |
| 95 | + AsyncWrite::poll_flush(Pin::new(&mut self.inner), cx) |
| 96 | + } |
| 97 | + |
| 98 | + fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> { |
| 99 | + AsyncWrite::poll_close(Pin::new(&mut self.inner), cx) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[async_trait::async_trait] |
| 104 | +impl TcpStream for async_std::net::TcpStream { |
| 105 | + async fn connect(addrs: &SocketAddr) -> io::Result<Self> { |
| 106 | + async_std::net::TcpStream::connect(addrs).await |
| 107 | + } |
| 108 | + fn set_ttl(&self, ttl: u32) -> io::Result<()> { |
| 109 | + async_std::net::TcpStream::set_ttl(self, ttl) |
| 110 | + } |
| 111 | + fn set_nodelay(&self, nodelay: bool) -> io::Result<()> { |
| 112 | + async_std::net::TcpStream::set_nodelay(self, nodelay) |
| 113 | + } |
| 114 | + fn peer_addr(&self) -> io::Result<SocketAddr> { |
| 115 | + async_std::net::TcpStream::peer_addr(self) |
| 116 | + } |
| 117 | + fn local_addr(&self) -> io::Result<SocketAddr> { |
| 118 | + async_std::net::TcpStream::local_addr(self) |
| 119 | + } |
| 120 | +} |
0 commit comments