java settcpnodelay,正确的set_socket_init_handler语法或修改源以使用websocket ++打开TCP_NODELAY...

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值