run.c
2.57 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
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
#include <poll.h> /* for poll system call and related */
#include <string.h> /* for memset and stuff */
#include <stdlib.h> /* for exit */
#include <errno.h> /* for errno */
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "server.h"
#include "socket.h"
#include "logger.h"
#include "signalHandling.h"
#include "interface/class.h"
#include "interface/stream_reader.h"
#include "interface/stream_writer.h"
#include "interface/logger.h"
#undef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#include "poll.c"
#include "handle_accept.c"
#include "read.c"
void
serverRun(Server this)
{
loggerLog(this->logger, LOGGER_INFO, "service started");
/**
* @TODO: actually this is the main loop of my server. When
* stuff becomes more complicated it might be feasabible to
* split stuff into separate processes. This will definetly
* involve some IPC and syncing. Right now as this is actually
* only a simple HTTP server implementation we go on with
* this single process.
* What we can first do to get some processing between read/write
* cicles is to use the poll timeout.
*/
while (!doShutdown) /* until error or signal */
{
int events;
unsigned int i;
events = serverPoll(this);
if (doShutdown) break;
for (i=0; i < this->nfds; i++) {
int fd = (this->fds)[i].fd;
int naccs = 10, nreads = 10, nwrites = 10;
if (0 >= events) break;
if (0 != ((this->fds)[i].revents & POLLIN) && 0 < nreads) {
events--;
/**
* handle accept
*/
if (this->sock->handle == (this->fds)[i].fd) {
while(-1 != serverHandleAccept(this) && 0 < naccs) {
naccs--;
switch(errno) {
case EAGAIN:
loggerLog(this->logger,
LOGGER_DEBUG,
"server accept blocks");
break;
default:
loggerLog(this->logger,
LOGGER_DEBUG,
"server accept error");
break;
}
}
}
/**
* handle reads
*/
else {
nreads--;
switch (serverRead(this, i)) {
case -2:
case -1:
serverCloseConn(this, i);
break;
case 0:
break;
default:
(this->fds)[i].events |= POLLOUT;
}
}
}
/**
* handle writes
*/
if (0 != ((this->fds)[i].revents & POLLOUT) && 0 < nwrites) {
events--;
nwrites--;
if (0 >= streamWriterWrite((this->conns)[fd].worker, fd)) {
serverCloseConn(this, i);
}
(this->fds)[i].events &= ~POLLOUT;
}
}
}
}
// vim: set ts=4 sw=4: