stream_server.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // The make_unique() wrapper doesn't like arrays, so initialize the unique_ptr directly.
  27. this->buf_ = std::unique_ptr<uint8_t[]>{new uint8_t[this->buf_size_]};
  28. struct sockaddr_storage bind_addr;
  29. socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), htons(this->port_));
  30. this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET);
  31. this->socket_->setblocking(false);
  32. this->socket_->bind(reinterpret_cast<struct sockaddr *>(&bind_addr), bind_addrlen);
  33. this->socket_->listen(8);
  34. }
  35. void StreamServerComponent::loop() {
  36. this->accept();
  37. this->read();
  38. this->flush();
  39. this->write();
  40. this->cleanup();
  41. }
  42. void StreamServerComponent::dump_config() {
  43. ESP_LOGCONFIG(TAG, "Stream Server:");
  44. ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_use_address().c_str(), this->port_);
  45. #ifdef USE_BINARY_SENSOR
  46. LOG_BINARY_SENSOR(" ", "Connected:", this->connected_sensor_);
  47. #endif
  48. #ifdef USE_SENSOR
  49. LOG_SENSOR(" ", "Connection count:", this->connection_count_sensor_);
  50. #endif
  51. }
  52. void StreamServerComponent::on_shutdown() {
  53. for (const Client &client : this->clients_)
  54. client.socket->shutdown(SHUT_RDWR);
  55. }
  56. void StreamServerComponent::publish_sensor() {
  57. #ifdef USE_BINARY_SENSOR
  58. if (this->connected_sensor_)
  59. this->connected_sensor_->publish_state(this->clients_.size() > 0);
  60. #endif
  61. #ifdef USE_SENSOR
  62. if (this->connection_count_sensor_)
  63. this->connection_count_sensor_->publish_state(this->clients_.size());
  64. #endif
  65. }
  66. void StreamServerComponent::accept() {
  67. struct sockaddr_storage client_addr;
  68. socklen_t client_addrlen = sizeof(client_addr);
  69. std::unique_ptr<socket::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&client_addr), &client_addrlen);
  70. if (!socket)
  71. return;
  72. socket->setblocking(false);
  73. std::string identifier = socket->getpeername();
  74. this->clients_.emplace_back(std::move(socket), identifier, this->buf_head_);
  75. ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
  76. this->publish_sensor();
  77. }
  78. void StreamServerComponent::cleanup() {
  79. auto discriminator = [](const Client &client) { return !client.disconnected; };
  80. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  81. if (last_client != this->clients_.end()) {
  82. this->clients_.erase(last_client, this->clients_.end());
  83. this->publish_sensor();
  84. }
  85. }
  86. void StreamServerComponent::read() {
  87. bool first_iteration = true;
  88. int available;
  89. while ((available = this->stream_->available()) > 0) {
  90. // Write until the tail is encountered, or wraparound of the ring buffer if that happens before.
  91. size_t max = std::min(this->buf_ahead(this->buf_head_), this->buf_tail_ + this->buf_size_ - this->buf_head_);
  92. if (max == 0) {
  93. // Only warn on the first iteration, the finite buffer size is also used as a throttling mechanism to avoid
  94. // blocking here for too long when a large amount of data comes in.
  95. if (first_iteration)
  96. ESP_LOGW(TAG, "Incoming bytes available in stream, but outgoing buffer is full!");
  97. break;
  98. }
  99. size_t len = std::min<size_t>(available, max);
  100. this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len);
  101. this->buf_head_ += len;
  102. first_iteration = false;
  103. }
  104. }
  105. void StreamServerComponent::flush() {
  106. this->buf_tail_ = this->buf_head_;
  107. for (Client &client : this->clients_) {
  108. if (client.position == this->buf_head_)
  109. continue;
  110. // Split the write into two parts: from the current position to the end of the ring buffer, and from the start
  111. // of the ring buffer until the head. The second part might be zero if no wraparound is necessary.
  112. struct iovec iov[2];
  113. iov[0].iov_base = &this->buf_[this->buf_index(client.position)];
  114. iov[0].iov_len = std::min(this->buf_head_ - client.position, this->buf_ahead(client.position));
  115. iov[1].iov_base = &this->buf_[0];
  116. iov[1].iov_len = this->buf_head_ - (client.position + iov[0].iov_len);
  117. client.position += client.socket->writev(iov, 2);
  118. this->buf_tail_ = std::min(this->buf_tail_, client.position);
  119. }
  120. }
  121. void StreamServerComponent::write() {
  122. uint8_t buf[128];
  123. ssize_t len;
  124. for (Client &client : this->clients_) {
  125. while ((len = client.socket->read(&buf, sizeof(buf))) > 0)
  126. this->stream_->write_array(buf, len);
  127. if (len == 0) {
  128. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  129. client.disconnected = true;
  130. continue;
  131. }
  132. }
  133. }
  134. StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier, size_t position)
  135. : socket(std::move(socket)), identifier{identifier}, position{position} {}