Struct ockam::TcpTransport
source · pub struct TcpTransport { /* private fields */ }
Expand description
High level management interface for TCP transports
Be aware that only one TcpTransport
can exist per node, as it
registers itself as a router for the TCP
address type. Multiple
calls to TcpTransport::create
will fail.
To listen for incoming connections use
tcp.listen()
.
To register additional connections on an already initialised
TcpTransport
, use tcp.connect()
.
This step is optional because the underlying TcpRouter is capable of lazily
establishing a connection upon arrival of an initial message.
use ockam_transport_tcp::{TcpConnectionOptions, TcpListenerOptions, TcpTransport};
let tcp = TcpTransport::create(&ctx).await?;
tcp.listen("127.0.0.1:8000", TcpListenerOptions::new()).await?; // Listen on port 8000
tcp.connect("127.0.0.1:5000", TcpConnectionOptions::new()).await?; // And connect to port 5000
The same TcpTransport
can also bind to multiple ports.
use ockam_transport_tcp::{TcpListenerOptions, TcpTransport};
let tcp = TcpTransport::create(&ctx).await?;
tcp.listen("127.0.0.1:8000", TcpListenerOptions::new()).await?; // Listen on port 8000
tcp.listen("127.0.0.1:9000", TcpListenerOptions::new()).await?; // Listen on port 9000
Implementations§
source§impl TcpTransport
impl TcpTransport
sourcepub async fn connect(
&self,
peer: impl Into<String>,
options: TcpConnectionOptions
) -> Result<TcpConnection, Error>
pub async fn connect( &self, peer: impl Into<String>, options: TcpConnectionOptions ) -> Result<TcpConnection, Error>
Establish an outgoing TCP connection.
use ockam_transport_tcp::{TcpConnectionOptions, TcpListenerOptions, TcpTransport};
let tcp = TcpTransport::create(&ctx).await?;
tcp.listen("127.0.0.1:8000", TcpListenerOptions::new()).await?; // Listen on port 8000
let connection = tcp.connect("127.0.0.1:5000", TcpConnectionOptions::new()).await?; // and connect to port 5000
source§impl TcpTransport
impl TcpTransport
source§impl TcpTransport
impl TcpTransport
sourcepub fn registry(&self) -> &TcpRegistry
pub fn registry(&self) -> &TcpRegistry
Registry of all active connections
sourcepub fn find_connection_by_socketaddr(
&self,
socket_address: SocketAddr
) -> Option<TcpSenderInfo>
pub fn find_connection_by_socketaddr( &self, socket_address: SocketAddr ) -> Option<TcpSenderInfo>
Search for a connection with the provided socket address
sourcepub fn find_connection(&self, address: String) -> Option<TcpSenderInfo>
pub fn find_connection(&self, address: String) -> Option<TcpSenderInfo>
Search for a connection with the provided address
sourcepub fn find_listener_by_socketaddress(
&self,
socket_address: SocketAddr
) -> Option<TcpListenerInfo>
pub fn find_listener_by_socketaddress( &self, socket_address: SocketAddr ) -> Option<TcpListenerInfo>
Search for a listener with the provided socket address
sourcepub fn find_listener(&self, address: String) -> Option<TcpListenerInfo>
pub fn find_listener(&self, address: String) -> Option<TcpListenerInfo>
Search for a listener with the provided address
source§impl TcpTransport
impl TcpTransport
sourcepub async fn listen(
&self,
bind_addr: impl AsRef<str>,
options: TcpListenerOptions
) -> Result<TcpListener, Error>
pub async fn listen( &self, bind_addr: impl AsRef<str>, options: TcpListenerOptions ) -> Result<TcpListener, Error>
Start listening to incoming connections on an existing transport
Returns the local address that this transport is bound to.
This can be useful, for example, when binding to port 0 to figure out which port was actually bound.
use ockam_transport_tcp::{TcpListenerOptions, TcpTransport};
let tcp = TcpTransport::create(&ctx).await?;
tcp.listen("127.0.0.1:8000", TcpListenerOptions::new()).await?;
source§impl TcpTransport
impl TcpTransport
sourcepub async fn create_inlet(
&self,
bind_addr: impl Into<String>,
outlet_route: impl Into<Route>,
options: TcpInletOptions
) -> Result<(SocketAddr, Address), Error>
pub async fn create_inlet( &self, bind_addr: impl Into<String>, outlet_route: impl Into<Route>, options: TcpInletOptions ) -> Result<(SocketAddr, Address), Error>
Create Tcp Inlet that listens on bind_addr, transforms Tcp stream into Ockam Routable Messages and forward them to Outlet using outlet_route. Inlet is bidirectional: Ockam Messages sent to Inlet from Outlet (using return route) will be streamed to Tcp connection. Pair of corresponding Inlet and Outlet is called Portal.
use ockam_transport_tcp::{TcpInletOptions, TcpTransport};
let route_path = route!["outlet"];
let tcp = TcpTransport::create(&ctx).await?;
tcp.create_inlet("inlet", route_path, TcpInletOptions::new()).await?;
sourcepub async fn stop_inlet(&self, addr: impl Into<Address>) -> Result<(), Error>
pub async fn stop_inlet(&self, addr: impl Into<Address>) -> Result<(), Error>
Stop inlet at addr
use ockam_transport_tcp::{TcpInletOptions, TcpTransport};
let route = route!["outlet"];
let tcp = TcpTransport::create(&ctx).await?;
tcp.create_inlet("inlet", route, TcpInletOptions::new()).await?;
tcp.stop_inlet("inlet").await?;
sourcepub async fn create_outlet(
&self,
address: impl Into<Address>,
peer: impl Into<String>,
options: TcpOutletOptions
) -> Result<(), Error>
pub async fn create_outlet( &self, address: impl Into<Address>, peer: impl Into<String>, options: TcpOutletOptions ) -> Result<(), Error>
Create Tcp Outlet Listener at address, that connects to peer using Tcp, transforms Ockam Messages received from Inlet into stream and sends it to peer Tcp stream. Outlet is bidirectional: Tcp stream received from peer is transformed into Ockam Routable Messages and sent to Inlet using return route. Pair of corresponding Inlet and Outlet is called Portal.
use ockam_transport_tcp::{TcpOutletOptions, TcpTransport};
let tcp = TcpTransport::create(&ctx).await?;
tcp.create_outlet("outlet", "localhost:9000", TcpOutletOptions::new()).await?;
sourcepub async fn create_tcp_outlet(
&self,
address: Address,
peer: SocketAddr,
options: TcpOutletOptions
) -> Result<(), Error>
pub async fn create_tcp_outlet( &self, address: Address, peer: SocketAddr, options: TcpOutletOptions ) -> Result<(), Error>
Create Tcp Outlet Listener at address, that connects to peer using Tcp
sourcepub async fn stop_outlet(&self, addr: impl Into<Address>) -> Result<(), Error>
pub async fn stop_outlet(&self, addr: impl Into<Address>) -> Result<(), Error>
Stop outlet at addr
use ockam_transport_tcp::{TcpOutletOptions, TcpTransport};
const TARGET_PEER: &str = "127.0.0.1:5000";
let tcp = TcpTransport::create(&ctx).await?;
tcp.create_outlet("outlet", TARGET_PEER, TcpOutletOptions::new()).await?;
tcp.stop_outlet("outlet").await?;
Trait Implementations§
source§impl AsyncTryClone for TcpTransport
impl AsyncTryClone for TcpTransport
source§fn async_try_clone<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<TcpTransport, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TcpTransport: 'async_trait,
fn async_try_clone<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<TcpTransport, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TcpTransport: 'async_trait,
Err
in case of failure.