diff options
Diffstat (limited to 'src/examples/suspend_resume_epoll.c')
-rw-r--r-- | src/examples/suspend_resume_epoll.c | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/src/examples/suspend_resume_epoll.c b/src/examples/suspend_resume_epoll.c index 3e0be24c..a69fdc4a 100644 --- a/src/examples/suspend_resume_epoll.c +++ b/src/examples/suspend_resume_epoll.c | |||
@@ -31,7 +31,8 @@ | |||
31 | 31 | ||
32 | #define TIMEOUT_INFINITE -1 | 32 | #define TIMEOUT_INFINITE -1 |
33 | 33 | ||
34 | struct Request { | 34 | struct Request |
35 | { | ||
35 | struct MHD_Connection *connection; | 36 | struct MHD_Connection *connection; |
36 | int timerfd; | 37 | int timerfd; |
37 | }; | 38 | }; |
@@ -52,7 +53,7 @@ ahc_echo (void *cls, | |||
52 | { | 53 | { |
53 | struct MHD_Response *response; | 54 | struct MHD_Response *response; |
54 | int ret; | 55 | int ret; |
55 | struct Request* req; | 56 | struct Request*req; |
56 | struct itimerspec ts; | 57 | struct itimerspec ts; |
57 | 58 | ||
58 | (void) cls; | 59 | (void) cls; |
@@ -89,29 +90,29 @@ ahc_echo (void *cls, | |||
89 | return ret; | 90 | return ret; |
90 | } | 91 | } |
91 | /* create timer and suspend connection */ | 92 | /* create timer and suspend connection */ |
92 | req->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); | 93 | req->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK); |
93 | if (-1 == req->timerfd) | 94 | if (-1 == req->timerfd) |
94 | { | 95 | { |
95 | printf("timerfd_create: %s", strerror(errno)); | 96 | printf ("timerfd_create: %s", strerror (errno)); |
96 | return MHD_NO; | 97 | return MHD_NO; |
97 | } | 98 | } |
98 | evt.events = EPOLLIN; | 99 | evt.events = EPOLLIN; |
99 | evt.data.ptr = req; | 100 | evt.data.ptr = req; |
100 | if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, req->timerfd, &evt)) | 101 | if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, req->timerfd, &evt)) |
101 | { | 102 | { |
102 | printf("epoll_ctl: %s", strerror(errno)); | 103 | printf ("epoll_ctl: %s", strerror (errno)); |
103 | return MHD_NO; | 104 | return MHD_NO; |
104 | } | 105 | } |
105 | ts.it_value.tv_sec = 1; | 106 | ts.it_value.tv_sec = 1; |
106 | ts.it_value.tv_nsec = 0; | 107 | ts.it_value.tv_nsec = 0; |
107 | ts.it_interval.tv_sec = 0; | 108 | ts.it_interval.tv_sec = 0; |
108 | ts.it_interval.tv_nsec = 0; | 109 | ts.it_interval.tv_nsec = 0; |
109 | if (-1 == timerfd_settime(req->timerfd, 0, &ts, NULL)) | 110 | if (-1 == timerfd_settime (req->timerfd, 0, &ts, NULL)) |
110 | { | 111 | { |
111 | printf("timerfd_settime: %s", strerror(errno)); | 112 | printf ("timerfd_settime: %s", strerror (errno)); |
112 | return MHD_NO; | 113 | return MHD_NO; |
113 | } | 114 | } |
114 | MHD_suspend_connection(connection); | 115 | MHD_suspend_connection (connection); |
115 | return MHD_YES; | 116 | return MHD_YES; |
116 | } | 117 | } |
117 | 118 | ||
@@ -130,7 +131,7 @@ connection_done (void *cls, | |||
130 | if (-1 != req->timerfd) | 131 | if (-1 != req->timerfd) |
131 | if (0 != close (req->timerfd)) | 132 | if (0 != close (req->timerfd)) |
132 | abort (); | 133 | abort (); |
133 | free(req); | 134 | free (req); |
134 | } | 135 | } |
135 | 136 | ||
136 | 137 | ||
@@ -139,17 +140,17 @@ main (int argc, | |||
139 | char *const *argv) | 140 | char *const *argv) |
140 | { | 141 | { |
141 | struct MHD_Daemon *d; | 142 | struct MHD_Daemon *d; |
142 | const union MHD_DaemonInfo * info; | 143 | const union MHD_DaemonInfo *info; |
143 | int current_event_count; | 144 | int current_event_count; |
144 | struct epoll_event events_list[1]; | 145 | struct epoll_event events_list[1]; |
145 | struct Request *req; | 146 | struct Request *req; |
146 | uint64_t timer_expirations; | 147 | uint64_t timer_expirations; |
147 | 148 | ||
148 | if (argc != 2) | 149 | if (argc != 2) |
149 | { | 150 | { |
150 | printf ("%s PORT\n", argv[0]); | 151 | printf ("%s PORT\n", argv[0]); |
151 | return 1; | 152 | return 1; |
152 | } | 153 | } |
153 | d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME, | 154 | d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME, |
154 | atoi (argv[1]), | 155 | atoi (argv[1]), |
155 | NULL, NULL, &ahc_echo, NULL, | 156 | NULL, NULL, &ahc_echo, NULL, |
@@ -158,17 +159,17 @@ main (int argc, | |||
158 | if (d == NULL) | 159 | if (d == NULL) |
159 | return 1; | 160 | return 1; |
160 | 161 | ||
161 | info = MHD_get_daemon_info(d, MHD_DAEMON_INFO_EPOLL_FD); | 162 | info = MHD_get_daemon_info (d, MHD_DAEMON_INFO_EPOLL_FD); |
162 | if (info == NULL) | 163 | if (info == NULL) |
163 | return 1; | 164 | return 1; |
164 | 165 | ||
165 | epfd = epoll_create1(EPOLL_CLOEXEC); | 166 | epfd = epoll_create1 (EPOLL_CLOEXEC); |
166 | if (-1 == epfd) | 167 | if (-1 == epfd) |
167 | return 1; | 168 | return 1; |
168 | 169 | ||
169 | evt.events = EPOLLIN; | 170 | evt.events = EPOLLIN; |
170 | evt.data.ptr = NULL; | 171 | evt.data.ptr = NULL; |
171 | if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, info->epoll_fd, &evt)) | 172 | if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, info->epoll_fd, &evt)) |
172 | return 1; | 173 | return 1; |
173 | 174 | ||
174 | while (1) | 175 | while (1) |
@@ -182,7 +183,7 @@ main (int argc, | |||
182 | timeout = TIMEOUT_INFINITE; | 183 | timeout = TIMEOUT_INFINITE; |
183 | else | 184 | else |
184 | timeout = (to < INT_MAX - 1) ? (int) to : (INT_MAX - 1); | 185 | timeout = (to < INT_MAX - 1) ? (int) to : (INT_MAX - 1); |
185 | current_event_count = epoll_wait(epfd, events_list, 1, timeout); | 186 | current_event_count = epoll_wait (epfd, events_list, 1, timeout); |
186 | 187 | ||
187 | if (1 == current_event_count) | 188 | if (1 == current_event_count) |
188 | { | 189 | { |
@@ -191,12 +192,13 @@ main (int argc, | |||
191 | /* A timer has timed out */ | 192 | /* A timer has timed out */ |
192 | req = events_list[0].data.ptr; | 193 | req = events_list[0].data.ptr; |
193 | /* read from the fd so the system knows we heard the notice */ | 194 | /* read from the fd so the system knows we heard the notice */ |
194 | if (-1 == read(req->timerfd, &timer_expirations, sizeof(timer_expirations))) | 195 | if (-1 == read (req->timerfd, &timer_expirations, |
196 | sizeof(timer_expirations))) | ||
195 | { | 197 | { |
196 | return 1; | 198 | return 1; |
197 | } | 199 | } |
198 | /* Now resume the connection */ | 200 | /* Now resume the connection */ |
199 | MHD_resume_connection(req->connection); | 201 | MHD_resume_connection (req->connection); |
200 | } | 202 | } |
201 | } | 203 | } |
202 | else if (0 == current_event_count) | 204 | else if (0 == current_event_count) |
@@ -208,7 +210,7 @@ main (int argc, | |||
208 | /* error */ | 210 | /* error */ |
209 | return 1; | 211 | return 1; |
210 | } | 212 | } |
211 | if (! MHD_run(d)) | 213 | if (! MHD_run (d)) |
212 | return 1; | 214 | return 1; |
213 | } | 215 | } |
214 | 216 | ||