正确的set_socket_init_handler语法或修改源以使用websocket ++打开TCP_NODELAY [英] Proper set_socket_init_handler syntax or modify source to turn on TCP_NODELAY with websocket++

查看:2408
本文介绍了正确的set_socket_init_handler语法或修改源以使用websocket ++打开TCP_NODELAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现示例代码时无法打开websocket ++端点的TCP_NODELAY。

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

这是一个客户端示例,这是一个服务器示例

示例按预期编译和工作, debug 示例频繁编译并按预期工作。

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

我试图设置 on_socket_init 处理程序,但每次我得到几乎相同的错误。一个这样的错误是这样:

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> >&)>}’


$ b $ p

我看到另一篇文章因为我不完全理解这个语法

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.

如何解决上述错误?

注意我正在实施TLS。

我最感兴趣的是整合此函数的内容

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?

推荐答案

socket_init_handler是一个低级钩子,旨在允许在初始化之后但在使用之前完全访问底层套接字。 socket_init_handler的签名取决于使用的传输策略。虽然在大多数方面非常相似,asio / plain和asio / tls实际上是不同的传输策略,并且有一些区别。

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.

其中一个区别是底层套接字类型在asio / tls策略中使用的是一个包装套接字的ssl流,而不仅仅是原始套接字。因此,用于asio / tls传输的socket_init_handler的签名不同于asio / plain的签名。使用asio / tls传输的端点上的处理程序的签名是:

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;

这可以在 http://doxygen.websocketpp.org/namespacewebsocketpp_1_1transport_1_1asio_1_1tls__socket.html

相应的套接字初始处理常式设置TCP_NODELAY可能类似于:

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

这篇关于正确的set_socket_init_handler语法或修改源以使用websocket ++打开TCP_NODELAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