esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
frame_helper.hpp
Go to the documentation of this file.
1#pragma once
2
6
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <string>
13
14namespace esphome::api {
15
17enum class FrameStatus {
18 Ok,
19 NeedMore,
20};
21
31public:
32 FrameHelper() = default;
33 virtual ~FrameHelper() = default;
34
35 FrameHelper(const FrameHelper&) = delete;
39
42 virtual void encode(std::uint32_t msg_type, ByteView payload, ByteBuffer& out) = 0;
43
45 virtual void feed(ByteView data) = 0;
46
54 virtual FrameStatus next(std::uint32_t& out_type, ByteView& out_payload) = 0;
55
56 // --- Handshake hooks (Noise overrides; plaintext is a no-op) -------------
57
58 using HandshakeReady = std::function<void()>;
59 using HandshakeError = std::function<void(const std::string& what)>;
60
63 [[nodiscard]] virtual bool needs_handshake() const {
64 return false;
65 }
66
70 return {};
71 }
72
75 // By value (not const ref) so overrides can move the callbacks into members;
76 // this base no-op simply ignores them.
77 // NOLINTNEXTLINE(performance-unnecessary-value-param)
78 virtual void set_handshake_handlers(HandshakeReady /*on_ready*/, HandshakeError /*on_error*/) {}
79};
80
83inline constexpr std::size_t max_frame_payload = std::size_t{16} * 1024 * 1024;
84
85} // namespace esphome::api
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
Stateful codec sitting between the transport and the connection.
Definition frame_helper.hpp:30
FrameHelper(const FrameHelper &)=delete
FrameHelper & operator=(FrameHelper &&)=delete
FrameHelper(FrameHelper &&)=delete
FrameHelper & operator=(const FrameHelper &)=delete
virtual void encode(std::uint32_t msg_type, ByteView payload, ByteBuffer &out)=0
Serialize a message (numeric type id + protobuf payload) to wire bytes, appending them to out.
virtual ByteBuffer connect_bytes()
Bytes to transmit immediately after the TCP connection is established (the client's first handshake f...
Definition frame_helper.hpp:69
std::function< void()> HandshakeReady
Definition frame_helper.hpp:58
virtual FrameStatus next(std::uint32_t &out_type, ByteView &out_payload)=0
Try to extract the next complete frame.
virtual bool needs_handshake() const
True if the transport requires a handshake before application messages (and before the connection sho...
Definition frame_helper.hpp:63
virtual ~FrameHelper()=default
std::function< void(const std::string &what)> HandshakeError
Definition frame_helper.hpp:59
virtual void set_handshake_handlers(HandshakeReady, HandshakeError)
Register callbacks fired when the handshake completes or fails.
Definition frame_helper.hpp:78
virtual void feed(ByteView data)=0
Append freshly received bytes to the internal reassembly buffer.
Definition bytes.hpp:10
FrameStatus
Result of attempting to pull a frame from the reassembly buffer.
Definition frame_helper.hpp:17
@ NeedMore
The buffer holds only a partial frame; feed more bytes.
@ Ok
A complete value was decoded.
std::vector< std::uint8_t > ByteBuffer
Owning, growable byte buffer.
Definition bytes.hpp:13
constexpr std::size_t max_frame_payload
Hard upper bound on a single frame's payload, guarding against a hostile or corrupt length prefix (16...
Definition frame_helper.hpp:83