server_8h.tex
10.9 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
\hypertarget{server_8h}{
\section{include/server.h File Reference}
\label{server_8h}\index{include/server.h@{include/server.h}}
}
{\ttfamily \#include $<$stdio.h$>$}\par
{\ttfamily \#include $<$poll.h$>$}\par
{\ttfamily \#include \char`\"{}class.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}socket.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}logger.h\char`\"{}}\par
Include dependency graph for server.h:
\subsection*{Classes}
\begin{DoxyCompactItemize}
\item
struct \hyperlink{structServer}{Server}
\end{DoxyCompactItemize}
\subsection*{Defines}
\begin{DoxyCompactItemize}
\item
\#define \hyperlink{server_8h_a6371240ebfb3156b737ddd83e1e142bd}{POLL\_\-FD\_\-NSIZE}~1024
\item
\#define \hyperlink{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}{POLL\_\-FD\_\-SIZE}~(sizeof(struct pollfd) $\ast$ POLL\_\-FD\_\-NSIZE)
\item
\#define \hyperlink{server_8h_a4a694160d07797872cb3861f94b95c94}{MOVE\_\-SIZE}(size, idx)~((size) $\ast$ (POLL\_\-FD\_\-NSIZE-\/((idx)+1)))
\item
\#define \hyperlink{server_8h_a11d5c89fb77bc941d085e8f38d014665}{CLEAR\_\-CONN}(server, idx)
\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item
void \hyperlink{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}{serverRun} (\hyperlink{structServer}{Server} this)
\item
void \hyperlink{server_8h_a81f787254ea87cbc851d292e5a7d195d}{serverCloseConn} (\hyperlink{structServer}{Server} this, unsigned int handle)
\end{DoxyCompactItemize}
\subsection{Define Documentation}
\hypertarget{server_8h_a11d5c89fb77bc941d085e8f38d014665}{
\index{server.h@{server.h}!CLEAR\_\-CONN@{CLEAR\_\-CONN}}
\index{CLEAR\_\-CONN@{CLEAR\_\-CONN}!server.h@{server.h}}
\subsubsection[{CLEAR\_\-CONN}]{\setlength{\rightskip}{0pt plus 5cm}\#define CLEAR\_\-CONN(
\begin{DoxyParamCaption}
\item[{}]{server, }
\item[{}]{idx}
\end{DoxyParamCaption}
)}}
\label{server_8h_a11d5c89fb77bc941d085e8f38d014665}
{\bfseries Value:}
\begin{DoxyCode}
memmove(&(((server)->fds)[(idx)]), \
&(((server)->fds)[(idx)+1]), \
MOVE_SIZE(sizeof(((server)->fds)[0]),(idx))); \
memmove(&(((server)->conns)[(idx)]), \
&(((server)->conns)[(idx)+1]), \
MOVE_SIZE(sizeof(((server)->conns)[0]),(idx)))
\end{DoxyCode}
Definition at line 15 of file server.h.
\hypertarget{server_8h_a4a694160d07797872cb3861f94b95c94}{
\index{server.h@{server.h}!MOVE\_\-SIZE@{MOVE\_\-SIZE}}
\index{MOVE\_\-SIZE@{MOVE\_\-SIZE}!server.h@{server.h}}
\subsubsection[{MOVE\_\-SIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define MOVE\_\-SIZE(
\begin{DoxyParamCaption}
\item[{}]{size, }
\item[{}]{idx}
\end{DoxyParamCaption}
)~((size) $\ast$ (POLL\_\-FD\_\-NSIZE-\/((idx)+1)))}}
\label{server_8h_a4a694160d07797872cb3861f94b95c94}
Definition at line 14 of file server.h.
\hypertarget{server_8h_a6371240ebfb3156b737ddd83e1e142bd}{
\index{server.h@{server.h}!POLL\_\-FD\_\-NSIZE@{POLL\_\-FD\_\-NSIZE}}
\index{POLL\_\-FD\_\-NSIZE@{POLL\_\-FD\_\-NSIZE}!server.h@{server.h}}
\subsubsection[{POLL\_\-FD\_\-NSIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define POLL\_\-FD\_\-NSIZE~1024}}
\label{server_8h_a6371240ebfb3156b737ddd83e1e142bd}
Definition at line 11 of file server.h.
\hypertarget{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}{
\index{server.h@{server.h}!POLL\_\-FD\_\-SIZE@{POLL\_\-FD\_\-SIZE}}
\index{POLL\_\-FD\_\-SIZE@{POLL\_\-FD\_\-SIZE}!server.h@{server.h}}
\subsubsection[{POLL\_\-FD\_\-SIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define POLL\_\-FD\_\-SIZE~(sizeof(struct pollfd) $\ast$ POLL\_\-FD\_\-NSIZE)}}
\label{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}
Definition at line 12 of file server.h.
\subsection{Function Documentation}
\hypertarget{server_8h_a81f787254ea87cbc851d292e5a7d195d}{
\index{server.h@{server.h}!serverCloseConn@{serverCloseConn}}
\index{serverCloseConn@{serverCloseConn}!server.h@{server.h}}
\subsubsection[{serverCloseConn}]{\setlength{\rightskip}{0pt plus 5cm}void serverCloseConn (
\begin{DoxyParamCaption}
\item[{{\bf Server}}]{this, }
\item[{unsigned int}]{handle}
\end{DoxyParamCaption}
)}}
\label{server_8h_a81f787254ea87cbc851d292e5a7d195d}
Definition at line 7 of file close\_\-conn.c.
\begin{DoxyCode}
{
int fd = (this->fds)[i].fd;
delete(&((this->conns)[fd].sock));
delete(&((this->conns)[fd].reader));
memset((this->conns)[fd].wbuf, 0, strlen((this->conns)[fd].wbuf));
(this->fds)[i].events = 0;
(this->fds)[i].revents = 0;
(this->fds)[i].fd = 0;
this->ndel++;
// CLEAR_CONN(this, i);
// this->nfds--;
}
\end{DoxyCode}
Here is the caller graph for this function:
\hypertarget{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}{
\index{server.h@{server.h}!serverRun@{serverRun}}
\index{serverRun@{serverRun}!server.h@{server.h}}
\subsubsection[{serverRun}]{\setlength{\rightskip}{0pt plus 5cm}void serverRun (
\begin{DoxyParamCaption}
\item[{{\bf Server}}]{this}
\end{DoxyParamCaption}
)}}
\label{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}
: 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.
handle accept
handle reads
do some other processing : actually this will hard assume that our stream reader is a http parser and it has its queue...think about more generalizing here.
: for now simply remove request and send not found. Make this sane.
: the complete response stuff have to be removed here.
: just to send an answer and be able to make some apache benchs i do it here...this definetly MUST BE moved
handle writes
Definition at line 30 of file run.c.
\begin{DoxyCode}
{
loggerLog(this->logger, LOGGER_INFO, "service started");
while (!doShutdown) /* until error or signal */
{
int events;
unsigned int i;
events = serverPoll(this);
if (doShutdown) break;
for (i=0; i < events; i++) {
int fd = (this->fds)[i].fd;
//int nreads = 0, nwrites = 0;
if (0 != ((this->fds)[i].revents & POLLIN)) {
if (this->sock->handle == (this->fds)[i].fd) {
serverHandleAccept(this);
}
else {
int size;
if (0 < (size=serverRead(this, i))) {
int j;
HttpRequestQueue queue =
((HttpRequestParser)(this
->conns)[fd].reader)->request_queue;
for (j=0; j<queue->nrequests; j++
) {
HttpRequest request = que
ue->requests[j];
//if (NULL != request->bo
dy) {
// puts("==REQUEST B
ODY==");
// puts(request->bod
y);
//}
delete(&request);
time_t t;
struct tm * tmp;
char timestr[200];
#define RESP_HEAD "HTTP/1.1 404 Not Found\r\n" \
"Content-Type: text/html\
r\n" \
"Content-Length: %lu\r\n"
\
"Date: %s\r\n" \
"Server: testserver\r\n"
#define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
"<!DOCTYPE html PUBLIC \"
-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
" \"http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
"<html xmlns=\"http://www
.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \
"<head><title>404 - Not F
ound</title></head>" \
"<body><h1>404 - Not Foun
d</h1></body>" \
"</html>"
t = time(NULL);
tmp = localtime(&t);
strftime(timestr, sizeof(
timestr), "%a, %d %b %Y %T %Z", tmp);
sprintf((this->conns)[fd]
.wbuf, RESP_HEAD "\r\n" RESP_DATA, sizeof(RESP_DATA), timestr);
(this->fds)[i].events = (
this->fds)[i].events | POLLOUT;
}
queue->nrequests = 0;
}
}
}
if (0 != ((this->fds)[i].revents & POLLOUT)) {
int size;
size = write(
(this->fds)[i].fd,
(this->conns)[fd].wbuf,
strlen((this->conns)[fd].wbuf));
if (size == strlen((this->conns)[fd].wbuf) ||
-1 == size) {
if (-1 == size) {
loggerLog(this->logger,
LOGGER_ERR,
"write error, clo
sing connection");
}
serverCloseConn(this, i);
}
else {
memmove((this->conns)[fd].wbuf,
(this->conns)[fd].wbuf +
size,
strlen((this->conns)[fd].
wbuf) - size + 1);
}
}
}
}
}
\end{DoxyCode}
Here is the call graph for this function:
Here is the caller graph for this function: