Struct tokio_resol_vbus::TcpServerHandshake
source · pub struct TcpServerHandshake { /* private fields */ }
Expand description
Handles the server-side of the VBus-over-TCP handshake.
Examples
This example simulates a RESOL DL2 by accepting TCP connections on port 7053, requiring the client to provide a password and then switch the connection into raw VBus data mode.
use tokio::prelude::*;
use tokio::net::TcpListener;
use tokio_resol_vbus::TcpServerHandshake;
let addr = "127.0.0.1:7053".parse().expect("Unable to parse address");
let listener = TcpListener::bind(&addr).expect("Unable to bind listener");
let server = listener
.incoming()
.map_err(|err| eprintln!("{}", err))
.for_each(|socket| {
let conn = TcpServerHandshake::start(socket)
.and_then(|hs| hs.receive_pass_command_and_verify_password(|password| {
if password == "vbus" {
Ok(Some(password))
} else {
Ok(None)
}
}))
.and_then(|(hs, _)| hs.receive_data_command())
.and_then(|socket| {
// do something with the socket
})
.map_err(|err| eprintln!("Server error: {}", err));
tokio::spawn(conn)
});
tokio::run(server);
Implementations§
source§impl TcpServerHandshake
impl TcpServerHandshake
sourcepub fn start(socket: TcpStream) -> impl Future<Item = Self, Error = Error>
pub fn start(socket: TcpStream) -> impl Future<Item = Self, Error = Error>
Start the VBus-over-TCP handshake as the client side connecting to a server.
sourcepub fn into_inner(self) -> TcpStream
pub fn into_inner(self) -> TcpStream
Consume self
and return the underlying TcpStream
.
sourcepub fn receive_command<V, R, T>(
self,
validator: V
) -> impl Future<Item = (Self, T), Error = Error>where
V: Fn(String, Option<String>) -> R,
R: Future<Item = StdResult<T, &'static str>, Error = Error> + Send + 'static,
T: Send + 'static,
pub fn receive_command<V, R, T>(
self,
validator: V
) -> impl Future<Item = (Self, T), Error = Error>where
V: Fn(String, Option<String>) -> R,
R: Future<Item = StdResult<T, &'static str>, Error = Error> + Send + 'static,
T: Send + 'static,
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 fn receive_connect_command(
self
) -> impl Future<Item = (Self, String), Error = Error>
pub fn receive_connect_command(
self
) -> impl Future<Item = (Self, String), Error = Error>
Wait for a CONNECT <via_tag>
command. The via tag argument is returned.
sourcepub fn receive_connect_command_and_verify_via_tag<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, String), Error = Error>where
V: Fn(String) -> F,
F: IntoFuture<Item = Option<String>, Error = Error, Future = R> + Send,
R: Future<Item = Option<String>, Error = Error> + Send + 'static,
pub fn receive_connect_command_and_verify_via_tag<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, String), Error = Error>where
V: Fn(String) -> F,
F: IntoFuture<Item = Option<String>, Error = Error, Future = R> + Send,
R: Future<Item = Option<String>, Error = Error> + Send + 'static,
Wait for a CONNECT <via_tag>
command.
sourcepub fn receive_pass_command(
self
) -> impl Future<Item = (Self, String), Error = Error>
pub fn receive_pass_command(
self
) -> impl Future<Item = (Self, String), Error = Error>
Wait for a PASS <password>
command.
sourcepub fn receive_pass_command_and_verify_password<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, String), Error = Error>where
V: Fn(String) -> F,
F: IntoFuture<Item = Option<String>, Error = Error, Future = R> + Send,
R: Future<Item = Option<String>, Error = Error> + Send + 'static,
pub fn receive_pass_command_and_verify_password<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, String), Error = Error>where
V: Fn(String) -> F,
F: IntoFuture<Item = Option<String>, Error = Error, Future = R> + Send,
R: Future<Item = Option<String>, Error = Error> + Send + 'static,
Wait for a PASS <password>
command and validate the provided password.
sourcepub fn receive_channel_command(
self
) -> impl Future<Item = (Self, u8), Error = Error>
pub fn receive_channel_command(
self
) -> impl Future<Item = (Self, u8), Error = Error>
Wait for a CHANNEL <channel>
command.
sourcepub fn receive_channel_command_and_verify_channel<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, u8), Error = Error>where
V: Fn(u8) -> F,
F: IntoFuture<Item = Option<u8>, Error = Error, Future = R> + Send + 'static,
R: Future<Item = Option<u8>, Error = Error> + Send + 'static,
pub fn receive_channel_command_and_verify_channel<V, F, R>(
self,
validator: V
) -> impl Future<Item = (Self, u8), Error = Error>where
V: Fn(u8) -> F,
F: IntoFuture<Item = Option<u8>, Error = Error, Future = R> + Send + 'static,
R: Future<Item = Option<u8>, Error = Error> + Send + 'static,
Wait for CHANNEL <channel>
command and validate the provided channel
sourcepub fn receive_data_command(
self
) -> impl Future<Item = TcpStream, Error = Error>
pub fn receive_data_command(
self
) -> impl Future<Item = TcpStream, Error = Error>
Wait for a DATA
command.