stream_server.cpp 4.2 KB

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