00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _DELAY_QUEUE_HH
00021 #define _DELAY_QUEUE_HH
00022
00023 #ifndef _NET_COMMON_H
00024 #include "NetCommon.h"
00025 #endif
00026
00027 #ifdef TIME_BASE
00028 typedef TIME_BASE time_base_seconds;
00029 #else
00030 typedef long time_base_seconds;
00031 #endif
00032
00034
00035 class Timeval {
00036 public:
00037 time_base_seconds seconds() const {
00038 return fTv.tv_sec;
00039 }
00040 time_base_seconds seconds() {
00041 return fTv.tv_sec;
00042 }
00043 time_base_seconds useconds() const {
00044 return fTv.tv_usec;
00045 }
00046 time_base_seconds useconds() {
00047 return fTv.tv_usec;
00048 }
00049
00050 int operator>=(Timeval const& arg2) const;
00051 int operator<=(Timeval const& arg2) const {
00052 return arg2 >= *this;
00053 }
00054 int operator<(Timeval const& arg2) const {
00055 return !(*this >= arg2);
00056 }
00057 int operator>(Timeval const& arg2) const {
00058 return arg2 < *this;
00059 }
00060 int operator==(Timeval const& arg2) const {
00061 return *this >= arg2 && arg2 >= *this;
00062 }
00063 int operator!=(Timeval const& arg2) const {
00064 return !(*this == arg2);
00065 }
00066
00067 void operator+=(class DelayInterval const& arg2);
00068 void operator-=(class DelayInterval const& arg2);
00069
00070
00071 protected:
00072 Timeval(time_base_seconds seconds, time_base_seconds useconds) {
00073 fTv.tv_sec = seconds; fTv.tv_usec = useconds;
00074 }
00075
00076 private:
00077 time_base_seconds& secs() {
00078 return (time_base_seconds&)fTv.tv_sec;
00079 }
00080 time_base_seconds& usecs() {
00081 return (time_base_seconds&)fTv.tv_usec;
00082 }
00083
00084 struct timeval fTv;
00085 };
00086
00087 #ifndef max
00088 inline Timeval max(Timeval const& arg1, Timeval const& arg2) {
00089 return arg1 >= arg2 ? arg1 : arg2;
00090 }
00091 #endif
00092 #ifndef min
00093 inline Timeval min(Timeval const& arg1, Timeval const& arg2) {
00094 return arg1 <= arg2 ? arg1 : arg2;
00095 }
00096 #endif
00097
00098 class DelayInterval operator-(Timeval const& arg1, Timeval const& arg2);
00099
00100
00101
00103
00104 class DelayInterval: public Timeval {
00105 public:
00106 DelayInterval(time_base_seconds seconds, time_base_seconds useconds)
00107 : Timeval(seconds, useconds) {}
00108 };
00109
00110 DelayInterval operator*(short arg1, DelayInterval const& arg2);
00111
00112 extern DelayInterval const DELAY_ZERO;
00113 extern DelayInterval const DELAY_SECOND;
00114 DelayInterval const DELAY_MINUTE = 60*DELAY_SECOND;
00115 DelayInterval const DELAY_HOUR = 60*DELAY_MINUTE;
00116 DelayInterval const DELAY_DAY = 24*DELAY_HOUR;
00117
00119
00120 class EventTime: public Timeval {
00121 public:
00122 EventTime(unsigned secondsSinceEpoch = 0,
00123 unsigned usecondsSinceEpoch = 0)
00124
00125 : Timeval(secondsSinceEpoch, usecondsSinceEpoch) {}
00126 };
00127
00128 EventTime TimeNow();
00129
00130 extern EventTime const THE_END_OF_TIME;
00131
00132
00134
00135 class DelayQueueEntry {
00136 public:
00137 virtual ~DelayQueueEntry();
00138
00139 intptr_t token() {
00140 return fToken;
00141 }
00142
00143 protected:
00144 DelayQueueEntry(DelayInterval delay);
00145
00146 virtual void handleTimeout();
00147
00148 private:
00149 friend class DelayQueue;
00150 DelayQueueEntry* fNext;
00151 DelayQueueEntry* fPrev;
00152 DelayInterval fDeltaTimeRemaining;
00153
00154 intptr_t fToken;
00155 static intptr_t tokenCounter;
00156 };
00157
00159
00160 class DelayQueue: public DelayQueueEntry {
00161 public:
00162 DelayQueue();
00163 virtual ~DelayQueue();
00164
00165 void addEntry(DelayQueueEntry* newEntry);
00166 void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);
00167 void updateEntry(intptr_t tokenToFind, DelayInterval newDelay);
00168 void removeEntry(DelayQueueEntry* entry);
00169 DelayQueueEntry* removeEntry(intptr_t tokenToFind);
00170
00171 DelayInterval const& timeToNextAlarm();
00172 void handleAlarm();
00173
00174 private:
00175 DelayQueueEntry* head() { return fNext; }
00176 DelayQueueEntry* findEntryByToken(intptr_t token);
00177 void synchronize();
00178
00179 EventTime fLastSyncTime;
00180 };
00181
00182 #endif