]> andersk Git - libfaim.git/blame - aim_txqueue.c
Misc cleanups.
[libfaim.git] / aim_txqueue.c
CommitLineData
9de3ca7e 1/*
2 aim_txqueue.c
3
4 Herein lies all the mangement routines for the transmit (Tx) queue.
5
6 */
7
8#include "aim.h"
9
10/*
11 aim_tx_enqeue()
12
13 The overall purpose here is to enqueue the passed in command struct
14 into the outgoing (tx) queue. Basically...
15 1) Make a scope-irrelevent copy of the struct
16 2) Lock the struct
17 3) Mark as not-sent-yet
18 4) Enqueue the struct into the list
19 5) Unlock the struct once it's linked in
20 6) Return
21
22 */
23
24int aim_tx_enqueue(struct command_tx_struct *newpacket)
25{
26 struct command_tx_struct *workingPtr = NULL;
27 struct command_tx_struct *newpacket_copy = NULL;
28
29 if (newpacket->conn == NULL)
30 {
31 printf("aim_tx_enqueue: WARNING: enqueueing packet with no connecetion, defaulting to BOS\n");
32 newpacket->conn = aim_getconn_type(AIM_CONN_TYPE_BOS);
33 }
34
35 newpacket_copy = (struct command_tx_struct *) malloc (sizeof(struct command_tx_struct));
36 memcpy(newpacket_copy, newpacket, sizeof(struct command_tx_struct));
37
38 /* assign seqnum */
39 newpacket_copy->seqnum = aim_get_next_txseqnum(newpacket_copy->conn);
40 /* set some more fields */
41 newpacket_copy->lock = 1; /* lock */
42 newpacket_copy->sent = 0; /* not sent yet */
43 newpacket_copy->next = NULL; /* always last */
44
45 if (aim_queue_outgoing == NULL)
46 {
47 aim_queue_outgoing = newpacket_copy;
48 }
49 else
50 {
51 workingPtr = aim_queue_outgoing;
52 while (workingPtr->next != NULL)
53 workingPtr = workingPtr->next;
54 workingPtr->next = newpacket_copy;
55 }
56
57 newpacket_copy->lock = 0; /* unlock so it can be sent */
58
59#if debug > 2
60 printf("calling aim_tx_printqueue()\n");
61 aim_tx_printqueue();
62 printf("back from aim_tx_printqueue()\n");
63#endif
64
65 /* we'll force a flush for now -- this behavior probably will change */
66#if debug > 1
67 printf("calling aim_tx_flushqueue()\n");
68#endif
69 aim_tx_flushqueue();
70#if debug > 1
71 printf("back from aim_tx_flushqueue()\n");
72#endif
73
74 return 0;
75}
76
77/*
78 aim_get_next_txseqnum()
79
80 This increments the tx command count, and returns the seqnum
81 that should be stamped on the next FLAP packet sent. This is
82 normally called during the final step of packet preparation
83 before enqueuement (in aim_tx_enqueue()).
84
85 */
86u_int aim_get_next_txseqnum(struct aim_conn_t *conn)
87{
88 return ( ++conn->seqnum );
89}
90
91/*
92 aim_tx_printqueue()
93
94 This is basically for debuging purposes only. It dumps all the
95 records in the tx queue and their current status. Very helpful
96 if the queue isn't working quite right.
97
98 */
99#if debug > 2
100int aim_tx_printqueue(void)
101{
102 struct command_tx_struct *workingPtr = NULL;
103
104 workingPtr = aim_queue_outgoing;
105#if debug > 2
106 printf("\ncurrent aim_queue_outgoing...\n");
107 printf("\ttype seqnum len lock sent\n");
108#endif
109 if (workingPtr == NULL)
110 printf("aim_tx_flushqueue(): queue empty");
111 else
112 {
113 while (workingPtr != NULL)
114 {
115 printf("\t %2x %4x %4x %1d %1d\n", workingPtr->type, workingPtr->seqnum, workingPtr->commandlen, workingPtr->lock, workingPtr->sent);
116
117 workingPtr = workingPtr->next;
118 }
119 }
120
121 printf("\n(done printing queue)\n");
122
123 return 0;
124}
125#endif
126
127/*
128 aim_tx_flushqueue()
129
130 This the function is responsable for putting the queued commands
131 onto the wire. This function is critical to the operation of
132 the queue and therefore is the most prone to brokenness. It
133 seems to be working quite well at this point.
134
135 Procedure:
136 1) Traverse the list, only operate on commands that are unlocked
137 and haven't been sent yet.
138 2) Lock the struct
139 3) Allocate a temporary buffer to store the finished, fully
140 processed packet in.
141 4) Build the packet from the command_tx_struct data.
142 5) Write the packet to the socket.
143 6) If success, mark the packet sent, if fail report failure, do NOT
144 mark the packet sent (so it will not get purged and therefore
145 be attempted again on next call).
146 7) Unlock the struct.
147 8) Free the temp buffer
148 9) Step to next struct in list and go back to 1.
149
150 */
151int aim_tx_flushqueue(void)
152{
153 struct command_tx_struct *workingPtr = NULL;
154 u_char *curPacket = NULL;
155#if debug > 1
156 int i = 0;
157#endif
158
159 workingPtr = aim_queue_outgoing;
160#if debug > 1
161 printf("beginning txflush...\n");
162#endif
163 while (workingPtr != NULL)
164 {
165 /* only process if its unlocked and unsent */
166 if ( (workingPtr->lock == 0) &&
167 (workingPtr->sent == 0) )
168 {
169
170 /*
171 * And now for the meager attempt to force transmit
172 * latency and avoid missed messages.
173 */
174 if ((workingPtr->conn->lastactivity + workingPtr->conn->forcedlatency)
175 >= time(NULL))
176 {
177 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
178 sleep((workingPtr->conn->lastactivity + workingPtr->conn->forcedlatency) - time(NULL));
179 }
180
181 workingPtr->lock = 1; /* lock the struct */
182
183 /* allocate full-packet buffer */
184 curPacket = (char *) malloc(workingPtr->commandlen + 6);
185
186 /* command byte */
187 curPacket[0] = 0x2a;
188 /* type/family byte */
189 curPacket[1] = workingPtr->type;
190 /* bytes 3+4: word: FLAP sequence number */
191 curPacket[2] = (char) ( (workingPtr->seqnum) >> 8);
192 curPacket[3] = (char) ( (workingPtr->seqnum) & 0xFF);
193 /* bytes 5+6: word: SNAC len */
194 curPacket[4] = (char) ( (workingPtr->commandlen) >> 8);
195 curPacket[5] = (char) ( (workingPtr->commandlen) & 0xFF);
196 /* bytes 7 and on: raw: SNAC data */
197 memcpy(&(curPacket[6]), workingPtr->data, workingPtr->commandlen);
198
199 /* full image of raw packet data now in curPacket */
200
201 if ( (u_int)write(workingPtr->conn->fd, curPacket, (workingPtr->commandlen + 6)) != (workingPtr->commandlen + 6))
202 {
203 perror("write");
204 printf("\nWARNING: Error in sending packet 0x%4x -- will try again next time\n\n", workingPtr->seqnum);
205 workingPtr->sent = 0; /* mark it unsent */
206 return -1; /* bail out */
207 }
208 else
209 {
210#if debug > 2
211 printf("\nSENT 0x%4x\n\n", workingPtr->seqnum);
212#endif
213 workingPtr->sent = 1; /* mark the struct as sent */
214 workingPtr->conn->lastactivity = time(NULL);
215 }
216#if debug > 2
217 printf("\nPacket:");
218 for (i = 0; i < (workingPtr->commandlen + 6); i++)
219 {
220 if ((i % 8) == 0)
221 printf("\n\t");
222 if (curPacket[i] >= ' ' && curPacket[i]<127)
223 printf("%c=%02x ",curPacket[i], curPacket[i]);
224 else
225 printf("0x%2x ", curPacket[i]);
226 }
227 printf("\n");
228#endif
229 workingPtr->lock = 0; /* unlock the struct */
230 free(curPacket); /* free up full-packet buffer */
231 }
232 workingPtr = workingPtr->next;
233 }
234
235 /* purge sent commands from queue */
236 /* this may not always occur explicitly--i may put this on a timer later */
237#if debug > 1
238 printf("calling aim_tx_purgequeue()\n");
239#endif
240 aim_tx_purgequeue();
241#if debug > 1
242 printf("back from aim_tx_purgequeu() [you must be a lucky one]\n");
243#endif
244
245 return 0;
246}
247
248/*
249 aim_tx_purgequeue()
250
251 This is responsable for removing sent commands from the transmit
252 queue. This is not a required operation, but it of course helps
253 reduce memory footprint at run time!
254
255 */
256int aim_tx_purgequeue(void)
257{
258 struct command_tx_struct *workingPtr = NULL;
259 struct command_tx_struct *workingPtr2 = NULL;
260#if debug > 1
261 printf("purgequeue(): starting purge\n");
262#endif
263 /* Empty queue: nothing to do */
264 if (aim_queue_outgoing == NULL)
265 {
266#if debug > 1
267 printf("purgequeue(): purge done (len=0)\n");
268#endif
269 return 0;
270 }
271 /* One Node queue: free node and return */
272 else if (aim_queue_outgoing->next == NULL)
273 {
274#if debug > 1
275 printf("purgequeue(): entered case len=1\n");
276#endif
277 /* only free if sent AND unlocked -- dont assume sent structs are done */
278 if ( (aim_queue_outgoing->lock == 0) &&
279 (aim_queue_outgoing->sent == 1) )
280 {
281#if debug > 1
282 printf("purgequeue(): purging seqnum 0x%04x\n", aim_queue_outgoing->seqnum);
283#endif
284 workingPtr2 = aim_queue_outgoing;
285 aim_queue_outgoing = NULL;
286 free(workingPtr2->data);
287 free(workingPtr2);
288 }
289#if debug > 1
290 printf("purgequeue(): purge done (len=1)\n");
291#endif
292 return 0;
293 }
294 else
295 {
296#if debug > 1
297 printf("purgequeue(): entering case len>1\n");
298#endif
299 while(workingPtr->next != NULL)
300 {
301 if ( (workingPtr->next->lock == 0) &&
302 (workingPtr->next->sent == 1) )
303 {
304#if debug > 1
305 printf("purgequeue(): purging seqnum 0x%04x\n", workingPtr->next->seqnum);
306#endif
307 workingPtr2 = workingPtr->next;
308 workingPtr->next = workingPtr2->next;
309 free(workingPtr2->data);
310 free(workingPtr2);
311 }
312 }
313#if debug > 1
314 printf("purgequeue(): purge done (len>1)\n");
315#endif
316 return 0;
317 }
318
319 /* no reach */
320}
This page took 0.417541 seconds and 5 git commands to generate.