<legend id='jg1qj'><style id='jg1qj'><dir id='jg1qj'><q id='jg1qj'></q></dir></style></legend>
    • <bdo id='jg1qj'></bdo><ul id='jg1qj'></ul>
    1. <i id='jg1qj'><tr id='jg1qj'><dt id='jg1qj'><q id='jg1qj'><span id='jg1qj'><b id='jg1qj'><form id='jg1qj'><ins id='jg1qj'></ins><ul id='jg1qj'></ul><sub id='jg1qj'></sub></form><legend id='jg1qj'></legend><bdo id='jg1qj'><pre id='jg1qj'><center id='jg1qj'></center></pre></bdo></b><th id='jg1qj'></th></span></q></dt></tr></i><div id='jg1qj'><tfoot id='jg1qj'></tfoot><dl id='jg1qj'><fieldset id='jg1qj'></fieldset></dl></div>
    2. <small id='jg1qj'></small><noframes id='jg1qj'>

      <tfoot id='jg1qj'></tfoot>

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

        Async wait on file descriptor using Boost Asio(使用 Boost Asio 异步等待文件描述符)

          <tbody id='szTSx'></tbody>

            <bdo id='szTSx'></bdo><ul id='szTSx'></ul>
              <i id='szTSx'><tr id='szTSx'><dt id='szTSx'><q id='szTSx'><span id='szTSx'><b id='szTSx'><form id='szTSx'><ins id='szTSx'></ins><ul id='szTSx'></ul><sub id='szTSx'></sub></form><legend id='szTSx'></legend><bdo id='szTSx'><pre id='szTSx'><center id='szTSx'></center></pre></bdo></b><th id='szTSx'></th></span></q></dt></tr></i><div id='szTSx'><tfoot id='szTSx'></tfoot><dl id='szTSx'><fieldset id='szTSx'></fieldset></dl></div>
            1. <small id='szTSx'></small><noframes id='szTSx'>

              <tfoot id='szTSx'></tfoot>

              <legend id='szTSx'><style id='szTSx'><dir id='szTSx'><q id='szTSx'></q></dir></style></legend>

                • 本文介绍了使用 Boost Asio 异步等待文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I'm trying to integrate D-Bus with my boost::asio application.

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

                  D-Bus has an API that enumerates a set of Unix file descriptors (mainly sockets but could also be FIFOs) to be watched. When those descriptors have something to be read I should inform the D-Bus API so it can read them and do it's thing.

                  目前我正在这样做:

                  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));
                  }
                  

                  但是立即调用处理程序说它有 0 个字节要读取.我希望只有在有东西要读取时才调用它,但是 boost::asio 不能 读取它.它应该像一个美化的 select().有没有简单的方法可以做到这一点?

                  But the handler is called right away saying that it has 0 bytes to be read. I would like it to be called only when there is something to be read, but boost::asio CAN NOT read it. It should act just as a glorified select(). Is there a simple way to do that?

                  PS:我在我的软件中广泛使用了 boost::asio,这只是其中的一小部分,所以我不想依赖 glib或其他主循环.

                  PS: I'm extensively using boost::asio in my software, this is just a small part of it, so I would like not to depend on glib or other mainloops.

                  推荐答案

                  这正是问题所在 null_buffers 是设计的 为.

                  This is precisely the problem null_buffers was designed for.

                  有时必须集成一个程序与想要的第三方库执行 I/O 操作本身.为了促进这一点,Boost.Asio包括一个 null_buffers 类型,可以与读和写一起使用操作.null_buffers 操作直到 I/O 对象出现时才返回准备好"执行操作.

                  Sometimes a program must be integrated with a third-party library that wants to perform the I/O operations itself. To facilitate this, Boost.Asio includes a null_buffers type that can be used with both read and write operations. A null_buffers operation doesn't return until the I/O object is "ready" to perform the operation.

                  例如,执行一个非阻塞读取类似可以使用以下内容:

                  As an example, to perform a non-blocking read something like the following may be used:

                  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));
                    }
                  }
                  

                  还有一个优秀示例文档.

                  这篇关于使用 Boost Asio 异步等待文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                    1. <legend id='AHwRP'><style id='AHwRP'><dir id='AHwRP'><q id='AHwRP'></q></dir></style></legend>
                      • <small id='AHwRP'></small><noframes id='AHwRP'>

                        <i id='AHwRP'><tr id='AHwRP'><dt id='AHwRP'><q id='AHwRP'><span id='AHwRP'><b id='AHwRP'><form id='AHwRP'><ins id='AHwRP'></ins><ul id='AHwRP'></ul><sub id='AHwRP'></sub></form><legend id='AHwRP'></legend><bdo id='AHwRP'><pre id='AHwRP'><center id='AHwRP'></center></pre></bdo></b><th id='AHwRP'></th></span></q></dt></tr></i><div id='AHwRP'><tfoot id='AHwRP'></tfoot><dl id='AHwRP'><fieldset id='AHwRP'></fieldset></dl></div>

                            <bdo id='AHwRP'></bdo><ul id='AHwRP'></ul>

                          • <tfoot id='AHwRP'></tfoot>
                              <tbody id='AHwRP'></tbody>