stream_server.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (C) 2020-2023 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/helpers.h"
  18. #include "esphome/core/log.h"
  19. #include "esphome/core/util.h"
  20. #include "esphome/components/network/util.h"
  21. #include "esphome/components/socket/socket.h"
  22. static const char *TAG = "stream_server";
  23. using namespace esphome;
  24. void StreamServerComponent::setup() {
  25. ESP_LOGCONFIG(TAG, "Setting up stream server...");
  26. struct sockaddr_storage bind_addr;
  27. socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), htons(this->port_));
  28. this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET);
  29. this->socket_->setblocking(false);
  30. this->socket_->bind(reinterpret_cast<struct sockaddr *>(&bind_addr), bind_addrlen);
  31. this->socket_->listen(8);
  32. }
  33. void StreamServerComponent::loop() {
  34. this->accept();
  35. this->read();
  36. this->write();
  37. this->cleanup();
  38. }
  39. void StreamServerComponent::dump_config() {
  40. ESP_LOGCONFIG(TAG, "Stream Server:");
  41. ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_ip_address().str().c_str(), this->port_);
  42. }
  43. void StreamServerComponent::on_shutdown() {
  44. for (const Client &client : this->clients_)
  45. client.socket->shutdown(SHUT_RDWR);
  46. }
  47. void StreamServerComponent::accept() {
  48. struct sockaddr_storage client_addr;
  49. socklen_t client_addrlen = sizeof(client_addr);
  50. std::unique_ptr<socket::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&client_addr), &client_addrlen);
  51. if (!socket)
  52. return;
  53. socket->setblocking(false);
  54. std::string identifier = socket->getpeername();
  55. this->clients_.emplace_back(std::move(socket), identifier);
  56. ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
  57. }
  58. void StreamServerComponent::cleanup() {
  59. auto discriminator = [](const Client &client) { return !client.disconnected; };
  60. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  61. this->clients_.erase(last_client, this->clients_.end());
  62. }
  63. void StreamServerComponent::read() {
  64. int len;
  65. while ((len = this->stream_->available()) > 0) {
  66. char buf[128];
  67. len = std::min(len, 128);
  68. this->stream_->read_array(reinterpret_cast<uint8_t *>(buf), len);
  69. for (const Client &client : this->clients_)
  70. client.socket->write(buf, len);
  71. }
  72. }
  73. void StreamServerComponent::write() {
  74. uint8_t buf[128];
  75. ssize_t len;
  76. for (Client &client : this->clients_) {
  77. while ((len = client.socket->read(&buf, sizeof(buf))) > 0)
  78. this->stream_->write_array(buf, len);
  79. if (len == 0) {
  80. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  81. client.disconnected = true;
  82. continue;
  83. }
  84. }
  85. }
  86. StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier)
  87. : socket(std::move(socket)), identifier{identifier} {}