diff options
| author | 2022-10-10 19:50:37 +0200 | |
|---|---|---|
| committer | 2022-10-10 19:50:37 +0200 | |
| commit | 1e9d21147ba24664bdeb64b49bae52992e2bec4e (patch) | |
| tree | 90ee56ebb8fd27f7a602fb0bc2e0c4a18f47e724 | |
| parent | Call xs_socket_accept() from httpd(). (diff) | |
| download | penes-snac2-1e9d21147ba24664bdeb64b49bae52992e2bec4e.tar.gz penes-snac2-1e9d21147ba24664bdeb64b49bae52992e2bec4e.tar.xz penes-snac2-1e9d21147ba24664bdeb64b49bae52992e2bec4e.zip | |
Connections are now attended by threads.
| -rw-r--r-- | httpd.c | 28 |
1 files changed, 22 insertions, 6 deletions
| @@ -197,10 +197,10 @@ void term_handler(int s) | |||
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | 199 | ||
| 200 | static void *helper_thread(void *arg) | 200 | static void *queue_thread(void *arg) |
| 201 | /* helper thread (queue management) */ | 201 | /* queue thread (queue management) */ |
| 202 | { | 202 | { |
| 203 | srv_log(xs_fmt("subthread start")); | 203 | srv_log(xs_fmt("queue thread start")); |
| 204 | 204 | ||
| 205 | while (srv_running) { | 205 | while (srv_running) { |
| 206 | xs *list = user_list(); | 206 | xs *list = user_list(); |
| @@ -219,12 +219,22 @@ static void *helper_thread(void *arg) | |||
| 219 | sleep(3); | 219 | sleep(3); |
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | srv_log(xs_fmt("subthread stop")); | 222 | srv_log(xs_fmt("queue thread stop")); |
| 223 | 223 | ||
| 224 | return NULL; | 224 | return NULL; |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | 227 | ||
| 228 | static void *connection_thread(void *arg) | ||
| 229 | /* connection thread */ | ||
| 230 | { | ||
| 231 | httpd_connection((FILE *)arg); | ||
| 232 | return NULL; | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | int threaded_connections = 1; | ||
| 237 | |||
| 228 | void httpd(void) | 238 | void httpd(void) |
| 229 | /* starts the server */ | 239 | /* starts the server */ |
| 230 | { | 240 | { |
| @@ -249,13 +259,19 @@ void httpd(void) | |||
| 249 | 259 | ||
| 250 | srv_log(xs_fmt("httpd start %s:%d", address, port)); | 260 | srv_log(xs_fmt("httpd start %s:%d", address, port)); |
| 251 | 261 | ||
| 252 | pthread_create(&htid, NULL, helper_thread, NULL); | 262 | pthread_create(&htid, NULL, queue_thread, NULL); |
| 253 | 263 | ||
| 254 | if (setjmp(on_break) == 0) { | 264 | if (setjmp(on_break) == 0) { |
| 255 | for (;;) { | 265 | for (;;) { |
| 256 | FILE *f = xs_socket_accept(rs); | 266 | FILE *f = xs_socket_accept(rs); |
| 257 | 267 | ||
| 258 | httpd_connection(f); | 268 | if (threaded_connections) { |
| 269 | pthread_t cth; | ||
| 270 | |||
| 271 | pthread_create(&cth, NULL, connection_thread, f); | ||
| 272 | } | ||
| 273 | else | ||
| 274 | httpd_connection(f); | ||
| 259 | } | 275 | } |
| 260 | } | 276 | } |
| 261 | 277 | ||