21#include <system_error>
45 using ErrorHandler = std::function<void(std::error_code,
const std::string&)>;
49 std::unique_ptr<Transport> transport,
50 std::unique_ptr<FrameHelper> frame_helper,
101 [[nodiscard]] std::exception_ptr
last_error() const noexcept {
107 void on_connected_transport(std::error_code ec);
108 void on_bytes(std::error_code ec,
ByteView data);
110 void handle_frame(std::uint32_t msg_type,
ByteView payload);
111 void handle_internal(std::uint32_t msg_type,
const ProtoMessage& msg);
112 void send_hello()
const;
113 void finish_connect(std::error_code ec);
114 void fail(std::error_code ec,
const std::string& what);
116 void start_keepalive();
117 void stop_keepalive();
121 std::unique_ptr<Transport> transport_;
122 std::unique_ptr<FrameHelper> frame_helper_;
128 std::vector<MessageHandler> handlers_;
134 std::exception_ptr last_error_;
139 bool awaiting_pong_ =
false;
Byte buffer / view aliases and protobuf base-128 varint codec.
Non-owning view over a contiguous run of bytes (a C++17 stand-in for std::span<const std::uint8_t>).
Definition bytes.hpp:17
Drives one logical session with an ESPHome device: TCP connect, optional Noise handshake (via the inj...
Definition connection.hpp:40
Connection(Executor &executor, std::unique_ptr< Transport > transport, std::unique_ptr< FrameHelper > frame_helper, ConnectionOptions options)
std::function< void(std::error_code)> ConnectHandler
Definition connection.hpp:46
std::function< void(std::error_code, const std::string &)> ErrorHandler
Definition connection.hpp:45
std::function< void(std::uint32_t msg_type, ByteView payload)> RawHandler
Definition connection.hpp:43
std::function< void(ConnectionState)> StateHandler
Definition connection.hpp:44
void send(const ProtoMessage &msg) const
Serialize and send a typed protobuf message.
bool is_connected() const noexcept
Definition connection.hpp:93
ConnectionState state() const noexcept
Definition connection.hpp:87
void start(const std::string &host, std::uint16_t port, ConnectHandler on_done)
Connect to host:port, perform the handshake and signal completion via on_done exactly once (empty err...
void on(std::uint32_t msg_type, MessageHandler handler)
Install a handler for one message id (replaces any previous handler).
Connection & operator=(const Connection &)=delete
void on_error(ErrorHandler handler)
Observe terminal errors (read failure, protocol violation, timeout).
Connection(const Connection &)=delete
void on_raw(RawHandler handler)
Install a handler invoked with the raw bytes of every inbound frame (even messages the registry canno...
const ServerHello & server_hello() const noexcept
Definition connection.hpp:90
void disconnect()
Begin a graceful disconnect (DisconnectRequest, then close).
void send_raw(std::uint32_t msg_type, ByteView payload) const
Send a pre-serialized payload under an explicit message type id.
std::exception_ptr last_error() const noexcept
The exception that caused the most recent failure, if it carried richer type/message information than...
Definition connection.hpp:101
std::function< void(const ProtoMessage &)> MessageHandler
Definition connection.hpp:42
void on_state(StateHandler handler)
Observe state transitions.
void on_any(MessageHandler handler)
Install a catch-all handler invoked for every decoded inbound message, after any id-specific handler.
Minimal executor: deferred execution plus one-shot timers.
Definition executor.hpp:20
Base class every generated message derives from.
Definition proto_message.hpp:23
Tunables for a Connection's handshake and keepalive behaviour.
Connection state-machine states.
Executor + timer abstraction (asio-free).
Abstract frame codec: turns a byte stream into ESPHome message frames and back, independent of the un...
std::uint64_t TimerId
Opaque handle to a scheduled timer; invalid_timer means "no timer".
Definition executor.hpp:14
ConnectionState
Lifecycle of a Connection.
Definition connection_state.hpp:11
@ Connected
Handshake complete; messages flow freely.
@ Disconnected
Idle, never connected or fully torn down.
constexpr TimerId invalid_timer
Definition executor.hpp:15
Lightweight base class for every generated ESPHome API message.
Knobs controlling a single Connection. Defaults mirror aioesphomeapi.
Definition connection_options.hpp:12
Information returned by the device in its HelloResponse.
Definition connection.hpp:27
int api_version_minor
Definition connection.hpp:29
std::string name
Definition connection.hpp:31
std::string server_info
Definition connection.hpp:30
int api_version_major
Definition connection.hpp:28
Transport abstraction (asio-free) the connection drives.