Commit 5b3fed549faa460d333f375948db7b5635a74c58
1 parent
58eb487e
refs #19: Added a stream class. This is a delegate that uses the correct read an…
…d write methods for the given stream type. This is implemented as prerequesite for the SSL implemented
Showing
35 changed files
with
243 additions
and
99 deletions
... | ... | @@ -20,6 +20,8 @@ AM_PROG_CC_C_O |
20 | 20 | # # Checks for libraries. |
21 | 21 | # AC_CHECK_LIB([json], [json_object_new_object], [], |
22 | 22 | # [AC_MSG_ERROR([json-c library not found], [1])]) |
23 | +AC_CHECK_LIB([ssl], [SSL_library_init], [], | |
24 | + [AC_MSG_ERROR([openssl not found], [1])]) | |
23 | 25 | |
24 | 26 | # Checks for header files. |
25 | 27 | AC_CHECK_HEADERS([stdarg.h string.h stdlib.h stdio.h unistd.h syslog.h sys/types.h json/json.h]) | ... | ... |
... | ... | @@ -35,22 +35,18 @@ |
35 | 35 | #include <sys/types.h> |
36 | 36 | |
37 | 37 | #include "class.h" |
38 | +#include "stream.h" | |
38 | 39 | |
39 | -#define ECBUFOVFL 100 | |
40 | +#include "commons.h" | |
40 | 41 | |
41 | -#ifndef TRUE | |
42 | -#define TRUE ((void *)1) | |
43 | -#endif | |
42 | +#define ECBUFOVFL 100 | |
44 | 43 | |
45 | -#ifndef FALSE | |
46 | -#define FALSE ((void *)0) | |
47 | -#endif | |
48 | 44 | |
49 | 45 | CLASS(Cbuf) { |
50 | 46 | char * shm_name; // shared memory identifier |
51 | 47 | |
52 | 48 | char * data; |
53 | - void * lock; | |
49 | + Bool lock; | |
54 | 50 | |
55 | 51 | size_t bsize; |
56 | 52 | size_t bused; |
... | ... | @@ -59,8 +55,8 @@ CLASS(Cbuf) { |
59 | 55 | size_t read; |
60 | 56 | }; |
61 | 57 | |
62 | -ssize_t cbufRead(Cbuf, int fd); | |
63 | -ssize_t cbufWrite(Cbuf, int fd); | |
58 | +ssize_t cbufRead(Cbuf, Stream); | |
59 | +ssize_t cbufWrite(Cbuf, Stream); | |
64 | 60 | |
65 | 61 | char * cbufGetLine(Cbuf, char **); |
66 | 62 | char * cbufGetData(Cbuf, size_t); |
... | ... | @@ -76,7 +72,7 @@ void cbufIncWrite(Cbuf this, size_t n); |
76 | 72 | size_t cbufGetFree(Cbuf this); |
77 | 73 | char cbufIsEmpty(Cbuf this); |
78 | 74 | void cbufSkipNonAlpha(Cbuf this); |
79 | -void * cbufIsLocked(Cbuf this); | |
75 | +Bool cbufIsLocked(Cbuf this); | |
80 | 76 | void cbufLock(Cbuf this); |
81 | 77 | void cbufRelease(Cbuf this); |
82 | 78 | ... | ... |
include/commons.h
0 → 100644
... | ... | @@ -26,6 +26,7 @@ |
26 | 26 | |
27 | 27 | #include "class.h" |
28 | 28 | #include "http/header.h" |
29 | +#include "stream.h" | |
29 | 30 | |
30 | 31 | typedef enum e_HttpMessageType { |
31 | 32 | HTTP_MESSAGE_BUFFERED=0, |
... | ... | @@ -39,8 +40,8 @@ CLASS(HttpMessage) { |
39 | 40 | HttpHeader header; |
40 | 41 | |
41 | 42 | HttpMessageType type; |
42 | - int handle; | |
43 | - char * body; | |
43 | + Stream handle; | |
44 | + char * body; | |
44 | 45 | int nbody; |
45 | 46 | int dbody; |
46 | 47 | }; | ... | ... |
... | ... | @@ -28,14 +28,9 @@ |
28 | 28 | #include "http/message.h" |
29 | 29 | #include "http/message/queue.h" |
30 | 30 | #include "cbuf.h" |
31 | +#include "stream.h" | |
31 | 32 | |
32 | -#ifndef TRUE | |
33 | -#define TRUE ((void *)1) | |
34 | -#endif | |
35 | - | |
36 | -#ifndef FALSE | |
37 | -#define FALSE ((void *)0) | |
38 | -#endif | |
33 | +#include "commons.h" | |
39 | 34 | |
40 | 35 | #define PARSER_MAX_BUF 131072 |
41 | 36 | |
... | ... | @@ -51,7 +46,7 @@ typedef enum e_HttpMessageState { |
51 | 46 | |
52 | 47 | CLASS(HttpParser) { |
53 | 48 | Cbuf buffer; |
54 | - void * ourLock; | |
49 | + Bool ourLock; | |
55 | 50 | |
56 | 51 | char * incomplete; |
57 | 52 | size_t isize; |
... | ... | @@ -62,7 +57,7 @@ CLASS(HttpParser) { |
62 | 57 | HttpMessageState state; |
63 | 58 | }; |
64 | 59 | |
65 | -ssize_t httpParserParse(void *, int); | |
60 | +ssize_t httpParserParse(void *, Stream); | |
66 | 61 | void httpParserHeader(HttpParser, const char *, const char *); |
67 | 62 | void httpParserNewMessage(HttpParser, const char *, const char * lend); |
68 | 63 | size_t httpParserBody(HttpParser, const char *, size_t); | ... | ... |
... | ... | @@ -30,14 +30,10 @@ |
30 | 30 | #include "http/message.h" |
31 | 31 | #include "http/message/queue.h" |
32 | 32 | #include "cbuf.h" |
33 | +#include "stream.h" | |
33 | 34 | |
34 | -#ifndef TRUE | |
35 | -#define TRUE ((void *)1) | |
36 | -#endif | |
35 | +#include "commons.h" | |
37 | 36 | |
38 | -#ifndef FALSE | |
39 | -#define FALSE ((void *)0) | |
40 | -#endif | |
41 | 37 | |
42 | 38 | #define WRITER_MAX_BUF 131072 |
43 | 39 | |
... | ... | @@ -50,7 +46,7 @@ typedef enum e_HttpWriterState { |
50 | 46 | |
51 | 47 | CLASS(HttpWriter) { |
52 | 48 | Cbuf buffer; |
53 | - void * ourLock; | |
49 | + Bool ourLock; | |
54 | 50 | |
55 | 51 | HttpMessageQueue queue; |
56 | 52 | HttpMessage current; |
... | ... | @@ -62,7 +58,7 @@ CLASS(HttpWriter) { |
62 | 58 | HttpWriterState state; |
63 | 59 | }; |
64 | 60 | |
65 | -ssize_t httpWriterWrite(void *, int); | |
61 | +ssize_t httpWriterWrite(void *, Stream); | |
66 | 62 | |
67 | 63 | #endif // __HTTP_WRITER_H__ |
68 | 64 | ... | ... |
... | ... | @@ -27,7 +27,9 @@ |
27 | 27 | |
28 | 28 | #include <sys/types.h> |
29 | 29 | |
30 | -typedef ssize_t (* fptr_streamReaderRead)(void *, int fd); | |
30 | +#include "stream.h" | |
31 | + | |
32 | +typedef ssize_t (* fptr_streamReaderRead)(void *, Stream); | |
31 | 33 | |
32 | 34 | extern const struct interface i_StreamReader; |
33 | 35 | |
... | ... | @@ -36,7 +38,7 @@ struct i_StreamReader { |
36 | 38 | fptr_streamReaderRead read; |
37 | 39 | }; |
38 | 40 | |
39 | -extern ssize_t streamReaderRead(void *, int fd); | |
41 | +extern ssize_t streamReaderRead(void *, Stream); | |
40 | 42 | |
41 | 43 | #endif // __STREAM_READER_H__ |
42 | 44 | ... | ... |
... | ... | @@ -27,7 +27,9 @@ |
27 | 27 | |
28 | 28 | #include <sys/types.h> |
29 | 29 | |
30 | -typedef ssize_t (* fptr_streamWriterWrite)(void *, int fd); | |
30 | +#include "stream.h" | |
31 | + | |
32 | +typedef ssize_t (* fptr_streamWriterWrite)(void *, Stream); | |
31 | 33 | |
32 | 34 | extern const struct interface i_StreamWriter; |
33 | 35 | |
... | ... | @@ -36,7 +38,7 @@ struct i_StreamWriter { |
36 | 38 | fptr_streamWriterWrite write; |
37 | 39 | }; |
38 | 40 | |
39 | -extern ssize_t streamWriterWrite(void *, int fd); | |
41 | +extern ssize_t streamWriterWrite(void *, Stream); | |
40 | 42 | |
41 | 43 | #endif // __STREAM_WRITER_H__ |
42 | 44 | ... | ... |
include/stream.h
0 → 100644
1 | +#ifndef __STREAM_H__ | |
2 | +#define __STREAM_H__ | |
3 | + | |
4 | +#include <sys/types.h> | |
5 | +#include <openssl/ssl.h> | |
6 | + | |
7 | +#include "class.h" | |
8 | + | |
9 | +typedef enum e_StreamHandleType { | |
10 | + STREAM_FD = 0, | |
11 | + STREAM_SSL | |
12 | +} StreamHandleType; | |
13 | + | |
14 | +CLASS(Stream) { | |
15 | + StreamHandleType type; | |
16 | + union { | |
17 | + int fd; | |
18 | + SSL * ssl; | |
19 | + } handle; | |
20 | +}; | |
21 | + | |
22 | +ssize_t streamRead(Stream, void *, size_t); | |
23 | +ssize_t streamWrite(Stream, void *, size_t); | |
24 | + | |
25 | +#endif // __STREAM_H__ | |
26 | + | |
27 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -5,6 +5,7 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ |
5 | 5 | interface/stream_writer.c interface/http_intro.c \ |
6 | 6 | interface/subject.c interface/observer.c interface.c |
7 | 7 | SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c |
8 | +STREAM = stream.c stream/read.c stream/write.c | |
8 | 9 | SERVER = server.c server/run.c server/close_conn.c server/poll.c \ |
9 | 10 | server/handle_accept.c server/read.c server/write.c |
10 | 11 | LOGGER = logger.c logger/stderr.c logger/syslog.c |
... | ... | @@ -60,6 +61,6 @@ bin_PROGRAMS = webgameserver |
60 | 61 | webgameserver_SOURCES = webgameserver.c \ |
61 | 62 | $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ |
62 | 63 | $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ |
63 | - $(UTILS) $(MSGQ) $(SESSION) | |
64 | + $(UTILS) $(MSGQ) $(SESSION) $(STREAM) | |
64 | 65 | webgameserver_CFLAGS = -Wall -I ../include/ |
65 | -webgameserver_LDFLAGS = -lrt | |
66 | +webgameserver_LDFLAGS = -lrt -lssl | ... | ... |
... | ... | @@ -25,10 +25,11 @@ |
25 | 25 | #include <errno.h> |
26 | 26 | |
27 | 27 | #include "cbuf.h" |
28 | +#include "stream.h" | |
28 | 29 | |
29 | 30 | |
30 | 31 | ssize_t |
31 | -cbufRead(Cbuf this, int fd) | |
32 | +cbufRead(Cbuf this, Stream st) | |
32 | 33 | { |
33 | 34 | ssize_t rrsize = 0; |
34 | 35 | size_t rsize = cbufGetFree(this); |
... | ... | @@ -38,7 +39,7 @@ cbufRead(Cbuf this, int fd) |
38 | 39 | return -1; |
39 | 40 | } |
40 | 41 | |
41 | - rrsize = read(fd, cbufGetWrite(this), rsize); | |
42 | + rrsize = streamRead(st, cbufGetWrite(this), rsize); | |
42 | 43 | |
43 | 44 | switch (rrsize) { |
44 | 45 | case 0: | ... | ... |
... | ... | @@ -24,17 +24,18 @@ |
24 | 24 | #include <unistd.h> |
25 | 25 | |
26 | 26 | #include "cbuf.h" |
27 | +#include "stream.h" | |
27 | 28 | |
28 | 29 | |
29 | 30 | ssize_t |
30 | -cbufWrite(Cbuf this, int fd) | |
31 | +cbufWrite(Cbuf this, Stream st) | |
31 | 32 | { |
32 | 33 | ssize_t wwsize = 0; |
33 | 34 | size_t wsize = this->bused; |
34 | 35 | |
35 | 36 | if (0 == wsize) return 0; |
36 | 37 | |
37 | - wwsize = write(fd, cbufGetRead(this), wsize); | |
38 | + wwsize = streamWrite(st, cbufGetRead(this), wsize); | |
38 | 39 | |
39 | 40 | switch (wwsize) { |
40 | 41 | case -1: | ... | ... |
... | ... | @@ -79,7 +79,10 @@ httpMessageDtor(void * _this) |
79 | 79 | break; |
80 | 80 | |
81 | 81 | case HTTP_MESSAGE_PIPED: |
82 | - if (2 < this->handle) close(this->handle); | |
82 | + if (2 < (this->handle->handle).fd) { | |
83 | + close((this->handle->handle).fd); | |
84 | + } | |
85 | + delete(this->handle); | |
83 | 86 | break; |
84 | 87 | |
85 | 88 | default: | ... | ... |
... | ... | @@ -26,9 +26,10 @@ |
26 | 26 | #include "interface/class.h" |
27 | 27 | #include "interface/http_intro.h" |
28 | 28 | #include "cbuf.h" |
29 | +#include "stream.h" | |
29 | 30 | |
30 | 31 | ssize_t |
31 | -httpParserParse(void * _this, int fd) | |
32 | +httpParserParse(void * _this, Stream st) | |
32 | 33 | { |
33 | 34 | HttpParser this = _this; |
34 | 35 | int cont = 1; |
... | ... | @@ -51,7 +52,7 @@ httpParserParse(void * _this, int fd) |
51 | 52 | this->incomplete = NULL; |
52 | 53 | } |
53 | 54 | |
54 | - if (0 > (read = cbufRead(this->buffer, fd))) { | |
55 | + if (0 > (read = cbufRead(this->buffer, st))) { | |
55 | 56 | return read; |
56 | 57 | } |
57 | 58 | ... | ... |
... | ... | @@ -28,6 +28,7 @@ |
28 | 28 | |
29 | 29 | #include "class.h" |
30 | 30 | #include "interface/class.h" |
31 | +#include "stream.h" | |
31 | 32 | |
32 | 33 | #include "http/response.h" |
33 | 34 | #include "http/message.h" |
... | ... | @@ -69,7 +70,7 @@ httpResponseAsset( |
69 | 70 | message = (HttpMessage)response; |
70 | 71 | |
71 | 72 | message->type = HTTP_MESSAGE_PIPED; |
72 | - message->handle = handle; | |
73 | + message->handle = new(Stream, STREAM_FD, handle); | |
73 | 74 | message->nbody = st.st_size; |
74 | 75 | |
75 | 76 | httpHeaderAdd(&(message->header), | ... | ... |
... | ... | @@ -29,6 +29,7 @@ |
29 | 29 | #include <search.h> |
30 | 30 | |
31 | 31 | #include "class.h" |
32 | +#include "stream.h" | |
32 | 33 | #include "http/worker.h" |
33 | 34 | #include "http/parser.h" |
34 | 35 | #include "http/writer.h" |
... | ... | @@ -107,8 +108,8 @@ httpWorkerClone(void * _this, void * _base) |
107 | 108 | this->sroot = &(base->session); |
108 | 109 | } |
109 | 110 | |
110 | -ssize_t httpWorkerProcess(void *, int); | |
111 | -ssize_t httpWorkerWrite(void *, int); | |
111 | +ssize_t httpWorkerProcess(void *, Stream); | |
112 | +ssize_t httpWorkerWrite(void *, Stream); | |
112 | 113 | |
113 | 114 | INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone); |
114 | 115 | INIT_IFACE(StreamReader, httpWorkerProcess); | ... | ... |
... | ... | @@ -35,6 +35,7 @@ |
35 | 35 | #include "http/message/queue.h" |
36 | 36 | #include "http/parser.h" |
37 | 37 | #include "session.h" |
38 | +#include "stream.h" | |
38 | 39 | |
39 | 40 | #include "utils/memory.h" |
40 | 41 | |
... | ... | @@ -42,14 +43,14 @@ HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); |
42 | 43 | void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); |
43 | 44 | |
44 | 45 | ssize_t |
45 | -httpWorkerProcess(HttpWorker this, int fd) | |
46 | +httpWorkerProcess(HttpWorker this, Stream st) | |
46 | 47 | { |
47 | 48 | time_t t; |
48 | 49 | struct tm * tmp; |
49 | 50 | char buffer[200]; |
50 | 51 | ssize_t size; |
51 | 52 | |
52 | - if (0 < (size = httpParserParse(this->parser, fd))) { | |
53 | + if (0 < (size = httpParserParse(this->parser, st))) { | |
53 | 54 | int i; |
54 | 55 | HttpMessageQueue reqq = this->parser->queue; |
55 | 56 | HttpMessageQueue respq = this->writer->queue; |
... | ... | @@ -92,7 +93,6 @@ httpWorkerProcess(HttpWorker this, int fd) |
92 | 93 | if (0 == strcmp("POST", request->method)) { |
93 | 94 | if (0 == strcmp("/me/", request->uri)) { |
94 | 95 | char * delim = memchr(rmessage->body, '=', rmessage->nbody); |
95 | - char * key = rmessage->body; | |
96 | 96 | char * val; |
97 | 97 | size_t nkey, nval; |
98 | 98 | char buffer[200]; | ... | ... |
... | ... | @@ -24,11 +24,12 @@ |
24 | 24 | |
25 | 25 | #include "http/worker.h" |
26 | 26 | #include "http/writer.h" |
27 | +#include "stream.h" | |
27 | 28 | |
28 | 29 | ssize_t |
29 | -httpWorkerWrite(HttpWorker this, int fd) | |
30 | +httpWorkerWrite(HttpWorker this, Stream st) | |
30 | 31 | { |
31 | - return httpWriterWrite(this->writer, fd); | |
32 | + return httpWriterWrite(this->writer, st); | |
32 | 33 | } |
33 | 34 | |
34 | 35 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -28,12 +28,13 @@ |
28 | 28 | #include "http/message.h" |
29 | 29 | #include "http/writer.h" |
30 | 30 | #include "cbuf.h" |
31 | +#include "stream.h" | |
31 | 32 | |
32 | 33 | #define MIN(x,y) ((x) < (y) ? (x) : (y)) |
33 | 34 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) |
34 | 35 | |
35 | 36 | ssize_t |
36 | -httpWriterWrite(void * _this, int fd) | |
37 | +httpWriterWrite(void * _this, Stream st) | |
37 | 38 | { |
38 | 39 | HttpWriter this = _this; |
39 | 40 | HttpMessageQueue respq = this->queue; |
... | ... | @@ -103,7 +104,7 @@ httpWriterWrite(void * _this, int fd) |
103 | 104 | * write |
104 | 105 | */ |
105 | 106 | { |
106 | - ssize_t written = cbufWrite(this->buffer, fd); | |
107 | + ssize_t written = cbufWrite(this->buffer, st); | |
107 | 108 | |
108 | 109 | if (0 <= written) { |
109 | 110 | this->written += written; | ... | ... |
... | ... | @@ -22,6 +22,7 @@ |
22 | 22 | |
23 | 23 | #include "class.h" |
24 | 24 | #include "interface/stream_reader.h" |
25 | +#include "stream.h" | |
25 | 26 | |
26 | 27 | const struct interface i_StreamReader = { |
27 | 28 | "streamReader", |
... | ... | @@ -29,11 +30,11 @@ const struct interface i_StreamReader = { |
29 | 30 | }; |
30 | 31 | |
31 | 32 | ssize_t |
32 | -streamReaderRead(void * object, int fd) | |
33 | +streamReaderRead(void * object, Stream st) | |
33 | 34 | { |
34 | 35 | ssize_t ret; |
35 | 36 | |
36 | - RETCALL(object, StreamReader, read, ret, fd); | |
37 | + RETCALL(object, StreamReader, read, ret, st); | |
37 | 38 | |
38 | 39 | return ret; |
39 | 40 | } | ... | ... |
... | ... | @@ -22,6 +22,7 @@ |
22 | 22 | |
23 | 23 | #include "class.h" |
24 | 24 | #include "interface/stream_writer.h" |
25 | +#include "stream.h" | |
25 | 26 | |
26 | 27 | const struct interface i_StreamWriter = { |
27 | 28 | "streamWriter", |
... | ... | @@ -29,11 +30,11 @@ const struct interface i_StreamWriter = { |
29 | 30 | }; |
30 | 31 | |
31 | 32 | ssize_t |
32 | -streamWriterWrite(void * object, int fd) | |
33 | +streamWriterWrite(void * object, Stream st) | |
33 | 34 | { |
34 | 35 | ssize_t ret; |
35 | 36 | |
36 | - RETCALL(object, StreamWriter, write, ret, fd); | |
37 | + RETCALL(object, StreamWriter, write, ret, st); | |
37 | 38 | |
38 | 39 | return ret; |
39 | 40 | } | ... | ... |
... | ... | @@ -28,6 +28,7 @@ |
28 | 28 | #include "server.h" |
29 | 29 | #include "interface/class.h" |
30 | 30 | #include "interface/logger.h" |
31 | +#include "stream.h" | |
31 | 32 | |
32 | 33 | int |
33 | 34 | serverHandleAccept(Server this) |
... | ... | @@ -47,6 +48,7 @@ serverHandleAccept(Server this) |
47 | 48 | |
48 | 49 | // clone worker |
49 | 50 | (this->conns)[acc->handle].worker = clone(this->worker); |
51 | + (this->conns)[acc->handle].stream = new(Stream, STREAM_FD, acc->handle); | |
50 | 52 | |
51 | 53 | (this->fds)[this->nfds].fd = acc->handle; |
52 | 54 | (this->fds)[this->nfds].events = POLLIN; | ... | ... |
... | ... | @@ -40,7 +40,10 @@ serverRead(Server this, unsigned int i) |
40 | 40 | return -1; |
41 | 41 | } |
42 | 42 | |
43 | - switch ((size = streamReaderRead((this->conns)[fd].worker, fd))) { | |
43 | + switch ((size = streamReaderRead( | |
44 | + (this->conns)[fd].worker, | |
45 | + (this->conns)[fd].stream))) | |
46 | + { | |
44 | 47 | case -2: |
45 | 48 | /** |
46 | 49 | * normal close: this must be mapped to -2 within the | ... | ... |
... | ... | @@ -40,7 +40,10 @@ serverWrite(Server this, unsigned int i) |
40 | 40 | return -1; |
41 | 41 | } |
42 | 42 | |
43 | - remaining = streamWriterWrite((this->conns)[fd].worker, fd); | |
43 | + remaining = streamWriterWrite( | |
44 | + (this->conns)[fd].worker, | |
45 | + (this->conns)[fd].stream); | |
46 | + | |
44 | 47 | switch(remaining) { |
45 | 48 | case -1: |
46 | 49 | serverCloseConn(this, i); | ... | ... |
src/stream.c
0 → 100644
1 | +#include <stdarg.h> | |
2 | +#include <openssl/ssl.h> | |
3 | + | |
4 | +#include "class.h" | |
5 | +#include "interface/class.h" | |
6 | +#include "stream.h" | |
7 | + | |
8 | + | |
9 | +static | |
10 | +int | |
11 | +streamCtor(void * _this, va_list * params) | |
12 | +{ | |
13 | + Stream this = _this; | |
14 | + this->type = va_arg(* params, StreamHandleType); | |
15 | + | |
16 | + switch(this->type) { | |
17 | + case STREAM_FD: | |
18 | + (this->handle).fd = va_arg(* params, int); | |
19 | + break; | |
20 | + | |
21 | + case STREAM_SSL: | |
22 | + (this->handle).ssl = va_arg(* params, SSL*); | |
23 | + break; | |
24 | + | |
25 | + default: | |
26 | + return -1; | |
27 | + } | |
28 | + | |
29 | + return 0; | |
30 | +} | |
31 | + | |
32 | +static | |
33 | +void | |
34 | +streamDtor(void * _this) | |
35 | +{ | |
36 | +} | |
37 | + | |
38 | +INIT_IFACE(Class, streamCtor, streamDtor, NULL); | |
39 | +CREATE_CLASS(Stream, NULL, IFACE(Class)); | |
40 | + | |
41 | +// vim: set ts=4 sw=4: | ... | ... |
src/stream/read.c
0 → 100644
1 | +#include <openssl/ssl.h> | |
2 | +#include <unistd.h> | |
3 | + | |
4 | +#include "stream.h" | |
5 | + | |
6 | +ssize_t | |
7 | +streamRead(Stream this, void * buf, size_t count) | |
8 | +{ | |
9 | + ssize_t done; | |
10 | + | |
11 | + switch(this->type) { | |
12 | + case STREAM_FD: | |
13 | + done = read((this->handle).fd, buf, count); | |
14 | + break; | |
15 | + | |
16 | + case STREAM_SSL: | |
17 | + done = SSL_read((this->handle).ssl, buf, count); | |
18 | + break; | |
19 | + | |
20 | + default: | |
21 | + break; | |
22 | + } | |
23 | + | |
24 | + return done; | |
25 | +} | |
26 | + | |
27 | +// vim: set ts=4 sw=4: | ... | ... |
src/stream/write.c
0 → 100644
1 | +#include <openssl/ssl.h> | |
2 | +#include <unistd.h> | |
3 | + | |
4 | +#include "stream.h" | |
5 | + | |
6 | +ssize_t | |
7 | +streamWrite(Stream this, void * buf, size_t count) | |
8 | +{ | |
9 | + ssize_t done; | |
10 | + | |
11 | + switch(this->type) { | |
12 | + case STREAM_FD: | |
13 | + done = write((this->handle).fd, buf, count); | |
14 | + break; | |
15 | + | |
16 | + case STREAM_SSL: | |
17 | + done = SSL_write((this->handle).ssl, buf, count); | |
18 | + break; | |
19 | + | |
20 | + default: | |
21 | + break; | |
22 | + } | |
23 | + | |
24 | + return done; | |
25 | +} | |
26 | + | |
27 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -33,6 +33,7 @@ |
33 | 33 | #include <sys/param.h> |
34 | 34 | #include <sys/stat.h> |
35 | 35 | #include <sys/mman.h> |
36 | +#include <errno.h> | |
36 | 37 | |
37 | 38 | #include "server.h" |
38 | 39 | #include "logger.h" |
... | ... | @@ -133,7 +134,7 @@ main() |
133 | 134 | shm_unlink("/fooshm"); |
134 | 135 | close(shm); |
135 | 136 | |
136 | - logger = new(LoggerSyslog, LOGGER_ERR); | |
137 | + logger = new(LoggerStderr, LOGGER_DEBUG); | |
137 | 138 | worker = new(HttpWorker, "testserver", value); |
138 | 139 | server = new(Server, logger, worker, 11212, SOMAXCONN); |
139 | 140 | |
... | ... | @@ -149,21 +150,34 @@ main() |
149 | 150 | do { |
150 | 151 | pid_t w; |
151 | 152 | |
152 | - w = waitpid(pid, &status, WUNTRACED | WCONTINUED); | |
153 | + w = waitpid(pid, &status, 0); | |
153 | 154 | |
154 | - if (w == -1) { | |
155 | - perror("waitpid"); | |
156 | - exit(EXIT_FAILURE); | |
155 | + while (w == -1) { | |
156 | + switch(errno) { | |
157 | + case EINTR: w = waitpid(pid, &status, 0); | |
158 | + break; | |
159 | + case ECHILD: perror("no child"); | |
160 | + // DROP THROUGH | |
161 | + default: w = 0; | |
162 | + } | |
157 | 163 | } |
158 | 164 | |
159 | - if (WIFEXITED(status)) { | |
160 | - printf("exited, status=%d\n", WEXITSTATUS(status)); | |
161 | - } else if (WIFSIGNALED(status)) { | |
162 | - printf("killed by signal %d\n", WTERMSIG(status)); | |
163 | - } else if (WIFSTOPPED(status)) { | |
164 | - printf("stopped by signal %d\n", WSTOPSIG(status)); | |
165 | - } else if (WIFCONTINUED(status)) { | |
166 | - printf("continued\n"); | |
165 | + if (0 < w) { | |
166 | + if (WIFEXITED(status)) { | |
167 | + loggerLog(logger, LOGGER_INFO, | |
168 | + "child exited, status=%d\n", | |
169 | + WEXITSTATUS(status)); | |
170 | + } else if (WIFSIGNALED(status)) { | |
171 | + loggerLog(logger, LOGGER_INFO, | |
172 | + "killed by signal %d\n", | |
173 | + WTERMSIG(status)); | |
174 | + } else if (WIFSTOPPED(status)) { | |
175 | + loggerLog(logger, LOGGER_INFO, | |
176 | + "stopped by signal %d\n", | |
177 | + WSTOPSIG(status)); | |
178 | + } else if (WIFCONTINUED(status)) { | |
179 | + loggerLog(logger, LOGGER_INFO, "continued\n"); | |
180 | + } | |
167 | 181 | } |
168 | 182 | } while (!WIFEXITED(status) && !WIFSIGNALED(status)); |
169 | 183 | ... | ... |
Please
register
or
login
to post a comment