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;
23 printf("aim_tx_new: ERROR: no connection specified\n");
27 new = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
30 memset(new, 0, sizeof(struct command_tx_struct));
36 new->data = (u_char *)malloc(datalen);
37 new->commandlen = datalen;
44 * aim_tx_enqeue__queuebased()
46 * The overall purpose here is to enqueue the passed in command struct
47 * into the outgoing (tx) queue. Basically...
48 * 1) Make a scope-irrelevent copy of the struct
50 * 3) Mark as not-sent-yet
51 * 4) Enqueue the struct into the list
52 * 5) Unlock the struct once it's linked in
55 * Note that this is only used when doing queue-based transmitting;
56 * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased.
59 int aim_tx_enqueue__queuebased(struct aim_session_t *sess,
60 struct command_tx_struct *newpacket)
62 struct command_tx_struct *cur;
64 if (newpacket->conn == NULL) {
65 faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
66 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
70 newpacket->seqnum = aim_get_next_txseqnum(newpacket->conn);
71 /* set some more fields */
72 newpacket->lock = 1; /* lock */
73 newpacket->sent = 0; /* not sent yet */
74 newpacket->next = NULL; /* always last */
76 /* see overhead note in aim_rxqueue counterpart */
77 if (sess->queue_outgoing == NULL) {
78 sess->queue_outgoing = newpacket;
80 for (cur = sess->queue_outgoing;
84 cur->next = newpacket;
87 newpacket->lock = 0; /* unlock so it can be sent */
90 faimdprintf(2, "calling aim_tx_printqueue()\n");
91 aim_tx_printqueue(sess);
92 faimdprintf(2, "back from aim_tx_printqueue()\n");
99 * aim_tx_enqueue__immediate()
101 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
102 * the whole queue mess when you want immediate writes to happen.
104 * Basically the same as its __queuebased couterpart, however
105 * instead of doing a list append, it just calls aim_tx_sendframe()
109 int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
111 if (newpacket->conn == NULL) {
112 faimdprintf(1, "aim_tx_enqueue: ERROR: packet has no connection\n");
114 free(newpacket->data);
119 newpacket->seqnum = aim_get_next_txseqnum(newpacket->conn);
121 newpacket->lock = 1; /* lock */
122 newpacket->sent = 0; /* not sent yet */
124 aim_tx_sendframe(newpacket);
127 free(newpacket->data);
134 * aim_get_next_txseqnum()
136 * This increments the tx command count, and returns the seqnum
137 * that should be stamped on the next FLAP packet sent. This is
138 * normally called during the final step of packet preparation
139 * before enqueuement (in aim_tx_enqueue()).
142 u_int aim_get_next_txseqnum(struct aim_conn_t *conn)
146 faim_mutex_lock(&conn->seqnum_lock);
147 ret = ++conn->seqnum;
148 faim_mutex_unlock(&conn->seqnum_lock);
153 * aim_tx_printqueue()
155 * This is basically for debuging purposes only. It dumps all the
156 * records in the tx queue and their current status. Very helpful
157 * if the queue isn't working quite right.
161 int aim_tx_printqueue(struct aim_session_t *sess)
163 struct command_tx_struct *cur;
165 faimdprintf(2, "\ncurrent aim_queue_outgoing...\n");
166 faimdprintf(2, "\ttype seqnum len lock sent\n");
168 if (sess->queue_outgoing == NULL)
169 faimdprintf(2, "aim_tx_flushqueue(): queue empty");
171 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
172 faimdprintf(2, "\t %2x %4x %4x %1d %1d\n",
173 cur->type, cur->seqnum,
174 cur->commandlen, cur->lock,
179 faimdprintf(2, "\n(done printing queue)\n");
186 * aim_tx_flushqueue()
188 * This the function is responsable for putting the queued commands
189 * onto the wire. This function is critical to the operation of
190 * the queue and therefore is the most prone to brokenness. It
191 * seems to be working quite well at this point.
194 * 1) Traverse the list, only operate on commands that are unlocked
195 * and haven't been sent yet.
197 * 3) Allocate a temporary buffer to store the finished, fully
198 * processed packet in.
199 * 4) Build the packet from the command_tx_struct data.
200 * 5) Write the packet to the socket.
201 * 6) If success, mark the packet sent, if fail report failure, do NOT
202 * mark the packet sent (so it will not get purged and therefore
203 * be attempted again on next call).
204 * 7) Unlock the struct.
205 * 8) Free the temp buffer
206 * 9) Step to next struct in list and go back to 1.
209 int aim_tx_sendframe(struct command_tx_struct *cur)
214 return -1; /* fatal */
216 cur->lock = 1; /* lock the struct */
218 /* allocate full-packet buffer */
219 curPacket = (char *) malloc(cur->commandlen + 6);
224 /* type/family byte */
225 curPacket[1] = cur->type;
227 /* bytes 3+4: word: FLAP sequence number */
228 aimutil_put16(curPacket+2, cur->seqnum);
230 /* bytes 5+6: word: SNAC len */
231 aimutil_put16(curPacket+4, cur->commandlen);
233 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
234 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
236 /* full image of raw packet data now in curPacket */
237 faim_mutex_lock(&cur->conn->active);
238 if ( (u_int)write(cur->conn->fd, curPacket, (cur->commandlen + 6)) != (cur->commandlen + 6)) {
239 faim_mutex_unlock(&cur->conn->active);
240 printf("\nWARNING: Error in sending packet 0x%4x -- will try again next time\n\n", cur->seqnum);
241 cur->sent = 0; /* mark it unsent */
242 return 0; /* bail out -- continuable error */
244 faimdprintf(2, "\nSENT 0x%4x\n\n", cur->seqnum);
246 cur->sent = 1; /* mark the struct as sent */
247 cur->conn->lastactivity = time(NULL);
249 faim_mutex_unlock(&cur->conn->active);
252 faimdprintf(2, "\nPacket:");
253 for (i = 0; i < (cur->commandlen + 6); i++) {
255 faimdprintf(2, "\n\t");
257 if (curPacket[i] >= ' ' && curPacket[i]<127) {
258 faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
260 faimdprintf(2, "0x%2x ", curPacket[i]);
263 faimdprintf(2, "\n");
265 cur->lock = 0; /* unlock the struct */
266 free(curPacket); /* free up full-packet buffer */
268 return 1; /* success */
271 int aim_tx_flushqueue(struct aim_session_t *sess)
273 struct command_tx_struct *cur;
279 if (sess->queue_outgoing == NULL)
282 faimdprintf(2, "beginning txflush...\n");
283 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
284 /* only process if its unlocked and unsent */
285 if (!cur->lock && !cur->sent) {
288 * And now for the meager attempt to force transmit
289 * latency and avoid missed messages.
291 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) {
292 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
293 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
296 if (aim_tx_sendframe(cur) == -1)
301 /* purge sent commands from queue */
302 aim_tx_purgequeue(sess);
308 * aim_tx_purgequeue()
310 * This is responsable for removing sent commands from the transmit
311 * queue. This is not a required operation, but it of course helps
312 * reduce memory footprint at run time!
315 void aim_tx_purgequeue(struct aim_session_t *sess)
317 struct command_tx_struct *cur = NULL;
318 struct command_tx_struct *tmp;
320 if (sess->queue_outgoing == NULL)
323 if (sess->queue_outgoing->next == NULL) {
324 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) {
325 tmp = sess->queue_outgoing;
326 sess->queue_outgoing = NULL;
333 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
334 if (!cur->next->lock && cur->next->sent) {
336 cur->next = tmp->next;
343 * Be careful here. Because of the way we just
344 * manipulated the pointer, cur may be NULL and
345 * the for() will segfault doing the check unless
346 * we find this case first.