stream_server.h 940 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include <memory>
  4. #include <string>
  5. #include <vector>
  6. #include <Stream.h>
  7. #include <ESPAsyncTCP.h>
  8. class StreamServerComponent : public esphome::Component {
  9. public:
  10. explicit StreamServerComponent(Stream *stream) : stream_{stream} {}
  11. void setup() override;
  12. void loop() override;
  13. void dump_config() override;
  14. void on_shutdown() override;
  15. void set_port(uint16_t port) { this->port_ = port; }
  16. protected:
  17. void cleanup();
  18. void read();
  19. void write();
  20. struct Client {
  21. Client(AsyncClient *client, std::vector<uint8_t> &recv_buf);
  22. ~Client();
  23. AsyncClient *tcp_client{nullptr};
  24. std::string identifier{};
  25. bool disconnected{false};
  26. };
  27. Stream *stream_{nullptr};
  28. AsyncServer server_{0};
  29. uint16_t port_{6638};
  30. std::vector<uint8_t> recv_buf_{};
  31. std::vector<std::unique_ptr<Client>> clients_{};
  32. };