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
impl TcpServerHandshake
Sourcepub async fn start(stream: TcpStream) -> Result<TcpServerHandshake>
pub async fn start(stream: TcpStream) -> Result<TcpServerHandshake>
Start the VBus-over-TCP handshake as the server side.
Sourcepub fn into_inner(self) -> TcpStream
pub fn into_inner(self) -> TcpStream
Consume self
and return the underlying TcpStream
.
Sourcepub async fn receive_command<V, R, T>(&mut self, validator: V) -> Result<T>
pub async fn receive_command<V, R, T>(&mut self, validator: V) -> Result<T>
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. Thevalue
is used to resolve thereceive_command
Future
.Err(reply)
if the validation failed. Thereply
is send back to the client and the command reception is repeated.
Sourcepub async fn receive_connect_command(&mut self) -> Result<String>
pub async fn receive_connect_command(&mut self) -> Result<String>
Wait for a CONNECT <via_tag>
command. The via tag argument is returned.
Sourcepub async fn receive_connect_command_and_verify_via_tag<V, R>(
&mut self,
validator: V,
) -> Result<String>
pub async fn receive_connect_command_and_verify_via_tag<V, R>( &mut self, validator: V, ) -> Result<String>
Wait for a CONNECT <via_tag>
command.
Sourcepub async fn receive_pass_command(&mut self) -> Result<String>
pub async fn receive_pass_command(&mut self) -> Result<String>
Wait for a PASS <password>
command.
Sourcepub async fn receive_pass_command_and_verify_password<V, R>(
&mut self,
validator: V,
) -> Result<String>
pub async fn receive_pass_command_and_verify_password<V, R>( &mut self, validator: V, ) -> Result<String>
Wait for a PASS <password>
command and validate the provided password.
Sourcepub async fn receive_channel_command(&mut self) -> Result<u8>
pub async fn receive_channel_command(&mut self) -> Result<u8>
Wait for a CHANNEL <channel>
command.
Sourcepub async fn receive_channel_command_and_verify_channel<V, R>(
&mut self,
validator: V,
) -> Result<u8>
pub async fn receive_channel_command_and_verify_channel<V, R>( &mut self, validator: V, ) -> Result<u8>
Wait for CHANNEL <channel>
command and validate the provided channel
Sourcepub async fn receive_data_command(self) -> Result<TcpStream>
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.