You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

4 months ago
  1. /*
  2. * IXHttpServer.h
  3. * Author: Benjamin Sergeant
  4. * Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
  5. */
  6. #pragma once
  7. #include "IXHttp.h"
  8. #include "IXSocketServer.h"
  9. #include "IXWebSocket.h"
  10. #include <functional>
  11. #include <memory>
  12. #include <mutex>
  13. #include <set>
  14. #include <string>
  15. #include <thread>
  16. #include <utility> // pair
  17. namespace ix
  18. {
  19. class HttpServer final : public SocketServer
  20. {
  21. public:
  22. using OnConnectionCallback =
  23. std::function<HttpResponsePtr(HttpRequestPtr, std::shared_ptr<ConnectionState>)>;
  24. HttpServer(int port = SocketServer::kDefaultPort,
  25. const std::string& host = SocketServer::kDefaultHost,
  26. int backlog = SocketServer::kDefaultTcpBacklog,
  27. size_t maxConnections = SocketServer::kDefaultMaxConnections,
  28. int addressFamily = SocketServer::kDefaultAddressFamily,
  29. int timeoutSecs = HttpServer::kDefaultTimeoutSecs);
  30. virtual ~HttpServer();
  31. virtual void stop() final;
  32. void setOnConnectionCallback(const OnConnectionCallback& callback);
  33. void makeRedirectServer(const std::string& redirectUrl);
  34. void makeDebugServer();
  35. int getTimeoutSecs();
  36. private:
  37. // Member variables
  38. OnConnectionCallback _onConnectionCallback;
  39. std::atomic<int> _connectedClientsCount;
  40. const static int kDefaultTimeoutSecs;
  41. int _timeoutSecs;
  42. // Methods
  43. virtual void handleConnection(std::unique_ptr<Socket>,
  44. std::shared_ptr<ConnectionState> connectionState) final;
  45. virtual size_t getConnectedClientsCount() final;
  46. void setDefaultConnectionCallback();
  47. };
  48. } // namespace ix