C ++套接字从未准备好使用poll进行写入

时间:2013-07-25 16:15:02

标签: c++ linux sockets polling

我正在为Linux上的套接字编写C ++包装器。 我可以将读/写连接到http服务器,我的poll函数可以完美地用于读取,但由于某种原因它不能用于写入。我尝试过使用gdb,当fd.events为fd.revents

时,轮询集POLLOUT显示为0

民意调查代码:

/**
 *@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()只返回socket(int,int,int)sys/socket.h返回的fd

测试功能:

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 PollType {POLL_READ, POLL_WRITE, POLL_RW};

POLL_REPLY_*常数:

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;

我很抱歉示例代码无法直接编译我试图缩短, 由于我得到了正确的HTTP 200 OK回复,您可以假设正确处理连接和读/写。轮询写入

时测试失败

1 个答案:

答案 0 :(得分:5)

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

breakPOLLOUT两个案例中都缺少defaultPOLLOUT落到default,删除POLLOUT发生的证据。