run.c
2.79 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
123
124
125
126
127
128
129
130
131
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "server.h"
#include "logger.h"
#include "utils/signalHandling.h"
int serverPoll(Server);
int serverHandleAccept(Server, unsigned int);
ssize_t serverRead(Server, unsigned int);
ssize_t serverWrite(Server, unsigned int);
void serverCloseConn(Server, unsigned int);
void
serverRun(Server this)
{
int events = 0;
loggerLog(this->logger, LOGGER_INFO, "service started");
while (!doShutdown) //! until error or signal
{
unsigned int i;
if (0 <= events) {
/*
* TODO check why sometimes events is less than 0
* There is still a misshandling here.
*/
events = serverPoll(this);
}
/**
* handle accept
*/
if (0 != ((this->fds)[0].revents & POLLIN)) {
if (0 > serverHandleAccept(this, 0)) {
events--;
}
}
// /**
// * handle accept SSL
// */
// if (0 != ((this->fds)[1].revents & POLLIN)) {
// if (-1 == serverHandleAccept(this, 1)) {
// events--;
// }
// }
for (i=2; i < this->nfds; i++) {
/**
* handle reads
*/
if (0 != ((this->fds)[i].revents & POLLIN)) {
ssize_t processed = serverRead(this, i);
if (0 > processed) {
events--;
switch (processed) {
case -2: // close me...
serverCloseConn(this, i);
case -1: // poll me again
break;
}
}
if (0 < processed) {
(this->fds)[i].events |= POLLOUT;
}
}
/**
* handle writes
*/
if (0 != ((this->fds)[i].revents & POLLOUT)) {
ssize_t remaining = serverWrite(this, i);
if (0 >= remaining) {
/*
* 0 means queue was empty...try again next
* time...no need to poll again.
* Anyway, most likely we need to read again
* so lets finish this event for now.
*/
events--;
switch (remaining) {
case 0: // nothing more to write stop polling
(this->fds)[i].events &= ~POLLOUT;
break;
case -2: // close me...
serverCloseConn(this, i);
case -1: // poll me again
break;
}
}
}
if (0 >= events)
break; // no more events to handle
}
}
}
// vim: set ts=4 sw=4: