Crow  1.1
A C++ microframework for the web
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

 Connection (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< bool(const crow::request &, void **)> accept_handler)
 Constructor for a connection. More...
 
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. More...
 
void send_pong (std::string msg) override
 Send a "Pong" message. More...
 
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. More...
 
std::string get_remote_ip () override
 
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 ()
 

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. More...
 
void do_read ()
 Read a websocket message. More...
 
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. More...
 
void do_write ()
 Send the buffers' data through the socket. More...
 
void check_destroy (websocket::CloseStatusCode code=CloseStatusCode::ClosedAbnormally)
 Destroy the Connection.
 
void send_data_impl (SendMessageType *s)
 
void send_data (int opcode, std::string &&msg)
 

Detailed Description

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

A websocket connection.

Constructor & Destructor Documentation

◆ Connection()

template<typename Adaptor , typename Handler >
crow::websocket::Connection< Adaptor, Handler >::Connection ( 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< bool(const crow::request &, void **)>  accept_handler 
)
inline

Constructor for a connection.

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

Member Function Documentation

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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 file: