stream_server.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_->bind(reinterpret_cast<struct sockaddr *>(&bind_addr), bind_addrlen);
  30. this->socket_->listen(8);
  31. }
  32. void StreamServerComponent::loop() {
  33. this->accept();
  34. this->read();
  35. this->write();
  36. this->cleanup();
  37. }
  38. void StreamServerComponent::dump_config() {
  39. ESP_LOGCONFIG(TAG, "Stream Server:");
  40. ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_ip_address().str().c_str(), this->port_);
  41. }
  42. void StreamServerComponent::on_shutdown() {
  43. for (const Client &client : this->clients_)
  44. client.socket->shutdown(SHUT_RDWR);
  45. }
  46. void StreamServerComponent::accept() {
  47. struct sockaddr_storage client_addr;
  48. socklen_t client_addrlen = sizeof(client_addr);
  49. std::unique_ptr<socket::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&client_addr), &client_addrlen);
  50. if (!socket)
  51. return;
  52. socket->setblocking(false);
  53. std::string identifier = socket->getpeername();
  54. this->clients_.emplace_back(std::move(socket), identifier);
  55. ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
  56. }
  57. void StreamServerComponent::cleanup() {
  58. auto discriminator = [](const Client &client) { return !client.disconnected; };
  59. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  60. this->clients_.erase(last_client, this->clients_.end());
  61. }
  62. void StreamServerComponent::read() {
  63. int len;
  64. while ((len = this->stream_->available()) > 0) {
  65. char buf[128];
  66. len = std::min(len, 128);
  67. this->stream_->read_array(reinterpret_cast<uint8_t *>(buf), len);
  68. for (const Client &client : this->clients_)
  69. client.socket->write(buf, len);
  70. }
  71. }
  72. void StreamServerComponent::write() {
  73. uint8_t buf[128];
  74. ssize_t len;
  75. for (Client &client : this->clients_) {
  76. while ((len = client.socket->read(&buf, sizeof(buf))) > 0)
  77. this->stream_->write_array(buf, len);
  78. if (len == 0) {
  79. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  80. client.disconnected = true;
  81. continue;
  82. }
  83. }
  84. }
  85. StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier)
  86. : socket(std::move(socket)), identifier{identifier} {}