stream_server.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2020-2021 Oxan van Leeuwen
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "stream_server.h"
  17. #include "esphome/core/log.h"
  18. #include "esphome/core/util.h"
  19. static const char *TAG = "streamserver";
  20. using namespace esphome;
  21. void StreamServerComponent::setup() {
  22. ESP_LOGCONFIG(TAG, "Setting up stream server...");
  23. this->recv_buf_.reserve(128);
  24. this->server_ = AsyncServer(this->port_);
  25. this->server_.begin();
  26. this->server_.onClient([this](void *h, AsyncClient *tcpClient) {
  27. if(tcpClient == nullptr)
  28. return;
  29. this->clients_.push_back(std::unique_ptr<Client>(new Client(tcpClient, this->recv_buf_)));
  30. }, this);
  31. }
  32. void StreamServerComponent::loop() {
  33. this->cleanup();
  34. this->read();
  35. this->write();
  36. }
  37. void StreamServerComponent::cleanup() {
  38. auto discriminator = [](std::unique_ptr<Client> &client) { return !client->disconnected; };
  39. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  40. for (auto it = last_client; it != this->clients_.end(); it++)
  41. ESP_LOGD(TAG, "Client %s disconnected", (*it)->identifier.c_str());
  42. this->clients_.erase(last_client, this->clients_.end());
  43. }
  44. void StreamServerComponent::read() {
  45. int len;
  46. while ((len = this->stream_->available()) > 0) {
  47. char buf[128];
  48. size_t read = this->stream_->readBytes(buf, min(len, 128));
  49. for (auto const& client : this->clients_)
  50. client->tcp_client->write(buf, read);
  51. }
  52. }
  53. void StreamServerComponent::write() {
  54. size_t len;
  55. while ((len = this->recv_buf_.size()) > 0) {
  56. this->stream_->write(this->recv_buf_.data(), len);
  57. this->recv_buf_.erase(this->recv_buf_.begin(), this->recv_buf_.begin() + len);
  58. }
  59. }
  60. void StreamServerComponent::dump_config() {
  61. ESP_LOGCONFIG(TAG, "Stream Server:");
  62. ESP_LOGCONFIG(TAG, " Address: %s:%u", network_get_address().c_str(), this->port_);
  63. }
  64. void StreamServerComponent::on_shutdown() {
  65. for (auto &client : this->clients_)
  66. client->tcp_client->close(true);
  67. }
  68. StreamServerComponent::Client::Client(AsyncClient *client, std::vector<uint8_t> &recv_buf) :
  69. tcp_client{client}, identifier{client->remoteIP().toString().c_str()}, disconnected{false} {
  70. ESP_LOGD(TAG, "New client connected from %s", this->identifier.c_str());
  71. this->tcp_client->onError( [this](void *h, AsyncClient *client, int8_t error) { this->disconnected = true; });
  72. this->tcp_client->onDisconnect([this](void *h, AsyncClient *client) { this->disconnected = true; });
  73. this->tcp_client->onTimeout( [this](void *h, AsyncClient *client, uint32_t time) { this->disconnected = true; });
  74. this->tcp_client->onData([&](void *h, AsyncClient *client, void *data, size_t len) {
  75. if (len == 0 || data == nullptr)
  76. return;
  77. auto buf = static_cast<uint8_t *>(data);
  78. recv_buf.insert(recv_buf.end(), buf, buf + len);
  79. }, nullptr);
  80. }
  81. StreamServerComponent::Client::~Client() {
  82. delete this->tcp_client;
  83. }