Crow  1.1
A C++ microframework for the web
 
Loading...
Searching...
No Matches
crow::websocket::Connection< Adaptor, Handler > Class Template Reference

A websocket connection. More...

#include <websocket.h>

Inheritance diagram for crow::websocket::Connection< Adaptor, Handler >:
crow::websocket::connection

Classes

struct  SendMessageType
 
struct  WeakWrappedMessage
 

Public Member Functions

template<typename CompletionHandler >
void dispatch (CompletionHandler &&handler)
 Send data through the socket.
 
template<typename CompletionHandler >
void post (CompletionHandler &&handler)
 Send data through the socket and return immediately.
 
void send_ping (std::string msg) override
 Send a "Ping" message.
 
void send_pong (std::string msg) override
 Send a "Pong" message.
 
void send_binary (std::string msg) override
 Send a binary encoded message.
 
void send_text (std::string msg) override
 Send a plaintext message.
 
void close (std::string const &msg, uint16_t status_code) override
 Send a close signal.
 
std::string get_remote_ip () override
 Returns the IP address the connection was opened from, also once the socket is closed (empty for a Unix domain socket).
 
uint16_t get_remote_port () override
 Returns the TCP port of the remote endpoint, 0 when unavailable (Unix domain socket or socket already closed).
 
void set_max_payload_size (uint64_t payload)
 
std::string get_subprotocol () const override
 Returns the matching client/server subprotocol, empty string if none matched.
 
- Public Member Functions inherited from crow::websocket::connection
void userdata (void *u)
 
void * userdata ()
 

Static Public Member Functions

static void create (const crow::request &req, Adaptor adaptor, Handler *handler, uint64_t max_payload, const std::vector< std::string > &subprotocols, std::function< void(crow::websocket::connection &)> open_handler, std::function< void(crow::websocket::connection &, const std::string &, bool)> message_handler, std::function< void(crow::websocket::connection &, const std::string &, uint16_t)> close_handler, std::function< void(crow::websocket::connection &, const std::string &)> error_handler, std::function< void(const crow::request &, std::optional< crow::response > &, void **)> accept_handler, bool mirror_protocols, const detail::socket::tcp_socket_options &tcp_options={})
 

Protected Member Functions

std::string build_header (int opcode, size_t size)
 Generate the websocket headers using an opcode and the message size (in bytes).
 
void start (std::string &&hello)
 Send the HTTP upgrade response.
 
void do_read ()
 Read a websocket message.
 
bool is_FIN ()
 Check if the FIN bit is set.
 
int opcode ()
 Extract the opcode from the header.
 
bool handle_fragment ()
 Process the payload fragment.
 
bool finish_message ()
 Pass the complete message to the message handler and get ready for the next one.
 
template<typename Callable >
bool call_handler (Callable &&handler)
 Call a user handler.
 
void do_write ()
 Send the buffers' data through the socket.
 
void check_destroy (websocket::CloseStatusCode code=CloseStatusCode::ClosedAbnormally)
 
void send_data_impl (SendMessageType *s)
 
void send_data (int opcode, std::string &&msg)
 

Static Protected Member Functions

static bool is_valid_close_code (uint16_t code)
 

Detailed Description

template<typename Adaptor, typename Handler>
class crow::websocket::Connection< Adaptor, Handler >

A websocket connection.

Member Function Documentation

◆ call_handler()

template<typename Adaptor , typename Handler >
template<typename Callable >
bool crow::websocket::Connection< Adaptor, Handler >::call_handler ( Callable &&  handler)
inlineprotected

Call a user handler.

An exception escaping the handler is reported to the error handler and the connection is closed with UnexpectedCondition.
Returns false in that case, nothing more should be read from the connection.

◆ close()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::close ( std::string const &  msg,
uint16_t  status_code 
)
inlineoverridevirtual

Send a close signal.

Sets a flag to destroy the object once the message is sent.

Implements crow::websocket::connection.

◆ create()

