Commit 19724f0d11d899521dcbbb4800af928796785228
1 parent
0aae8658
split server implementation for readability
Showing
4 changed files
with
106 additions
and
96 deletions
src/server/handle_accept.c
0 → 100644
| 1 | +static | |
| 2 | +void | |
| 3 | +serverHandleAccept(Server this) | |
| 4 | +{ | |
| 5 | + if (0 != ((this->fds)[0].revents & POLLIN)) { | |
| 6 | + char remoteAddr[16] = ""; | |
| 7 | + Sock acc; | |
| 8 | + | |
| 9 | + acc = socketAccept(this->sock, remoteAddr); | |
| 10 | + | |
| 11 | + if (-1 != acc->handle) { | |
| 12 | + (this->conns)[this->nfds].sock = acc; // save the socket handle | |
| 13 | + (this->conns)[this->nfds].reader = clone(this->reader); // clone reader | |
| 14 | + (this->fds)[this->nfds].fd = acc->handle; | |
| 15 | + (this->fds)[this->nfds].events = POLLIN; | |
| 16 | + this->nfds++; | |
| 17 | + } else { | |
| 18 | + delete(&acc); | |
| 19 | + } | |
| 20 | + | |
| 21 | + (this->fds)[0].revents |= POLLIN; | |
| 22 | + } | |
| 23 | +} | |
| 24 | + | |
| 25 | +// vim: set ts=4 sw=4: | ... | ... |
src/server/poll.c
0 → 100644
| 1 | +static | |
| 2 | +int | |
| 3 | +serverPoll(Server this) { | |
| 4 | + int events; | |
| 5 | + | |
| 6 | + /* | |
| 7 | + * wait for handles to become ready | |
| 8 | + */ | |
| 9 | + if (-1 == (events = poll(this->fds, this->nfds, -1))) { | |
| 10 | + switch (errno) { | |
| 11 | + default: | |
| 12 | + case EBADF: | |
| 13 | + case EINVAL: | |
| 14 | + case ENOMEM: | |
| 15 | + doShutdown = 1; | |
| 16 | + /* Fallthrough */ | |
| 17 | + | |
| 18 | + case EINTR: | |
| 19 | + loggerLog(this->logger, LOGGER_CRIT, | |
| 20 | + "poll systemcall failed: [%s] - service terminated", | |
| 21 | + strerror(errno)); | |
| 22 | + //exit(EXIT_FAILURE); /* @TODO do real shutdown here */ | |
| 23 | + } | |
| 24 | + } | |
| 25 | + | |
| 26 | + return events; | |
| 27 | +} | |
| 28 | + | |
| 29 | +// vim: set ts=4 sw=4: | ... | ... |
src/server/read.c
0 → 100644
| 1 | +static | |
| 2 | +int | |
| 3 | +serverRead(Server this) | |
| 4 | +{ | |
| 5 | + unsigned int i; | |
| 6 | + | |
| 7 | + for (i=1; i<this->nfds; i++) { | |
| 8 | + if (0 != ((this->fds)[i].revents & POLLIN)) { | |
| 9 | + if (NULL == (this->conns)[i].reader) { | |
| 10 | + loggerLog( | |
| 11 | + this->logger, | |
| 12 | + LOGGER_INFO, | |
| 13 | + "initialization error: NULL reader"); | |
| 14 | + serverCloseConn(this, i); | |
| 15 | + } | |
| 16 | + | |
| 17 | + switch (streamReaderRead((this->conns)[i].reader, (this->fds)[i].fd)) { | |
| 18 | + case 0: | |
| 19 | + /* | |
| 20 | + * normal close: write remaining data | |
| 21 | + * @TODO: actually we have no remaining data here.... | |
| 22 | + */ | |
| 23 | + /* DROP-THROUGH */ | |
| 24 | + | |
| 25 | + case -1: | |
| 26 | + /* | |
| 27 | + * read failure / close connection | |
| 28 | + */ | |
| 29 | + loggerLog(this->logger, LOGGER_INFO, "connection closed..."); | |
| 30 | + serverCloseConn(this, i); | |
| 31 | + break; | |
| 32 | + | |
| 33 | + default: | |
| 34 | + break; | |
| 35 | + } | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + return 0; | |
| 40 | +} | |
| 41 | + | |
| 42 | +// vim: set ts=4 sw=4: | ... | ... |
| 1 | -#include <poll.h> /* for select system call and related */ | |
| 1 | +#include <poll.h> /* for poll system call and related */ | |
| 2 | 2 | #include <string.h> /* for memset and stuff */ |
| 3 | 3 | #include <stdlib.h> /* for exit */ |
| 4 | 4 | #include <errno.h> /* for errno */ |
| ... | ... | @@ -14,98 +14,9 @@ |
| 14 | 14 | #undef MAX |
| 15 | 15 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 16 | 16 | |
| 17 | -static | |
| 18 | -int | |
| 19 | -serverPoll(Server this) { | |
| 20 | - int events; | |
| 21 | - | |
| 22 | - /* | |
| 23 | - * wait for handles to become ready | |
| 24 | - */ | |
| 25 | - if (-1 == (events = poll(this->fds, this->nfds, -1))) { | |
| 26 | - switch (errno) { | |
| 27 | - default: | |
| 28 | - case EBADF: | |
| 29 | - case EINVAL: | |
| 30 | - case ENOMEM: | |
| 31 | - doShutdown = 1; | |
| 32 | - /* Fallthrough */ | |
| 33 | - | |
| 34 | - case EINTR: | |
| 35 | - loggerLog(this->logger, LOGGER_CRIT, | |
| 36 | - "poll systemcall failed: [%s] - service terminated", | |
| 37 | - strerror(errno)); | |
| 38 | - //exit(EXIT_FAILURE); /* @TODO do real shutdown here */ | |
| 39 | - } | |
| 40 | - } | |
| 41 | - | |
| 42 | - return events; | |
| 43 | -} | |
| 44 | - | |
| 45 | -static | |
| 46 | -void | |
| 47 | -serverHandleAccept(Server this) | |
| 48 | -{ | |
| 49 | - if (0 != ((this->fds)[0].revents & POLLIN)) { | |
| 50 | - char remoteAddr[16] = ""; | |
| 51 | - Sock acc; | |
| 52 | - | |
| 53 | - acc = socketAccept(this->sock, remoteAddr); | |
| 54 | - | |
| 55 | - if (-1 != acc->handle) { | |
| 56 | - (this->conns)[this->nfds].sock = acc; // save the socket handle | |
| 57 | - (this->conns)[this->nfds].reader = clone(this->reader); // clone reader | |
| 58 | - (this->fds)[this->nfds].fd = acc->handle; | |
| 59 | - (this->fds)[this->nfds].events = POLLIN; | |
| 60 | - this->nfds++; | |
| 61 | - } else { | |
| 62 | - delete(&acc); | |
| 63 | - } | |
| 64 | - | |
| 65 | - (this->fds)[0].revents |= POLLIN; | |
| 66 | - } | |
| 67 | -} | |
| 68 | - | |
| 69 | -static | |
| 70 | -int | |
| 71 | -serverRead(Server this) | |
| 72 | -{ | |
| 73 | - unsigned int i; | |
| 74 | - | |
| 75 | - for (i=1; i<this->nfds; i++) { | |
| 76 | - if (0 != ((this->fds)[i].revents & POLLIN)) { | |
| 77 | - if (NULL == (this->conns)[i].reader) { | |
| 78 | - loggerLog( | |
| 79 | - this->logger, | |
| 80 | - LOGGER_INFO, | |
| 81 | - "initialization error: NULL reader"); | |
| 82 | - serverCloseConn(this, i); | |
| 83 | - } | |
| 84 | - | |
| 85 | - switch (streamReaderRead((this->conns)[i].reader, (this->fds)[i].fd)) { | |
| 86 | - case 0: | |
| 87 | - /* | |
| 88 | - * normal close: write remaining data | |
| 89 | - * @TODO: actually we have no remaining data here.... | |
| 90 | - */ | |
| 91 | - /* DROP-THROUGH */ | |
| 92 | - | |
| 93 | - case -1: | |
| 94 | - /* | |
| 95 | - * read failure / close connection | |
| 96 | - */ | |
| 97 | - loggerLog(this->logger, LOGGER_INFO, "connection closed..."); | |
| 98 | - serverCloseConn(this, i); | |
| 99 | - break; | |
| 100 | - | |
| 101 | - default: | |
| 102 | - break; | |
| 103 | - } | |
| 104 | - } | |
| 105 | - } | |
| 106 | - | |
| 107 | - return 0; | |
| 108 | -} | |
| 17 | +#include "poll.c" | |
| 18 | +#include "handle_accept.c" | |
| 19 | +#include "read.c" | |
| 109 | 20 | |
| 110 | 21 | void |
| 111 | 22 | serverRun(Server this) |
| ... | ... | @@ -118,19 +29,22 @@ serverRun(Server this) |
| 118 | 29 | while (!doShutdown) /* until error or signal */ |
| 119 | 30 | { |
| 120 | 31 | int events; |
| 121 | - /* | |
| 32 | + | |
| 33 | + /** | |
| 122 | 34 | * @TODO take return value of poll into account with |
| 123 | 35 | * further handling! |
| 124 | 36 | */ |
| 125 | 37 | events = serverPoll(this); |
| 126 | 38 | if (doShutdown) break; |
| 127 | 39 | |
| 128 | - /* | |
| 40 | + /** | |
| 129 | 41 | * handle accept |
| 130 | 42 | */ |
| 131 | 43 | serverHandleAccept(this); |
| 132 | 44 | |
| 133 | - /* handle reads */ | |
| 45 | + /** | |
| 46 | + * handle reads | |
| 47 | + */ | |
| 134 | 48 | serverRead(this); |
| 135 | 49 | } |
| 136 | 50 | } | ... | ... |
Please
register
or
login
to post a comment