stream_server.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  49. void StreamServerComponent::on_shutdown() {
  50. for (const Client &client : this->clients_)
  51. client.socket->shutdown(SHUT_RDWR);
  52. }
  53. void StreamServerComponent::publish_sensor() {
  54. #ifdef USE_BINARY_SENSOR
  55. if (this->connected_sensor_)
  56. this->connected_sensor_->publish_state(this->clients_.size() > 0);
  57. #endif
  58. }
  59. void StreamServerComponent::accept() {
  60. struct sockaddr_storage client_addr;
  61. socklen_t client_addrlen = sizeof(client_addr);
  62. std::unique_ptr<socket::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&client_addr), &client_addrlen);
  63. if (!socket)
  64. return;
  65. socket->setblocking(false);
  66. std::string identifier = socket->getpeername();
  67. this->clients_.emplace_back(std::move(socket), identifier, this->buf_head_);
  68. ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
  69. this->publish_sensor();
  70. }
  71. void StreamServerComponent::cleanup() {
  72. auto discriminator = [](const Client &client) { return !client.disconnected; };
  73. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  74. if (last_client != this->clients_.end()) {
  75. this->clients_.erase(last_client, this->clients_.end());
  76. this->publish_sensor();
  77. }
  78. }
  79. void StreamServerComponent::read() {
  80. bool first_iteration = true;
  81. int available;
  82. while ((available = this->stream_->available()) > 0) {
  83. // Write until the tail is encountered, or wraparound of the ring buffer if that happens before.
  84. size_t max = std::min(this->buf_ahead(this->buf_head_), this->buf_tail_ + this->buf_size_ - this->buf_head_);
  85. if (max == 0) {
  86. // Only warn on the first iteration, the finite buffer size is also used as a throttling mechanism to avoid
  87. // blocking here for too long when a large amount of data comes in.
  88. if (first_iteration)
  89. ESP_LOGW(TAG, "Incoming bytes available in stream, but outgoing buffer is full!");
  90. break;
  91. }
  92. size_t len = std::min<size_t>(available, max);
  93. this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len);
  94. this->buf_head_ += len;
  95. first_iteration = false;
  96. }
  97. }
  98. void StreamServerComponent::flush() {
  99. this->buf_tail_ = this->buf_head_;
  100. for (Client &client : this->clients_) {
  101. if (client.position == this->buf_head_)
  102. continue;
  103. // Split the write into two parts: from the current position to the end of the ring buffer, and from the start
  104. // of the ring buffer until the head. The second part might be zero if no wraparound is necessary.
  105. struct iovec iov[2];
  106. iov[0].iov_base = &this->buf_[this->buf_index(client.position)];
  107. iov[0].iov_len = std::min(this->buf_head_ - client.position, this->buf_ahead(client.position));
  108. iov[1].iov_base = &this->buf_[0];
  109. iov[1].iov_len = this->buf_head_ - (client.position + iov[0].iov_len);
  110. client.position += client.socket->writev(iov, 2);
  111. this->buf_tail_ = std::min(this->buf_tail_, client.position);
  112. }
  113. }
  114. void StreamServerComponent::write() {
  115. uint8_t buf[128];
  116. ssize_t len;
  117. for (Client &client : this->clients_) {
  118. while ((len = client.socket->read(&buf, sizeof(buf))) > 0)
  119. this->stream_->write_array(buf, len);
  120. if (len == 0) {
  121. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  122. client.disconnected = true;
  123. continue;
  124. }
  125. }
  126. }
  127. StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier, size_t position)
  128. : socket(std::move(socket)), identifier{identifier}, position{position} {}