| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <string> |
| 4 |
#include <LibLsp/lsp/asio.h> |
| 5 |
#include <ixwebsocket/IXWebSocketServer.h> |
| 6 |
#include "RemoteEndPoint.h" |
| 7 |
#include "stream.h" |
| 8 |
#include "threaded_queue.h" |
| 9 |
|
| 10 |
namespace lsp |
| 11 |
{ |
| 12 |
class Log; |
| 13 |
} |
| 14 |
|
| 15 |
namespace lsp |
| 16 |
{ |
| 17 |
|
| 18 |
class websocket_stream_wrapper : public istream, public ostream |
| 19 |
{ |
| 20 |
public: |
| 21 |
websocket_stream_wrapper(std::shared_ptr<ix::WebSocket> _w); |
| 22 |
|
| 23 |
std::shared_ptr<ix::WebSocket> ws_; |
| 24 |
std::atomic<bool> quit {}; |
| 25 |
std::shared_ptr<MultiQueueWaiter> request_waiter; |
| 26 |
ThreadedQueue<char> on_request; |
| 27 |
std::string error_message; |
| 28 |
bool fail() override; |
| 29 |
|
| 30 |
bool eof() override; |
| 31 |
|
| 32 |
bool good() override; |
| 33 |
|
| 34 |
websocket_stream_wrapper& read(char* str, std::streamsize count) override; |
| 35 |
|
| 36 |
int get() override; |
| 37 |
|
| 38 |
bool bad() override; |
| 39 |
|
| 40 |
websocket_stream_wrapper& write(std::string const& c) override; |
| 41 |
|
| 42 |
websocket_stream_wrapper& write(std::streamsize _s) override; |
| 43 |
|
| 44 |
websocket_stream_wrapper& flush() override; |
| 45 |
|
| 46 |
void clear() override; |
| 47 |
|
| 48 |
std::string what() override; |
| 49 |
}; |
| 50 |
|
| 51 |
/// The top-level class of the HTTP server. |
| 52 |
class WebSocketServer |
| 53 |
{ |
| 54 |
public: |
| 55 |
WebSocketServer(WebSocketServer const&) = delete; |
| 56 |
WebSocketServer& operator=(WebSocketServer const&) = delete; |
| 57 |
~WebSocketServer(); |
| 58 |
/// Construct the server to listen on the specified TCP address and port, and |
| 59 |
/// serve up files from the given directory. |
| 60 |
explicit WebSocketServer( |
| 61 |
std::string const& user_agent, std::string const& address, std::string const& port, |
| 62 |
std::shared_ptr<MessageJsonHandler> json_handler, std::shared_ptr<Endpoint> localEndPoint, lsp::Log&, |
| 63 |
uint32_t _max_workers = 2 |
| 64 |
); |
| 65 |
|
| 66 |
/// Run the server's io_context loop. |
| 67 |
void run(); |
| 68 |
void stop(); |
| 69 |
|
| 70 |
RemoteEndPoint point; |
| 71 |
|
| 72 |
private: |
| 73 |
struct Data; |
| 74 |
|
| 75 |
Data* d_ptr = nullptr; |
| 76 |
}; |
| 77 |
|
| 78 |
} // namespace lsp |