Mercurial > pidgin.yaz
annotate libfaim/aim_txqueue.c @ 641:e947cb269c9d
[gaim-migrate @ 651]
solaris is b0rken
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 08 Aug 2000 21:05:49 +0000 |
parents | e4c34ca88d9b |
children | 595ac7759563 |
rev | line source |
---|---|
2 | 1 /* |
237 | 2 * aim_txqueue.c |
3 * | |
4 * Herein lies all the mangement routines for the transmit (Tx) queue. | |
5 * | |
2 | 6 */ |
7 | |
283
0f14e6d8a51b
[gaim-migrate @ 293]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
281
diff
changeset
|
8 #include <faim/aim.h> |
237 | 9 |
10 /* | |
11 * Allocate a new tx frame. | |
12 * | |
13 * This is more for looks than anything else. | |
14 * | |
15 * Right now, that is. If/when we implement a pool of transmit | |
16 * frames, this will become the request-an-unused-frame part. | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
17 * |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
18 * framing = AIM_FRAMETYPE_OFT/OSCAR |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
19 * chan = channel for OSCAR, hdrtype for OFT |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
20 * |
237 | 21 */ |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
22 struct command_tx_struct *aim_tx_new(unsigned short framing, int chan, struct aim_conn_t *conn, int datalen) |
2 | 23 { |
237 | 24 struct command_tx_struct *new; |
2 | 25 |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
26 if (!conn) { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
27 printf("aim_tx_new: ERROR: no connection specified\n"); |
237 | 28 return NULL; |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
29 } |
237 | 30 |
31 new = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct)); | |
32 if (!new) | |
33 return NULL; | |
34 memset(new, 0, sizeof(struct command_tx_struct)); | |
2 | 35 |
237 | 36 new->conn = conn; |
37 | |
38 if(datalen) { | |
39 new->data = (u_char *)malloc(datalen); | |
40 new->commandlen = datalen; | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
41 } else |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
42 new->data = NULL; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
43 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
44 new->hdrtype = framing; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
45 if (new->hdrtype == AIM_FRAMETYPE_OSCAR) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
46 new->hdr.oscar.type = chan; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
47 } else if (new->hdrtype == AIM_FRAMETYPE_OFT) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
48 new->hdr.oft.type = chan; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
49 new->hdr.oft.hdr2len = 0; /* this will get setup by caller */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
50 } else { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
51 printf("tx_new: unknown framing\n"); |
237 | 52 } |
53 | |
54 return new; | |
55 } | |
2 | 56 |
237 | 57 /* |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
58 * aim_tx_enqeue__queuebased() |
237 | 59 * |
60 * The overall purpose here is to enqueue the passed in command struct | |
61 * into the outgoing (tx) queue. Basically... | |
62 * 1) Make a scope-irrelevent copy of the struct | |
63 * 2) Lock the struct | |
64 * 3) Mark as not-sent-yet | |
65 * 4) Enqueue the struct into the list | |
66 * 5) Unlock the struct once it's linked in | |
67 * 6) Return | |
68 * | |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
69 * Note that this is only used when doing queue-based transmitting; |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
70 * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased. |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
71 * |
237 | 72 */ |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
73 int aim_tx_enqueue__queuebased(struct aim_session_t *sess, |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
74 struct command_tx_struct *newpacket) |
237 | 75 { |
76 struct command_tx_struct *cur; | |
2 | 77 |
237 | 78 if (newpacket->conn == NULL) { |
79 faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n"); | |
80 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS); | |
81 } | |
82 | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
83 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
84 /* assign seqnum */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
85 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
86 } |
237 | 87 /* set some more fields */ |
88 newpacket->lock = 1; /* lock */ | |
89 newpacket->sent = 0; /* not sent yet */ | |
90 newpacket->next = NULL; /* always last */ | |
2 | 91 |
237 | 92 /* see overhead note in aim_rxqueue counterpart */ |
93 if (sess->queue_outgoing == NULL) { | |
94 sess->queue_outgoing = newpacket; | |
95 } else { | |
96 for (cur = sess->queue_outgoing; | |
97 cur->next; | |
98 cur = cur->next) | |
99 ; | |
100 cur->next = newpacket; | |
101 } | |
102 | |
103 newpacket->lock = 0; /* unlock so it can be sent */ | |
104 | |
105 #if debug == 2 | |
106 faimdprintf(2, "calling aim_tx_printqueue()\n"); | |
107 aim_tx_printqueue(sess); | |
108 faimdprintf(2, "back from aim_tx_printqueue()\n"); | |
2 | 109 #endif |
110 | |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
111 return 0; |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
112 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
113 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
114 /* |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
115 * aim_tx_enqueue__immediate() |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
116 * |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
117 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
118 * the whole queue mess when you want immediate writes to happen. |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
119 * |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
120 * Basically the same as its __queuebased couterpart, however |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
121 * instead of doing a list append, it just calls aim_tx_sendframe() |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
122 * right here. |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
123 * |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
124 */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
125 int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket) |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
126 { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
127 if (newpacket->conn == NULL) { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
128 faimdprintf(1, "aim_tx_enqueue: ERROR: packet has no connection\n"); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
129 if (newpacket->data) |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
130 free(newpacket->data); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
131 free(newpacket); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
132 return -1; |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
133 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
134 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
135 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
136 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn); |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
137 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
138 newpacket->lock = 1; /* lock */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
139 newpacket->sent = 0; /* not sent yet */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
140 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
141 aim_tx_sendframe(sess, newpacket); |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
142 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
143 if (newpacket->data) |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
144 free(newpacket->data); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
145 free(newpacket); |
270
cfa39d39dec6
[gaim-migrate @ 280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
237
diff
changeset
|
146 |
2 | 147 return 0; |
148 } | |
149 | |
150 /* | |
237 | 151 * aim_get_next_txseqnum() |
152 * | |
153 * This increments the tx command count, and returns the seqnum | |
154 * that should be stamped on the next FLAP packet sent. This is | |
155 * normally called during the final step of packet preparation | |
156 * before enqueuement (in aim_tx_enqueue()). | |
157 * | |
2 | 158 */ |
237 | 159 u_int aim_get_next_txseqnum(struct aim_conn_t *conn) |
2 | 160 { |
281
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
161 u_int ret; |
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
162 |
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
163 faim_mutex_lock(&conn->seqnum_lock); |
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
164 ret = ++conn->seqnum; |
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
165 faim_mutex_unlock(&conn->seqnum_lock); |
7b06ba09ffe2
[gaim-migrate @ 291]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
279
diff
changeset
|
166 return ret; |
2 | 167 } |
168 | |
169 /* | |
237 | 170 * aim_tx_printqueue() |
171 * | |
172 * This is basically for debuging purposes only. It dumps all the | |
173 * records in the tx queue and their current status. Very helpful | |
174 * if the queue isn't working quite right. | |
175 * | |
2 | 176 */ |
237 | 177 #if debug == 2 |
178 int aim_tx_printqueue(struct aim_session_t *sess) | |
2 | 179 { |
237 | 180 struct command_tx_struct *cur; |
2 | 181 |
237 | 182 faimdprintf(2, "\ncurrent aim_queue_outgoing...\n"); |
183 faimdprintf(2, "\ttype seqnum len lock sent\n"); | |
2 | 184 |
237 | 185 if (sess->queue_outgoing == NULL) |
186 faimdprintf(2, "aim_tx_flushqueue(): queue empty"); | |
187 else { | |
188 for (cur = sess->queue_outgoing; cur; cur = cur->next) { | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
189 faimdprintf(2, "\t %2x %2x %4x %4x %1d %1d\n", |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
190 cur->hdrtype, |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
191 (cur->hdrtype==AIM_FRAMETYPE_OFT)?cur->hdr.oft.type:cur->hdr.oscar.type, |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
192 (cur->hdrtype==AIM_FRAMETYPE_OSCAR)?cur->seqnum:0, |
237 | 193 cur->commandlen, cur->lock, |
194 cur->sent); | |
195 } | |
196 } | |
197 | |
198 faimdprintf(2, "\n(done printing queue)\n"); | |
2 | 199 |
200 return 0; | |
201 } | |
202 #endif | |
203 | |
204 /* | |
237 | 205 * aim_tx_flushqueue() |
206 * | |
207 * This the function is responsable for putting the queued commands | |
208 * onto the wire. This function is critical to the operation of | |
209 * the queue and therefore is the most prone to brokenness. It | |
210 * seems to be working quite well at this point. | |
211 * | |
212 * Procedure: | |
213 * 1) Traverse the list, only operate on commands that are unlocked | |
214 * and haven't been sent yet. | |
215 * 2) Lock the struct | |
216 * 3) Allocate a temporary buffer to store the finished, fully | |
217 * processed packet in. | |
218 * 4) Build the packet from the command_tx_struct data. | |
219 * 5) Write the packet to the socket. | |
220 * 6) If success, mark the packet sent, if fail report failure, do NOT | |
221 * mark the packet sent (so it will not get purged and therefore | |
222 * be attempted again on next call). | |
223 * 7) Unlock the struct. | |
224 * 8) Free the temp buffer | |
225 * 9) Step to next struct in list and go back to 1. | |
226 * | |
2 | 227 */ |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
228 int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur) |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
229 { |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
230 int buflen = 0; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
231 unsigned char *curPacket; |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
232 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
233 if (!cur) |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
234 return -1; /* fatal */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
235 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
236 cur->lock = 1; /* lock the struct */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
237 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
238 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
239 buflen = cur->commandlen + 6; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
240 else if (cur->hdrtype == AIM_FRAMETYPE_OFT) |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
241 buflen = cur->hdr.oft.hdr2len + 8; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
242 else { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
243 cur->lock = 0; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
244 return -1; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
245 } |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
246 |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
247 /* allocate full-packet buffer */ |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
248 if (!(curPacket = (unsigned char *) malloc(buflen))) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
249 cur->lock = 0; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
250 return -1; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
251 } |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
252 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
253 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
254 /* command byte */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
255 curPacket[0] = 0x2a; |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
256 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
257 /* type/family byte */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
258 curPacket[1] = cur->hdr.oscar.type; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
259 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
260 /* bytes 3+4: word: FLAP sequence number */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
261 aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum); |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
262 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
263 /* bytes 5+6: word: SNAC len */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
264 aimutil_put16(curPacket+4, cur->commandlen); |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
265 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
266 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
267 memcpy(&(curPacket[6]), cur->data, cur->commandlen); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
268 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
269 } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
270 int z = 0; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
271 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
272 z += aimutil_put8(curPacket+z, 0x4f); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
273 z += aimutil_put8(curPacket+z, 0x44); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
274 z += aimutil_put8(curPacket+z, 0x43); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
275 z += aimutil_put8(curPacket+z, 0x32); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
276 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
277 z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
278 z += aimutil_put16(curPacket+z, cur->hdr.oft.type); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
279 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
280 memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
281 } |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
282 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
283 /* |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
284 * For OSCAR, a full image of the raw packet data now in curPacket. |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
285 * For OFT, an image of just the bloated header is in curPacket, |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
286 * since OFT allows us to do the data in a different write (yay!). |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
287 */ |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
288 faim_mutex_lock(&cur->conn->active); |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
289 if ( (u_int)write(cur->conn->fd, curPacket, buflen) != buflen) { |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
290 faim_mutex_unlock(&cur->conn->active); |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
291 cur->sent = 1; |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
292 aim_conn_kill(sess, &cur->conn); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
293 return 0; /* bail out */ |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
294 } |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
295 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
296 if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
297 if (write(cur->conn->fd, cur->data, cur->commandlen) != cur->commandlen) { |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
298 /* |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
299 * Theres nothing we can do about this since we've already sent the |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
300 * header! The connection is unstable. |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
301 */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
302 } |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
303 } |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
304 |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
305 cur->sent = 1; /* mark the struct as sent */ |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
306 cur->conn->lastactivity = time(NULL); |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
307 |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
308 faim_mutex_unlock(&cur->conn->active); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
309 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
310 #if debug > 2 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
311 faimdprintf(2, "\nPacket:"); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
312 for (i = 0; i < (cur->commandlen + 6); i++) { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
313 if ((i % 8) == 0) { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
314 faimdprintf(2, "\n\t"); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
315 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
316 if (curPacket[i] >= ' ' && curPacket[i]<127) { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
317 faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
318 } else { |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
319 faimdprintf(2, "0x%2x ", curPacket[i]); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
320 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
321 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
322 faimdprintf(2, "\n"); |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
323 #endif |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
324 cur->lock = 0; /* unlock the struct */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
325 free(curPacket); /* free up full-packet buffer */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
326 |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
327 return 1; /* success */ |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
328 } |
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
329 |
237 | 330 int aim_tx_flushqueue(struct aim_session_t *sess) |
2 | 331 { |
237 | 332 struct command_tx_struct *cur; |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
333 |
2 | 334 #if debug > 1 |
335 int i = 0; | |
336 #endif | |
337 | |
237 | 338 if (sess->queue_outgoing == NULL) |
339 return 0; | |
340 | |
341 faimdprintf(2, "beginning txflush...\n"); | |
342 for (cur = sess->queue_outgoing; cur; cur = cur->next) { | |
343 /* only process if its unlocked and unsent */ | |
344 if (!cur->lock && !cur->sent) { | |
345 | |
346 /* | |
347 * And now for the meager attempt to force transmit | |
348 * latency and avoid missed messages. | |
349 */ | |
350 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) { | |
351 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */ | |
352 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL)); | |
353 } | |
2 | 354 |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
355 if (aim_tx_sendframe(sess, cur) == -1) |
279
501e09c51cbc
[gaim-migrate @ 289]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
270
diff
changeset
|
356 break; |
2 | 357 } |
237 | 358 } |
2 | 359 |
360 /* purge sent commands from queue */ | |
237 | 361 aim_tx_purgequeue(sess); |
2 | 362 |
363 return 0; | |
364 } | |
365 | |
366 /* | |
237 | 367 * aim_tx_purgequeue() |
368 * | |
369 * This is responsable for removing sent commands from the transmit | |
370 * queue. This is not a required operation, but it of course helps | |
371 * reduce memory footprint at run time! | |
372 * | |
2 | 373 */ |
237 | 374 void aim_tx_purgequeue(struct aim_session_t *sess) |
2 | 375 { |
237 | 376 struct command_tx_struct *cur = NULL; |
377 struct command_tx_struct *tmp; | |
378 | |
379 if (sess->queue_outgoing == NULL) | |
380 return; | |
381 | |
382 if (sess->queue_outgoing->next == NULL) { | |
383 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) { | |
384 tmp = sess->queue_outgoing; | |
385 sess->queue_outgoing = NULL; | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
386 if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
387 free(tmp->hdr.oft.hdr2); |
237 | 388 free(tmp->data); |
389 free(tmp); | |
2 | 390 } |
237 | 391 return; |
392 } | |
393 | |
394 for(cur = sess->queue_outgoing; cur->next != NULL; ) { | |
395 if (!cur->next->lock && cur->next->sent) { | |
396 tmp = cur->next; | |
397 cur->next = tmp->next; | |
445
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
398 if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
e4c34ca88d9b
[gaim-migrate @ 455]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
399 free(tmp->hdr.oft.hdr2); |
237 | 400 free(tmp->data); |
401 free(tmp); | |
402 } | |
403 cur = cur->next; | |
2 | 404 |
237 | 405 /* |
406 * Be careful here. Because of the way we just | |
407 * manipulated the pointer, cur may be NULL and | |
408 * the for() will segfault doing the check unless | |
409 * we find this case first. | |
410 */ | |
411 if (cur == NULL) | |
412 break; | |
413 } | |
414 return; | |
2 | 415 } |