| 587 | } |
| 588 | |
| 589 | int loop() { |
| 590 | while (m_fd > 0 || m_eventTimerList.size()) { |
| 591 | const int64_t start = getMillisecond(); |
| 592 | |
| 593 | const int checkCount = checkSendQueue(); |
| 594 | const int64_t timeout = m_eventTimerList.size() ? (*m_eventTimerList.begin())->timeout - start : 0; |
| 595 | |
| 596 | pdbg("startLoop ,timeout:%d timer:%d sender:%d checkcnt:[sq:%d %d run:%d] ", |
| 597 | TOINT(timeout), (int)m_eventTimerList.size(), (int)m_mapSender.size(), (int)sendQueue.size(), checkCount, TOINT(getMillisecond() - start)); |
| 598 | |
| 599 | if (m_eventTimerList.size() && timeout <= 0) { //timeout |
| 600 | const std::list<TimeoutEvent *>::iterator it = m_eventTimerList.begin(); |
| 601 | int ret = processTimeout(*it); |
| 602 | pdbg("processTimeout: timeout:%d ret:%d run:%d %s", TOINT(timeout), ret, TOINT(getMillisecond() - start), (*it)->toString().c_str()); |
| 603 | m_eventTimerList.erase(it); |
| 604 | if (ret < 0) { |
| 605 | return -1; |
| 606 | } |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | fd_set fdset; |
| 611 | FD_ZERO(&fdset); |
| 612 | FD_SET(m_fd, &fdset); |
| 613 | FD_SET(m_pipefd[0], &fdset); |
| 614 | |
| 615 | struct timeval diff, *pdiff = NULL; |
| 616 | if (m_eventTimerList.size() && timeout > 0) { |
| 617 | diff.tv_sec = timeout / 1000; |
| 618 | diff.tv_usec = (timeout % 1000) * 1000; |
| 619 | pdiff = &diff; |
| 620 | } |
| 621 | const int selectRet = select(std::max(m_fd, m_pipefd[0]) + 1, &fdset, NULL, NULL, pdiff); |
| 622 | pdbg("SelectRet:%d err:%d time:%d realTime:%d diff[%d %d]", selectRet, errno, TOINT(timeout), TOINT(getMillisecond() - start), |
| 623 | TOINT(diff.tv_sec), TOINT(diff.tv_usec)); |
| 624 | |
| 625 | if (selectRet == -1 && errno != EINTR) { |
| 626 | pdbg("eventloop: select errno:%d", errno); |
| 627 | continue; |
| 628 | } |
| 629 | |
| 630 | if (FD_ISSET(m_fd, &fdset)) { |
| 631 | int ret = processReceive(); |
| 632 | pdbg("eventloop: processReceive ret:%d run:%d", ret, TOINT(getMillisecond() - start)); |
| 633 | if (ret < 0) { |
| 634 | return -1; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (FD_ISSET(m_pipefd[0], &fdset)) { |
| 639 | int i = 0; |
| 640 | int ret = read(m_pipefd[0], &i, sizeof(int)); |
| 641 | pdbg("read pipe ret:%d run:%d ", ret, TOINT(getMillisecond() - start)); |
| 642 | } |
| 643 | } |
| 644 | pdbg("end loop , fd:%d timer:%d", m_fd, (int)m_eventTimerList.size()); |
| 645 | return 0; |
| 646 | } |
nothing calls this directly
no test coverage detected