stream_server.cpp 3.7 KB

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