Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Boost Asio的异步等待文件描述符_C++_Boost_Boost Asio_Dbus - Fatal编程技术网

C++ 使用Boost Asio的异步等待文件描述符

C++ 使用Boost Asio的异步等待文件描述符,c++,boost,boost-asio,dbus,C++,Boost,Boost Asio,Dbus,我正在尝试将D-Bus与我的boost::asio应用程序集成 D-Bus有一个API,它枚举一组要监视的Unix文件描述符(主要是套接字,但也可能是FIFO)。 当这些描述符有需要读取的内容时,我应该通知D-Bus API,以便它能够读取它们并执行它的操作 目前我正在这样做: using boost::asio::posix::stream_descriptor; void read_handle(stream_descriptor* desc, const boost::system::er

我正在尝试将D-Bus与我的
boost::asio
应用程序集成

D-Bus有一个API,它枚举一组要监视的Unix文件描述符(主要是套接字,但也可能是FIFO)。 当这些描述符有需要读取的内容时,我应该通知D-Bus API,以便它能够读取它们并执行它的操作

目前我正在这样做:

using boost::asio::posix::stream_descriptor;
void read_handle(stream_descriptor* desc, const boost::system::error_code& ec,
                 std::size_t bytes_read)
{
    if (!ec) {
        stream_descriptor::bytes_readable command(true);
        descriptor->io_control(command);
        std::size_t bytes_readable = command.get();
        std::cout << "It thinks I should read" << bytes_readable
            << " bytes" << std::endl;
    } else {
        std::cout << "There was an error" << std::endl;
    }
}

void watch_descriptor(boost::asio::io_service& ios, int file_descriptor)
{
    // Create the asio representation of the descriptor
    stream_descriptor* desc = new stream_descriptor(ios);
    desc->assign(file_descriptor);

    // Try to read 0 bytes just to be informed that there is something to be read
    std::vector<char> buffer(0);
    desc->async_read_some(boost::asio::buffer(buffer, 0),
        boost::bind(read_handle, desc, _1, _2));
}
使用boost::asio::posix::stream\u描述符;
无效读取句柄(流描述符*desc,常量boost::系统::错误代码&ec,
std::大小(字节数(读取)
{
如果(!ec){
流描述符::字节可读命令(true);
描述符->io_控制(命令);
std::size\u t bytes\u readable=command.get();

这正是问题的症结所在

有时,必须整合一个程序 第三方图书馆想要 执行I/O操作本身。 为了促进这一点,Boost.Asio 包括可以 可与读和写一起使用 空缓冲区操作 直到I/O对象被删除后才返回 “准备好”执行操作

例如,执行 非阻塞读取类似 可使用以下方法:

ip::tcp::socket套接字(我的io服务);
...
ip::tcp::socket::非阻塞nb(true);
插座io_控制(nb);
...
socket.async\u read\u some(null\u buffers(),read\u handler);
...
无效读取处理程序(boost::system::error\u code ec)
{
如果(!ec)
{
std::vector buf(socket.available());
socket.read_some(buffer(buf));
}
}

文档中还包含了一个示例。

这正是我想要的。它集成得非常完美。非常感谢!嗨,山姆,你能详细解释一下你提到的文档中包含的优秀示例吗?我想使用
第三方库会话
来读/异步读/写/异步写一些东西,我该怎么做?我还不太清楚。模拟第三方库的示例实际上让我很困惑。谢谢。@Peter请问一个新问题,包括您觉得困惑的部分。@Sam,我发布了另一个问题:请检查。谢谢。我用这种方式使用了libcurl的async\u read\u some,但在返回wi时遇到了一个问题t当服务器关闭套接字时出现错误,boost/asio套接字也仍然认为它是打开的。我使用async_receive和消息_peek标志以及大小为1的缓冲区来检测EOF。您使用的D-Bus API是什么?是低级C API吗?
ip::tcp::socket socket(my_io_service);
...
ip::tcp::socket::non_blocking nb(true);
socket.io_control(nb);
...
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));
  }
}