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