Mercurial > pidgin
annotate plugins/icq/timeout.c @ 1529:e2f256502345
[gaim-migrate @ 1539]
no more socklen_t
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 03 Mar 2001 12:56:25 +0000 |
parents | ba8e6e211af5 |
children | 8ed70631ed15 |
rev | line source |
---|---|
1498 | 1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | |
3 #include "timeout.h" | |
4 | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
5 icq_Timeout *icq_CurrentTimeout = NULL; |
1498 | 6 list *icq_TimeoutList = NULL; |
7 | |
8 void (*icq_SetTimeout)(long length); | |
9 | |
10 int icq_TimeoutCompare(icq_Timeout *t1, icq_Timeout *t2) | |
11 { | |
12 return (t1->expire_time - t2->expire_time); | |
13 } | |
14 | |
15 icq_Timeout *icq_TimeoutNew(int length, icq_TimeoutHandler handler, | |
16 void *data) | |
17 { | |
18 icq_Timeout *t = (icq_Timeout *)malloc(sizeof(icq_Timeout)); | |
19 | |
20 if (t) | |
21 { | |
22 int count = icq_TimeoutList->count; | |
23 | |
24 t->length = length; | |
25 t->handler = handler; | |
26 t->data = data; | |
27 t->expire_time = time(NULL) + length; | |
28 t->single_shot = 1; | |
29 | |
30 list_insert_sorted(icq_TimeoutList, t); | |
31 | |
32 if (count == 0) | |
33 icq_TimeoutDoNotify(); | |
34 } | |
35 | |
36 return t; | |
37 } | |
38 | |
39 void icq_TimeoutDelete(icq_Timeout *timeout) | |
40 { | |
41 list_remove(icq_TimeoutList, timeout); | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
42 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
43 /* if this was the timeout we were currently waiting on, move on |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
44 * to the next */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
45 if (icq_CurrentTimeout = timeout) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
46 { |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
47 icq_CurrentTimeout = NULL; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
48 icq_TimeoutDoNotify(); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
49 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
50 |
1498 | 51 free(timeout); |
52 } | |
53 | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
54 int _icq_HandleTimeout1(void *p, va_list data) |
1498 | 55 { |
56 icq_Timeout *t = p; | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
57 int complete = 0; |
1498 | 58 time_t current_time = va_arg(data, time_t); |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
59 list *expired_timeouts = va_arg(data, list *); |
1498 | 60 |
61 if (t->expire_time <= current_time) | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
62 list_enqueue(expired_timeouts, t); |
1498 | 63 else |
64 /* traversal is complete when we reach an expire time in the future */ | |
65 complete = 1; | |
66 | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
67 return complete; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
68 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
69 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
70 int _icq_HandleTimeout2(void *p, va_list data) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
71 { |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
72 icq_Timeout *t = p; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
73 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
74 /* maybe a previously executed timeout caused us to be deleted, so |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
75 * make sure we're still around */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
76 if (list_find(icq_TimeoutList, t)) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
77 (t->handler)(t->data); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
78 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
79 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
80 int _icq_HandleTimeout3(void *p, va_list data) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
81 { |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
82 icq_Timeout *t = p; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
83 int complete = 0; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
84 time_t current_time = va_arg(data, time_t); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
85 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
86 if (t->expire_time <= current_time) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
87 { |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
88 if (t->single_shot) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
89 icq_TimeoutDelete(t); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
90 else |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
91 t->expire_time = current_time + t->length; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
92 } |
1498 | 93 else |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
94 /* traversal is complete when we reach an expire time in the future */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
95 complete = 1; |
1498 | 96 |
97 return complete; | |
98 } | |
99 | |
100 void icq_HandleTimeout() | |
101 { | |
102 time_t current_time = time(NULL); | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
103 list *expired_timeouts = list_new(); |
1498 | 104 |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
105 icq_CurrentTimeout = NULL; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
106 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
107 /* these three operations must be split up, in the case where a |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
108 * timeout function causes timers to be deleted - this ensures |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
109 * we don't try to free any timers that have already been removed |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
110 * or corrupt the list traversal process */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
111 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
112 /* determine which timeouts that have expired */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
113 list_traverse(icq_TimeoutList, _icq_HandleTimeout1, current_time, |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
114 expired_timeouts); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
115 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
116 /* call handler function for expired timeouts */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
117 list_traverse(expired_timeouts, _icq_HandleTimeout2); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
118 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
119 /* delete any expired timeouts */ |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
120 list_traverse(icq_TimeoutList, _icq_HandleTimeout3, current_time); |
1498 | 121 |
122 if (icq_TimeoutList->count) | |
123 icq_TimeoutDoNotify(); | |
124 } | |
125 | |
126 void icq_TimeoutDoNotify() | |
127 { | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
128 time_t length, current_time = time(NULL); |
1498 | 129 |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
130 if (!icq_TimeoutList->count) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
131 { |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
132 if (icq_SetTimeout) |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
133 (*icq_SetTimeout)(0); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
134 return; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
135 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
136 |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
137 icq_CurrentTimeout = (icq_Timeout *)list_first(icq_TimeoutList); |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
138 length = icq_CurrentTimeout->expire_time - current_time; |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
139 |
1498 | 140 if (icq_SetTimeout) |
141 (*icq_SetTimeout)(length); | |
142 } |