|
Malloy
|
Represents a connection via the WebSocket protocol. More...
#include <connection.hpp>


Public Types | |
| enum class | state { handshaking , active , closing , closed , inactive } |
| using | handler_t = std::function< void(const malloy::http::request<> &, const std::shared_ptr< connection > &)> |
Public Member Functions | |
| virtual | ~connection () noexcept |
| std::shared_ptr< spdlog::logger > | logger () const noexcept |
| void | set_binary (const bool enabled) |
| bool | binary () |
| template<concepts::accept_handler Callback> requires (isClient) | |
| void | connect (const boost::asio::ip::tcp::resolver::results_type &target, const std::string &resource, Callback &&done) |
| Connect to a remote (websocket) endpoint. | |
| template<class Body , class Fields , std::invocable<> Callback> requires (!isClient) | |
| void | accept (const boost::beast::http::request< Body, Fields > &req, Callback &&done) |
| Accept an incoming connection. | |
| void | disconnect (boost::beast::websocket::close_reason why=boost::beast::websocket::normal) |
| Disconnect/stop/close the connection. | |
| void | force_disconnect (boost::beast::websocket::close_reason why=boost::beast::websocket::normal) |
| Same as disconnect, but bypasses all queues and runs immediately. | |
| void | read (concepts::dynamic_buffer auto &buff, concepts::async_read_handler auto &&done) |
| Read a complete message into a buffer. | |
| template<concepts::async_read_handler Callback> | |
| void | send (const concepts::const_buffer_sequence auto &payload, Callback &&done) |
| Send the contents of a buffer to the client. | |
Static Public Member Functions | |
| static std::shared_ptr< connection > | make (const std::shared_ptr< spdlog::logger > logger, stream &&ws, const std::string &agent_string) |
| Construct a new connection object. | |
Represents a connection via the WebSocket protocol.
| isClient | Whether it is the client end of a websocket connection |
Provides basic management of a websocket connection. Will close the connection on destruction. The interface is entirely asynchronous and uses callback functions
|
strong |
The connection state.
|
inlinevirtualnoexcept |
Destructor.
|
inline |
Accept an incoming connection.
make was passed a connected stream ready to be accepted (e.g. http stream that req was read from) done is only invoked on successful connection | req | The request to accept. boost::beast::websocket::is_upgrade(req) must be true |
| done | Callback invoked on successful accepting of the request. NOT invoked on failure. Must be invocable without any parameters (i.e. f()) |
|
inline |
|
inline |
Connect to a remote (websocket) endpoint.
| target | The list of resolved endpoints to connect to |
| resource | A suburl to make the connection on (e.g. /api/websocket) |
| done | Callback invoked on accepting the handshake or an error occurring |

|
inline |
Disconnect/stop/close the connection.
| why | Reason why the connection is being closed. |

|
inline |
Same as disconnect, but bypasses all queues and runs immediately.
|
inlinenoexcept |
Get the logger instance.
This allows different components to log to the connection specific logger.
|
inlinestatic |
Construct a new connection object.
| logger | Logger to use. Must not be nullptr |
| ws | Stream to use. May be unopened/connected but in that case connect must be called before this connection can be used |

|
inline |
Read a complete message into a buffer.
buff alive until done is called| buff | Buffer to put the message into. Must satisfy dynamic_buffer Core Concepts |
| done | Callback invoked on the message being read (successfully or otherwise). Must satisfy async_read_handler Core Concepts. Will NOT be invoked on the websocket being closed before the message could be fully or partially read |

|
inline |
Send the contents of a buffer to the client.
payload alive until done is invoked| payload | The payload to send. Must satisfy const_buffer_sequence Core Concepts |
| done | Callback invoked after the message is written (successfully or otherwise). Must satisfy async_read_handler Core Concepts |

|
inline |