gpt4 book ai didi

sockets - 在 boost 套接字上设置选项 SO_SETFIB

转载 作者:行者123 更新时间:2023-12-03 11:53:12 25 4
gpt4 key购买 nike

我没有看到任何在 boost sockets 上设置 SO_SETFIB 的选项。有人有任何想法或指出我如何实现这一目标的正确方向吗?

最佳答案

如果 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)是 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);

关于sockets - 在 boost 套接字上设置选项 SO_SETFIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28674200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com