📄 eventloop.cpp
字号:
// Are we removing the max fd? else if (fd == (_maxFD - 1)) { // Yes. Then look for the next max fd. for (i = (fd -1); i >= 0; --i) { if (FD_ISSET(i, &_fdSet)) { _maxFD = i + 1; break; } } } return;} // end of Eventloop::removeFD(int)//---------------------------------------------------------------// startTimer(const char*, unsigned long, TimerListener&) (Public)// Start the named timer.//void Eventloop::startTimer(const char *name, long duration, TimerListener& listener){ timeval expiration; Eventloop::Timer *timer, **timerIt; // Get the current time and add the duration to it. gettime(expiration); expiration.tv_usec += duration * USECS_PER_MSEC; if (expiration.tv_usec >= USECS_PER_SEC) { expiration.tv_sec += (expiration.tv_usec / USECS_PER_SEC); expiration.tv_usec = (expiration.tv_usec % USECS_PER_SEC); } // Check if this timer already exists. If yes, then remove the // timer from the list and update its duration and expiration. timer = NULL; for (timerIt = &_timerTable; *timerIt != NULL; timerIt = &((*timerIt)->_next)) { if (strcmp((*timerIt)->getName(), name) == 0 && &((*timerIt)->getListener()) == &listener) { timer = *timerIt; *timerIt = timer->getNext(); timer->setNext(NULL); timer->setDuration(duration); timer->setExpiration(expiration); break; } } if (timer == NULL) { timer = new Timer(name, duration, expiration, listener); } // Find out where to put this timer in the list. for (timerIt = &_timerTable; *timerIt != NULL && **timerIt <= expiration; timerIt = &((*timerIt)->_next)) ; timer->setNext(*timerIt); *timerIt = timer; return;} // end of Eventloop::startTimer(const char*, long, TimerListener&)//---------------------------------------------------------------// stopTimer(const char*, const TimerListener&) (Public)// Stop the named timer.//void Eventloop::stopTimer(const char *name, const TimerListener& listener){ Eventloop::Timer **timerIt, *timer; for (timerIt = &_timerTable; *timerIt != NULL; timerIt = &((*timerIt)->_next)) { if (strcmp((*timerIt)->getName(), name) == 0 && &((*timerIt)->getListener()) == &listener) { timer = *timerIt; *timerIt = timer->getNext(); timer->setNext(NULL); delete timer; break; } } return;} // end of Eventloop::stopTimer(const char*, const TimerListener&)//---------------------------------------------------------------// doesTimerExist(const char*) const (Public)// Return true if the named timer exists.//bool Eventloop::doesTimerExist(const char *name) const{ Eventloop::Timer **timerIt; bool retval; for (timerIt = const_cast<Eventloop::Timer**>(&_timerTable), retval = false; *timerIt != NULL && retval == false; timerIt = &((*timerIt)->_next)) { if (strcmp((*timerIt)->getName(), name) == 0) { retval = true; } } return (retval);} // end of Eventloop::doesTimerExist(const char*) const//---------------------------------------------------------------// Timer(const char*, long, const timeval&, TimerListener&)// (Public)// Constructor.//Eventloop::Timer::Timer(const char *name, long duration, const timeval& expiration, TimerListener& listener): _name(NULL), _duration(duration), _listener(listener), _next(NULL){ _name = new char[strlen(name) + 1]; (void) strcpy(_name, name); (void) memcpy(&_expiration, &expiration, sizeof(_expiration));} // end of Eventloop::Timer::Timer(...)//---------------------------------------------------------------// ~Timer() (Public)// Destructor.//Eventloop::Timer::~Timer(){ if (_name != NULL) { delete[] _name; _name = NULL; } return;} // end of Eventloop::Timer::~Timer()//---------------------------------------------------------------// operator<(const timeval&) (Public)// Is the expiration time less than the given time?//int Eventloop::Timer::operator<(const timeval& time){ return(_expiration.tv_sec < time.tv_sec || (_expiration.tv_sec == time.tv_sec && _expiration.tv_usec < time.tv_usec));} // end of Eventloop::Timer::operator<(const timeval&)//---------------------------------------------------------------// operator<=(const timeval&) (Public)// Is the expiration time less than or equal to the given time?//int Eventloop::Timer::operator<=(const timeval& time){ return(_expiration.tv_sec < time.tv_sec || (_expiration.tv_sec == time.tv_sec && _expiration.tv_usec <= time.tv_usec));} // end of Eventloop::Timer::operator<=(const timeval&)//---------------------------------------------------------------// operator==(const timeval&) (Public)// Is the expiration time equal to the given time?//int Eventloop::Timer::operator==(const timeval& time){ return(_expiration.tv_sec == time.tv_sec && _expiration.tv_usec == time.tv_usec);} // end of Eventloop::Timer::operator==(const timeval&)//---------------------------------------------------------------// operator>=(const timeval&) (Public)// Is the expiration time greater than or equal to the given// time?//int Eventloop::Timer::operator>=(const timeval& time){ return(_expiration.tv_sec > time.tv_sec || (_expiration.tv_sec == time.tv_sec && _expiration.tv_usec >= time.tv_usec));} // end of Eventloop::Timer::operator>=(const timeval&)//---------------------------------------------------------------// operator>(const timeval&) (Public)// Is the expiration time greater than the given time?//int Eventloop::Timer::operator>(const timeval& time){ return(_expiration.tv_sec > time.tv_sec || (_expiration.tv_sec == time.tv_sec && _expiration.tv_usec > time.tv_usec));} // end of Eventloop::Timer::operator>(const timeval&)//---------------------------------------------------------------// getName() const (Public)// Return the timer's name.//const char* Eventloop::Timer::getName() const{ return(_name);} // end of Eventloop::Timer::getName() const//---------------------------------------------------------------// getDuration() const (Public)// Return the timer's duration in milliseconds.//long Eventloop::Timer::getDuration() const{ return(_duration);} // end of Eventloop::Timer::getDuration() const//---------------------------------------------------------------// setDuration(long) (Public)// Set the timer's duration.//void Eventloop::Timer::setDuration(long duration){ _duration = duration; return;} // end of Eventloop::Timer::setDuration(long)//---------------------------------------------------------------// getExpiration() const (Public)// Return the timer's expiration.//const timeval& Eventloop::Timer::getExpiration() const{ return(_expiration);} // end of Eventloop::Timer::getExpiration() const//---------------------------------------------------------------// setExpiration(const timeval&) (Public)// Set the timer's expiration.//void Eventloop::Timer::setExpiration(const timeval& expiration){ (void) memcpy(&_expiration, &expiration, sizeof(_expiration)); return;} // end of Eventloop::Timer::setExpiration(const timeval&)//---------------------------------------------------------------// getListener() const (Public)// Return timer's callback connection object.//TimerListener& Eventloop::Timer::getListener() const{ return(_listener);} // end of Eventloop::Timer::getListener() const//---------------------------------------------------------------// getNext() const (Public)// Return the next pointer.//Eventloop::Timer* Eventloop::Timer::getNext() const{ return(_next);} // end of Eventloop::Timer::getNext() const//---------------------------------------------------------------// setNext(Timer*) (Public)// Set the next pointer.//void Eventloop::Timer::setNext(Eventloop::Timer *timer){ _next = timer; return;} // end of Eventloop::Timer::setNext(Eventloop::Timer*)//---------------------------------------------------------------// gettime(timeval&) const (Private)// Get the current time.//void Eventloop::gettime(timeval& timeval) const{#if defined(WIN32) _timeb currTime; _ftime(&currTime); timeval.tv_sec = currTime.time; timeval.tv_usec = currTime.millitm * USECS_PER_MSEC;#else (void) gettimeofday(&timeval, NULL);#endif return;} // end of Eventloop::gettime(timeval&) const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -