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
impl Connection
Sourcepub fn sender(&self) -> Sender<ProtoMessage>
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.
Sourcepub fn receiver(&self) -> Receiver<ProtoMessage>
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.
Sourcepub async fn wait(self) -> Result<(), Error>
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.