Mercurial > pidgin.yaz
annotate libfaim/aim_snac.c @ 833:8d8ef8c3afe2
[gaim-migrate @ 843]
handle getting disconnected from chat more gracefully
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 02 Sep 2000 10:57:07 +0000 |
parents | 72e556f6b99d |
children | 88f8f98de02d |
rev | line source |
---|---|
2 | 1 |
2 /* | |
3 * | |
4 * Various SNAC-related dodads... | |
5 * | |
6 * outstanding_snacs is a list of aim_snac_t structs. A SNAC should be added | |
7 * whenever a new SNAC is sent and it should remain in the list until the | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
8 * response for it has been receieved. |
2 | 9 * |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
10 * cleansnacs() should be called periodically by the client in order |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
11 * to facilitate the aging out of unreplied-to SNACs. This can and does |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
12 * happen, so it should be handled. |
2 | 13 * |
14 */ | |
15 | |
283
0f14e6d8a51b
[gaim-migrate @ 293]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
237
diff
changeset
|
16 #include <faim/aim.h> |
2 | 17 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
18 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
19 * Called from aim_session_init() to initialize the hash. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
20 */ |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
21 void aim_initsnachash(struct aim_session_t *sess) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
22 { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
23 int i; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
24 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
26 sess->snac_hash[i] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
27 faim_mutex_init(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
28 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
29 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
30 return; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
31 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
32 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
33 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
34 * Clones the passed snac structure and caches it in the |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
35 * list/hash. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
36 */ |
237 | 37 u_long aim_newsnac(struct aim_session_t *sess, |
38 struct aim_snac_t *newsnac) | |
39 { | |
40 struct aim_snac_t *snac = NULL, *cur = NULL; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
41 int index; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
42 |
237 | 43 if (!newsnac) |
44 return 0; | |
45 | |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
46 if (!(snac = calloc(1, sizeof(struct aim_snac_t)))) |
237 | 47 return 0; |
48 memcpy(snac, newsnac, sizeof(struct aim_snac_t)); | |
49 snac->issuetime = time(&snac->issuetime); | |
50 snac->next = NULL; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
51 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
52 index = snac->id % FAIM_SNAC_HASH_SIZE; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
53 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
54 faim_mutex_lock(&sess->snac_hash_locks[index]); |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
55 snac->next = sess->snac_hash[index]; |
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
56 sess->snac_hash[index] = snac; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
57 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
237 | 58 |
59 return(snac->id); | |
2 | 60 } |
61 | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
62 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
63 * Finds a snac structure with the passed SNAC ID, |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
64 * removes it from the list/hash, and returns a pointer to it. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
65 * |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
66 * The returned structure must be freed by the caller. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
67 * |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
68 */ |
237 | 69 struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, |
70 u_long id) | |
71 { | |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
72 struct aim_snac_t *cur = NULL; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
73 int index; |
237 | 74 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
75 index = id % FAIM_SNAC_HASH_SIZE; |
237 | 76 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
77 faim_mutex_lock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
78 if (!sess->snac_hash[index]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
79 ; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
80 else if (!sess->snac_hash[index]->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
81 if (sess->snac_hash[index]->id == id) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
82 cur = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
83 sess->snac_hash[index] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
84 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
85 } else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
86 cur = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
87 while (cur->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
88 if (cur->next->id == id) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
89 struct aim_snac_t *tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
90 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
91 tmp = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
92 cur->next = cur->next->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
93 cur = tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
94 break; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
95 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
96 cur = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
97 } |
237 | 98 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
99 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
100 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
101 return cur; |
2 | 102 } |
103 | |
104 /* | |
105 * This is for cleaning up old SNACs that either don't get replies or | |
106 * a reply was never received for. Garabage collection. Plain and simple. | |
107 * | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
108 * maxage is the _minimum_ age in seconds to keep SNACs. |
2 | 109 * |
110 */ | |
237 | 111 int aim_cleansnacs(struct aim_session_t *sess, |
112 int maxage) | |
2 | 113 { |
237 | 114 struct aim_snac_t *cur; |
2 | 115 struct aim_snac_t *remed = NULL; |
116 time_t curtime; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
117 int i; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
118 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
119 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
120 faim_mutex_lock(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
121 if (!sess->snac_hash[i]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
122 ; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
123 else if (!sess->snac_hash[i]->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
124 if ((sess->snac_hash[i]->issuetime + maxage) >= curtime) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
125 remed = sess->snac_hash[i]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
126 if(remed->data) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
127 free(remed->data); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
128 free(remed); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
129 sess->snac_hash[i] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
130 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
131 } else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
132 cur = sess->snac_hash[i]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
133 while(cur && cur->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
134 if ((cur->next->issuetime + maxage) >= curtime) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
135 remed = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
136 cur->next = cur->next->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
137 if (remed->data) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
138 free(remed->data); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
139 free(remed); |
2 | 140 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
141 cur = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
142 } |
2 | 143 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
144 faim_mutex_unlock(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
145 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
146 |
2 | 147 return 0; |
148 } | |
149 | |
150 int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) | |
151 { | |
152 int curbyte = 0; | |
237 | 153 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); |
154 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); | |
155 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); | |
156 curbyte += aimutil_put32(buf+curbyte, snacid); | |
2 | 157 return curbyte; |
158 } |