aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-07-12 20:54:59 +0200
committerMattias Andrée <maandree@kth.se>2016-07-12 20:54:59 +0200
commitf3bfd838f5c0106cd5e95110f711974e4e899f46 (patch)
tree6147662101f8ea8c43b3d77707152b8df13072bb
parentAdd -q and -qq (diff)
downloadcoopgammad-f3bfd838f5c0106cd5e95110f711974e4e899f46.tar.gz
coopgammad-f3bfd838f5c0106cd5e95110f711974e4e899f46.tar.bz2
coopgammad-f3bfd838f5c0106cd5e95110f711974e4e899f46.tar.xz
Implement main loop and handling of inbound connections + remove server_message (though like this was an mds server and not a stand-alone server)
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--src/coopgammad.c25
-rw-r--r--src/server.c126
-rw-r--r--src/server.h12
3 files changed, 94 insertions, 69 deletions
diff --git a/src/coopgammad.c b/src/coopgammad.c
index 410d722..7398d51 100644
--- a/src/coopgammad.c
+++ b/src/coopgammad.c
@@ -87,14 +87,6 @@ char* socketpath = NULL;
int gerror;
/**
- * Initialisation stage
- *
- * Used to keep track on what to destroy, of those things that
- * must only be destroyed if they have been initialised
- */
-int init_stage = 0;
-
-/**
* The adjustment method, -1 for automatic
*/
int method = -1;
@@ -740,14 +732,6 @@ static int initialise(int full, int preserve, int foreground, int keep_stderr, i
if (signal(SIGTERM, sig_terminate) == SIG_ERR)
goto fail;
- if (full)
- {
- /* Initialise the server */
- if (server_initialise() < 0)
- goto fail;
- init_stage++;
- }
-
/* Place in the background unless -f */
if (full && (foreground == 0))
{
@@ -860,8 +844,6 @@ static void destroy(int full)
{
size_t i;
- if (init_stage >= 1)
- server_destroy(full);
if (full && (socketfd >= 0))
{
shutdown(socketfd, SHUT_RDWR);
@@ -982,10 +964,6 @@ static size_t marshal(void* buf)
off += server_marshal(bs ? bs + off : NULL);
if (bs != NULL)
- *(int*)(bs + off) = init_stage;
- off += sizeof(int);
-
- if (bs != NULL)
*(int*)(bs + off) = method;
off += sizeof(int);
@@ -1061,9 +1039,6 @@ static size_t unmarshal(void* buf)
if (n == 0)
return 0;
- init_stage = *(int*)(bs + off);
- off += sizeof(int);
-
method = *(int*)(bs + off);
off += sizeof(int);
diff --git a/src/server.c b/src/server.c
index fc4bc5c..96300e6 100644
--- a/src/server.c
+++ b/src/server.c
@@ -51,11 +51,6 @@ size_t connections_ptr = 0;
size_t connections_used = 0;
/**
- * The server connection's message buffer
- */
-struct message server_message;
-
-/**
* The clients' connections' message buffers
*/
struct message* client_messages = NULL;
@@ -81,17 +76,6 @@ extern volatile sig_atomic_t terminate;
/**
- * Initialise the state of the connections
- *
- * @param Zero on success, -1 on error
- */
-int server_initialise(void)
-{
- return message_initialise(&server_message);
-}
-
-
-/**
* Destroy the state of the connections
*
* @param disconnect Disconnect all connections?
@@ -106,7 +90,6 @@ void server_destroy(int disconnect)
if (connections[i] >= 0)
message_destroy(client_messages + i);
free(client_messages);
- message_destroy(&server_message);
free(connections);
}
@@ -136,8 +119,6 @@ size_t server_marshal(void* buf)
memcpy(bs + off, connections, connections_used * sizeof(*connections));
off += connections_used * sizeof(*connections);
- off += message_marshal(&server_message, bs ? bs + off : NULL);
-
for (i = 0; i < connections_used; i++)
if (connections[i] >= 0)
off += message_marshal(client_messages + i, bs ? bs + off : NULL);
@@ -159,7 +140,6 @@ size_t server_unmarshal(const void* buf)
connections = NULL;
client_messages = NULL;
- memset(&server_message, 0, sizeof(server_message));
connections_ptr = *(const size_t*)(bs + off);
off += sizeof(size_t);
@@ -179,10 +159,6 @@ size_t server_unmarshal(const void* buf)
return 0;
}
- off += n = message_unmarshal(&server_message, bs + off);
- if (n == 0)
- return 0;
-
for (i = 0; i < connections_used; i++)
if (connections[i] >= 0)
{
@@ -220,30 +196,116 @@ static int update_fdset(fd_set* fds)
/**
+ * Handle event on the server socket
+ *
+ * @return 1: New connection accepted
+ * 0: Successful
+ * -1: Failure
+ */
+static int handle_server(void)
+{
+ int fd;
+
+ fd = accept(socketfd, NULL, NULL);
+ if (fd < 0)
+ switch (errno)
+ {
+ case EINTR:
+ return 0;
+ case ECONNABORTED:
+ case EINVAL:
+ terminate = 1;
+ return 0;
+ default:
+ return -1;
+ }
+
+ if (connections_ptr == connections_alloc)
+ {
+ int* new;
+ new = realloc(connections, (connections_alloc + 10) * sizeof(*connections));
+ if (new == NULL)
+ return -1;
+ connections = new;
+ connections_alloc += 10;
+ }
+
+ connections[connections_ptr++] = fd;
+ while (connections_ptr < connections_used)
+ if (connections[connections_ptr] >= 0)
+ connections_ptr++;
+ if (connections_used < connections_ptr)
+ connections_used = connections_ptr;
+
+ return 1;
+}
+
+
+/**
+ * Handle event on a connection to a client
+ *
+ * @param conn The index of the connection
+ * @return 1: New connection accepted
+ * 0: Successful
+ * -1: Failure
+ */
+static int handle_connection(size_t conn)
+{
+ /* TODO */
+ return 0;
+}
+
+
+/**
* 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);
+ fd_set fds_orig, fds_read, fds_ex;
+ int i, r, update, fdn = update_fdset(&fds_orig);
+ size_t j;
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)
+ memcpy(&fds_ex, &fds_orig, sizeof(fd_set));
+ if (select(fdn, &fds_read, NULL, &fds_ex, NULL) < 0)
{
if (errno == EINTR)
continue;
- goto fail;
+ return -1;
}
- /* TODO */
+
+ update = 0;
+ for (i = 0; i < fdn; i++)
+ if (FD_ISSET(i, &fds_read) || FD_ISSET(i, &fds_ex))
+ {
+ if (i == socketfd)
+ r = handle_server();
+ else
+ {
+ for (j = 0;; j++)
+ if (connections[j] == i)
+ break;
+ r = handle_connection(j);
+ }
+ switch (r)
+ {
+ case 0:
+ break;
+ case 1:
+ update = 1;
+ break;
+ default:
+ return -1;
+ }
+ }
+ if (update)
+ update_fdset(&fds_orig);
}
return 0;
- fail:
- return -1;
}
diff --git a/src/server.h b/src/server.h
index f490f22..31b7914 100644
--- a/src/server.h
+++ b/src/server.h
@@ -45,11 +45,6 @@ extern size_t connections_ptr;
extern size_t connections_used;
/**
- * The server connection's message buffer
- */
-extern struct message server_message;
-
-/**
* The clients' connections' message buffers
*/
extern struct message* client_messages;
@@ -57,13 +52,6 @@ extern struct message* client_messages;
/**
- * Initialise the state of the connections
- *
- * @param Zero on success, -1 on error
- */
-int server_initialise(void);
-
-/**
* Destroy the state of the connections
*
* @param disconnect Disconnect all connections?