--- libaitio/src/sock.c 2013/11/21 15:01:22 1.4.4.3 +++ libaitio/src/sock.c 2013/11/21 15:13:52 1.4.4.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: sock.c,v 1.4.4.3 2013/11/21 15:01:22 misho Exp $ +* $Id: sock.c,v 1.4.4.4 2013/11/21 15:13:52 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -46,6 +46,78 @@ SUCH DAMAGE. #include "global.h" +static void * +io_closeClient(sched_task_t *task) +{ + sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task); + sock_t *s = (sock_t*) cli->cli_parent; + int sock = (int) TASK_DATLEN(task); + + pthread_mutex_lock(&s->sock_mtx); + TAILQ_REMOVE(&s->sock_cli, cli, cli_node); + pthread_mutex_unlock(&s->sock_mtx); + + if (s->sock_type == SOCK_STREAM) { + shutdown(sock, SHUT_RDWR); + close(sock); + } + AIT_FREE_VAL(&cli->cli_buf); + + e_free(cli); + taskExit(task, NULL); +} + +static void * +io_acceptClient(sched_task_t *task) +{ + int c, rlen; + sockaddr_t sa; + socklen_t salen = sizeof sa.ss; + sock_cli_t *cli = NULL; + sock_t *s = (sock_t*) TASK_ARG(task); + + if (s->sock_type == SOCK_STREAM) { + if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) { + LOGERR; + goto end; + } + } else { + if ((rlen = recvfrom(TASK_FD(task), + AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf), + MSG_PEEK, &sa.sa, &salen)) == -1) { + LOGERR; + goto end; + } else + c = TASK_FD(task); + } + + cli = e_malloc(sizeof(sock_cli_t)); + if (!cli) { + io_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + if (s->sock_type == SOCK_STREAM) + close(c); + goto end; + } else { + memset(cli, 0, sizeof(sock_cli_t)); + pthread_mutex_lock(&s->sock_mtx); + TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node); + pthread_mutex_unlock(&s->sock_mtx); + } + + cli->cli_parent = TASK_ARG(task); + cli->cli_fd = c; + cli->cli_func = TASK_DATA(task); + memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr); + AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf)); + + schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0); + ioUpdTimerSocket(cli); +end: + schedReadSelf(task); + taskExit(task, NULL); +} + + /* * ioInitSocket() - Init socket and allocate resources * @@ -141,14 +213,22 @@ ioInitSocket(int role, int type, int proto, const char void ioCloseSocket(sock_t ** __restrict s) { - sock_cli_t *cli; + sock_cli_t *cli; if (s && *s) { pthread_mutex_lock(&(*s)->sock_mtx); while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) { TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node); - shutdown(cli->cli_fd, SHUT_RDWR); - close(cli->cli_fd); + + schedCancelby((*s)->sock_root, taskTIMER, CRITERIA_DATLEN, + (void*) cli->cli_fd, NULL); + schedCancelby((*s)->sock_root, taskMAX, CRITERIA_FD, + (void*) cli->cli_fd, NULL); + + if ((*s)->sock_type == SOCK_STREAM) { + shutdown(cli->cli_fd, SHUT_RDWR); + close(cli->cli_fd); + } AIT_FREE_VAL(&cli->cli_buf); e_free(cli); } @@ -217,77 +297,6 @@ ioUpSocket(sock_t * __restrict s, void *arg, int timeo fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK); return ret; -} - -static void * -io_closeClient(sched_task_t *task) -{ - sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task); - sock_t *s = (sock_t*) cli->cli_parent; - int sock = (int) TASK_DATLEN(task); - - if (s->sock_type == SOCK_STREAM) { - shutdown(sock, SHUT_RDWR); - close(sock); - } - AIT_FREE_VAL(&cli->cli_buf); - - pthread_mutex_lock(&s->sock_mtx); - TAILQ_REMOVE(&s->sock_cli, cli, cli_node); - pthread_mutex_unlock(&s->sock_mtx); - - e_free(cli); - taskExit(task, NULL); -} - -static void * -io_acceptClient(sched_task_t *task) -{ - int c, rlen; - sockaddr_t sa; - socklen_t salen = sizeof sa.ss; - sock_cli_t *cli = NULL; - sock_t *s = (sock_t*) TASK_ARG(task); - - if (s->sock_type == SOCK_STREAM) { - if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) { - LOGERR; - goto end; - } - } else { - if ((rlen = recvfrom(TASK_FD(task), - AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf), - MSG_PEEK, &sa.sa, &salen)) == -1) { - LOGERR; - goto end; - } else - c = TASK_FD(task); - } - - cli = e_malloc(sizeof(sock_cli_t)); - if (!cli) { - io_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); - if (s->sock_type == SOCK_STREAM) - close(c); - goto end; - } else { - memset(cli, 0, sizeof(sock_cli_t)); - pthread_mutex_lock(&s->sock_mtx); - TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node); - pthread_mutex_unlock(&s->sock_mtx); - } - - cli->cli_parent = TASK_ARG(task); - cli->cli_fd = c; - cli->cli_func = TASK_DATA(task); - memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr); - AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf)); - - schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0); - ioUpdTimerSocket(cli); -end: - schedReadSelf(task); - taskExit(task, NULL); } /*