将 Handler 绑定到 boost::asio::strand 时的 boost::bind 占位符问题
void Connection::HandleRecvData(const boost::system::error_code & error)
{
boost::asio::async_read( m_socket, boost::asio::buffer( m_recv_buffer ),
m_io_strand.wrap( boost::bind( &Connection::HandleRecvData,
shared_from_this(), _1, _2 ) ) ); //works
boost::asio::async_read( m_socket, boost::asio::buffer( inbound_data_ ),
m_io_strand.wrap( boost::bind( &Connection::HandleRecv,
shared_from_this(), _1, _2 ) ) ); //doesn't work
}
处理程序
void Connection::HandleRecv( const boost::system::error_code & error, int32_t actual_bytes )
{
if( error || HasError() || m_hive->HasStopped() )
{
StartError( error );
}
else
{
m_recv_buffer.resize( actual_bytes );
OnRecv( m_recv_buffer );
m_pending_recvs.pop_front();
if( !m_pending_recvs.empty() )
{
StartRecv( m_pending_recvs.front() );
}
}
}
不同的缓冲区类型:
std::vector< uint8_t > m_recv_buffer; //and
std::vector< char > m_recv_buffer;
我没有收到编译器错误,但在运行时,当接收数据的事件链启动时,永远不会调用“&Connection::HandleRecv”。我假设这是因为在评估占位符并且函数从未被调用时函数签名不正确。
任何人都可以帮助解释占位符是如何评估/插入的。
我检查了绑定文档,我看到:
bind(&X::f, ref(x), _1)(i); // x.f(i)
bind(&X::f, &x, _1)(i); //(&x)->f(i)
bind(&X::f, x, _1)(i); // (internal copy of x).f(i)
bind(&X::f, p, _1)(i); // (internal copy of p)->f(i)
但是当我调用它时没有“(i)”。
boost::bind( &Connection::HandleRecvData, shared_from_this(), _1, _2 )
PS这是我的第一篇文章,就我提供的详细程度以及下次我可以更清楚地描述我的问题而言,任何提示都会有所帮助。先感谢您。
void Connection::HandleRecvData(const boost::system::error_code & error)
{
boost::asio::async_read( m_socket, boost::asio::buffer( m_recv_buffer ),
m_io_strand.wrap( boost::bind( &Connection::HandleRecvData,
shared_from_this(), _1, _2 ) ) ); //works
boost::asio::async_read( m_socket, boost::asio::buffer( inbound_data_ ),
m_io_strand.wrap( boost::bind( &Connection::HandleRecv,
shared_from_this(), _1, _2 ) ) ); //doesn't work
}
Handler
void Connection::HandleRecv( const boost::system::error_code & error, int32_t actual_bytes )
{
if( error || HasError() || m_hive->HasStopped() )
{
StartError( error );
}
else
{
m_recv_buffer.resize( actual_bytes );
OnRecv( m_recv_buffer );
m_pending_recvs.pop_front();
if( !m_pending_recvs.empty() )
{
StartRecv( m_pending_recvs.front() );
}
}
}
Different buffer types:
std::vector< uint8_t > m_recv_buffer; //and
std::vector< char > m_recv_buffer;
I am not getting a compiler error but at runtime when the chain of events to recieve the data is kicked off the "&Connection::HandleRecv" is never called. I am assuming that it is because the function signature is incorrect when the placeholders are evaluated and the function is never called.
Can anyone help explain how the placeholders are evaluated/sub'd in.
I have checked the bind documentation and I see:
bind(&X::f, ref(x), _1)(i); // x.f(i)
bind(&X::f, &x, _1)(i); //(&x)->f(i)
bind(&X::f, x, _1)(i); // (internal copy of x).f(i)
bind(&X::f, p, _1)(i); // (internal copy of p)->f(i)
but there is no "(i)" when I am calling it.
boost::bind( &Connection::HandleRecvData, shared_from_this(), _1, _2 )
PS this is my first post any tips would be helpful as far as the level of detail I've provided and where I can be more clear in describing my issue next time. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论