This commit is contained in:
J. Nick Koston
2025-12-27 17:49:04 -10:00
parent f4cb379d6b
commit f9659fc693
2 changed files with 6 additions and 15 deletions
+4 -10
View File
@@ -16,16 +16,9 @@
namespace esphome::api {
// Client information structure - uses fixed buffer to avoid std::string heap allocation
// Max client name length (e.g., "Home Assistant 2026.1.0.dev0" = 28 chars)
static constexpr size_t CLIENT_INFO_NAME_MAX_LEN = 32;
struct ClientInfo {
char name[CLIENT_INFO_NAME_MAX_LEN]{}; // Client name from Hello message
// Note: peername (IP address) is not stored here to save memory.
// Use helper_->getpeername_to() when needed.
};
// Keepalive timeout in milliseconds
static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
// Maximum number of entities to process in a single batch during initial state/info sending
@@ -294,7 +287,7 @@ class APIConnection final : public APIServerConnection {
bool try_to_clear_buffer(bool log_out_of_space);
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
StringRef get_name() const { return StringRef(this->client_info_.name); }
StringRef get_name() const { return StringRef(this->client_name_); }
/// Get peer name (IP address) into a stack buffer - avoids heap allocation
size_t get_peername_to(std::span<char, socket::PEERNAME_MAX_LEN> buf) const {
return this->helper_->getpeername_to(buf);
@@ -532,8 +525,9 @@ class APIConnection final : public APIServerConnection {
std::unique_ptr<camera::CameraImageReader> image_reader_;
#endif
// Group 3: Client info struct (24 bytes on 32-bit: 2 strings × 12 bytes each)
ClientInfo client_info_;
// Group 3: Client name (32 bytes fixed buffer, avoids heap allocation)
// Note: peername (IP address) is formatted on-demand via helper_->getpeername_to()
char client_name_[CLIENT_INFO_NAME_MAX_LEN]{};
// Group 4: 4-byte types
uint32_t last_traffic_;
+2 -5
View File
@@ -29,9 +29,6 @@ static constexpr uint16_t MAX_MESSAGE_SIZE = 8192; // 8 KiB for ESP8266
static constexpr uint16_t MAX_MESSAGE_SIZE = 32768; // 32 KiB for ESP32 and other platforms
#endif
// Forward declaration
struct ClientInfo;
class ProtoWriteBuffer;
struct ReadPacketBuffer {
@@ -82,8 +79,8 @@ const LogString *api_error_to_logstr(APIError err);
class APIFrameHelper {
public:
APIFrameHelper() = default;
explicit APIFrameHelper(std::unique_ptr<socket::Socket> socket, const ClientInfo *client_info)
: socket_(std::move(socket)), client_info_(client_info) {}
explicit APIFrameHelper(std::unique_ptr<socket::Socket> socket, const char *client_name)
: socket_(std::move(socket)), client_name_(client_name) {}
virtual ~APIFrameHelper() = default;
virtual APIError init() = 0;
virtual APIError loop();