void EPoller::UpdateChannel(Channel* pChannel) { AssertInLoopThread(); LOG_TRACE << "fd = " << pChannel->GetFd() << "events = " << pChannel->GetEvents(); const int nIndex = pChannel->GetIndex(); int nFd = pChannel->GetFd(); if (NEW == nIndex || DELETED == nIndex) { if (NEW == nIndex) { assert(m_mpChannel.find(nFd) == m_mpChannel.end()); m_mpChannel[nFd] = pChannel; } else { assert(m_mpChannel.find(nFd) != m_mpChannel.end()); assert(m_mpChannel[nFd] == pChannel); } pChannel->SetIndex(ADDED); update(EPOLL_CTL_ADD, pChannel); } else { assert(m_mpChannel.find(nFd) != m_mpChannel.end()); assert(m_mpChannel[nFd] == pChannel); assert(ADDED == nIndex); if (pChannel->IsNoneEvent()) { update(EPOLL_CTL_DEL, pChannel); pChannel->SetIndex(DELETED); } else { update(EPOLL_CTL_MOD, pChannel); } } }
void PollPoller::UpdateChannel(Channel* pChannel) { AssertInLoopThread(); std::cout << "fd = " << pChannel->GetFd() << " events = " << pChannel->GetEvents(); if (pChannel->GetIndex() < 0) { assert(m_Channels.find(pChannel->GetFd()) == m_Channels.end()); struct pollfd pfd; pfd.fd = pChannel->GetFd(); pfd.events = static_cast<short>(pChannel->GetEvents()); pfd.revents = 0; m_PollFds.push_back(std::move(pfd)); // mark! int idx = static_cast<int>(m_PollFds.size()) - 1; pChannel->SetIndex(idx); m_Channels[pfd.fd] = pChannel; } else { assert(m_Channels.find(pChannel->GetFd()) == m_Channels.end()); assert(m_Channels[pChannel->GetFd()] == pChannel); int idx = pChannel->GetIndex(); assert(idx >= 0 && idx < static_cast<int>(m_PollFds.size())); struct pollfd& pfd = m_PollFds[idx]; assert(pfd.fd == pChannel->GetFd() || pfd.fd == -1); pfd.events = static_cast<short>(pChannel->GetEvents()); pfd.revents = 0; if (pChannel->IsNoneEvent()) { pfd.fd = -1; } } }
void EPoller::RemoveChannel(Channel* pChannel) { AssertInLoopThread(); int nFd = pChannel->GetFd(); LOG_TRACE << "fd = " << nFd; assert(m_mpChannel.find(nFd) != m_mpChannel.end()); assert(m_mpChannel[nFd] == pChannel); assert(pChannel->IsNoneEvent()); int nIndex = pChannel->GetIndex(); assert(ADDED == nIndex || DELETED == nIndex); size_t n = m_mpChannel.erase(nFd); assert(1 == n); if (ADDED == nIndex) { update(EPOLL_CTL_DEL, pChannel); } pChannel->SetIndex(NEW); }
void EventLoop::Loop() { assert(!m_bLooping); AssertInLoopThread(); m_bLooping = true; m_bQuit = false; while (!m_bQuit) { m_ActiveChannels.clear(); m_PollReturnTime = m_pPoller->Poll(POLL_TIME_MS, &m_ActiveChannels); for (ChannelList::const_iterator itor = m_ActiveChannels.begin(), end = m_ActiveChannels.end() ; end != itor ; ++ itor) { (*itor)->HandleEvent(m_PollReturnTime); } doPendingFunctors(); } // LOG_TRACE << "EventLoop " << this << " stop looping"; m_bLooping = false; }
void EventLoop::RemoveChannel(Channel* pChannel) { assert(pChannel->OwnerLoop() == this); AssertInLoopThread(); m_pPoller->RemoveChannel(pChannel); }