【发布时间】:2015-02-23 12:48:29
【问题描述】:
我没有看到在boost sockets 上设置 SO_SETFIB 的任何选项。任何人有任何想法或指出正确的方向如何实现?
【问题讨论】:
标签: sockets boost boost-asio
我没有看到在boost sockets 上设置 SO_SETFIB 的任何选项。任何人有任何想法或指出正确的方向如何实现?
【问题讨论】:
标签: sockets boost boost-asio
如果 Boost.Asio 不支持套接字选项,则可以创建 GettableSocketOption 和/或 SettableSocketOption 类型的模型以满足需求。
socket::set_option() 接受模拟 SettableSocketOption 类型要求的对象。 SettableSocketOption 类型要求文档模型必须提供一些返回值的函数,这些函数返回值适合传递给 POSIX setsockopt():
class option
{
int level(Protocol) const; // The 'level' argument.
int name(Protocol) const; // The 'name' argument.
const int* data(Protocol) const // The 'option_value' argument.
std::size_t size(Protocol) const // The 'option_len' argument.
};
可以把socket.set_option(option) 想象成这样:
setsocketopt(socket.native_handle(), option.level(protocol),
option.name(protocol), option.data(protocol),
option.size(protocol));
传递给函数的协议是Protocol 类型要求的模型。
这是一个 set_fib 类,它是 SettableSocketOption 的模型:
class set_fib
{
public:
// Construct option with specific value.
explicit set_fib(int value)
: value_(value)
{}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol&) const { return SOL_SOCKET; }
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol&) const { return SO_SETFIB; }
// Get the address of the option value.
template <typename Protocol>
const int* data(const Protocol&) const { return &value_; }
// Get the size of the option.
template <typename Protocol>
std::size_t size(const Protocol&) const { return sizeof(value_); }
private:
int value_;
};
用法:
boost::asio::ip::tcp::socket socket(io_service);
// ...
set_fib option(42);
socket.set_option(option);
【讨论】:
您将使用boost::asio::detail::socket_option::integer 套接字选项帮助程序模板:
typedef boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_SETFIB> set_fib;
// ...
sock.set_option(set_fib(42));
【讨论】:
set_option 接口(@987654325@ 概念)没有记录;您必须查看头文件才能了解如何使用它。
getaddrinfo 的包装器,它不能在套接字上运行。
detail 命名空间中的类型。对于这种特殊情况,detail::socket_option::integer 为 GettableSocketOption 和 SettableSocketOption 建模,SO_SETFIB 是一个仅设置的套接字选项。