diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index cb2b3717ab4..0b4f4590a56 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -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 buf) const { return this->helper_->getpeername_to(buf); @@ -532,8 +525,9 @@ class APIConnection final : public APIServerConnection { std::unique_ptr 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_; diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 85a74e32cf3..e2c870ff694 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -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, const ClientInfo *client_info) - : socket_(std::move(socket)), client_info_(client_info) {} + explicit APIFrameHelper(std::unique_ptr socket, const char *client_name) + : socket_(std::move(socket)), client_name_(client_name) {} virtual ~APIFrameHelper() = default; virtual APIError init() = 0; virtual APIError loop();