1 | #ifndef ABSTRACTTHREADEDSERVER_H |
2 | #define ABSTRACTTHREADEDSERVER_H |
3 | |
4 | #include "abstractserverconnector.h" |
5 | #include "threadpool.h" |
6 | #include <memory> |
7 | #include <thread> |
8 | |
9 | namespace jsonrpc { |
10 | class AbstractThreadedServer : public AbstractServerConnector { |
11 | public: |
12 | AbstractThreadedServer(size_t threads); |
13 | virtual ~AbstractThreadedServer(); |
14 | |
15 | virtual bool StartListening(); |
16 | virtual bool StopListening(); |
17 | |
18 | protected: |
19 | /** |
20 | * @brief InitializeListener should initialize sockets, file descriptors etc. |
21 | * @return |
22 | */ |
23 | virtual bool InitializeListener() = 0; |
24 | |
25 | /** |
26 | * @brief CheckForConnection should poll for a new connection. This must be |
27 | * a non-blocking call. |
28 | * @return a handle which is passed on to HandleConnection() |
29 | */ |
30 | virtual int CheckForConnection() = 0; |
31 | |
32 | /** |
33 | * @brief HandleConnection must handle connection information for a given |
34 | * handle that has been returned by CheckForConnection() |
35 | * @param connection |
36 | */ |
37 | virtual void HandleConnection(int connection) = 0; |
38 | |
39 | private: |
40 | bool running; |
41 | std::unique_ptr<std::thread> listenerThread; |
42 | ThreadPool threadPool; |
43 | size_t threads; |
44 | |
45 | void ListenLoop(); |
46 | }; |
47 | } // namespace jsonrpc |
48 | |
49 | #endif // ABSTRACTTHREADEDSERVER_H |
50 | |