Skip to main content

Connection

Struct Connection 

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

A live connection to an ESPHome peer.

Returned by crate::esphomeapi::EspHomeApi::start and crate::esphomeserver::EspHomeServer::start, this handle owns the channels used to talk to the peer and lets a consumer observe when — and why — the connection ends.

§Observing termination

Unlike a bare (Sender, Receiver) pair, a Connection reports its terminal outcome via Connection::wait. A Error::Disconnected result is the normal way a session ends and is usually not treated as a failure:

let stream = TcpStream::connect("192.168.1.100:6053").await?;
let api = EspHomeApi::builder().name("client".to_string()).build()?;
let connection = api.start(stream).await?;

let sender = connection.sender();
let mut receiver = connection.receiver();

match connection.wait().await {
    Ok(()) | Err(Error::Disconnected(_)) => { /* peer left — expected */ }
    Err(e) => eprintln!("connection fault: {e}"),
}

Implementations§

Source§

impl Connection

Source

pub fn sender(&self) -> Sender<ProtoMessage>

Returns a sender for messages to the peer. Can be called multiple times; each call yields an independent, cloneable mpsc::Sender.

Source

pub fn receiver(&self) -> Receiver<ProtoMessage>

Returns a receiver for messages from the peer. Each call yields a fresh broadcast::Receiver that observes messages sent from this point on.

Source

pub async fn wait(self) -> Result<(), Error>

Wait for the connection to terminate and return its outcome.

Returns Ok(()) for a clean shutdown, Err(Error::Disconnected(_)) when the peer went away (the normal case), or another Error for a genuine fault — including Error::TaskFailed if the connection’s background task died (for example, panicked) without reporting an outcome. Consuming self here is deliberate: obtain a Connection::sender and Connection::receiver first if you need them for the session.

Trait Implementations§

Source§

impl Debug for Connection

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> Same for T

Source§

type Output = T

Should always be Self
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.