Struct TcpServerHandshake

Source
pub struct TcpServerHandshake { /* private fields */ }
Expand description

Handles the server-side of the VBus-over-TCP handshake.

§Examples

use async_std::net::{SocketAddr, TcpListener, TcpStream};

use async_resol_vbus::TcpServerHandshake;

let address = "0.0.0.0:7053".parse::<SocketAddr>()?;
let listener = TcpListener::bind(address).await?;
let (stream, _) = listener.accept().await?;
let mut hs = TcpServerHandshake::start(stream).await?;
let password = hs.receive_pass_command().await?;
// ...
let stream = hs.receive_data_command().await?;
// ...

Implementations§

Source§

impl TcpServerHandshake

Source

pub async fn start(stream: TcpStream) -> Result<TcpServerHandshake>

Start the VBus-over-TCP handshake as the server side.

Source

pub fn into_inner(self) -> TcpStream

Consume self and return the underlying TcpStream.

Source

pub async fn receive_command<V, R, T>(&mut self, validator: V) -> Result<T>
where V: Fn(String, Option<String>) -> R, R: Future<Output = Result<T, &'static str>>,

Receive a command and verify it and its provided arguments. The command reception is repeated as long as the verification fails.

The preferred way to receive commands documented in the VBus-over-TCP specification is through the receive_xxx_command and receive_xxx_command_and_verify_yyy methods which use the receive_command method internally.

This method takes a validator function that is called with the received command and its optional arguments. The validator returns a Future that can resolve into an std::result::Result<T, &'static str>. It can either be:

  • Ok(value) if the validation succeeded. The value is used to resolve the receive_command Future.
  • Err(reply) if the validation failed. The reply is send back to the client and the command reception is repeated.
Source

pub async fn receive_connect_command(&mut self) -> Result<String>

Wait for a CONNECT <via_tag> command. The via tag argument is returned.

Source

pub async fn receive_connect_command_and_verify_via_tag<V, R>( &mut self, validator: V, ) -> Result<String>
where V: Fn(String) -> R, R: Future<Output = Result<String, &'static str>>,

Wait for a CONNECT <via_tag> command.

Source

pub async fn receive_pass_command(&mut self) -> Result<String>

Wait for a PASS <password> command.

Source

pub async fn receive_pass_command_and_verify_password<V, R>( &mut self, validator: V, ) -> Result<String>
where V: Fn(String) -> R, R: Future<Output = Result<String, &'static str>>,

Wait for a PASS <password> command and validate the provided password.

Source

pub async fn receive_channel_command(&mut self) -> Result<u8>

Wait for a CHANNEL <channel> command.

Source

pub async fn receive_channel_command_and_verify_channel<V, R>( &mut self, validator: V, ) -> Result<u8>
where V: Fn(u8) -> R, R: Future<Output = Result<u8, &'static str>>,

Wait for CHANNEL <channel> command and validate the provided channel

Source

pub async fn receive_data_command(self) -> Result<TcpStream>

Wait for a DATA command.

This function returns the underlying TcpStream since the handshake is complete after sending this command.

Trait Implementations§

Source§

impl Debug for TcpServerHandshake

Source§

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

Formats the value using the given formatter. Read more

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.