Boost Asio Peek and Completion Condition

笑着哭i 提交于 2019-12-12 01:48:06

问题


I am using Boost Asio to set up a socket connection. I would like to peek at the data in the buffer without consuming it, and I would like to use a completion condition to ensure that I could stop the blocking call if necessary.

I can get the peek functionality from basic_stream_socket::receive:

template<
    typename MutableBufferSequence>
std::size_t receive(
    const MutableBufferSequence & buffers,
    socket_base::message_flags flags,
    boost::system::error_code & ec);

One of the possible message_flags is basic_stream_socket::message_peek. However, this call blocks until at least one byte is read or an error occurs. I can get the completion condition functionality from read:

template<
    typename SyncReadStream,
    typename MutableBufferSequence,
    typename CompletionCondition>
std::size_t read(
    SyncReadStream & s,
    const MutableBufferSequence & buffers,
    CompletionCondition completion_condition,
    boost::system::error_code & ec);

I can provide a completion_condition method which checks if the call should be aborted before continuing.

My question is this: Is there a way to get a message_flags parameter and a completion_condition parameter in the same method call?


回答1:


I would like to peek at the data in the buffer without consuming it, and I would like to use a completion condition to ensure that I could stop the blocking call if necessary.

Don't do this. Use asynchronous methods such as async_read() and async_write(). To stop outstanding asynchronous operations, use cancel().



来源:https://stackoverflow.com/questions/7337248/boost-asio-peek-and-completion-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!