template<typename Adaptor , typename Handler >
static void crow::websocket::Connection< Adaptor, Handler >::create ( const crow::request &  req,
Adaptor  adaptor,
Handler *  handler,
uint64_t  max_payload,
const std::vector< std::string > &  subprotocols,
std::function< void(crow::websocket::connection &)>  open_handler,
std::function< void(crow::websocket::connection &, const std::string &, bool)>  message_handler,
std::function< void(crow::websocket::connection &, const std::string &, uint16_t)>  close_handler,
std::function< void(crow::websocket::connection &, const std::string &)>  error_handler,
std::function< void(const crow::request &, std::optional< crow::response > &, void **)>  accept_handler,
bool  mirror_protocols,
const detail::socket::tcp_socket_options &  tcp_options = {} 
)
inlinestatic

Factory for a connection.

Requires a request with an "Upgrade: websocket" header.
Automatically handles the handshake.

◆ do_read()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::do_read ( )
inlineprotected

Read a websocket message.

Involves:
Handling headers (opcodes, size).
Unmasking the payload.
Reading the actual payload.

◆ do_write()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::do_write ( )
inlineprotected

Send the buffers' data through the socket.

Also destroys the object if the Close flag is set.

◆ finish_message()

template<typename Adaptor , typename Handler >
bool crow::websocket::Connection< Adaptor, Handler >::finish_message ( )
inlineprotected

Pass the complete message to the message handler and get ready for the next one.

Returns false if the handler threw, in which case the connection is being closed.

◆ get_remote_ip()

template<typename Adaptor , typename Handler >
std::string crow::websocket::Connection< Adaptor, Handler >::get_remote_ip ( )
inlineoverridevirtual

Returns the IP address the connection was opened from, also once the socket is closed (empty for a Unix domain socket).

Implements crow::websocket::connection.

◆ get_remote_port()

template<typename Adaptor , typename Handler >
uint16_t crow::websocket::Connection< Adaptor, Handler >::get_remote_port ( )
inlineoverridevirtual

Returns the TCP port of the remote endpoint, 0 when unavailable (Unix domain socket or socket already closed).

Implements crow::websocket::connection.

◆ get_subprotocol()

template<typename Adaptor , typename Handler >
std::string crow::websocket::Connection< Adaptor, Handler >::get_subprotocol ( ) const
inlineoverridevirtual

Returns the matching client/server subprotocol, empty string if none matched.

Implements crow::websocket::connection.

◆ handle_fragment()

template<typename Adaptor , typename Handler >
bool crow::websocket::Connection< Adaptor, Handler >::handle_fragment ( )
inlineprotected

Process the payload fragment.

Unmasks the fragment, checks the opcode, merges fragments into 1 message body, and calls the appropriate handler.

◆ is_valid_close_code()

template<typename Adaptor , typename Handler >
static bool crow::websocket::Connection< Adaptor, Handler >::is_valid_close_code ( uint16_t  code)
inlinestaticprotected

Destroy the Connection. Whether a peer may send the status code in a Close frame (RFC 6455 Section 7.4).

◆ send_binary()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::send_binary ( std::string  msg)
inlineoverridevirtual

Send a binary encoded message.

Implements crow::websocket::connection.

◆ send_ping()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::send_ping ( std::string  msg)
inlineoverridevirtual

Send a "Ping" message.

Usually invoked to check if the other point is still online.

Implements crow::websocket::connection.

◆ send_pong()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::send_pong ( std::string  msg)
inlineoverridevirtual

Send a "Pong" message.

Usually automatically invoked as a response to a "Ping" message.

Implements crow::websocket::connection.

◆ send_text()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::send_text ( std::string  msg)
inlineoverridevirtual

Send a plaintext message.

Implements crow::websocket::connection.

◆ start()

template<typename Adaptor , typename Handler >
void crow::websocket::Connection< Adaptor, Handler >::start ( std::string &&  hello)
inlineprotected

Send the HTTP upgrade response.

Finishes the handshake process, then starts reading messages from the socket.


The documentation for this class was generated from the following files: