stream_server.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. int len;
  31. while ((len = this->stream_->available()) > 0) {
  32. char buf[128];
  33. size_t read = this->stream_->readBytes(buf, min(len, 128));
  34. for (auto const& client : this->clients_)
  35. client->tcp_client->write(buf, read);
  36. }
  37. }
  38. void StreamServerComponent::write() {
  39. size_t len;
  40. while ((len = this->recv_buf_.size()) > 0) {
  41. this->stream_->write(this->recv_buf_.data(), len);
  42. this->recv_buf_.erase(this->recv_buf_.begin(), this->recv_buf_.begin() + len);
  43. }
  44. }
  45. void StreamServerComponent::dump_config() {
  46. ESP_LOGCONFIG(TAG, "Stream Server:");
  47. ESP_LOGCONFIG(TAG, " Address: %s:%u", network_get_address().c_str(), this->port_);
  48. }
  49. void StreamServerComponent::on_shutdown() {
  50. for (auto &client : this->clients_)
  51. client->tcp_client->close(true);
  52. }
  53. StreamServerComponent::Client::Client(AsyncClient *client, std::vector<uint8_t> &recv_buf) :
  54. tcp_client{client}, identifier{client->remoteIP().toString().c_str()}, disconnected{false} {
  55. ESP_LOGD(TAG, "New client connected from %s", this->identifier.c_str());
  56. this->tcp_client->onError( [this](void *h, AsyncClient *client, int8_t error) { this->disconnected = true; });
  57. this->tcp_client->onDisconnect([this](void *h, AsyncClient *client) { this->disconnected = true; });
  58. this->tcp_client->onTimeout( [this](void *h, AsyncClient *client, uint32_t time) { this->disconnected = true; });
  59. this->tcp_client->onData([&](void *h, AsyncClient *client, void *data, size_t len) {
  60. if (len == 0 || data == nullptr)
  61. return;
  62. auto buf = static_cast<uint8_t *>(data);
  63. recv_buf.insert(recv_buf.end(), buf, buf + len);
  64. }, nullptr);
  65. }
  66. StreamServerComponent::Client::~Client() {
  67. delete this->tcp_client;
  68. }