永远不会调用 Asio 异步处理程序,不会调用其他处理程序
Asio async handler is never called, no other handler invoked
我有 asio 同步连接。 ioService在一个线程中(我只有一个线程)。
小问题:
boost::asio::async_write(m_socket, boost::asio::buffer(requestStr.data(), requestStr.size()), handler);
从未调用处理程序,但服务器获取它并回复我收到的内容。
更大的问题:
boost::asio::async_read_until(m_socket, sbuf, '\n', sendingHandler);
它也不调用处理程序。 sbuf 立即被填充,我可以在那里读取它,但我不知道分隔符的位置。因此我需要处理程序来获取 bytes_transferred 参数。 (我不打算迭代缓冲区。)
我尝试了几件事,我可以调用一次处理程序,但我不记得在小折射后出现了什么问题。有什么帮助吗?谢谢!
当我使用同步消息时,一切都很好,但那里没有超时。
编辑:
如果您知道找到分隔符的任何好的解决方案,我不需要处理程序。
因为,我会发送消息 sync_write 并读取异步。
它不会被调用,因为它是async。用于写入和读取的异步方法从不从它们被调用的地方调用处理程序:
Regardless of whether the asynchronous operation completes immediately
or not, the handler will not be invoked from within this function.
Invocation of the handler will be performed in a manner equivalent to
using boost::asio::io_service::post().
您需要手动调用 io_service 方法,如 run
或 run_once
来执行操作,这就是您的回调将被调用的时刻。
我有 asio 同步连接。 ioService在一个线程中(我只有一个线程)。
小问题:
boost::asio::async_write(m_socket, boost::asio::buffer(requestStr.data(), requestStr.size()), handler);
从未调用处理程序,但服务器获取它并回复我收到的内容。
更大的问题:
boost::asio::async_read_until(m_socket, sbuf, '\n', sendingHandler);
它也不调用处理程序。 sbuf 立即被填充,我可以在那里读取它,但我不知道分隔符的位置。因此我需要处理程序来获取 bytes_transferred 参数。 (我不打算迭代缓冲区。)
我尝试了几件事,我可以调用一次处理程序,但我不记得在小折射后出现了什么问题。有什么帮助吗?谢谢!
当我使用同步消息时,一切都很好,但那里没有超时。
编辑: 如果您知道找到分隔符的任何好的解决方案,我不需要处理程序。 因为,我会发送消息 sync_write 并读取异步。
它不会被调用,因为它是async。用于写入和读取的异步方法从不从它们被调用的地方调用处理程序:
Regardless of whether the asynchronous operation completes immediately or not, the handler will not be invoked from within this function. Invocation of the handler will be performed in a manner equivalent to using boost::asio::io_service::post().
您需要手动调用 io_service 方法,如 run
或 run_once
来执行操作,这就是您的回调将被调用的时刻。