stream_server.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "stream_server.h"
  2. #include "esphome/core/log.h"
  3. #include "esphome/core/util.h"
  4. static const char *TAG = "streamserver";
  5. using namespace esphome;
  6. void StreamServerComponent::setup() {
  7. ESP_LOGCONFIG(TAG, "Setting up stream server...");
  8. this->recv_buf_.reserve(128);
  9. this->server_ = AsyncServer(this->port_);
  10. this->server_.begin();
  11. this->server_.onClient([this](void *h, AsyncClient *tcpClient) {
  12. if(tcpClient == nullptr)
  13. return;
  14. this->clients_.push_back(std::unique_ptr<Client>(new Client(tcpClient, this->recv_buf_)));
  15. }, this);
  16. }
  17. void StreamServerComponent::loop() {
  18. this->cleanup();
  19. this->read();
  20. this->write();
  21. }
  22. void StreamServerComponent::cleanup() {
  23. auto discriminator = [](std::unique_ptr<Client> &client) { return !client->disconnected; };
  24. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  25. for (auto it = last_client; it != this->clients_.end(); it++)
  26. ESP_LOGD(TAG, "Client %s disconnected", (*it)->identifier.c_str());
  27. this->clients_.erase(last_client, this->clients_.end());
  28. }
  29. void StreamServerComponent::read() {
  30. while ((int len = this->stream_->available()) > 0) {
  31. char buf[128];
  32. size_t read = this->stream_->readBytes(buf, min(len, 128));
  33. for (auto const& client : this->clients_)
  34. client->tcp_client->write(buf, read);
  35. }
  36. }
  37. void StreamServerComponent::write() {
  38. size_t len;
  39. while ((len = this->recv_buf_.size()) > 0) {
  40. this->stream_->write(this->recv_buf_.data(), len);
  41. this->recv_buf_.erase(this->recv_buf_.begin(), this->recv_buf_.begin() + len);
  42. }
  43. }
  44. void StreamServerComponent::dump_config() {
  45. ESP_LOGCONFIG(TAG, "Stream Server:");
  46. ESP_LOGCONFIG(TAG, " Address: %s:%u", network_get_address().c_str(), this->port_);
  47. }
  48. void StreamServerComponent::on_shutdown() {
  49. for (auto &client : this->clients_)
  50. client->tcp_client->close(true);
  51. }
  52. StreamServerComponent::Client::Client(AsyncClient *client, std::vector<uint8_t> &recv_buf) :
  53. tcp_client{client}, identifier{client->remoteIP().toString().c_str()}, disconnected{false} {
  54. ESP_LOGD(TAG, "New client connected from %s", this->identifier.c_str());
  55. this->tcp_client->onError( [this](void *h, AsyncClient *client, int8_t error) { this->disconnected = true; });
  56. this->tcp_client->onDisconnect([this](void *h, AsyncClient *client) { this->disconnected = true; });
  57. this->tcp_client->onTimeout( [this](void *h, AsyncClient *client, uint32_t time) { this->disconnected = true; });
  58. this->tcp_client->onData([&](void *h, AsyncClient *client, void *data, size_t len) {
  59. if (len == 0 || data == nullptr)
  60. return;
  61. auto buf = static_cast<uint8_t *>(data);
  62. recv_buf.insert(recv_buf.end(), buf, buf + len);
  63. }, nullptr);
  64. }
  65. StreamServerComponent::Client::~Client() {
  66. delete this->tcp_client;
  67. }