| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /* 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 <https://www.gnu.org/licenses/>.
- */
- #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<uint8_t[]>{new uint8_t[this->buf_size_]};
- struct sockaddr_storage bind_addr;
- socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&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<struct sockaddr *>(&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::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&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() {
- bool first_iteration = true;
- int available;
- while ((available = this->stream_->available()) > 0) {
- // Write until the tail is encountered, or wraparound of the ring buffer if that happens before.
- size_t max = std::min(this->buf_ahead(this->buf_head_), this->buf_tail_ + this->buf_size_ - this->buf_head_);
- if (max == 0) {
- // Only warn on the first iteration, the finite buffer size is also used as a throttling mechanism to avoid
- // blocking here for too long when a large amount of data comes in.
- if (first_iteration)
- ESP_LOGW(TAG, "Incoming bytes available in stream, but outgoing buffer is full!");
- break;
- }
- size_t len = std::min<size_t>(available, max);
- this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len);
- this->buf_head_ += len;
- first_iteration = false;
- }
- }
- 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<esphome::socket::Socket> socket, std::string identifier, size_t position)
- : socket(std::move(socket)), identifier{identifier}, position{position} {}
|