mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	limtis
This commit is contained in:
		| @@ -59,6 +59,8 @@ CONF_BATCH_DELAY = "batch_delay" | ||||
| CONF_CUSTOM_SERVICES = "custom_services" | ||||
| CONF_HOMEASSISTANT_SERVICES = "homeassistant_services" | ||||
| CONF_HOMEASSISTANT_STATES = "homeassistant_states" | ||||
| CONF_LISTEN_BACKLOG = "listen_backlog" | ||||
| CONF_MAX_CONNECTIONS = "max_connections" | ||||
|  | ||||
|  | ||||
| def validate_encryption_key(value): | ||||
| @@ -158,6 +160,12 @@ CONFIG_SCHEMA = cv.All( | ||||
|             cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation( | ||||
|                 single=True | ||||
|             ), | ||||
|             cv.SplitDefault(CONF_LISTEN_BACKLOG, esp8266=1, default=4): cv.int_range( | ||||
|                 min=1, max=10 | ||||
|             ), | ||||
|             cv.SplitDefault(CONF_MAX_CONNECTIONS, esp8266=4, default=8): cv.int_range( | ||||
|                 min=1, max=20 | ||||
|             ), | ||||
|         } | ||||
|     ).extend(cv.COMPONENT_SCHEMA), | ||||
|     cv.rename_key(CONF_SERVICES, CONF_ACTIONS), | ||||
| @@ -176,6 +184,8 @@ async def to_code(config): | ||||
|         cg.add(var.set_password(config[CONF_PASSWORD])) | ||||
|     cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) | ||||
|     cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY])) | ||||
|     cg.add(var.set_listen_backlog(config[CONF_LISTEN_BACKLOG])) | ||||
|     cg.add(var.set_max_connections(config[CONF_MAX_CONNECTIONS])) | ||||
|  | ||||
|     # Set USE_API_SERVICES if any services are enabled | ||||
|     if config.get(CONF_ACTIONS) or config[CONF_CUSTOM_SERVICES]: | ||||
|   | ||||
| @@ -87,7 +87,7 @@ void APIServer::setup() { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   err = this->socket_->listen(4); | ||||
|   err = this->socket_->listen(this->listen_backlog_); | ||||
|   if (err != 0) { | ||||
|     ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); | ||||
|     this->mark_failed(); | ||||
| @@ -140,9 +140,19 @@ void APIServer::loop() { | ||||
|     while (true) { | ||||
|       struct sockaddr_storage source_addr; | ||||
|       socklen_t addr_len = sizeof(source_addr); | ||||
|  | ||||
|       auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len); | ||||
|       if (!sock) | ||||
|         break; | ||||
|  | ||||
|       // Check if we're at the connection limit | ||||
|       if (this->clients_.size() >= this->max_connections_) { | ||||
|         ESP_LOGW(TAG, "Max connections (%d), rejecting %s", this->max_connections_, sock->getpeername().c_str()); | ||||
|         // Immediately close - socket destructor will handle cleanup | ||||
|         sock.reset(); | ||||
|         continue; | ||||
|       } | ||||
|  | ||||
|       ESP_LOGD(TAG, "Accept %s", sock->getpeername().c_str()); | ||||
|  | ||||
|       auto *conn = new APIConnection(std::move(sock), this); | ||||
| @@ -206,8 +216,10 @@ void APIServer::loop() { | ||||
| void APIServer::dump_config() { | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "Server:\n" | ||||
|                 "  Address: %s:%u", | ||||
|                 network::get_use_address().c_str(), this->port_); | ||||
|                 "  Address: %s:%u\n" | ||||
|                 "  Listen backlog: %u\n" | ||||
|                 "  Max connections: %u", | ||||
|                 network::get_use_address().c_str(), this->port_, this->listen_backlog_, this->max_connections_); | ||||
| #ifdef USE_API_NOISE | ||||
|   ESP_LOGCONFIG(TAG, "  Noise encryption: %s", YESNO(this->noise_ctx_->has_psk())); | ||||
|   if (!this->noise_ctx_->has_psk()) { | ||||
|   | ||||
| @@ -44,6 +44,8 @@ class APIServer : public Component, public Controller { | ||||
|   void set_reboot_timeout(uint32_t reboot_timeout); | ||||
|   void set_batch_delay(uint16_t batch_delay); | ||||
|   uint16_t get_batch_delay() const { return batch_delay_; } | ||||
|   void set_listen_backlog(uint8_t listen_backlog) { this->listen_backlog_ = listen_backlog; } | ||||
|   void set_max_connections(uint8_t max_connections) { this->max_connections_ = max_connections; } | ||||
|  | ||||
|   // Get reference to shared buffer for API connections | ||||
|   std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; } | ||||
| @@ -189,8 +191,15 @@ class APIServer : public Component, public Controller { | ||||
|   // Group smaller types together | ||||
|   uint16_t port_{6053}; | ||||
|   uint16_t batch_delay_{100}; | ||||
| #ifdef USE_ESP8266 | ||||
|   uint8_t listen_backlog_{1}; | ||||
|   uint8_t max_connections_{4}; | ||||
| #else | ||||
|   uint8_t listen_backlog_{4}; | ||||
|   uint8_t max_connections_{8}; | ||||
| #endif | ||||
|   bool shutting_down_ = false; | ||||
|   // 5 bytes used, 3 bytes padding | ||||
|   // 7 bytes used, 1 byte padding | ||||
|  | ||||
| #ifdef USE_API_NOISE | ||||
|   std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user