simple_server.h 787 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/stream/input.h>
  4. #include <util/stream/output.h>
  5. #include <util/thread/pool.h>
  6. #include <functional>
  7. class TInetStreamSocket;
  8. // Simple server listens on the specified port and launches
  9. // requestHandler in the separate thread for each incoming connection.
  10. class TSimpleServer
  11. {
  12. public:
  13. using TRequestHandler = std::function<void(IInputStream* input, IOutputStream* output)>;
  14. public:
  15. TSimpleServer(int port, TRequestHandler requestHandler);
  16. ~TSimpleServer();
  17. void Stop();
  18. int GetPort() const;
  19. TString GetAddress() const;
  20. private:
  21. const int Port_;
  22. THolder<IThreadPool> ThreadPool_;
  23. THolder<IThreadFactory::IThread> ListenerThread_;
  24. THolder<TInetStreamSocket> SendFinishSocket_;
  25. };