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

A websocket connection. More...

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

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. More...
 
template<typename CompletionHandler >
void post (CompletionHandler handler)
 Send data through the socket and return immediately. More...
 
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. More...
 
void send_text (const std::string &msg) override
 Send a plaintext message. More...
 
void close (const std::string &msg) override
 Send a close signal. More...
 
std::string get_remote_ip () override
 
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). More...
 
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. More...
 
int opcode ()
 Extract the opcode from the header. More...
 
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. More...
 

Private Attributes

Adaptor adaptor_
 
std::vector< std::string > sending_buffers_
 
std::vector< std::string > write_buffers_
 
boost::array< char, 4096 > buffer_
 
bool is_binary_
 
std::string message_
 
std::string fragment_
 
WebSocketReadState state_ {WebSocketReadState::MiniHeader}
 
uint16_t remaining_length16_ {0}
 
uint64_t remaining_length_ {0}
 
bool close_connection_ {false}
 
bool is_reading {false}
 
bool has_mask_ {false}
 
uint32_t mask_
 
uint16_t mini_header_
 
bool has_sent_close_ {false}
 
bool has_recv_close_ {false}
 
bool error_occured_ {false}
 
bool pong_received_ {false}
 
bool is_close_handler_called_ {false}
 
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_
 
void * userdata_
 

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.

Here is the call graph for this function:

Member Function Documentation

◆ dispatch()

template<typename Adaptor >
template<typename CompletionHandler >
void crow::websocket::Connection< Adaptor >::dispatch ( CompletionHandler  handler)
inline

Send data through the socket.

Here is the caller graph for this function:

◆ post()

template<typename Adaptor >
template<typename CompletionHandler >
void crow::websocket::Connection< Adaptor >::post ( CompletionHandler  handler)
inline

Send data through the socket and return immediately.

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

Here is the call graph for this function:

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

Here is the call graph for this function:

◆ send_binary()

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

Send a binary encoded message.

Implements crow::websocket::connection.

Here is the call graph for this function:

◆ send_text()

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

Send a plaintext message.

Implements crow::websocket::connection.

Here is the call graph for this function:

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

Here is the call graph for this function:

◆ get_remote_ip()

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

◆ build_header()

template<typename Adaptor >
std::string crow::websocket::Connection< Adaptor >::build_header ( int  opcode,
size_t  size 
)
inlineprotected

Generate the websocket headers using an opcode and the message size (in bytes).

Here is the call graph for this function:
Here is the caller graph for this function:

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

Here is the call graph for this function:
Here is the caller graph for this function:

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_FIN()

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::is_FIN ( )
inlineprotected

Check if the FIN bit is set.

◆ opcode()

template<typename Adaptor >
int crow::websocket::Connection< Adaptor >::opcode ( )
inlineprotected

Extract the opcode from the header.

Here is the caller graph for this function:

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

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

Here is the caller graph for this function:

◆ check_destroy()

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

Destroy the Connection.

Here is the caller graph for this function:

◆ userdata() [1/2]

void crow::websocket::connection::userdata ( void *  u)
inlineinherited

◆ userdata() [2/2]

void* crow::websocket::connection::userdata ( )
inlineinherited

Member Data Documentation

◆ adaptor_

template<typename Adaptor >
Adaptor crow::websocket::Connection< Adaptor >::adaptor_
private

◆ sending_buffers_

template<typename Adaptor >
std::vector<std::string> crow::websocket::Connection< Adaptor >::sending_buffers_
private

◆ write_buffers_

template<typename Adaptor >
std::vector<std::string> crow::websocket::Connection< Adaptor >::write_buffers_
private

◆ buffer_

template<typename Adaptor >
boost::array<char, 4096> crow::websocket::Connection< Adaptor >::buffer_
private

◆ is_binary_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::is_binary_
private

◆ message_

template<typename Adaptor >
std::string crow::websocket::Connection< Adaptor >::message_
private

◆ fragment_

template<typename Adaptor >
std::string crow::websocket::Connection< Adaptor >::fragment_
private

◆ state_

template<typename Adaptor >
WebSocketReadState crow::websocket::Connection< Adaptor >::state_ {WebSocketReadState::MiniHeader}
private

◆ remaining_length16_

template<typename Adaptor >
uint16_t crow::websocket::Connection< Adaptor >::remaining_length16_ {0}
private

◆ remaining_length_

template<typename Adaptor >
uint64_t crow::websocket::Connection< Adaptor >::remaining_length_ {0}
private

◆ close_connection_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::close_connection_ {false}
private

◆ is_reading

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::is_reading {false}
private

◆ has_mask_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::has_mask_ {false}
private

◆ mask_

template<typename Adaptor >
uint32_t crow::websocket::Connection< Adaptor >::mask_
private

◆ mini_header_

template<typename Adaptor >
uint16_t crow::websocket::Connection< Adaptor >::mini_header_
private

◆ has_sent_close_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::has_sent_close_ {false}
private

◆ has_recv_close_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::has_recv_close_ {false}
private

◆ error_occured_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::error_occured_ {false}
private

◆ pong_received_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::pong_received_ {false}
private

◆ is_close_handler_called_

template<typename Adaptor >
bool crow::websocket::Connection< Adaptor >::is_close_handler_called_ {false}
private

◆ open_handler_

template<typename Adaptor >
std::function<void(crow::websocket::connection&)> crow::websocket::Connection< Adaptor >::open_handler_
private

◆ message_handler_

template<typename Adaptor >
std::function<void(crow::websocket::connection&, const std::string&, bool)> crow::websocket::Connection< Adaptor >::message_handler_
private

◆ close_handler_

template<typename Adaptor >
std::function<void(crow::websocket::connection&, const std::string&)> crow::websocket::Connection< Adaptor >::close_handler_
private

◆ error_handler_

template<typename Adaptor >
std::function<void(crow::websocket::connection&)> crow::websocket::Connection< Adaptor >::error_handler_
private

◆ accept_handler_

template<typename Adaptor >
std::function<bool(const crow::request&)> crow::websocket::Connection< Adaptor >::accept_handler_
private

◆ userdata_

void* crow::websocket::connection::userdata_
privateinherited

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