I'm having trouble implementing the example code to turn on TCP_NODELAY for a websocket++ endpoint.
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<:config::asio_tls_client>::set_socket_init_handler(std::_Bind_helper, boost::asio::basic_stream_socket<: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, boost::asio::basic_stream_socket<:asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind, std::_Placeholder<2>))(std::weak_ptr, boost::asio::basic_stream_socket<:asio::ip::tcp>&)>}’ to ‘websocketpp::transport::asio::tls_socket::socket_init_handler {aka std::function, boost::asio::ssl::stream<:asio::basic_stream_socket> >&)>}’
error: no matching function for call to ‘websocketpp::server<:config::asio_tls>::set_socket_init_handler(std::_Bind_helper, boost::asio::basic_stream_socket<: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, boost::asio::basic_stream_socket<:asio::ip::tcp>&), const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind, std::_Placeholder<2>))(std::weak_ptr, boost::asio::basic_stream_socket<:asio::ip::tcp>&)>}’ to ‘websocketpp::transport::asio::tls_socket::socket_init_handler {aka std::function, boost::asio::ssl::stream<:asio::basic_stream_socket> >&)>}’
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?
解决方案
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&)> socket_init_handler;
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<: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);