stream_server.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "stream_server.h"
  2. #include "esphome/core/helpers.h"
  3. #include "esphome/core/log.h"
  4. #include "esphome/core/util.h"
  5. #include "esphome/core/version.h"
  6. #include "esphome/components/network/util.h"
  7. #include "esphome/components/socket/socket.h"
  8. static const char *TAG = "stream_server";
  9. using namespace esphome;
  10. void StreamServerComponent::setup() {
  11. ESP_LOGCONFIG(TAG, "Setting up stream server...");
  12. // The make_unique() wrapper doesn't like arrays, so initialize the unique_ptr directly.
  13. this->buf_ = std::unique_ptr<uint8_t[]>{new uint8_t[this->buf_size_]};
  14. struct sockaddr_storage bind_addr;
  15. #if ESPHOME_VERSION_CODE >= VERSION_CODE(2023, 4, 0)
  16. socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), this->port_);
  17. #else
  18. socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), htons(this->port_));
  19. #endif
  20. this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET);
  21. this->socket_->setblocking(false);
  22. this->socket_->bind(reinterpret_cast<struct sockaddr *>(&bind_addr), bind_addrlen);
  23. this->socket_->listen(8);
  24. this->publish_sensor();
  25. }
  26. void StreamServerComponent::loop() {
  27. this->accept();
  28. this->read();
  29. this->flush();
  30. this->write();
  31. this->cleanup();
  32. }
  33. void StreamServerComponent::dump_config() {
  34. ESP_LOGCONFIG(TAG, "Stream Server:");
  35. #if ESPHOME_VERSION_CODE >= VERSION_CODE(2025, 11, 0)
  36. ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_use_address(), this->port_);
  37. #else
  38. ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_use_address().c_str(), this->port_);
  39. #endif
  40. #ifdef USE_BINARY_SENSOR
  41. LOG_BINARY_SENSOR(" ", "Connected:", this->connected_sensor_);
  42. #endif
  43. #ifdef USE_SENSOR
  44. LOG_SENSOR(" ", "Connection count:", this->connection_count_sensor_);
  45. #endif
  46. }
  47. void StreamServerComponent::on_shutdown() {
  48. for (const Client &client : this->clients_)
  49. client.socket->shutdown(SHUT_RDWR);
  50. }
  51. void StreamServerComponent::publish_sensor() {
  52. #ifdef USE_BINARY_SENSOR
  53. if (this->connected_sensor_)
  54. this->connected_sensor_->publish_state(this->clients_.size() > 0);
  55. #endif
  56. #ifdef USE_SENSOR
  57. if (this->connection_count_sensor_)
  58. this->connection_count_sensor_->publish_state(this->clients_.size());
  59. #endif
  60. }
  61. void StreamServerComponent::accept() {
  62. struct sockaddr_storage client_addr;
  63. socklen_t client_addrlen = sizeof(client_addr);
  64. std::unique_ptr<socket::Socket> socket = this->socket_->accept(reinterpret_cast<struct sockaddr *>(&client_addr), &client_addrlen);
  65. if (!socket)
  66. return;
  67. socket->setblocking(false);
  68. #if ESPHOME_VERSION_CODE >= VERSION_CODE(2026, 1, 0)
  69. std::string identifier = std::string{esphome::socket::SOCKADDR_STR_LEN, 0};
  70. auto identifier_span = std::span<char, esphome::socket::SOCKADDR_STR_LEN>(identifier.data(), identifier.size());
  71. identifier.resize(socket->getpeername_to(identifier_span));
  72. #else
  73. std::string identifier = socket->getpeername();
  74. #endif
  75. this->clients_.emplace_back(std::move(socket), identifier, this->buf_head_);
  76. ESP_LOGD(TAG, "New client connected from %s", identifier.c_str());
  77. this->publish_sensor();
  78. }
  79. void StreamServerComponent::cleanup() {
  80. auto discriminator = [](const Client &client) { return !client.disconnected; };
  81. auto last_client = std::partition(this->clients_.begin(), this->clients_.end(), discriminator);
  82. if (last_client != this->clients_.end()) {
  83. this->clients_.erase(last_client, this->clients_.end());
  84. this->publish_sensor();
  85. }
  86. }
  87. void StreamServerComponent::read() {
  88. size_t len = 0;
  89. int available;
  90. while ((available = this->stream_->available()) > 0) {
  91. size_t free = this->buf_size_ - (this->buf_head_ - this->buf_tail_);
  92. if (free == 0) {
  93. // Only overwrite if nothing has been added yet, otherwise give flush() a chance to empty the buffer first.
  94. if (len > 0)
  95. return;
  96. ESP_LOGE(TAG, "Incoming bytes available, but outgoing buffer is full: stream will be corrupted!");
  97. free = std::min<size_t>(available, this->buf_size_);
  98. this->buf_tail_ += free;
  99. for (Client &client : this->clients_) {
  100. if (client.position < this->buf_tail_) {
  101. ESP_LOGW(TAG, "Dropped %u pending bytes for client %s", this->buf_tail_ - client.position, client.identifier.c_str());
  102. client.position = this->buf_tail_;
  103. }
  104. }
  105. }
  106. // Fill all available contiguous space in the ring buffer.
  107. len = std::min<size_t>(available, std::min<size_t>(this->buf_ahead(this->buf_head_), free));
  108. this->stream_->read_array(&this->buf_[this->buf_index(this->buf_head_)], len);
  109. this->buf_head_ += len;
  110. }
  111. }
  112. void StreamServerComponent::flush() {
  113. ssize_t written;
  114. this->buf_tail_ = this->buf_head_;
  115. for (Client &client : this->clients_) {
  116. if (client.disconnected || client.position == this->buf_head_)
  117. continue;
  118. // Split the write into two parts: from the current position to the end of the ring buffer, and from the start
  119. // of the ring buffer until the head. The second part might be zero if no wraparound is necessary.
  120. struct iovec iov[2];
  121. iov[0].iov_base = &this->buf_[this->buf_index(client.position)];
  122. iov[0].iov_len = std::min(this->buf_head_ - client.position, this->buf_ahead(client.position));
  123. iov[1].iov_base = &this->buf_[0];
  124. iov[1].iov_len = this->buf_head_ - (client.position + iov[0].iov_len);
  125. if ((written = client.socket->writev(iov, 2)) > 0) {
  126. client.position += written;
  127. } else if (written == 0 || errno == ECONNRESET) {
  128. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  129. client.disconnected = true;
  130. continue; // don't consider this client when calculating the tail position
  131. } else if (errno == EWOULDBLOCK || errno == EAGAIN) {
  132. // Expected if the (TCP) transmit buffer is full, nothing to do.
  133. } else {
  134. ESP_LOGE(TAG, "Failed to write to client %s with error %d!", client.identifier.c_str(), errno);
  135. }
  136. this->buf_tail_ = std::min(this->buf_tail_, client.position);
  137. }
  138. }
  139. void StreamServerComponent::write() {
  140. uint8_t buf[128];
  141. ssize_t read;
  142. for (Client &client : this->clients_) {
  143. if (client.disconnected)
  144. continue;
  145. while ((read = client.socket->read(&buf, sizeof(buf))) > 0)
  146. this->stream_->write_array(buf, read);
  147. if (read == 0 || errno == ECONNRESET) {
  148. ESP_LOGD(TAG, "Client %s disconnected", client.identifier.c_str());
  149. client.disconnected = true;
  150. } else if (errno == EWOULDBLOCK || errno == EAGAIN) {
  151. // Expected if the (TCP) receive buffer is empty, nothing to do.
  152. } else {
  153. ESP_LOGW(TAG, "Failed to read from client %s with error %d!", client.identifier.c_str(), errno);
  154. }
  155. }
  156. }
  157. StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier, size_t position)
  158. : socket(std::move(socket)), identifier{identifier}, position{position} {}