stream_server.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "esphome/components/network/util.h"
  20. static const char *TAG = "streamserver";
  21. using namespace esphome;
  22. void StreamServerComponent::setup() {
  23. ESP_LOGCONFIG(TAG, "Setting up stream server...");
  24. this->recv_buf_.reserve(128);
  25. this->server_ = AsyncServer(this->port_);
  26. this->server_.begin();
  27. this->server_.onClient([this](void *h, AsyncClient *tcpClient) {
  28. if(tcpClient == nullptr)
  29. return;
  30. this->clients_.push_back(std::unique_ptr<Client>(new Client(tcpClient, this->recv_buf_)));
  31. }, this);
  32. }
  33. void StreamServerComponent::loop() {
  34. this->cleanup();
  35. this->read();
  36. this->write();
  37. }
  38. void StreamServerComponent::cleanup() {
  39. auto discriminator = [](std::unique_ptr<Client> &client) { return !client->disconnected; };
  40. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  41. for (auto it = last_client; it != this->clients_.end(); it++)
  42. ESP_LOGD(TAG, "Client %s disconnected", (*it)->identifier.c_str());
  43. this->clients_.erase(last_client, this->clients_.end());
  44. }
  45. void StreamServerComponent::read() {
  46. int len;
  47. while ((len = this->stream_->available()) > 0) {
  48. char buf[128];
  49. len = std::min(len, 128);
  50. this->stream_->read_array(reinterpret_cast<uint8_t*>(buf), len);
  51. for (auto const& client : this->clients_)
  52. client->tcp_client->write(buf, len);
  53. }
  54. }
  55. void StreamServerComponent::write() {
  56. this->stream_->write_array(this->recv_buf_);
  57. this->recv_buf_.clear();
  58. }
  59. void StreamServerComponent::dump_config() {
  60. ESP_LOGCONFIG(TAG, "Stream Server:");
  61. ESP_LOGCONFIG(TAG, " Address: %s:%u",
  62. esphome::network::get_ip_address().str().c_str(),
  63. this->port_);
  64. }
  65. void StreamServerComponent::on_shutdown() {
  66. for (auto &client : this->clients_)
  67. client->tcp_client->close(true);
  68. }
  69. StreamServerComponent::Client::Client(AsyncClient *client, std::vector<uint8_t> &recv_buf) :
  70. tcp_client{client}, identifier{client->remoteIP().toString().c_str()}, disconnected{false} {
  71. ESP_LOGD(TAG, "New client connected from %s", this->identifier.c_str());
  72. this->tcp_client->onError( [this](void *h, AsyncClient *client, int8_t error) { this->disconnected = true; });
  73. this->tcp_client->onDisconnect([this](void *h, AsyncClient *client) { this->disconnected = true; });
  74. this->tcp_client->onTimeout( [this](void *h, AsyncClient *client, uint32_t time) { this->disconnected = true; });
  75. this->tcp_client->onData([&](void *h, AsyncClient *client, void *data, size_t len) {
  76. if (len == 0 || data == nullptr)
  77. return;
  78. auto buf = static_cast<uint8_t *>(data);
  79. recv_buf.insert(recv_buf.end(), buf, buf + len);
  80. }, nullptr);
  81. }
  82. StreamServerComponent::Client::~Client() {
  83. delete this->tcp_client;
  84. }