Crow  0.3
A C++ microframework for the web
crow::websocket::Connection< Adaptor > Class Template Reference

A websocket connection. More...

#include <websocket.h>

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

Public Member Functions

 Connection (const crow::request &req, Adaptor &&adaptor, 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 &)> close_handler, std::function< void(crow::websocket::connection &)> error_handler, std::function< bool(const crow::request &)> 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 (const std::string &msg) override
 Send a "Ping" message. More...
 
void send_pong (const std::string &msg) override
 Send a "Pong" message. More...
 
void send_binary (const std::string &msg) override
 Send a binary encoded message.
 
void send_text (const std::string &msg) override
 Send a plaintext message.
 
void close (const std::string &msg) override
 Send a close signal. More...
 
- 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.
 
void handle_fragment ()
 Process the payload fragment. More...
 
void do_write ()
 Send the buffers' data through the socket. More...
 
void check_destroy ()
 Destroy the Connection.
 

Detailed Description

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

A websocket connection.

Constructor & Destructor Documentation

◆ Connection()

template<typename Adaptor >
crow::websocket::Connection< Adaptor >::Connection ( const crow::request req,
Adaptor &&  adaptor,
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 &)>  close_handler,
std::function< void(crow::websocket::connection &)>  error_handler,
std::function< bool(const crow::request &)>  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 >
void crow::websocket::Connection< Adaptor >::close ( const std::string &  msg)
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 >
void crow::websocket::Connection< Adaptor >::do_read ( )
inlineprotected

Read a websocket message.

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

◆ do_write()

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

Send the buffers' data through the socket.

Also destroyes the object if the Close flag is set.

◆ handle_fragment()

template<typename Adaptor >
void crow::websocket::Connection< Adaptor >::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 >
void crow::websocket::Connection< Adaptor >::send_ping ( const 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 >
void crow::websocket::Connection< Adaptor >::send_pong ( const 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 >
void crow::websocket::Connection< Adaptor >::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: