|
|
@@ -1,4 +1,4 @@
|
|
|
-/* Copyright (C) 2020-2021 Oxan van Leeuwen
|
|
|
+/* 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
|
|
|
@@ -16,108 +16,151 @@
|
|
|
|
|
|
#include "stream_server.h"
|
|
|
|
|
|
+#include "esphome/core/helpers.h"
|
|
|
#include "esphome/core/log.h"
|
|
|
#include "esphome/core/util.h"
|
|
|
|
|
|
-#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
|
|
|
#include "esphome/components/network/util.h"
|
|
|
-#endif
|
|
|
-
|
|
|
+#include "esphome/components/socket/socket.h"
|
|
|
|
|
|
-static const char *TAG = "streamserver";
|
|
|
+static const char *TAG = "stream_server";
|
|
|
|
|
|
using namespace esphome;
|
|
|
|
|
|
void StreamServerComponent::setup() {
|
|
|
ESP_LOGCONFIG(TAG, "Setting up stream server...");
|
|
|
- this->recv_buf_.reserve(128);
|
|
|
|
|
|
- this->server_ = AsyncServer(this->port_);
|
|
|
- this->server_.begin();
|
|
|
- this->server_.onClient([this](void *h, AsyncClient *tcpClient) {
|
|
|
- if(tcpClient == nullptr)
|
|
|
- return;
|
|
|
+ // 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->clients_.push_back(std::unique_ptr<Client>(new Client(tcpClient, this->recv_buf_)));
|
|
|
- }, this);
|
|
|
+ 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->cleanup();
|
|
|
+ this->accept();
|
|
|
this->read();
|
|
|
+ this->flush();
|
|
|
this->write();
|
|
|
+ this->cleanup();
|
|
|
}
|
|
|
|
|
|
-void StreamServerComponent::cleanup() {
|
|
|
- auto discriminator = [](std::unique_ptr<Client> &client) { return !client->disconnected; };
|
|
|
- auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
|
|
|
- for (auto it = last_client; it != this->clients_.end(); it++)
|
|
|
- ESP_LOGD(TAG, "Client %s disconnected", (*it)->identifier.c_str());
|
|
|
-
|
|
|
- this->clients_.erase(last_client, this->clients_.end());
|
|
|
-}
|
|
|
-
|
|
|
-void StreamServerComponent::read() {
|
|
|
- int len;
|
|
|
- while ((len = this->stream_->available()) > 0) {
|
|
|
- char buf[128];
|
|
|
- len = std::min(len, 128);
|
|
|
-#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
|
|
|
- this->stream_->read_array(reinterpret_cast<uint8_t*>(buf), len);
|
|
|
-#else
|
|
|
- this->stream_->readBytes(buf, len);
|
|
|
+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
|
|
|
- for (auto const& client : this->clients_)
|
|
|
- client->tcp_client->write(buf, len);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
-void StreamServerComponent::write() {
|
|
|
-#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
|
|
|
- this->stream_->write_array(this->recv_buf_);
|
|
|
- this->recv_buf_.clear();
|
|
|
-#else
|
|
|
- size_t len;
|
|
|
- while ((len = this->recv_buf_.size()) > 0) {
|
|
|
- this->stream_->write(this->recv_buf_.data(), len);
|
|
|
- this->recv_buf_.erase(this->recv_buf_.begin(), this->recv_buf_.begin() + len);
|
|
|
- }
|
|
|
-#endif
|
|
|
+void StreamServerComponent::on_shutdown() {
|
|
|
+ for (const Client &client : this->clients_)
|
|
|
+ client.socket->shutdown(SHUT_RDWR);
|
|
|
}
|
|
|
|
|
|
-void StreamServerComponent::dump_config() {
|
|
|
- ESP_LOGCONFIG(TAG, "Stream Server:");
|
|
|
- ESP_LOGCONFIG(TAG, " Address: %s:%u",
|
|
|
-#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
|
|
|
- esphome::network::get_ip_address().str().c_str(),
|
|
|
-#else
|
|
|
- network_get_address().c_str(),
|
|
|
+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
|
|
|
- this->port_);
|
|
|
}
|
|
|
|
|
|
-void StreamServerComponent::on_shutdown() {
|
|
|
- for (auto &client : this->clients_)
|
|
|
- client->tcp_client->close(true);
|
|
|
+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();
|
|
|
}
|
|
|
|
|
|
-StreamServerComponent::Client::Client(AsyncClient *client, std::vector<uint8_t> &recv_buf) :
|
|
|
- tcp_client{client}, identifier{client->remoteIP().toString().c_str()}, disconnected{false} {
|
|
|
- ESP_LOGD(TAG, "New client connected from %s", this->identifier.c_str());
|
|
|
-
|
|
|
- this->tcp_client->onError( [this](void *h, AsyncClient *client, int8_t error) { this->disconnected = true; });
|
|
|
- this->tcp_client->onDisconnect([this](void *h, AsyncClient *client) { this->disconnected = true; });
|
|
|
- this->tcp_client->onTimeout( [this](void *h, AsyncClient *client, uint32_t time) { this->disconnected = true; });
|
|
|
+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();
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- this->tcp_client->onData([&](void *h, AsyncClient *client, void *data, size_t len) {
|
|
|
- if (len == 0 || data == nullptr)
|
|
|
- return;
|
|
|
+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<size_t>(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<size_t>(available, std::min<size_t>(this->buf_ahead(this->buf_head_), free));
|
|
|
+ this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len);
|
|
|
+ this->buf_head_ += len;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- auto buf = static_cast<uint8_t *>(data);
|
|
|
- recv_buf.insert(recv_buf.end(), buf, buf + len);
|
|
|
- }, nullptr);
|
|
|
+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);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-StreamServerComponent::Client::~Client() {
|
|
|
- delete this->tcp_client;
|
|
|
+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} {}
|