From 4ef23f01d499f44a707ca9dd838158741d029840 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Thu, 20 Apr 2023 16:40:20 +1000 Subject: [PATCH 1/3] Fix compatibility with ESPHome 2023.4 Since ESPHome 2023.4 the `stream_server` component doesn't listen on the specified port. I think this was broken by esphome/esphome#4574. --- components/stream_server/stream_server.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/stream_server/stream_server.cpp b/components/stream_server/stream_server.cpp index 212aff5..9882391 100644 --- a/components/stream_server/stream_server.cpp +++ b/components/stream_server/stream_server.cpp @@ -3,6 +3,7 @@ #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/util.h" +#include "esphome/core/version.h" #include "esphome/components/network/util.h" #include "esphome/components/socket/socket.h" @@ -18,7 +19,11 @@ void StreamServerComponent::setup() { this->buf_ = std::unique_ptr{new uint8_t[this->buf_size_]}; struct sockaddr_storage bind_addr; +#if ESPHOME_VERSION_CODE >= VERSION_CODE(2023, 4, 0) + socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), this->port_); +#else socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), htons(this->port_)); +#endif this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET); this->socket_->setblocking(false); From 3b253a0e95dd55317a9869591d7029825fa36ae6 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Thu, 20 Apr 2023 22:02:32 +1000 Subject: [PATCH 2/3] Add error checking --- components/stream_server/stream_server.cpp | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/components/stream_server/stream_server.cpp b/components/stream_server/stream_server.cpp index 9882391..6374c58 100644 --- a/components/stream_server/stream_server.cpp +++ b/components/stream_server/stream_server.cpp @@ -19,16 +19,45 @@ void StreamServerComponent::setup() { this->buf_ = std::unique_ptr{new uint8_t[this->buf_size_]}; struct sockaddr_storage bind_addr; + #if ESPHOME_VERSION_CODE >= VERSION_CODE(2023, 4, 0) socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), this->port_); #else socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), htons(this->port_)); #endif + if (bind_addrlen == 0) { + ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); + this->mark_failed(); + return; + } this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET); - this->socket_->setblocking(false); - this->socket_->bind(reinterpret_cast(&bind_addr), bind_addrlen); - this->socket_->listen(8); + if (this->socket_ == nullptr) { + ESP_LOGW(TAG, "Could not create socket."); + this->mark_failed(); + return; + } + + int err = this->socket_->setblocking(false); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); + this->mark_failed(); + return; + } + + err = this->socket_->bind(reinterpret_cast(&bind_addr), sizeof(bind_addr)); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); + this->mark_failed(); + return; + } + + err = this->socket_->listen(8); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); + this->mark_failed(); + return; + } this->publish_sensor(); } From bf80a89aef3f822c195833e8e1993bde7746889f Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Fri, 21 Apr 2023 10:23:28 +1000 Subject: [PATCH 3/3] Address feedback --- components/stream_server/stream_server.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/stream_server/stream_server.cpp b/components/stream_server/stream_server.cpp index 6374c58..9bfa9b4 100644 --- a/components/stream_server/stream_server.cpp +++ b/components/stream_server/stream_server.cpp @@ -26,35 +26,35 @@ void StreamServerComponent::setup() { socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), htons(this->port_)); #endif if (bind_addrlen == 0) { - ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); + ESP_LOGE(TAG, "Socket unable to set sockaddr: errno %d", errno); this->mark_failed(); return; } this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET); if (this->socket_ == nullptr) { - ESP_LOGW(TAG, "Could not create socket."); + ESP_LOGE(TAG, "Could not create socket."); this->mark_failed(); return; } int err = this->socket_->setblocking(false); if (err != 0) { - ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); + ESP_LOGE(TAG, "Socket unable to set nonblocking mode: errno %d", err); this->mark_failed(); return; } - err = this->socket_->bind(reinterpret_cast(&bind_addr), sizeof(bind_addr)); + err = this->socket_->bind(reinterpret_cast(&bind_addr), bind_addrlen); if (err != 0) { - ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); + ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); this->mark_failed(); return; } err = this->socket_->listen(8); if (err != 0) { - ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); + ESP_LOGE(TAG, "Socket unable to listen: errno %d", errno); this->mark_failed(); return; }