diff --git a/src/server/distrend.c b/src/server/distrend.c --- a/src/server/distrend.c +++ b/src/server/distrend.c @@ -93,13 +93,6 @@ int distrend_do() return 0; } /** - Accepts a client's connection - */ -void distrend_accept() -{ - -} -/** Frees the action */ void distrend_action_free() @@ -1182,10 +1175,10 @@ int main(int argc, char *argv[]) /* This is called the "main loop" */ while(cont) { - struct distren_action *action; + struct distrend_action *action; int clientsays = 0; /*< temporary example variable, will be replaced when we can handle messages */ - distrend_accept(&action); + distrend_accept(config, clients, &action); cont = distrend_do(action); /* Make the following code more event-driven */ diff --git a/src/server/distrend.h b/src/server/distrend.h --- a/src/server/distrend.h +++ b/src/server/distrend.h @@ -35,5 +35,9 @@ struct distrend_config char *datadir; }; +struct distrend_action +{ + struct distrend_client *client; +}; #endif diff --git a/src/server/listen.c b/src/server/listen.c --- a/src/server/listen.c +++ b/src/server/listen.c @@ -23,20 +23,23 @@ #include #include #include +#include #include +#include #include /* local */ -struct distrend_client -{ - int sock; - int state; -}; - struct distrend_clientset { LIST *clients; + + /* + for select() + */ + fd_set readfds; + fd_set writefds; + int nfds; }; @@ -71,6 +74,51 @@ int distrend_listen(struct distrend_conf tmp = listen(config->listens->sock, 1); + FD_ZERO(&(*clients)->readfds); + FD_ZERO(&(*clients)->writefds); + + return 0; +} + +int distrend_accept_find_client(fd_set *fdset, struct distrend_client *client) +{ + if(FD_ISSET(client->sock, fdset)) + return FALSE; + return TRUE; +} + +int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action) +{ + int tmp; + fd_set readfds; + fd_set writefds; + + struct distrend_client *cli; + + memcpy(&readfds, &clients->readfds, sizeof(fd_set)); + memcpy(&writefds, &clients->writefds, sizeof(fd_set)); + + tmp = select(clients->nfds, &readfds, &writefds, NULL, (struct timeval *)NULL); + if(tmp == -1) + { + perror("select"); + + return 1; + } + + /** + check if our traversal function found a bit that was set + */ + if(list_traverse(clients->clients, &readfds, &distrend_accept_find_client, LIST_FRNT | LIST_ALTR) == LIST_OK) + { + cli = (struct distrend_client *)list_curr(clients->clients); + fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting, which is %s\n", + __FILE__, __LINE__, + cli->sock, FD_ISSET(cli->sock, &readfds) ? "true" : "false"); + fprintf(stderr, "stub\n"); + return 1; + } + return 0; } diff --git a/src/server/listen.h b/src/server/listen.h --- a/src/server/listen.h +++ b/src/server/listen.h @@ -19,6 +19,7 @@ struct distrend_clientset; struct distrend_listen; +struct distrend_client; #ifndef _DISTREN_LISTEN_H #define _DISTREN_LISTEN_H @@ -31,6 +32,13 @@ struct distrend_listen int sock; }; +struct distrend_client +{ + int sock; + int state; +}; + + /** initializes the listens and clientset @@ -40,6 +48,15 @@ struct distrend_listen int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients); /** + checks states of the sockets I'm managing. If there are any new connections, + or activity on any sockets, I'll call the appropriate function. + I will block until there is some sort of activity, including + signals. If you want to cleanly shut down, it's best to register + signal handlers somewhere + */ +int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action); + +/** cleans listening socket. Unnecessary for a working server, currently a stub. */ int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients);