【问题标题】:Is poll() an edge triggered function?poll() 是边缘触发函数吗?
【发布时间】:2013-02-25 16:56:50
【问题描述】:

我负责通过 TCP 连接导出数据的服务器。对于服务器传输的每条数据记录,它都要求客户端发回一个简短的“\n”确认消息。我有一个客户声称他发送的确认不是从 Web 服务器读取的。以下是我在套接字上用于 I/O 的代码:

bool can_send = true;
char tx_buff[1024];
char rx_buff[1024];
struct pollfd poll_descriptor;
int rcd;

poll_descriptor.fd = socket_handle;
poll_descriptor.events = POLLIN | POLLOUT;
poll_descriptor.revents = 0;
while(!should_quit && is_connected)
{
   // if we know that data can be written, we need to do this before we poll the OS for
   // events.  This will prevent the 100 msec latency that would otherwise occur
   fill_write_buffer(write_buffer);
   while(can_send && !should_quit && !write_buffer.empty())
   {
      uint4 tx_len = write_buffer.copy(tx_buff, sizeof(tx_buff));
      rcd = ::send(
         socket_handle,
         tx_buff,
         tx_len,
         0);
      if(rcd == -1 && errno != EINTR)
         throw SocketException("socket write failure");
      write_buffer.pop(rcd);
      if(rcd > 0)
         on_low_level_write(tx_buff, rcd);
      if(rcd < tx_len)
         can_send = false;
   }

   // we will use poll for up to 100 msec to determine whether the socket can be read or
   // written
   if(!can_send)
      poll_descriptor.events = POLLIN | POLLOUT;
   else
      poll_descriptor.events = POLLIN;
   poll(&poll_descriptor, 1, 100);

   // check to see if an error has occurred
   if((poll_descriptor.revents & POLLERR) != 0 ||
      (poll_descriptor.revents & POLLHUP) != 0 ||
      (poll_descriptor.revents & POLLNVAL) != 0)
      throw SocketException("socket hung up or socket error");

   // check to see if anything can be written
   if((poll_descriptor.revents & POLLOUT) != 0)
      can_send = true;

   // check to see if anything can be read
   if((poll_descriptor.revents & POLLIN) != 0)
   {
      ssize_t bytes_read;
      ssize_t total_bytes_read = 0;
      int bytes_remaining = 0;
      do
      {
         bytes_read = ::recv(
            socket_handle,
            rx_buff,
            sizeof(rx_buff),
            0);
         if(bytes_read > 0)
         {
            total_bytes_read += bytes_read;
            on_low_level_read(rx_buff,bytes_read);
         }
         else if(bytes_read == -1)
            throw SocketException("read failure");
         ioctl(
            socket_handle,
            FIONREAD,
            &bytes_remaining);
      }
      while(bytes_remaining != 0);

      // recv() will return 0 if the socket has been closed
      if(total_bytes_read > 0)
         read_event::cpost(this);
      else
      {
         is_connected = false;
         closed_event::cpost(this);
      }
   }
}

我基于 poll() 是一个级别触发函数的假设编写了这段代码,只要有数据要从套接字读取,就会立即解除阻塞。我读过的一切似乎都支持这个假设。是否有我可能错过的原因会导致上述代码错过读取事件?

【问题讨论】:

  • 我认为问题不在于您发布的代码。尽管我确实质疑您对 FIONREAD ioctl 的使用,但您可以通过检测小于缓冲区大小的读取来完成同样的事情。但是您将它与poll 结合使用的方式不是问题。
  • 100 毫秒是一个相当激进的超时。例如,Nagle 超时比这更长。我会在任何网络上使用至少几秒钟的超时时间。

标签: c++ linux networking


【解决方案1】:

它不是边沿触发的。它始终是水平触发的。不过,我将不得不阅读您的代码来回答您的实际问题。但这回答了标题中的问题。 :-)

我在您的代码中看不到为什么您可能会看到您所看到的行为的明确原因。但是您的问题范围比您提供的代码要大得多,我不能假装这是一个完整的问题诊断。

【讨论】:

    【解决方案2】:

    它是水平触发的。如果轮询时套接字接收缓冲区中有数据,则 POLLIN 触发,如果套接字发送缓冲区中有空间(几乎总是有空间),则 POLLOUT 触发。

    【讨论】:

      【解决方案3】:

      根据您自己对问题的评估(即,当您希望能够阅读确认时,您在poll 上被阻止),那么您最终将获得超时。

      如果客户的机器距离您的服务器超过 50 毫秒,那么您将始终在收到确认之前连接超时,因为您只等待 100 毫秒。这是因为数据至少需要 50 毫秒才能到达客户手中,而确认返回至少需要 50 毫秒。

      【讨论】:

      • 由于所有的读/写代码都在一个while循环中,我希望下一次迭代能够处理数据。
      • @JonTrauntvein:我已经更新了我为什么提供它的答案。如果您对问题可能实际存在的位置有更多详细信息,则应在问题中提出。
      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 2014-10-14
      • 2016-07-08
      • 2013-01-25
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多