|
4 | 4 | #include <stdlib.h> |
5 | 5 | #include <unistd.h> |
6 | 6 | #include <sys/socket.h> |
| 7 | +#include <pthread.h> |
7 | 8 | #define EV_SOURCE // add before ev.h |
8 | 9 | #include "../ev.h" |
9 | 10 |
|
@@ -130,7 +131,7 @@ static void on_data(ev_context *ctx, void *data) { |
130 | 131 | return; |
131 | 132 | } |
132 | 133 |
|
133 | | - printf("Received %lu bytes\n", conn->bufsize); |
| 134 | + /* printf("Received %lu bytes\n", conn->bufsize); */ |
134 | 135 |
|
135 | 136 | /* Fire an event to schedule a response */ |
136 | 137 | ev_fire_event(ctx, conn->fd, EV_WRITE, on_response, conn); |
@@ -168,6 +169,29 @@ static void on_response(ev_context *ctx, void *data) { |
168 | 169 | fprintf(stderr, "write(2) - error sending data: %s\n", strerror(errno)); |
169 | 170 | } |
170 | 171 |
|
| 172 | +static void *worker(void *data) { |
| 173 | + int listen_fd = *((int *) data); |
| 174 | + ev_context ctx; |
| 175 | + int err = ev_init(&ctx, 1024); |
| 176 | + if (err < EV_OK) { |
| 177 | + fprintf(stderr, "Ev context init failed: Out of memory"); |
| 178 | + exit(EXIT_FAILURE); |
| 179 | + } |
| 180 | + |
| 181 | + /* Register a callback on the listening socket for incoming connections */ |
| 182 | + ev_register_event(&ctx, listen_fd, EV_READ, on_connection, &listen_fd); |
| 183 | + |
| 184 | + printf("Listening on %s:%i\n", HOST, PORT); |
| 185 | + |
| 186 | + /* Start the loop */ |
| 187 | + ev_run(&ctx); |
| 188 | + |
| 189 | + /* Release resources after the loop has been stopped */ |
| 190 | + ev_destroy(&ctx); |
| 191 | + |
| 192 | + return NULL; |
| 193 | +} |
| 194 | + |
171 | 195 | int main(void) { |
172 | 196 |
|
173 | 197 | ev_context ctx; |
@@ -210,10 +234,14 @@ int main(void) { |
210 | 234 | if (listen(listen_fd, 32) != 0) |
211 | 235 | goto err; |
212 | 236 |
|
213 | | - int err = ev_init(&ctx, 32); |
| 237 | + pthread_t handle[4]; |
| 238 | + for (int i = 0; i < 4; i++) { |
| 239 | + pthread_create(&handle[i], NULL, (void * (*) (void *)) &worker, &listen_fd); |
| 240 | + } |
| 241 | + int err = ev_init(&ctx, 1024); |
214 | 242 | if (err < EV_OK) { |
215 | | - fprintf(stderr, "Ev context init failed: Out of memory"); |
216 | | - exit(EXIT_FAILURE); |
| 243 | + fprintf(stderr, "Ev context init failed: Out of memory"); |
| 244 | + exit(EXIT_FAILURE); |
217 | 245 | } |
218 | 246 |
|
219 | 247 | /* Register a callback on the listening socket for incoming connections */ |
|
0 commit comments