diff --git a/src/core/iocalls.c b/src/core/iocalls.c index 3bd790f..3ae03fb 100644 --- a/src/core/iocalls.c +++ b/src/core/iocalls.c @@ -139,12 +139,13 @@ static void * internal_add_timer(H3270 *session, unsigned long interval_ms, int t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t)); t_new->proc = proc; - t_new->session = session; t_new->in_play = False; #if defined(_WIN32) + ms_ts(&t_new->ts); t_new->ts += interval_ms; + #else gettimeofday(&t_new->tv, NULL); @@ -156,10 +157,11 @@ static void * internal_add_timer(H3270 *session, unsigned long interval_ms, int t_new->tv.tv_sec += t_new->tv.tv_usec / MILLION; t_new->tv.tv_usec %= MILLION; } + #endif /*]*/ /* Find where to insert this item. */ - for (t = session->timeouts; t != TN; t = t->next) + for (t = (timeout_t *) session->timeouts.first; t != TN; t = t->next) { #if defined(_WIN32) if (t->ts > t_new->ts) @@ -173,17 +175,20 @@ static void * internal_add_timer(H3270 *session, unsigned long interval_ms, int // Insert it. if (prev == TN) - { // Front. - t_new->next = session->timeouts; - session->timeouts = t_new; + { + // t_new is Front. + t_new->next = (timeout_t *) session->timeouts.first; + session->timeouts.first = t_new; } else if (t == TN) - { // Rear. + { + // t_new is Rear. t_new->next = TN; prev->next = t_new; + session->timeouts.last = (timeout_t *) t_new; } else - { // Middle. + { // t_new is Middle. t_new->next = t; prev->next = t_new; } @@ -196,10 +201,16 @@ static void * internal_add_timer(H3270 *session, unsigned long interval_ms, int static void internal_remove_timer(H3270 *session, void * timer) { timeout_t *st = (timeout_t *)timer; + + trace("Removing timeout: %p",st); + + if(!st->in_play) + lib3270_linked_list_delete_node(&session->timeouts,timer); + + /* timeout_t *t; timeout_t *prev = TN; - trace("Removing timeout: %p",st); if (st->in_play) return; @@ -217,6 +228,7 @@ static void internal_remove_timer(H3270 *session, void * timer) } prev = t; } + */ } /* I/O events. */ @@ -225,7 +237,6 @@ static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, vo { input_t *ip = (input_t *) lib3270_linked_list_append_node(&session->input.list,sizeof(input_t), userdata); - ip->session = session; ip->enabled = 1; ip->fd = fd; ip->flag = flag; diff --git a/src/core/linux/event_dispatcher.c b/src/core/linux/event_dispatcher.c index d998e1f..01ff8c0 100644 --- a/src/core/linux/event_dispatcher.c +++ b/src/core/linux/event_dispatcher.c @@ -104,11 +104,11 @@ retry: if (block) { - if (hSession->timeouts != TN) + if (hSession->timeouts.first) { (void) gettimeofday(&now, (void *)NULL); - twait.tv_sec = hSession->timeouts->tv.tv_sec - now.tv_sec; - twait.tv_usec = hSession->timeouts->tv.tv_usec - now.tv_usec; + twait.tv_sec = ((timeout_t *) hSession->timeouts.first)->tv.tv_sec - now.tv_sec; + twait.tv_usec = ((timeout_t *) hSession->timeouts.first)->tv.tv_usec - now.tv_usec; if (twait.tv_usec < 0L) { twait.tv_sec--; twait.tv_usec += MILLION; @@ -151,7 +151,7 @@ retry: { if((ip->flag & LIB3270_IO_FLAG_READ) && FD_ISSET(ip->fd, &rfds)) { - (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_READ,ip->userdata); + (*ip->call)(hSession,ip->fd,LIB3270_IO_FLAG_READ,ip->userdata); processed_any = True; if (hSession->input.changed) goto retry; @@ -159,7 +159,7 @@ retry: if((ip->flag & LIB3270_IO_FLAG_WRITE) && FD_ISSET(ip->fd, &wfds)) { - (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_WRITE,ip->userdata); + (*ip->call)(hSession,ip->fd,LIB3270_IO_FLAG_WRITE,ip->userdata); processed_any = True; if (hSession->input.changed) goto retry; @@ -167,7 +167,7 @@ retry: if((ip->flag & LIB3270_IO_FLAG_EXCEPTION) && FD_ISSET(ip->fd, &xfds)) { - (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_EXCEPTION,ip->userdata); + (*ip->call)(hSession,ip->fd,LIB3270_IO_FLAG_EXCEPTION,ip->userdata); processed_any = True; if (hSession->input.changed) goto retry; @@ -176,23 +176,51 @@ retry: } // See what's expired. - if (hSession->timeouts != TN) + if (hSession->timeouts.first) { struct timeout *t; (void) gettimeofday(&now, (void *)NULL); - while ((t = hSession->timeouts) != TN) + while(hSession->timeouts.first) + { + t = (struct timeout *) hSession->timeouts.first; + + if (t->tv.tv_sec < now.tv_sec ||(t->tv.tv_sec == now.tv_sec && t->tv.tv_usec < now.tv_usec)) + { + t->in_play = True; + (*t->proc)(hSession); + processed_any = True; + + lib3270_linked_list_delete_node(&hSession->timeouts,t); + + } + else + { + break; + } + + } + + /* + + while ((t = ((timeout_t *) hSession->timeouts.first)) != TN) { if (t->tv.tv_sec < now.tv_sec ||(t->tv.tv_sec == now.tv_sec && t->tv.tv_usec < now.tv_usec)) { - hSession->timeouts = t->next; t->in_play = True; (*t->proc)(t->session); processed_any = True; - lib3270_free(t); - } else + + lib3270_linked_list_delete_node(&hSession->timeouts,t); + + } + else + { break; + } } + + */ } if (hSession->input.changed) diff --git a/src/core/session.c b/src/core/session.c index 917580d..a46a0aa 100644 --- a/src/core/session.c +++ b/src/core/session.c @@ -133,13 +133,7 @@ void lib3270_session_free(H3270 *h) release_pointer(h->tabs); // Release timeouts - while(h->timeouts) - { - timeout_t *t = h->timeouts; - h->timeouts = t->next; - - lib3270_free(t); - } + lib3270_linked_list_free(&h->timeouts); // Release inputs; lib3270_linked_list_free(&h->input.list); diff --git a/src/include/lib3270-internals.h b/src/include/lib3270-internals.h index 705e831..714c066 100644 --- a/src/include/lib3270-internals.h +++ b/src/include/lib3270-internals.h @@ -287,7 +287,9 @@ struct lib3270_text */ typedef struct timeout { - struct timeout *next; + LIB3270_LINKED_LIST_HEAD; + + unsigned char in_play; #if defined(_WIN32) /*[*/ unsigned long long ts; @@ -297,9 +299,6 @@ typedef struct timeout int (*proc)(H3270 *session); - H3270 *session; - - unsigned char in_play; } timeout_t; @@ -313,7 +312,6 @@ typedef struct _input_t LIB3270_LINKED_LIST_HEAD; unsigned char enabled; - H3270 * session; int fd; LIB3270_IO_FLAG flag; @@ -684,7 +682,7 @@ struct _h3270 } ssl; #endif // HAVE_LIBSSL - timeout_t * timeouts; + struct lib3270_linked_list_head timeouts; struct { -- libgit2 0.21.2