Struct ockam_transport_tcp::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>

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<()>

Interrupt an active TCP connection given its Sender Address

source§

impl TcpTransport

source

pub async fn create(ctx: &Context) -> Result<Self>

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>

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<()>

Interrupt an active TCP listener given its Address

source§

impl TcpTransport

source

pub async fn create_inlet( &self, bind_addr: impl Into<String> + Clone + Debug, outlet_route: impl Into<Route> + Clone + Debug, options: TcpInletOptions ) -> Result<(SocketAddr, Address)>

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> + Clone + Debug ) -> Result<()>

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> + Clone + Debug, hostname_port: impl TryInto<HostnamePort, Error = Error> + Clone + Debug, options: TcpOutletOptions ) -> Result<()>

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::{HostnamePort, 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, hostname_port: HostnamePort, options: TcpOutletOptions ) -> Result<()>

Create Tcp Outlet Listener at address, that connects to peer using Tcp

source

pub async fn stop_outlet( &self, addr: impl Into<Address> + Clone + Debug ) -> Result<()>

Stop outlet at addr

use ockam_transport_tcp::{HostnamePort, TcpOutletOptions, TcpTransport};

let tcp = TcpTransport::create(&ctx).await?;
tcp.create_outlet("outlet", "127.0.0.1:5000", TcpOutletOptions::new()).await?;
tcp.stop_outlet("outlet").await?;

Trait Implementations§

source§

impl Clone for TcpTransport

source§

fn clone(&self) -> TcpTransport

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TcpTransport

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

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

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

Stop all workers and free all resources associated with the connection

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<D> AsyncTryClone for D
where D: Clone + Sync,

source§

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

Try cloning a object and return an Err in case of failure.
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> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
source§

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

source§

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