Commit 4f95d8ce1138ed82fd62edfefa4181fe2ce75671

Authored by Georg Hopp
1 parent c26071a1

change worker to a generic one, some cleanups in server

@@ -37,6 +37,8 @@ @@ -37,6 +37,8 @@
37 #define FALSE ((void *)0) 37 #define FALSE ((void *)0)
38 #endif 38 #endif
39 39
  40 +#define PARSER_MAX_BUF 131072
  41 +
40 42
41 typedef enum e_HttpMessageState { 43 typedef enum e_HttpMessageState {
42 HTTP_MESSAGE_GARBAGE=0, 44 HTTP_MESSAGE_GARBAGE=0,
@@ -28,11 +28,9 @@ @@ -28,11 +28,9 @@
28 28
29 #include "class.h" 29 #include "class.h"
30 #include "http/parser.h" 30 #include "http/parser.h"
31 -#include "http/response/writer.h" 31 +#include "http/writer.h"
32 #include "cbuf.h" 32 #include "cbuf.h"
33 33
34 -#define RESPONSE_WRITER_MAX_BUF 131072  
35 -#define REQUEST_PARSER_BUFFER_MAX 8192  
36 34
37 #ifndef TRUE 35 #ifndef TRUE
38 #define TRUE ((void *)1) 36 #define TRUE ((void *)1)
@@ -42,15 +40,16 @@ @@ -42,15 +40,16 @@
42 #define FALSE ((void *)0) 40 #define FALSE ((void *)0)
43 #endif 41 #endif
44 42
  43 +
45 CLASS(HttpWorker) { 44 CLASS(HttpWorker) {
46 - char * id;  
47 - int * val; 45 + char * id;
  46 + int * val;
48 47
49 - Cbuf pbuf;  
50 - Cbuf wbuf; 48 + Cbuf pbuf;
  49 + Cbuf wbuf;
51 50
52 - HttpParser parser;  
53 - HttpResponseWriter writer; 51 + HttpParser parser;
  52 + HttpWriter writer;
54 }; 53 };
55 54
56 #endif // __HTTP_WORKER_H__ 55 #endif // __HTTP_WORKER_H__
@@ -21,13 +21,13 @@ @@ -21,13 +21,13 @@
21 * - along with this program. If not, see <http://www.gnu.org/licenses/>. 21 * - along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */ 22 */
23 23
24 -#ifndef __HTTP_RESPONSE_WRITER_H__  
25 -#define __HTTP_RESPONSE_WRITER_H__ 24 +#ifndef __HTTP_WRITER_H__
  25 +#define __HTTP_WRITER_H__
26 26
27 #include <sys/types.h> 27 #include <sys/types.h>
28 28
29 #include "class.h" 29 #include "class.h"
30 -#include "http/response.h" 30 +#include "http/message.h"
31 #include "http/message/queue.h" 31 #include "http/message/queue.h"
32 #include "cbuf.h" 32 #include "cbuf.h"
33 33
@@ -39,29 +39,31 @@ @@ -39,29 +39,31 @@
39 #define FALSE ((void *)0) 39 #define FALSE ((void *)0)
40 #endif 40 #endif
41 41
  42 +#define WRITER_MAX_BUF 131072
42 43
43 -typedef enum e_HttpResponseState {  
44 - HTTP_RESPONSE_GET=0,  
45 - HTTP_RESPONSE_WRITE,  
46 - HTTP_RESPONSE_DONE  
47 -} HttpResponseState;  
48 44
49 -CLASS(HttpResponseWriter) {  
50 - Cbuf buffer;  
51 - void * ourLock; 45 +typedef enum e_HttpWriterState {
  46 + HTTP_WRITER_GET=0,
  47 + HTTP_WRITER_WRITE,
  48 + HTTP_WRITER_DONE
  49 +} HttpWriterState;
52 50
53 - HttpMessageQueue response_queue;  
54 - HttpResponse cur_response; 51 +CLASS(HttpWriter) {
  52 + Cbuf buffer;
  53 + void * ourLock;
55 54
56 - size_t nheader;  
57 - size_t nbody;  
58 - size_t written; 55 + HttpMessageQueue queue;
  56 + HttpMessage current;
59 57
60 - HttpResponseState state; 58 + size_t nheader;
  59 + size_t nbody;
  60 + size_t written;
  61 +
  62 + HttpWriterState state;
61 }; 63 };
62 64
63 -ssize_t httpResponseWriterWrite(HttpResponseWriter, int); 65 +ssize_t httpWriterWrite(void *, int);
64 66
65 -#endif // __HTTP_RESPONSE_WRITER_H__ 67 +#endif // __HTTP_WRITER_H__
66 68
67 // vim: set ts=4 sw=4: 69 // vim: set ts=4 sw=4:
@@ -6,7 +6,7 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ @@ -6,7 +6,7 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \
6 interface/subject.c interface/observer.c interface.c 6 interface/subject.c interface/observer.c interface.c
7 SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c 7 SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c
8 SERVER = server.c server/run.c server/close_conn.c server/poll.c \ 8 SERVER = server.c server/run.c server/close_conn.c server/poll.c \
9 - server/handle_accept.c server/read.c 9 + server/handle_accept.c server/read.c server/write.c
10 LOGGER = logger.c logger/stderr.c logger/syslog.c 10 LOGGER = logger.c logger/stderr.c logger/syslog.c
11 CB = cbuf.c cbuf/read.c cbuf/write.c \ 11 CB = cbuf.c cbuf/read.c cbuf/write.c \
12 cbuf/get_line.c cbuf/set_data.c cbuf/get_data.c \ 12 cbuf/get_line.c cbuf/set_data.c cbuf/get_data.c \
@@ -33,12 +33,13 @@ PARSER = http/parser.c \ @@ -33,12 +33,13 @@ PARSER = http/parser.c \
33 http/parser/new_message.c \ 33 http/parser/new_message.c \
34 http/parser/header.c \ 34 http/parser/header.c \
35 http/parser/body.c 35 http/parser/body.c
  36 +WRITER = http/writer.c \
  37 + http/writer/write.c
36 WORKER = http/worker.c \ 38 WORKER = http/worker.c \
37 http/worker/process.c \ 39 http/worker/process.c \
38 http/worker/write.c \ 40 http/worker/write.c \
39 http/worker/get_asset.c \ 41 http/worker/get_asset.c \
40 http/worker/add_common_header.c 42 http/worker/add_common_header.c
41 -WRITER = http/response/writer.c http/response/writer/write.c  
42 HEADER = http/header.c http/header/get.c http/header/add.c \ 43 HEADER = http/header.c http/header/get.c http/header/add.c \
43 http/header/to_string.c 44 http/header/to_string.c
44 UTILS = utils/hash.c \ 45 UTILS = utils/hash.c \
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 #include "class.h" 7 #include "class.h"
8 #include "http/worker.h" 8 #include "http/worker.h"
9 #include "http/parser.h" 9 #include "http/parser.h"
10 -#include "http/response/writer.h" 10 +#include "http/writer.h"
11 11
12 #include "interface/class.h" 12 #include "interface/class.h"
13 #include "interface/stream_reader.h" 13 #include "interface/stream_reader.h"
@@ -29,13 +29,13 @@ httpWorkerCtor(void * _this, va_list * params) @@ -29,13 +29,13 @@ httpWorkerCtor(void * _this, va_list * params)
29 this->val = val; 29 this->val = val;
30 30
31 sprintf(cbuf_id, "%s_%s", "parser", id); 31 sprintf(cbuf_id, "%s_%s", "parser", id);
32 - this->pbuf = new(Cbuf, cbuf_id, REQUEST_PARSER_BUFFER_MAX); 32 + this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF);
33 33
34 sprintf(cbuf_id, "%s_%s", "writer", id); 34 sprintf(cbuf_id, "%s_%s", "writer", id);
35 - this->wbuf = new(Cbuf, cbuf_id, RESPONSE_WRITER_MAX_BUF); 35 + this->wbuf = new(Cbuf, cbuf_id, WRITER_MAX_BUF);
36 36
37 this->parser = new(HttpParser, this->pbuf); 37 this->parser = new(HttpParser, this->pbuf);
38 - this->writer = new(HttpResponseWriter, this->wbuf); 38 + this->writer = new(HttpWriter, this->wbuf);
39 39
40 return 0; 40 return 0;
41 } 41 }
@@ -51,30 +51,27 @@ httpWorkerDtor(void * _this) @@ -51,30 +51,27 @@ httpWorkerDtor(void * _this)
51 delete(this->parser); 51 delete(this->parser);
52 delete(this->writer); 52 delete(this->writer);
53 53
54 - if (NULL != this->pbuf) delete(this->pbuf);  
55 - if (NULL != this->wbuf) delete(this->wbuf); 54 + delete(this->pbuf); //!< cloned workers have NULL, so delete won't do anything
  55 + delete(this->wbuf); //!< cloned workers have NULL, so delete won't do anything
