Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is poll() an edge triggered function?

I am responsible for a server that exports data over a TCP connection. With each data record that the server transmits, it requires the client to send a short "\n" acknowledgement message back. I have a customer who claims that the acknowledgement that he sends is not read from the web server. The following is code that I am using for I/O on the socket:

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);
      }
   }
}

I have written this code based upon the assumption that poll() is a level triggered function and will unblock immediately as long as there is data to be read from the socket. Everything that I have read seems to back up this assumption. Is there a reason that I may have missed that would cause the above code to miss a read event?

like image 557
Jon Trauntvein Avatar asked Feb 18 '23 10:02

Jon Trauntvein


1 Answers

It is not edge triggered. It is always level triggered. I will have to read your code to answer your actual question though. But that answers the question in the title. :-)

I can see no clear reason in your code why you might be seeing the behavior you are seeing. But the scope of your question is a lot larger than the code you're presenting, and I cannot pretend that this is a complete problem diagnosis.

like image 196
Omnifarious Avatar answered Feb 19 '23 22:02

Omnifarious