-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpWorkerThread.c
More file actions
76 lines (59 loc) · 2.01 KB
/
httpWorkerThread.c
File metadata and controls
76 lines (59 loc) · 2.01 KB
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
//
// Created by rose on 2/7/19.
//
#include <malloc.h>
#include <sys/socket.h>
#include "httpWorkerThread.h"
#include "http.h"
#include "loggerWorkerThread.h"
/**
* Parameters passed to each individual worker thread on startup.
*/
struct httpWorkerThreadParams {
channel_t inputChannel;
channel_t loggingChannel;
fileCache_t cache;
};
typedef struct httpWorkerThreadParams *httpWorkerThreadParams_t;
httpWorkerThreadParams_t
createHttpWorkerParams(channel_t input, channel_t logging, fileCache_t cache);
void destroyHttpWorkerParams(httpWorkerThreadParams_t);
void *httpFileWorker(httpWorkerThreadParams_t params);
httpWorkerThreadParams_t
createHttpWorkerParams(channel_t input, channel_t logging, fileCache_t cache) {
httpWorkerThreadParams_t ret = malloc(sizeof(struct httpWorkerThreadParams));
ret->inputChannel = input;
ret->loggingChannel = logging;
ret->cache = cache;
return ret;
}
void destroyHttpWorkerParams(httpWorkerThreadParams_t workerParams) {
free(workerParams);
}
void *httpFileWorker(httpWorkerThreadParams_t params) {
// extract params.
channel_t in = params->inputChannel;
channel_t logger = params->loggingChannel;
fileCache_t cache = params->cache;
destroyHttpWorkerParams(params);
while (true) {
int *fd;
// process input until input channel closes.
if (channelReceive(in, (void **) &fd) == CHANNEL_CLOSED) {
return NULL;
}
logMessageWithIp(logger, *fd, stringFromCString("Connection received."));
handleHttpConnection(*fd, logger, cache);
free(fd);
}
}
void
createHttpWorkerPool(channel_t logger, fileCache_t cache, pthread_t *threads, size_t numThreads,
channel_t *inputChannel) {
channel_t channel = createChannel();
for (size_t i = 0; i < numThreads; i++) {
pthread_create(threads + i, NULL, (void *(*)(void *)) &httpFileWorker,
createHttpWorkerParams(channel, logger, cache));
}
*inputChannel = channel;
}