Boost Asio:检查套接字能力是否可读/可写

时间:2015-11-05 07:08:13

标签: c++ sockets boost boost-asio

在我的应用程序中,我必须混合使用asio创建的套接字和本地套接字(来自C posgresql库)。

我需要的是能够从特定套接字上的boost的io_service类实例获取通知,使其处于非阻塞可读/可写状态,但不执行实际的读/写(将由第三方图书馆),即有效只做选择()/ 民意调查()

可以通过将0作为缓冲区长度传递给 async_read_some ()这样的函数来实现吗?

我做了一个快速测试,确实调用 async_read_some ()并且缓冲区长度为零会调用读取事件处理程序,但我不确定它是否在中阻塞后完成选择()/ 轮询()通过相应的套接字句柄,等待真正的"可以读取"状态。

1 个答案:

答案 0 :(得分:4)

这通常被称为反应堆式操作。

这些可以通过向异步操作提供boost::asio::null_buffers来获得。 Reactor样式的操作提供了一种在执行读取或写入操作时获得通知的方式,并且可用于与第三方库集成,使用共享内存池等.Boost.Asio documentation提供了一些信息和以下示例代码:

ip::tcp::socket socket(my_io_service);
...
socket.non_blocking(true);
...
socket.async_read_some(null_buffers(), read_handler);
...
void read_handler(boost::system::error_code ec)
{
  if (!ec)
  {
    std::vector<char> buf(socket.available());
    socket.read_some(buffer(buf));
  }
}

Boost.Asio还提供了一个官方nonblocking example,说明了如何与想要直接在套接字上执行读写操作的库集成。

为操作提供零长度缓冲区通常会导致无操作,因为操作的完成条件将在不尝试执行任何I / O的情况下得到满足。以下是完整示例demonstrating两者之间的区别:

#include <array>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

// This example is not interested in the handlers, so provide a noop function
// that will be passed to bind to meet the handler concept requirements.
void noop() {}

void print_status(
  const boost::system::error_code& error,
  std::size_t bytes_transferred,
  boost::asio::ip::tcp::socket& socket)
{
  std::cout << "error: " << error.message() << "; "
               "transferred: " << bytes_transferred << "; "
               "available: " << socket.available() << std::endl;
}

int main()
{
  using boost::asio::ip::tcp;

  // Create all I/O objects.
  boost::asio::io_service io_service;
  tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
  tcp::socket socket1(io_service);
  tcp::socket socket2(io_service);

  // Connect the sockets.
  acceptor.async_accept(socket1, boost::bind(&noop));
  socket2.async_connect(acceptor.local_endpoint(), boost::bind(&noop));
  io_service.run();
  io_service.reset();

  std::array<char, 512> buffer;

  // Reading into a zero-length buffer is a no-op and will be
  // considered immediately completed.
  socket1.async_receive(boost::asio::buffer(buffer, 0), 
    boost::bind(&print_status,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred,
      boost::ref(socket1))
  );
  // Guarantee the handler runs.
  assert(1 == io_service.poll());
  io_service.reset();

  // Start a reactor-style read operation by providing a null_buffer.
  socket1.async_receive(boost::asio::null_buffers(),
    boost::bind(&print_status,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred,
      boost::ref(socket1))
  );
  // Guarantee that the handler did not run.
  assert(0 == io_service.poll());
  // Write to the socket so that data becomes available.
  boost::asio::write(socket2, boost::asio::buffer("hello"));  
  assert(1 == io_service.poll());
}

输出:

error: Success; transferred: 0; available: 0
error: Success; transferred: 0; available: 6