4 * Herein lies all the mangement routines for the transmit (Tx) queue.
11 * Allocate a new tx frame.
13 * This is more for looks than anything else.
15 * Right now, that is. If/when we implement a pool of transmit
16 * frames, this will become the request-an-unused-frame part.
18 struct command_tx_struct *aim_tx_new(int chan, struct aim_conn_t *conn, int datalen)
20 struct command_tx_struct *new;
25 new = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
28 memset(new, 0, sizeof(struct command_tx_struct));
34 new->data = (u_char *)malloc(datalen);
35 new->commandlen = datalen;
44 * The overall purpose here is to enqueue the passed in command struct
45 * into the outgoing (tx) queue. Basically...
46 * 1) Make a scope-irrelevent copy of the struct
48 * 3) Mark as not-sent-yet
49 * 4) Enqueue the struct into the list
50 * 5) Unlock the struct once it's linked in
54 int aim_tx_enqueue(struct aim_session_t *sess,
55 struct command_tx_struct *newpacket)
57 struct command_tx_struct *cur;
59 if (newpacket->conn == NULL) {
60 faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
61 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
65 newpacket->seqnum = aim_get_next_txseqnum(newpacket->conn);
66 /* set some more fields */
67 newpacket->lock = 1; /* lock */
68 newpacket->sent = 0; /* not sent yet */
69 newpacket->next = NULL; /* always last */
71 /* see overhead note in aim_rxqueue counterpart */
72 if (sess->queue_outgoing == NULL) {
73 sess->queue_outgoing = newpacket;
75 for (cur = sess->queue_outgoing;
79 cur->next = newpacket;
82 newpacket->lock = 0; /* unlock so it can be sent */
85 faimdprintf(2, "calling aim_tx_printqueue()\n");
86 aim_tx_printqueue(sess);
87 faimdprintf(2, "back from aim_tx_printqueue()\n");
94 * aim_get_next_txseqnum()
96 * This increments the tx command count, and returns the seqnum
97 * that should be stamped on the next FLAP packet sent. This is
98 * normally called during the final step of packet preparation
99 * before enqueuement (in aim_tx_enqueue()).
102 u_int aim_get_next_txseqnum(struct aim_conn_t *conn)
104 return ( ++conn->seqnum );
108 * aim_tx_printqueue()
110 * This is basically for debuging purposes only. It dumps all the
111 * records in the tx queue and their current status. Very helpful
112 * if the queue isn't working quite right.
116 int aim_tx_printqueue(struct aim_session_t *sess)
118 struct command_tx_struct *cur;
120 faimdprintf(2, "\ncurrent aim_queue_outgoing...\n");
121 faimdprintf(2, "\ttype seqnum len lock sent\n");
123 if (sess->queue_outgoing == NULL)
124 faimdprintf(2, "aim_tx_flushqueue(): queue empty");
126 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
127 faimdprintf(2, "\t %2x %4x %4x %1d %1d\n",
128 cur->type, cur->seqnum,
129 cur->commandlen, cur->lock,
134 faimdprintf(2, "\n(done printing queue)\n");
141 * aim_tx_flushqueue()
143 * This the function is responsable for putting the queued commands
144 * onto the wire. This function is critical to the operation of
145 * the queue and therefore is the most prone to brokenness. It
146 * seems to be working quite well at this point.
149 * 1) Traverse the list, only operate on commands that are unlocked
150 * and haven't been sent yet.
152 * 3) Allocate a temporary buffer to store the finished, fully
153 * processed packet in.
154 * 4) Build the packet from the command_tx_struct data.
155 * 5) Write the packet to the socket.
156 * 6) If success, mark the packet sent, if fail report failure, do NOT
157 * mark the packet sent (so it will not get purged and therefore
158 * be attempted again on next call).
159 * 7) Unlock the struct.
160 * 8) Free the temp buffer
161 * 9) Step to next struct in list and go back to 1.
164 int aim_tx_flushqueue(struct aim_session_t *sess)
166 struct command_tx_struct *cur;
167 u_char *curPacket = NULL;
172 if (sess->queue_outgoing == NULL)
175 faimdprintf(2, "beginning txflush...\n");
176 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
177 /* only process if its unlocked and unsent */
178 if (!cur->lock && !cur->sent) {
181 * And now for the meager attempt to force transmit
182 * latency and avoid missed messages.
184 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) {
185 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
186 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
189 cur->lock = 1; /* lock the struct */
191 /* allocate full-packet buffer */
192 curPacket = (char *) malloc(cur->commandlen + 6);
197 /* type/family byte */
198 curPacket[1] = cur->type;
200 /* bytes 3+4: word: FLAP sequence number */
201 aimutil_put16(curPacket+2, cur->seqnum);
203 /* bytes 5+6: word: SNAC len */
204 aimutil_put16(curPacket+4, cur->commandlen);
206 /* bytes 7 and on: raw: SNAC data */
207 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
209 /* full image of raw packet data now in curPacket */
210 if ( (u_int)write(cur->conn->fd, curPacket, (cur->commandlen + 6)) != (cur->commandlen + 6)) {
211 printf("\nWARNING: Error in sending packet 0x%4x -- will try again next time\n\n", cur->seqnum);
212 cur->sent = 0; /* mark it unsent */
213 continue; /* bail out */
215 faimdprintf(2, "\nSENT 0x%4x\n\n", cur->seqnum);
217 cur->sent = 1; /* mark the struct as sent */
218 cur->conn->lastactivity = time(NULL);
221 faimdprintf(2, "\nPacket:");
222 for (i = 0; i < (cur->commandlen + 6); i++) {
224 faimdprintf(2, "\n\t");
226 if (curPacket[i] >= ' ' && curPacket[i]<127) {
227 faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
229 faimdprintf(2, "0x%2x ", curPacket[i]);
232 faimdprintf(2, "\n");
234 cur->lock = 0; /* unlock the struct */
235 free(curPacket); /* free up full-packet buffer */
239 /* purge sent commands from queue */
240 aim_tx_purgequeue(sess);
246 * aim_tx_purgequeue()
248 * This is responsable for removing sent commands from the transmit
249 * queue. This is not a required operation, but it of course helps
250 * reduce memory footprint at run time!
253 void aim_tx_purgequeue(struct aim_session_t *sess)
255 struct command_tx_struct *cur = NULL;
256 struct command_tx_struct *tmp;
258 if (sess->queue_outgoing == NULL)
261 if (sess->queue_outgoing->next == NULL) {
262 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) {
263 tmp = sess->queue_outgoing;
264 sess->queue_outgoing = NULL;
271 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
272 if (!cur->next->lock && cur->next->sent) {
274 cur->next = tmp->next;
281 * Be careful here. Because of the way we just
282 * manipulated the pointer, cur may be NULL and
283 * the for() will segfault doing the check unless
284 * we find this case first.