1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
| #ifndef TIME_WHEEL_TIMER #define TIME_WHEEL_TIMER
#include <time.h> #include <netinet/in.h> #include <stdio.h> #include <sys/timerfd.h> #include <unistd.h> #include <fcntl.h> #include <cstring> #include <cassert> #include <sys/epoll.h>
#define BUFFER_SIZE 64
class tw_timer;
struct client_data { sockaddr_in address; int sockfd; char buf[BUFFER_SIZE]; tw_timer *timer; };
class tw_timer { public: tw_timer(int rot, int ts) : next(nullptr), prev(nullptr), rotation(rot), time_slot(ts) {}
public: int rotation; int time_slot; void (*cb_func)(client_data *); client_data *user_data; tw_timer *next; tw_timer *prev; };
class time_wheel { public: time_wheel() : cur_slot(0), timer_fd(-1) { for (int i = 0; i < N; ++i) { slots[i] = nullptr; } setup_timerfd(); }
~time_wheel() { for (int i = 0; i < N; ++i) { tw_timer *tmp = slots[i]; while (tmp) { slots[i] = tmp->next; delete tmp; tmp = slots[i]; } } close(timer_fd); }
tw_timer *add_timer(int timeout) { if (timeout < 0) { return nullptr; } int ticks = 0; if (timeout < TI) { ticks = 1; } else { ticks = timeout / TI; } int rotation = ticks / N; int ts = (cur_slot + (ticks % N)) % N; tw_timer *timer = new tw_timer(rotation, ts); if (!slots[ts]) { printf("add timer, rotation is %d, ts is %d, cur_slot is %d\n", rotation, ts, cur_slot); slots[ts] = timer; } else { timer->next = slots[ts]; slots[ts]->prev = timer; slots[ts] = timer; } return timer; }
void del_timer(tw_timer *timer) { if (!timer) { return; } int ts = timer->time_slot; if (timer == slots[ts]) { slots[ts] = slots[ts]->next; if (slots[ts]) { slots[ts]->prev = nullptr; } delete timer; } else { timer->prev->next = timer->next; if (timer->next) { timer->next->prev = timer->prev; } delete timer; } }
void tick() { tw_timer *tmp = slots[cur_slot]; printf("current slot is %d\n", cur_slot); while (tmp) { printf("tick the timer once\n"); if (tmp->rotation > 0) { tmp->rotation--; tmp = tmp->next; } else { tmp->cb_func(tmp->user_data); if (tmp == slots[cur_slot]) { printf("delete header in cur_slot\n"); slots[cur_slot] = tmp->next; delete tmp; if (slots[cur_slot]) { slots[cur_slot]->prev = nullptr; } tmp = slots[cur_slot]; } else { tmp->prev->next = tmp->next; if (tmp->next) { tmp->next->prev = tmp->prev; } tw_timer *tmp2 = tmp->next; delete tmp; tmp = tmp2; } } } cur_slot = ++cur_slot % N; }
int get_timer_fd() const { return timer_fd; }
private: void setup_timerfd() { timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC); assert(timer_fd != -1);
struct itimerspec new_value; memset(&new_value, 0, sizeof(new_value)); new_value.it_value.tv_sec = TI; new_value.it_interval.tv_sec = TI; timerfd_settime(timer_fd, 0, &new_value, nullptr); }
private: static const int N = 60; static const int TI = 1; tw_timer *slots[N]; int cur_slot; int timer_fd; };
int setnonblocking(int fd) { int old_option = fcntl(fd, F_GETFL); int new_option = old_option | O_NONBLOCK; fcntl(fd, F_SETFL, new_option); return old_option; }
void addfd(int epollfd, int fd) { epoll_event event; event.data.fd = fd; event.events = EPOLLIN | EPOLLET; epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event); setnonblocking(fd); }
void epoll_main_loop(int listenfd, time_wheel &tw) { int epollfd = epoll_create(5); assert(epollfd != -1); epoll_event events[1024];
addfd(epollfd, listenfd); addfd(epollfd, tw.get_timer_fd());
while (true) { int num_events = epoll_wait(epollfd, events, 1024, -1); if (num_events < 0 && errno != EINTR) { printf("epoll failure\n"); break; }
for (int i = 0; i < num_events; ++i) { int sockfd = events[i].data.fd; if (sockfd == listenfd) { sockaddr_in client_address; socklen_t client_addrlen = sizeof(client_address); int connfd = accept(listenfd, (sockaddr *)&client_address, &client_addrlen); addfd(epollfd, connfd);
client_data *data = new client_data; data->address = client_address; data->sockfd = connfd;
tw_timer *timer = tw.add_timer(20); timer->user_data = data; timer->cb_func = [](client_data *data) { close(data->sockfd); printf("close fd %d\n", data->sockfd); delete data; }; data->timer = timer; } else if (sockfd == tw.get_timer_fd()) { uint64_t exp; read(sockfd, &exp, sizeof(uint64_t)); tw.tick(); } else if (events[i].events & EPOLLIN) { client_data *data = nullptr; char buf[BUFFER_SIZE]; int ret = recv(sockfd, buf, BUFFER_SIZE - 1, 0); if (ret <= 0) { tw.del_timer(data->timer); close(sockfd); delete data; } else { tw.del_timer(data->timer); tw_timer *timer = tw.add_timer(20); timer->user_data = data; timer->cb_func = [](client_data *data) { close(data->sockfd); printf("close fd %d\n", data->sockfd); delete data; }; data->timer = timer; } } } }
close(epollfd); }
int main() { int listenfd = socket(PF_INET, SOCK_STREAM, 0); assert(listenfd != -1);
sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(8888); int ret = bind(listenfd, (sockaddr *)&address, sizeof(address)); assert(ret != -1); ret = listen(listenfd, 5); assert(ret != -1);
time_wheel tw;
epoll_main_loop(listenfd, tw);
close(listenfd); return 0; }
|