56 } 56 }
57 57
58 static 58 static
59 void 59 void
60 -_clone(void * _this, void * _base) 60 +httpWorkerClone(void * _this, void * _base)
61 { 61 {
62 HttpWorker this = _this; 62 HttpWorker this = _this;
63 HttpWorker base = _base; 63 HttpWorker base = _base;
64 64
65 - this->id = NULL;  
66 this->val = base->val; 65 this->val = base->val;
67 - this->pbuf = NULL;  
68 - this->wbuf = NULL;  
69 66
70 this->parser = new(HttpParser, base->pbuf); 67 this->parser = new(HttpParser, base->pbuf);
71 - this->writer = new(HttpResponseWriter, base->wbuf); 68 + this->writer = new(HttpWriter, base->wbuf);
72 } 69 }
73 70
74 ssize_t httpWorkerProcess(void *, int); 71 ssize_t httpWorkerProcess(void *, int);
75 ssize_t httpWorkerWrite(void *, int); 72 ssize_t httpWorkerWrite(void *, int);
76 73
77 -INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, _clone); 74 +INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone);
78 INIT_IFACE(StreamReader, httpWorkerProcess); 75 INIT_IFACE(StreamReader, httpWorkerProcess);
79 INIT_IFACE(StreamWriter, httpWorkerWrite); 76 INIT_IFACE(StreamWriter, httpWorkerWrite);
80 CREATE_CLASS( 77 CREATE_CLASS(
@@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
28 #include "http/worker.h" 28 #include "http/worker.h"
29 #include "http/message.h" 29 #include "http/message.h"
30 #include "http/request.h" 30 #include "http/request.h"
  31 +#include "http/response.h"
31 #include "http/message/queue.h" 32 #include "http/message/queue.h"
32 #include "http/parser.h" 33 #include "http/parser.h"
33 34
@@ -45,7 +46,7 @@ httpWorkerProcess(HttpWorker this, int fd) @@ -45,7 +46,7 @@ httpWorkerProcess(HttpWorker this, int fd)
45 if (0 < (size = httpParserParse(this->parser, fd))) { 46 if (0 < (size = httpParserParse(this->parser, fd))) {
46 int i; 47 int i;
47 HttpMessageQueue reqq = this->parser->queue; 48 HttpMessageQueue reqq = this->parser->queue;
48 - HttpMessageQueue respq = this->writer->response_queue; 49 + HttpMessageQueue respq = this->writer->queue;
49 50
50 for (i=0; i<reqq->nmsgs; i++) { 51 for (i=0; i<reqq->nmsgs; i++) {
51 HttpRequest request = (HttpRequest)(reqq->msgs[i]); 52 HttpRequest request = (HttpRequest)(reqq->msgs[i]);
@@ -23,12 +23,12 @@ @@ -23,12 +23,12 @@
23 #include <sys/types.h> 23 #include <sys/types.h>
24 24
25 #include "http/worker.h" 25 #include "http/worker.h"
26 -#include "http/response/writer.h" 26 +#include "http/writer.h"
27 27
28 ssize_t 28 ssize_t
29 httpWorkerWrite(HttpWorker this, int fd) 29 httpWorkerWrite(HttpWorker this, int fd)
30 { 30 {
31 - return httpResponseWriterWrite(this->writer, fd); 31 + return httpWriterWrite(this->writer, fd);
32 } 32 }
33 33
34 // vim: set ts=4 sw=4: 34 // vim: set ts=4 sw=4:
@@ -27,37 +27,37 @@ @@ -27,37 +27,37 @@
27 #include "interface/stream_writer.h" 27 #include "interface/stream_writer.h"
28 28
29 #include "http/message/queue.h" 29 #include "http/message/queue.h"
30 -#include "http/response/writer.h" 30 +#include "http/writer.h"
31 31
32 static 32 static
33 int 33 int
34 -responseWriterCtor(void * _this, va_list * params) 34 +httpWriterCtor(void * _this, va_list * params)
35 { 35 {
36 - HttpResponseWriter this = _this; 36 + HttpWriter this = _this;
37 37
38 - this->buffer = va_arg(*params, Cbuf);  
39 - this->response_queue = new(HttpMessageQueue); 38 + this->buffer = va_arg(*params, Cbuf);
  39 + this->queue = new(HttpMessageQueue);
40 40
41 return 0; 41 return 0;
42 } 42 }
43 43
44 static 44 static
45 void 45 void
46 -responseWriterDtor(void * _this) 46 +httpWriterDtor(void * _this)
47 { 47 {
48 - HttpResponseWriter this = _this; 48 + HttpWriter this = _this;
49 49
50 - delete(this->response_queue); 50 + delete(this->queue);
51 51
52 if (TRUE == this->ourLock) 52 if (TRUE == this->ourLock)
53 cbufRelease(this->buffer); 53 cbufRelease(this->buffer);
54 54
55 - if (NULL != this->cur_response)  
56 - delete(this->cur_response); 55 + if (NULL != this->current)
  56 + delete(this->current);
57 } 57 }
58 58
59 -INIT_IFACE(Class, responseWriterCtor, responseWriterDtor, NULL);  
60 -INIT_IFACE(StreamWriter, (fptr_streamWriterWrite)httpResponseWriterWrite);  
61 -CREATE_CLASS(HttpResponseWriter, NULL, IFACE(Class), IFACE(StreamWriter)); 59 +INIT_IFACE(Class, httpWriterCtor, httpWriterDtor, NULL);
  60 +INIT_IFACE(StreamWriter, httpWriterWrite);
  61 +CREATE_CLASS(HttpWriter, NULL, IFACE(Class), IFACE(StreamWriter));
62 62
63 // vim: set ts=4 sw=4: 63 // vim: set ts=4 sw=4:
@@ -26,21 +26,18 @@ @@ -26,21 +26,18 @@
26 #include "class.h" 26 #include "class.h"
27 #include "interface/class.h" 27 #include "interface/class.h"
28 #include "http/message.h" 28 #include "http/message.h"
29 -#include "http/response.h"  
30 -#include "http/response/writer.h" 29 +#include "http/writer.h"
31 #include "cbuf.h" 30 #include "cbuf.h"
32 31
33 #define MIN(x,y) ((x) < (y) ? (x) : (y)) 32 #define MIN(x,y) ((x) < (y) ? (x) : (y))
34 #define MAX(x,y) ((x) > (y) ? (x) : (y)) 33 #define MAX(x,y) ((x) > (y) ? (x) : (y))
35 -#define _PSIZE(x) (MAX((x),RESPONSE_WRITER_MAX_BUF))  
36 -#define PSIZE _PSIZE(this->nheader+message->nbody)  
37 34
38 ssize_t 35 ssize_t
39 -httpResponseWriterWrite(HttpResponseWriter this, int fd) 36 +httpWriterWrite(void * _this, int fd)
40 { 37 {
41 - HttpMessageQueue respq = this->response_queue;  
42 - HttpMessage message = (HttpMessage)this->cur_response;  
43 - int cont = 1; 38 + HttpWriter this = _this;
  39 + HttpMessageQueue respq = this->queue;
  40 + int cont = 1;
44 41
45 if (cbufIsLocked(this->buffer)) { 42 if (cbufIsLocked(this->buffer)) {
46 if (FALSE == this->ourLock) 43 if (FALSE == this->ourLock)
@@ -53,19 +50,20 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -53,19 +50,20 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
53 50
54 while (cont) { 51 while (cont) {
55 switch (this->state) { 52 switch (this->state) {
56 - case HTTP_RESPONSE_GET:  
57 - if (NULL == this->cur_response && 0 < respq->nmsgs) {  
58 - message = respq->msgs[0];  
59 - this->cur_response = (HttpResponse)message; 53 + case HTTP_WRITER_GET:
  54 + if (NULL == this->current && 0 < respq->nmsgs) {
  55 + this->current = respq->msgs[0];
60 56
61 this->written = 0; 57 this->written = 0;
62 this->nbody = 0; 58 this->nbody = 0;
63 - this->nheader = httpMessageHeaderSizeGet(message); 59 + this->nheader = httpMessageHeaderSizeGet(this->current);
64 60
65 - httpMessageHeaderToString(message, cbufGetWrite(this->buffer)); 61 + httpMessageHeaderToString(
  62 + this->current,
  63 + cbufGetWrite(this->buffer));
66 cbufIncWrite(this->buffer, this->nheader); 64 cbufIncWrite(this->buffer, this->nheader);
67 65
68 - this->state = HTTP_RESPONSE_WRITE; 66 + this->state = HTTP_WRITER_WRITE;
69 } 67 }
70 else { 68 else {
71 cbufRelease(this->buffer); 69 cbufRelease(this->buffer);
@@ -74,24 +72,24 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -74,24 +72,24 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
74 } 72 }
75 break; 73 break;
76 74
77 - case HTTP_RESPONSE_WRITE: 75 + case HTTP_WRITER_WRITE:
78 /** 76 /**
79 * read 77 * read
80 */ 78 */
81 - if (this->nbody < message->nbody) { 79 + if (this->nbody < this->current->nbody) {
82 size_t size = MIN( 80 size_t size = MIN(
83 - message->nbody - this->nbody, 81 + this->current->nbody - this->nbody,
84 cbufGetFree(this->buffer)); 82 cbufGetFree(this->buffer));
85 83
86 - switch (message->type) { 84 + switch (this->current->type) {
87 case HTTP_MESSAGE_BUFFERED: 85 case HTTP_MESSAGE_BUFFERED:
88 cbufSetData(this->buffer, 86 cbufSetData(this->buffer,
89 - message->body + this->nbody, 87 + this->current->body + this->nbody,
90 size); 88 size);
91 break; 89 break;
92 90
93 case HTTP_MESSAGE_PIPED: 91 case HTTP_MESSAGE_PIPED:
94 - size = cbufRead(this->buffer, message->handle); 92 + size = cbufRead(this->buffer, this->current->handle);
95 break; 93 break;
96 94
97 default: 95 default:
@@ -115,42 +113,41 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd) @@ -115,42 +113,41 @@ httpResponseWriterWrite(HttpResponseWriter this, int fd)
115 } 113 }
116 } 114 }
117 115
118 - if (this->written == message->nbody + this->nheader) {  
119 - this->state = HTTP_RESPONSE_DONE; 116 + if (this->written == this->current->nbody + this->nheader) {
  117 + this->state = HTTP_WRITER_DONE;
120 } 118 }
121 else { 119 else {
122 cont = 0; 120 cont = 0;
123 } 121 }
124 break; 122 break;
125 123
126 - case HTTP_RESPONSE_DONE:  
127 - if (HTTP_MESSAGE_PIPED == message->type) {  
128 - close(message->handle); 124 + case HTTP_WRITER_DONE:
  125 + if (HTTP_MESSAGE_PIPED == this->current->type) {
  126 + close(this->current->handle);
129 } 127 }
130 128
131 - this->state = HTTP_RESPONSE_GET; 129 + this->state = HTTP_WRITER_GET;
132 130
133 memmove(respq->msgs, 131 memmove(respq->msgs,
134 &(respq->msgs[1]), 132 &(respq->msgs[1]),
135 sizeof(void*) * (--respq->nmsgs + 1)); 133 sizeof(void*) * (--respq->nmsgs + 1));
136 134
137 - if (! httpMessageHasKeepAlive(message)) { 135 + cbufRelease(this->buffer);
  136 + this->ourLock = FALSE;
  137 +
  138 + if (! httpMessageHasKeepAlive(this->current)) {
138 /** 139 /**
139 * if the message did not have the keep-alive feature 140 * if the message did not have the keep-alive feature
140 * we don't care about further pipelined messages and 141 * we don't care about further pipelined messages and
141 * return to the caller with a -1 indicating that the 142 * return to the caller with a -1 indicating that the
142 - * underlying connection should be closed. 143 + * underlying connection should be closed at their side.
  144 + * Then we close to connection.
143 */ 145 */
144 - cbufRelease(this->buffer);  
145 - this->ourLock = FALSE;  
146 - delete(this->cur_response); 146 + delete(this->current);
147 return -1; 147 return -1;
148 } 148 }
149 149
150 - cbufRelease(this->buffer);  
151 - this->ourLock = FALSE;  
152 - delete(this->cur_response);  
153 - 150 + delete(this->current);
154 break; 151 break;
155 } 152 }
156 } 153 }
@@ -51,7 +51,7 @@ serverPoll(Server this) { @@ -51,7 +51,7 @@ serverPoll(Server this) {
51 51
52 if (fda < fdb) { 52 if (fda < fdb) {
53 memcpy(fda, fdb, sizeof(struct pollfd)); 53 memcpy(fda, fdb, sizeof(struct pollfd));
54 - memset(fdb, 0, sizeof(struct pollfd)); 54 + //memset(fdb, 0, sizeof(struct pollfd)); // this might be unneccessary
55 fdb--; 55 fdb--;
56 this->nfds--; 56 this->nfds--;
57 } 57 }
@@ -24,6 +24,8 @@ @@ -24,6 +24,8 @@
24 #include "interface/logger.h" 24 #include "interface/logger.h"
25 #include "interface/stream_reader.h" 25 #include "interface/stream_reader.h"
26 26
  27 +void serverCloseConn(Server, unsigned int);
  28 +
27 ssize_t 29 ssize_t
28 serverRead(Server this, unsigned int i) 30 serverRead(Server this, unsigned int i)
29 { 31 {
@@ -57,9 +59,14 @@ serverRead(Server this, unsigned int i) @@ -57,9 +59,14 @@ serverRead(Server this, unsigned int i)
57 "connection[%d] closed...%s", 59 "connection[%d] closed...%s",
58 fd, 60 fd,
59 inet_ntoa((((this->conns)[fd].sock)->addr).sin_addr)); 61 inet_ntoa((((this->conns)[fd].sock)->addr).sin_addr));
  62 + serverCloseConn(this, i);
  63 + break;
  64 +
  65 + case 0:
60 break; 66 break;
61 67
62 default: 68 default:
  69 + (this->fds)[i].events |= POLLOUT;
63 break; 70 break;
64 } 71 }
65 72
@@ -21,112 +21,66 @@ @@ -21,112 +21,66 @@
21 */ 21 */
22 22
23 #include "server.h" 23 #include "server.h"
24 -#include "interface/stream_writer.h"  
25 #include "interface/logger.h" 24 #include "interface/logger.h"
26 25
27 #include "utils/signalHandling.h" 26 #include "utils/signalHandling.h"
28 27
29 -#undef MAX  
30 -#define MAX(x,y) ((x) > (y) ? (x) : (y))  
31 -  
32 int serverPoll(Server); 28 int serverPoll(Server);
33 int serverHandleAccept(Server); 29 int serverHandleAccept(Server);
34 void serverCloseConn(Server, unsigned int); 30 void serverCloseConn(Server, unsigned int);
35 ssize_t serverRead(Server, unsigned int); 31 ssize_t serverRead(Server, unsigned int);
  32 +ssize_t serverWrite(Server, unsigned int);
36 33
37 void 34 void
38 serverRun(Server this) 35 serverRun(Server this)
39 { 36 {
40 loggerLog(this->logger, LOGGER_INFO, "service started"); 37 loggerLog(this->logger, LOGGER_INFO, "service started");
41 38
42 - /**  
43 - * \todo actually this is the main loop of my server. When  
44 - * stuff becomes more complicated it might be feasabible to  
45 - * split stuff into separate processes. This will definetly  
46 - * involve some IPC and syncing. Right now as this is actually  
47 - * only a simple HTTP server implementation we go on with  
48 - * this single process.  
49 - * What we can first do to get some processing between read/write  
50 - * cicles is to use the poll timeout.  
51 - * A first candidate for a separate process would be the  
52 - * generation of the responses piped responses then still need  
53 - * to open the filehandle in this process and reading and  
54 - * writing would be done here. So the benefit might not be  
55 - * very big. Otherwise we could share the read and write  
56 - * ringbuffer as well as the message queues. Then the child  
57 - * process can do the file readings, but this would involve  
58 - * some more IPC.  
59 - */  
60 while (!doShutdown) //! until error or signal 39 while (!doShutdown) //! until error or signal
61 { 40 {
62 int events; 41 int events;
63 unsigned int i; 42 unsigned int i;
  43 + int naccs = 10;
64 44
65 events = serverPoll(this); 45 events = serverPoll(this);
66 - if (doShutdown) break; 46 + if (doShutdown || 0 >= events) break;
  47 +
  48 + /**
  49 + * handle accept
  50 + */
  51 + if (0 != ((this->fds)[0].revents & POLLIN)) {
  52 + events--;
  53 + while(-1 != serverHandleAccept(this) && 0 < naccs) {
  54 + naccs--;
  55 + }
  56 + }
67 57
68 - for (i=0; i < this->nfds; i++) { 58 + for (i=1; i < this->nfds; i++) {
69 int fd = (this->fds)[i].fd; 59 int fd = (this->fds)[i].fd;
70 - int naccs = 10, nreads = 10, nwrites = 10;  
71 -  
72 - if (0 >= events) break; 60 + int nreads = 10, nwrites = 10;
73 61
  62 + /**
  63 + * handle reads
  64 + */
74 if (0 != ((this->fds)[i].revents & POLLIN) && 0 < nreads) { 65 if (0 != ((this->fds)[i].revents & POLLIN) && 0 < nreads) {
75 events--; 66 events--;
  67 + nreads--;
76 68
77 - /**  
78 - * handle accept  
79 - */  
80 - if (this->sock->handle == (this->fds)[i].fd) {  
81 - while(-1 != serverHandleAccept(this) && 0 < naccs) {  
82 - naccs--;  
83 - }  
84 - }  
85 -  
86 - /**  
87 - * handle reads  
88 - */  
89 - else {  
90 - nreads--;  
91 -  
92 - switch (serverRead(this, i)) {  
93 - case 0:  
94 - break;  
95 -  
96 - case -1:  
97 - case -2:  
98 - serverCloseConn(this, i);  
99 - break;  
100 -  
101 - default:  
102 - (this->fds)[i].events |= POLLOUT;  
103 - }  
104 - } 69 + serverRead(this, i);
105 } 70 }
106 71
107 /** 72 /**
108 * handle writes 73 * handle writes
109 */ 74 */
110 if (0 != ((this->fds)[i].revents & POLLOUT) && 0 < nwrites) { 75 if (0 != ((this->fds)[i].revents & POLLOUT) && 0 < nwrites) {
111 - size_t remaining;  
112 -  
113 events--; 76 events--;
114 nwrites--; 77 nwrites--;
115 78
116 - remaining = streamWriterWrite((this->conns)[fd].worker, fd);  
117 - switch(remaining) {  
118 - case -1:  
119 - serverCloseConn(this, i);  
120 - break;  
121 -  
122 - case 0:  
123 - (this->fds)[i].events &= ~POLLOUT;  
124 - break;  
125 -  
126 - default:  
127 - break;  
128 - } 79 + serverWrite(this, i);
129 } 80 }
  81 +
  82 + if (0 > events)
  83 + break; // no more events to handle
130 } 84 }
131 } 85 }
132 } 86 }
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright (C) 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include "server.h"
  24 +#include "interface/logger.h"
  25 +#include "interface/stream_writer.h"
  26 +
  27 +void serverCloseConn(Server, unsigned int);
  28 +
  29 +ssize_t
  30 +serverWrite(Server this, unsigned int i)
  31 +{
  32 + int fd = (this->fds)[i].fd;
  33 + ssize_t remaining;
  34 +
  35 + if (NULL == (this->conns)[fd].worker) {
  36 + loggerLog(
  37 + this->logger,
  38 + LOGGER_INFO,
  39 + "initialization error: NULL reader");
  40 + return -1;
  41 + }
  42 +
  43 + remaining = streamWriterWrite((this->conns)[fd].worker, fd);
  44 + switch(remaining) {
  45 + case -1:
  46 + serverCloseConn(this, i);
  47 + break;
  48 +
  49 + case 0:
  50 + (this->fds)[i].events &= ~POLLOUT;
  51 + break;
  52 +
  53 + default:
  54 + break;
  55 + }
  56 +
  57 + return remaining;
  58 +}
  59 +
  60 +// vim: set ts=4 sw=4:
@@ -40,7 +40,7 @@ socketCtor(void * _this, va_list * params) @@ -40,7 +40,7 @@ socketCtor(void * _this, va_list * params)
40 this->port = va_arg(* params, int); 40 this->port = va_arg(* params, int);
41 41
42 //! Create socket for incoming connections 42 //! Create socket for incoming connections
43 - if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { 43 + if (-1 == (this->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) {
44 loggerLog(this->log, LOGGER_CRIT, 44 loggerLog(this->log, LOGGER_CRIT,
45 "error opening socket: %s - service terminated", 45 "error opening socket: %s - service terminated",
46 strerror(errno)); 46 strerror(errno));
@@ -35,7 +35,11 @@ socketConnect(Sock this, const char * addr) @@ -35,7 +35,11 @@ socketConnect(Sock this, const char * addr)
35 (this->addr).sin_family = AF_INET; // Internet address family 35 (this->addr).sin_family = AF_INET; // Internet address family
36 (this->addr).sin_port = htons(this->port); // Local port 36 (this->addr).sin_port = htons(this->port); // Local port
37 37
38 - if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) { 38 + if (-1 == connect(
  39 + this->handle,
  40 + (struct sockaddr*) &(this->addr),
  41 + sizeof(this->addr)))
  42 + {
39 loggerLog(this->log, LOGGER_CRIT, 43 loggerLog(this->log, LOGGER_CRIT,
40 "error connection socket: %s - service terminated", 44 "error connection socket: %s - service terminated",
41 strerror(errno)); 45 strerror(errno));
@@ -132,7 +132,7 @@ main() @@ -132,7 +132,7 @@ main()
132 close(shm); 132 close(shm);
133 133
134 logger = new(LoggerSyslog, LOGGER_ERR); 134 logger = new(LoggerSyslog, LOGGER_ERR);
135 - worker = new(HttpWorker, "my", value); 135 + worker = new(HttpWorker, "testserver", value);
136 server = new(Server, logger, worker, 11212, SOMAXCONN); 136 server = new(Server, logger, worker, 11212, SOMAXCONN);
137 137
138 //daemonize(); 138 //daemonize();
Please register or login to post a comment