gpt4 book ai didi

linux - Linux 上的套接字接收超时

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:29 26 4
gpt4 key购买 nike

我正在编写一个同步客户端。它的一部分是一个 Connection 对象,负责实际发送和接收数据。整个库是使用 Boost ASIO ip::tcp::socket 类编写的。

我有一个测试,其中客户端调用服务器上的一个方法(休眠 2 秒),超时时间为 1 秒。我的代码检测到执行花费的时间超过了请求的时间,但它没有及时返回。相反,它在 2 整秒后返回。

我已将问题范围缩小到 receive 方法:

void Connection::receive(const mutable_buffers_1& buffers, const DurationType& timeout)
{
// to make sure it isn't 0 by mistake
auto actualTimeout = std::max(DurationType(milliseconds(1)), timeout);
SocketReceiveTimeoutOption timeoutOption(actualTimeout);
error_code ec;
_socket.set_option(timeoutOption, ec);
RPC_LOG(TRACE) << "Setting timeout " << actualTimeout << " returned: " << ec.message();
RPC_LOG(TRACE) << "Receiving...";
if (_socket.receive(buffers, MSG_WAITALL, ec) != buffer_size(buffers))
{
throw RpcCommunicationError("Did not receive the expected number of bytes from connection");
}
RPC_LOG(TRACE) << "Received! With error code: " << ec.message();
}

DurationType 只是一个方便的类型定义:

typedef boost::chrono::system_clock ClockType;
typedef ClockType::time_point::duration DurationType;

SocketReceiveTimeoutOption 是为套接字实现的选项:

template <int Name>
class SocketTimeoutOption
{
public:
#ifdef BSII_WINDOWS
SocketTimeoutOption(const DurationType& timeout) : _value(static_cast<DWORD>(boost::chrono::duration_cast<boost::chrono::milliseconds>(timeout).count())) {}
#else
SocketTimeoutOption(const DurationType& timeout) : _value(Utils::toTimeval(timeout)) {}
#endif

// 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 Name;
}

// Get the address of the timeout data.
template <typename Protocol>
void* data(const Protocol&)
{
return &_value;
}

// Get the address of the timeout data.
template <typename Protocol>
const void* data(const Protocol&) const
{
return &_value;
}

// Get the size of the boolean data.
template <typename Protocol>
std::size_t size(const Protocol&) const
{
return sizeof(_value);
}

private:
#ifdef BSII_WINDOWS
DWORD _value;
#else
timeval _value;
#endif
};

typedef SocketTimeoutOption<SO_RCVTIMEO> SocketReceiveTimeoutOption;
typedef SocketTimeoutOption<SO_SNDTIMEO> SocketSendTimeoutOption;

最后

namespace Utils
{
inline
timeval toTimeval(const DurationType& duration)
{
timeval val;
auto seconds = boost::chrono::duration_cast<boost::chrono::seconds>(duration); // TODO: make sure this is truncated down in case there's fractional seconds
val.tv_sec = static_cast<long>(seconds.count());

auto micro = boost::chrono::duration_cast<boost::chrono::microseconds>(duration - seconds);
val.tv_usec = static_cast<long>(micro.count());

return val;
}
}

问题是,即使我指定了 1 秒超时,receive 方法仍然需要整整 2 秒。这是日志:

2014-09-14 10:27:53.348383 | trace | 0x007f24e50ae7c0 | Setting timeout 999917107 nanoseconds returned: Success
2014-09-14 10:27:53.348422 | trace | 0x007f24e50ae7c0 | Receiving...
2014-09-14 10:27:55.349152 | trace | 0x007f24e50ae7c0 | Received! With error code: Success

如您所见,设置超时有效,但 receive 方法仍然花费了 2 秒。

相同的代码在 Windows 上运行得很好。

最佳答案

socket::receive()将阻塞直到:

  • 已成功接收一个或多个字节的数据
  • 发生错误,导致无法接收数据

对于阻塞同步操作,如果底层操作系统操作返回一个非关键错误,例如指示该操作将阻塞或应重试的错误,则 Boost.Asio 将在 poll() 中阻塞。等待文件描述符准备好。对 poll() 的阻塞调用不受 SO_RCVTIMEO 套接字选项的影响。一旦文件描述符准备好,Boost.Asio将重新尝试该操作。

因此,原问题中的场景如下:

Time | Client                                 | Server
-----+----------------------------------------+-------------------------------
| socket.connect(...); | acceptor.accept(...);
0.00 | socket.set_option(timeout(second(1))); | sleep(seconds(2));
0.01 | socket.receive(...); |
0.02 | |-- recv(...); |
1.02 | | // timeout, errno = EAGAIN |
1.03 | |-- poll(socket); |
2.00 | | // data available, poll unblocks | socket.write(...);
2.01 | `-- recv(...);// success |

要获得所需的超时行为,可以:

  • 使用官方Boost.Asio timeout examples中提供的模式.
  • 直接调用操作系统。但是,请小心,因为其他操作可能会间接影响此方法。例如,如果在套接字上发起异步操作,则套接字将被设置为非阻塞。这将导致 recv() 函数立即在非阻塞套接字上返回,无论 SO_RCVTIMEO 套接字选项如何。

关于linux - Linux 上的套接字接收超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831136/

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