/* Copyright (C) 2020-2023 Oxan van Leeuwen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "stream_server.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/util.h" #include "esphome/components/network/util.h" #include "esphome/components/socket/socket.h" static const char *TAG = "stream_server"; using namespace esphome; void StreamServerComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up stream server..."); // The make_unique() wrapper doesn't like arrays, so initialize the unique_ptr directly. this->buf_ = std::unique_ptr{new uint8_t[this->buf_size_]}; struct sockaddr_storage bind_addr; socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast(&bind_addr), sizeof(bind_addr), htons(this->port_)); 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); } void StreamServerComponent::loop() { this->accept(); this->read(); this->flush(); this->write(); this->cleanup(); } void StreamServerComponent::dump_config() { ESP_LOGCONFIG(TAG, "Stream Server:"); ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_use_address().c_str(), this->port_); #ifdef USE_BINARY_SENSOR LOG_BINARY_SENSOR(" ", "Connected:", this->connected_sensor_); #endif #ifdef USE_SENSOR LOG_SENSOR(" ", "Connection count:", this->connection_count_sensor_); #endif } void StreamServerComponent::on_shutdown() { for (const Client &client : this->clients_) client.socket->shutdown(SHUT_RDWR); } void StreamServerComponent::publish_sensor() { #ifdef USE_BINARY_SENSOR if (this->connected_sensor_) this->connected_sensor_->publish_state(this->clients_.size() > 0); #endif #ifdef USE_SENSOR if (this->connection_count_sensor_) this->connection_count_sensor_->publish_state(this->clients_.size()); #endif } void StreamServerComponent::accept() { struct sockaddr_storage client_addr; socklen_t client_addrlen = sizeof(client_addr); std::unique_ptr socket = this->socket_->accept(reinterpret_cast(&client_addr), &client_addrlen); if (!socket) return; socket->setblocking(false); std::string identifier = socket->getpeername(); this->clients_.emplace_back(std::move(socket), identifier, this->buf_head_); ESP_LOGD(TAG, "New client connected from %s", identifier.c_str()); this->publish_sensor(); } void StreamServerComponent::cleanup() { auto discriminator = [](const Client &client) { return !client.disconnected; }; auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator); if (last_client != this->clients_.end()) { this->clients_.erase(last_client, this->clients_.end()); this->publish_sensor(); } } void StreamServerComponent::read() { size_t len = 0; int available; while ((available = this->stream_->available()) > 0) { size_t free = this->buf_size_ - (this->buf_head_ - this->buf_tail_); if (free == 0) { // Only overwrite if nothing has been added yet, otherwise give flush() a chance to empty the buffer first. if (len > 0) return; ESP_LOGE(TAG, "Incoming bytes available, but outgoing buffer is full: stream will be corrupted!"); free = std::min(available, this->buf_size_); this->buf_tail_ += free; for (Client &client : this->clients_) { if (client.position < this->buf_tail_) { ESP_LOGW(TAG, "Dropped %u pending bytes for client %s", this->buf_tail_ - client.position, client.identifier.c_str()); client.position = this->buf_tail_; } } } // Fill all available contiguous space in the ring buffer. len = std::min(available, std::min(this->buf_ahead(this->buf_head_), free)); this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len); this->buf_head_ += len; } } void StreamServerComponent::flush() { this->buf_tail_ = this->buf_head_; for (Client &client : this->clients_) { if (client.position == this->buf_head_) continue; // Split the write into two parts: from the current position to the end of the ring buffer, and from the start // of the ring buffer until the head. The second part might be zero if no wraparound is necessary. struct iovec iov[2]; iov[0].iov_base = &this->buf_[this->buf_index(client.position)]; iov[0].iov_len = std::min(this->buf_head_ - client.position, this->buf_ahead(client.position)); iov[1].iov_base = &this->buf_[0]; iov[1].iov_len = this->buf_head_ - (client.position + iov[0].iov_len); client.position += client.socket->writev(iov, 2); this->buf_tail_ = std::min(this->buf_tail_, client.position); } } void StreamServerComponent::write() { uint8_t buf[128]; ssize_t len; for (Client &client : this->clients_) { while ((len = client.socket->read(&buf, sizeof(buf))) > 0) this->stream_->write_array(buf, len); if (len == 0) { ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str()); client.disconnected = true; continue; } } } StreamServerComponent::Client::Client(std::unique_ptr socket, std::string identifier, size_t position) : socket(std::move(socket)), identifier{identifier}, position{position} {}