Question

I'm having trouble implementing the example code to turn on TCP_NODELAY for a websocket++ endpoint.

This is a client example, and this is a server example.

The testee examples compile and work as expected, and the debug examples frequently compile and work as expected.

I've tried to set on_socket_init handler both ways, but I get nearly the same error each time. One such set of errors is this:

In constructor ‘broadcast_server::broadcast_server()’:
error: no matching function for call to ‘websocketpp::client<websocketpp::config::asio_tls_client>::set_socket_init_handler(std::_Bind_helper<false, void (*)(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type)’
         m_endpoint.set_socket_init_handler(bind(&on_socket_init,::_1,::_2));
                                                                           ^
note: candidate is:
In file included from websocketpp/config/asio.hpp:33:0,
                 from websocketpp/transport/asio/security/tls.hpp:373:10: note: void websocketpp::transport::asio::tls_socket::endpoint::set_socket_init_handler(websocketpp::transport::asio::tls_socket::socket_init_handler)
     void set_socket_init_handler(socket_init_handler h) {
          ^
websocketpp/transport/asio/security/tls.hpp:373:10: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, void (*)(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind<void (*(std::_Placeholder<1>, std::_Placeholder<2>))(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&)>}’ to ‘websocketpp::transport::asio::tls_socket::socket_init_handler {aka std::function<void(std::weak_ptr<void>, boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >&)>}’
error: no matching function for call to ‘websocketpp::server<websocketpp::config::asio_tls>::set_socket_init_handler(std::_Bind_helper<false, void (*)(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type)’
         m_server.set_socket_init_handler(bind(&on_socket_init,::_1,::_2));
                                                                         ^
note: candidate is:
In file included from websocketpp/config/asio.hpp:33:0,
                 from websocketpp/transport/asio/security/tls.hpp:373:10: note: void websocketpp::transport::asio::tls_socket::endpoint::set_socket_init_handler(websocketpp::transport::asio::tls_socket::socket_init_handler)
     void set_socket_init_handler(socket_init_handler h) {
          ^
websocketpp/transport/asio/security/tls.hpp:373:10: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, void (*)(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind<void (*(std::_Placeholder<1>, std::_Placeholder<2>))(std::weak_ptr<void>, boost::asio::basic_stream_socket<boost::asio::ip::tcp>&)>}’ to ‘websocketpp::transport::asio::tls_socket::socket_init_handler {aka std::function<void(std::weak_ptr<void>, boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >&)>}’

I've seen another post where another developer claimed to have it running, and since I do not understand this syntax fully

m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));

I am unsure of how to begin to diagnose this problem.

How can the above errors be resolved?

NOTE I am implementing TLS.

Needed code

I'm mostly interested in incorporating the contents of this function:

void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket & s) {
    boost::asio::ip::tcp::no_delay option(true);
    s.set_option(option);
}

Is there a way to modify the source so that this setting is made by default?

Was it helpful?

Solution

The socket_init_handler is a low level hook designed to allow full access to the underlying socket after it is initialized but before it is used. The signature of the socket_init_handler depends on what transport policy in use. While very similar in most ways, asio/plain and asio/tls are actually distinct transport policies and do have some differences.

One of those differences is that the underlying socket typed used in the asio/tls policy is an ssl stream wrapping a socket rather than just the raw socket. As such, the signature of the socket_init_handler for the asio/tls transport is different than that of asio/plain. The signature for the handler on an endpoint using asio/tls transport is:

typedef lib::function<void(connection_hdl,boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&)> socket_init_handler;

This can be found in the documentation at http://doxygen.websocketpp.org/namespacewebsocketpp_1_1transport_1_1asio_1_1tls__socket.html

A corresponding socket init handler that sets TCP_NODELAY for might look like:

void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ssl::stream<boost::asio::ip::tcp::socket> & s) {
    boost::asio::ip::tcp::no_delay option(true);
    s.lowest_layer().set_option(option);
}

endpoint.set_socket_init_handler(&on_socket_init);

OTHER TIPS

socket_init_handler is a 2-argument function:

typedef lib::function<void(connection_hdl,boost::asio::ip::tcp::socket&)> socket_init_handler;

if on_socket_init is free function (not a method of a class type), then it should be like

m_endpoint.set_socket_init_handler( &on_socket_init );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top