【问题标题】:Boost Asio message_flags提升 Asio message_flags
【发布时间】:2011-08-17 19:57:58
【问题描述】:

我最近开始使用 Boost Asio。我注意到receive method of a TCP socket 接受message_flags 作为参数。但是,我为 message_flags 找到的文档只说它是一个整数,而没有指定有效值。可以分配给 message_flags 的值是什么?它们的含义是什么?

【问题讨论】:

    标签: boost tcp boost-asio


    【解决方案1】:

    我搜索了一段时间,最后尝试查看 Boost 的源代码。我在socket_base.hpp找到了这个:

      /// Bitmask type for flags that can be passed to send and receive operations.
      typedef int message_flags;
    
      #if defined(GENERATING_DOCUMENTATION)
      /// Peek at incoming data without removing it from the input queue.
      static const int message_peek = implementation_defined;
    
      /// Process out-of-band data.
      static const int message_out_of_band = implementation_defined;
    
      /// Specify that the data should not be subject to routing.
      static const int message_do_not_route = implementation_defined;
    
      /// Specifies that the data marks the end of a record.
      static const int message_end_of_record = implementation_defined;
      #else
      BOOST_ASIO_STATIC_CONSTANT(int,
          message_peek = BOOST_ASIO_OS_DEF(MSG_PEEK));
      BOOST_ASIO_STATIC_CONSTANT(int,
          message_out_of_band = BOOST_ASIO_OS_DEF(MSG_OOB));
      BOOST_ASIO_STATIC_CONSTANT(int,
          message_do_not_route = BOOST_ASIO_OS_DEF(MSG_DONTROUTE));
      BOOST_ASIO_STATIC_CONSTANT(int,
          message_end_of_record = BOOST_ASIO_OS_DEF(MSG_EOR));
      #endif
    

    基于此,看起来message_peekmessage_out_of_bandmessage_do_not_route 是可能的值。我要试一试,看看能不能让它们发挥作用。

    【讨论】:

      【解决方案2】:

      这个参数被转发给系统调用。例如,在 Windows 中,它直接转发到 WSASend。应在 OS 文档中检查 param 的含义:WSASend for Windows 否则为 recvmsg

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,我的解决方案是使用不带 message_flags 参数的重载(http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/basic_datagram_socket/send_to/overload1.html)。

        缺点是如果你想要错误代码错误报告你不能使用它(重载使用异常,并且不接受 ec 参数)

        【讨论】:

        • 传递 0 作为标志怎么样?它不会给你想要的行为吗?
        • 我用的是 0,感觉还不错
        猜你喜欢
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 2016-05-02
        • 2010-12-16
        相关资源
        最近更新 更多