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

source

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

pub async fn disconnect(&self, address: impl Into<Address>) -> Result<(), Error>

Interrupt an active TCP connection given its Sender Address

source§

impl TcpTransport

source

pub async fn create(ctx: &Context) -> Result<TcpTransport, Error>

Create a TCP transport

use ockam_transport_tcp::TcpTransport;
let tcp = TcpTransport::create(&ctx).await?;
source§

impl TcpTransport

source

pub fn ctx(&self) -> &Context

Getter

source

pub fn registry(&self) -> &TcpRegistry

Registry of all active connections

source

pub fn find_connection_by_socketaddr( &self, socket_address: SocketAddr ) -> Option<TcpSenderInfo>

Search for a connection with the provided socket address

source

pub fn find_connection(&self, address: String) -> Option<TcpSenderInfo>

Search for a connection with the provided address

source

pub fn find_listener_by_socketaddress( &self, socket_address: SocketAddr ) -> Option<TcpListenerInfo>

Search for a listener with the provided socket address

source

pub fn find_listener(&self, address: String) -> Option<TcpListenerInfo>

Search for a listener with the provided address

source§

impl TcpTransport

source

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

pub async fn stop_listener(&self, address: &Address) -> Result<(), Error>

Interrupt an active TCP listener given its Address

source§

impl TcpTransport

source

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?;
source

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?;
source

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?;
source

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

source

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

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,

Try cloning a object and return an Err in case of failure.
source§

impl Transport for TcpTransport

source§

fn transport_type(&self) -> TransportType

Return the type of the Transport
source§

fn resolve_address<'life0, 'async_trait>( &'life0 self, address: Address ) -> Pin<Box<dyn Future<Output = Result<Address, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, TcpTransport: 'async_trait,

Instantiate transport workers for in order to communicate with a remote address and return the local address of the transport worker

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more