Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like an inheritance based solution may be more appropriate for you. </p> <p>Here’s an example base class using <code>boost::signals2</code> to signal a received message:</p> <pre><code>class Connection { public: typedef boost::signals2::signal&lt;void (const std::vector&lt;char&gt;&amp;)&gt; PacketReceived; protected: PacketReceived packet_received_; size_t max_rx_packet_size_; std::vector&lt;char&gt; read_buffer_; std::deque&lt;std::vector&lt;char&gt; &gt; tx_queue_; void read_handler(boost::system::error_code const&amp; error, size_t bytes_transferred) { if (boost::asio::error::operation_aborted != error) { if (error) ; // TODO handle the error, it may be a disconnect. else { read_buffer_.resize(bytes_transferred); packet_received_(read_buffer_); enable_reception(max_rx_packet_size_); } } } void write_handler(boost::system::error_code const&amp; error, size_t bytes_transferred) { if (boost::asio::error::operation_aborted != error) { tx_queue_.pop_front(); if (error) ; // TODO handle the error, it may be a disconnect. else if (!tx_queue_.empty()) transmit(); } } virtual void receive() = 0; virtual void transmit() = 0; explicit Connection() : packet_received_(), max_rx_packet_size_(), read_buffer_(), tx_queue_() {} public: virtual void close() = 0; virtual ~Connection() {} void connectPacketReceived(const PacketReceived::slot_type&amp; slot) { packet_received_.connect(slot); } void enable_reception(size_t max_rx_packet_size) { max_rx_packet_size_ = max_rx_packet_size; receive(); } const std::vector&lt;char&gt;&amp; read_buffer() const { return read_buffer_; } #if defined(BOOST_ASIO_HAS_MOVE) void send(std::vector&lt;char&gt;&amp;&amp; packet ) #else void send(const std::vector&lt;char&gt;&amp; packet ) #endif { bool queue_empty(tx_queue_.empty()); tx_queue_.push_back(packet); if (queue_empty) transmit(); } }; </code></pre> <p>And here's the outline of a class to implement an SSL socket:</p> <pre><code>class SslConnection : public Connection, public boost::enable_shared_from_this&lt;SslConnection&gt; { boost::asio::ssl::stream&lt;boost::asio::ip::tcp::socket&gt; ssl_socket_; virtual void receive() { ssl_socket_.async_read_some(boost::asio::buffer(read_buffer_), boost::bind(&amp;SslConnection::read_handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } virtual void transmit() { boost::asio::async_write(ssl_socket_, boost::asio::buffer(tx_queue_.front()), boost::bind(&amp;SslConnection::write_handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } SslConnection(boost::asio::io_service&amp; io_service, boost::asio::ssl::context&amp; ssl_context) : Connection(), ssl_socket_(io_service, ssl_context) {} public: static boost::shared_ptr&lt;SslConnection&gt; create (boost::asio::io_service&amp; io_service, boost::asio::ssl::context&amp; ssl_context) { return boost::shared_ptr&lt;SslConnection&gt; (new SslConnection(io_service, ssl_context)); } virtual void close() { boost::system::error_code ignoredEc; ssl_socket_.lowest_layer().close(ignoredEc); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload