Question

I am writing a C++ wrapper for sockets on Linux. I can connect read/write to a http server my poll function works perfectly for reading but for some reason it won't work with writing. I have tried using gdb and it appears poll sets fd.revents to 0 when fd.events is POLLOUT

poll code:

/**
 *@brief assigns request.reply based on fd.revents
 */
static void translate_request(mizaru::PollRequest &request, pollfd &fd)
{

    assert( (fd.revents & POLLHUP) == 0);
    assert( (fd.revents & POLLERR) == 0);
    assert( (fd.revents & POLLNVAL) == 0);


    switch(fd.revents)
  {
      case (POLLIN | POLLOUT) :
        request.reply = mizaru::POLL_REPLY_RW;
        break;
      case POLLIN :
        request.reply = mizaru::POLL_REPLY_READ;
        break;
      case POLLOUT :
        request.reply = mizaru::POLL_REPLY_WRITE;
      default :
        request.reply = 0;
  }

}

/**
 * @fills in fd.events based on request.request
 * and fd.fd based on request.sock
 */
static void prep_request(mizaru::PollRequest &request, pollfd &fd)
{

    fd.fd = request.sock.get_handle();
    switch(request.request)
  {

    case mizaru::PollType::POLL_READ :
      fd.events = POLLIN;
      break;
    case mizaru::PollType::POLL_WRITE :
      fd.events = POLLOUT;
      break;
    default :
      fd.events = POLLIN | POLLOUT;

  }


}

void mizaru::trans::poll(mizaru::PollRequest &request,const std::chrono::milliseconds& wait_time) noexcept
{

  pollfd fd;
  prep_request(request, fd);
  poll(&fd, 1, wait_time.count());
  translate_request(request, fd);
}

mizaru::PollRequest struct :

struct PollRequest
{
  PollRequest(PollType request, const SyncSocket &sock) : sock(sock), request(request), reply(POLL_REPLY_FAIL) {}
  const SyncSocket & sock;
  PollType request;
  uint8_t reply;
};

SyncSocket.get_handle() just returns the fd returned by socket(int,int,int) in sys/socket.h

test function :

bool test_poll()
{
    mizaru::IPv4 ip ( "54.225.138.124" );
    mizaru::SyncSocketTCP sock ( ip, 80 ,true);
    mizaru::PollRequest  request{mizaru::PollType::POLL_READ, sock};
    std::chrono::milliseconds time(1000);

    mizaru::poll(request, time);

    if(request.reply == mizaru::POLL_REPLY_READ)
    {
      std::cout << "fail test_poll first read" <<std::endl;
       return false;
    } 

    request.request = mizaru::PollType::POLL_WRITE;
    mizaru::poll(request, time);

    if(request.reply != mizaru::POLL_REPLY_WRITE)
    {
      std::cout << "fail test_poll first write" << std::endl;
      return false;
    }

 std::string toWrite ( "GET / http/1.1\nHost: httpbin.org\n\n" );
 mizaru::byte_buffer write_buff;

for ( char c : toWrite )
    {
      write_buff.push_back ( c );
    }

  unsigned int r_value = sock.write ( write_buff );

   if (r_value != toWrite.size()) 
   {
      std::cout << "fail test_poll r_value" << std::endl;
      return false;
   }



    request.request = mizaru::PollType::POLL_READ;

    mizaru::poll(request, time);

    if(request.reply != mizaru::POLL_REPLY_READ)
    {
       std::cout << "fail test_poll second read" << std::endl;
        return false;
    }

    request.request = mizaru::PollType::POLL_WRITE;

    mizaru::poll(request, time);

    if(request.reply != mizaru::POLL_REPLY_WRITE)
    {
      std::cout << "fail test_poll second write " << std::endl;
        return false;
    }

    return true;
}

PollType enum :

enum PollType {POLL_READ, POLL_WRITE, POLL_RW};

POLL_REPLY_* constants :

constexpr uint8_t POLL_REPLY_READ = 0x01;
constexpr uint8_t POLL_REPLY_WRITE = 0x02;
constexpr uint8_t POLL_REPLY_RW = POLL_REPLY_READ | POLL_REPLY_WRITE;
constexpr uint8_t POLL_REPLY_FAIL = 0;

I am sorry that the sample code is not directly compilable I was trying to make it short, Since I get a proper HTTP 200 OK reply you can assume connecting and read/write are handled properly. The test fails when polling for write

Was it helpful?

Solution

In translate_request():

switch(fd.revents)
{
  case (POLLIN | POLLOUT) :
    request.reply = mizaru::POLL_REPLY_RW;
  break;

  case POLLIN :
    request.reply = mizaru::POLL_REPLY_READ;
  break;

  case POLLOUT :
    request.reply = mizaru::POLL_REPLY_WRITE;
  default :
    request.reply = 0;
}

You are missing a break in both the POLLOUT and default cases. POLLOUT falls through to default, erasing the evidence that POLLOUT occurred.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top