diff options
author | Mattias Andrée <maandree@kth.se> | 2016-07-12 11:24:50 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2016-07-12 11:24:50 +0200 |
commit | a27250396644e7f603dc6de3e685b53e1671bf1f (patch) | |
tree | f3c48f6f863dece7a1562bdd2a4e5ccd4ca631fe /src | |
parent | Rename project: gammad => coopgammad (diff) | |
download | coopgammad-a27250396644e7f603dc6de3e685b53e1671bf1f.tar.gz coopgammad-a27250396644e7f603dc6de3e685b53e1671bf1f.tar.bz2 coopgammad-a27250396644e7f603dc6de3e685b53e1671bf1f.tar.xz |
Add loop that waits on sockets
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src')
-rw-r--r-- | src/coopgammad.c | 3 | ||||
-rw-r--r-- | src/server.c | 69 | ||||
-rw-r--r-- | src/server.h | 7 |
3 files changed, 79 insertions, 0 deletions
diff --git a/src/coopgammad.c b/src/coopgammad.c index 26a9d05..7af87ab 100644 --- a/src/coopgammad.c +++ b/src/coopgammad.c @@ -1271,6 +1271,9 @@ int main(int argc, char** argv) reexec = 0; /* See `if (reexec && !terminate)` */ } + if (main_loop() < 0) + goto fail; + if (reexec && !terminate) { size_t buffer_size; diff --git a/src/server.c b/src/server.c index c79f325..fc4bc5c 100644 --- a/src/server.c +++ b/src/server.c @@ -18,7 +18,10 @@ #include "server.h" #include "util.h" +#include <sys/select.h> #include <sys/socket.h> +#include <errno.h> +#include <signal.h> #include <stdlib.h> #include <string.h> @@ -57,11 +60,24 @@ struct message server_message; */ struct message* client_messages = NULL; + /** * The server socket's file descriptor */ extern int socketfd; +/** + * Has the process receive a signal + * telling it to re-execute? + */ +extern volatile sig_atomic_t reexec; + +/** + * Has the process receive a signal + * telling it to terminate? + */ +extern volatile sig_atomic_t terminate; + /** @@ -178,3 +194,56 @@ size_t server_unmarshal(const void* buf) return off; } + +/** + * Sets the file descriptor set that includes + * the server socket and all connections + * + * @param fds The file descritor set + * @return The highest set file descritor plus 1 + */ +static int update_fdset(fd_set* fds) +{ + int fdmax = socketfd; + size_t i; + FD_ZERO(fds); + FD_SET(socketfd, fds); + for (i = 0; i < connections_used; i++) + if (connections[i] >= 0) + { + FD_SET(connections[i], fds); + if (fdmax < connections[i]) + fdmax = connections[i]; + } + return fdmax + 1; +} + + +/** + * The program's main loop + * + * @return Zero on success, -1 on error + */ +int main_loop(void) +{ + fd_set fds_orig, fds_read, fds_err; + int fdn = update_fdset(&fds_orig); + + while (!reexec && !terminate) + { + memcpy(&fds_read, &fds_orig, sizeof(fd_set)); + memcpy(&fds_err, &fds_orig, sizeof(fd_set)); + if (select(fdn, &fds_read, NULL, &fds_err, NULL) < 0) + { + if (errno == EINTR) + continue; + goto fail; + } + /* TODO */ + } + + return 0; + fail: + return -1; +} + diff --git a/src/server.h b/src/server.h index 1edcf0e..f490f22 100644 --- a/src/server.h +++ b/src/server.h @@ -88,3 +88,10 @@ size_t server_marshal(void* buf); */ size_t server_unmarshal(const void* buf); +/** + * The program's main loop + * + * @return Zero on success, -1 on error + */ +int main_loop(void); + |