annotate plugins/icq/timeout.c @ 1522:31c0850f9340

[gaim-migrate @ 1532] this should fix a segfault or two committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Thu, 01 Mar 2001 10:05:18 +0000
parents 0ef6603d986e
children ba8e6e211af5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1498
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
2
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
3 #include "timeout.h"
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
4
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
5 list *icq_TimeoutList = NULL;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
6
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
7 void (*icq_SetTimeout)(long length);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
8
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
9 int icq_TimeoutCompare(icq_Timeout *t1, icq_Timeout *t2)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
10 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
11 return (t1->expire_time - t2->expire_time);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
12 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
13
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
14 icq_Timeout *icq_TimeoutNew(int length, icq_TimeoutHandler handler,
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
15 void *data)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
16 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
17 icq_Timeout *t = (icq_Timeout *)malloc(sizeof(icq_Timeout));
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
18
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
19 if (t)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
20 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
21 int count = icq_TimeoutList->count;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
22
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
23 t->length = length;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
24 t->handler = handler;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
25 t->data = data;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
26 t->expire_time = time(NULL) + length;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
27 t->single_shot = 1;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
28
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
29 list_insert_sorted(icq_TimeoutList, t);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
30
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
31 if (count == 0)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
32 icq_TimeoutDoNotify();
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
33 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
34
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
35 return t;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
36 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
37
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
38 void icq_TimeoutDelete(icq_Timeout *timeout)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
39 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
40 list_remove(icq_TimeoutList, timeout);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
41 free(timeout);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
42 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
43
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
44 int _icq_HandleTimeout(void *p, va_list data)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
45 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
46 icq_Timeout *t = p;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
47 time_t current_time = va_arg(data, time_t);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
48 int complete = 0;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
49
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
50 if (t->expire_time <= current_time)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
51 (*t->handler)(t->data);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
52 else
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
53 /* traversal is complete when we reach an expire time in the future */
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
54 complete = 1;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
55
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
56 if (t->single_shot)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
57 icq_TimeoutDelete(t);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
58 else
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
59 t->expire_time = current_time + t->length;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
60
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
61 return complete;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
62 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
63
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
64 void icq_HandleTimeout()
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
65 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
66 time_t current_time = time(NULL);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
67
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
68 /* call handler functions for all timeouts that have expired */
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
69 list_traverse(icq_TimeoutList, _icq_HandleTimeout, current_time);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
70
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
71 if (icq_TimeoutList->count)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
72 icq_TimeoutDoNotify();
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
73 }
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
74
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
75 void icq_TimeoutDoNotify()
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
76 {
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
77 time_t current_time = time(NULL);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
78
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
79 icq_Timeout *t = (icq_Timeout *)list_first(icq_TimeoutList);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
80 long length = t->expire_time - current_time;
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
81 if (icq_SetTimeout)
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
82 (*icq_SetTimeout)(length);
0ef6603d986e [gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
83 }