|
29 | 29 | namespace libbitcoin { |
30 | 30 | namespace network { |
31 | 31 |
|
32 | | -/// Websocket tcp/ip channel, uses channel_http for upgrade/multiplex. |
| 32 | +/// Abstract base websocket tcp/ip channel, on base http channel. |
33 | 33 | class BCT_API channel_ws |
34 | 34 | : public channel_http, protected tracker<channel_ws> |
35 | 35 | { |
36 | 36 | public: |
37 | 37 | typedef std::shared_ptr<channel_ws> ptr; |
38 | 38 | using options_t = settings_t::websocket_server; |
39 | 39 |
|
40 | | - /// Subscribe to messages post-upgrade (requires strand). |
41 | | - /// Event handler is always invoked on the channel strand. |
42 | | - template <class Message> |
43 | | - inline void subscribe(auto&& ) NOEXCEPT |
44 | | - { |
45 | | - BC_ASSERT(stranded()); |
46 | | - ////using message_handler = distributor_ws::handler<Message>; |
47 | | - ////ws_distributor_.subscribe(std::forward<message_handler>(handler)); |
48 | | - } |
49 | | - |
50 | | - /// Serialize and write websocket message to peer (requires strand). |
51 | | - /// Completion handler is always invoked on the channel strand. |
52 | | - inline void send(system::data_chunk&& message, bool binary, |
53 | | - result_handler&& handler) NOEXCEPT |
54 | | - { |
55 | | - BC_ASSERT(stranded()); |
56 | | - BC_ASSERT(upgraded_); |
57 | | - using namespace std::placeholders; |
58 | | - |
59 | | - // TODO: Serialize message. |
60 | | - const auto ptr = system::move_shared(std::move(message)); |
61 | | - count_handler complete = std::bind(&channel_ws::handle_send, |
62 | | - shared_from_base<channel_ws>(), _1, _2, ptr, |
63 | | - std::move(handler)); |
64 | | - |
65 | | - if (!ptr) |
66 | | - { |
67 | | - complete(error::bad_alloc, {}); |
68 | | - return; |
69 | | - } |
70 | | - |
71 | | - // TODO: serialize message to send. |
72 | | - // TODO: websocket is full duplex, so writes must be queued. |
73 | | - ws_write(asio::const_buffer{ ptr->data(), ptr->size() }, |
74 | | - binary, std::move(complete)); |
75 | | - } |
76 | | - |
| 40 | +protected: |
77 | 41 | inline channel_ws(const logger& log, const socket::ptr& socket, |
78 | 42 | uint64_t identifier, const settings_t& settings, |
79 | 43 | const options_t& options) NOEXCEPT |
80 | 44 | : channel_http(log, socket, identifier, settings, options), |
81 | | - ////distributor_(socket->strand()), |
82 | 45 | tracker<channel_ws>(log) |
83 | 46 | { |
84 | 47 | } |
85 | 48 |
|
86 | | - /// Half-duplex http until upgraded to full-duplex websockets. |
| 49 | + /// Reads are never buffered, restart the reader. |
87 | 50 | void read_request() NOEXCEPT override; |
88 | 51 |
|
89 | | -protected: |
| 52 | + /// Pre-upgrade http read. |
90 | 53 | void handle_read_request(const code& ec, size_t bytes, |
91 | 54 | const http::request_cptr& request) NOEXCEPT override; |
| 55 | + |
| 56 | + /// Post-upgrade websocket read. |
92 | 57 | virtual void handle_read_websocket(const code& ec, size_t bytes) NOEXCEPT; |
93 | 58 |
|
94 | | -private: |
95 | | - inline void handle_send(const code& ec, size_t, const system::chunk_ptr&, |
96 | | - const result_handler& handler) NOEXCEPT |
97 | | - { |
98 | | - if (ec) stop(ec); |
99 | | - handler(ec); |
100 | | - } |
| 59 | + /// Dispatch websocket buffer via derived handlers (override to handle). |
| 60 | + /// Override to handle dispatch, must invoke read_request() on complete. |
| 61 | + virtual void dispatch_websocket(const http::flat_buffer& buffer, |
| 62 | + size_t bytes) NOEXCEPT; |
101 | 63 |
|
102 | | - // These are protected by strand. |
103 | | - ////distributor_rest distributor_; |
| 64 | +private: |
| 65 | + // This is protected by strand. |
104 | 66 | bool upgraded_{ false }; |
105 | 67 | }; |
106 | 68 |
|
|
0 commit comments