]> andersk Git - libfaim.git/blobdiff - aim_txqueue.c
Fix the long break-on-invite-off-line-user problem.
[libfaim.git] / aim_txqueue.c
index 8a42e333e2a06af6a6309085f881182d3ee7183a..bdc3b14bf6b0845630271fc99d62fc774245da0c 100644 (file)
@@ -8,7 +8,40 @@
 #include <faim/aim.h>
 
 /*
- * aim_tx_enqeue()
+ * Allocate a new tx frame.
+ *
+ * This is more for looks than anything else.
+ *
+ * Right now, that is.  If/when we implement a pool of transmit
+ * frames, this will become the request-an-unused-frame part.
+ */
+struct command_tx_struct *aim_tx_new(int chan, struct aim_conn_t *conn, int datalen)
+{
+  struct command_tx_struct *new;
+
+  if (!conn) {
+    printf("aim_tx_new: ERROR: no connection specified\n");
+    return NULL;
+  }
+
+  new = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
+  if (!new)
+    return NULL;
+  memset(new, 0, sizeof(struct command_tx_struct));
+
+  new->conn = conn; 
+  new->type = chan;
+
+  if(datalen) {
+    new->data = (u_char *)malloc(datalen);
+    new->commandlen = datalen;
+  }
+
+  return new;
+}
+
+/*
+ * aim_tx_enqeue__queuebased()
  *
  * The overall purpose here is to enqueue the passed in command struct
  * into the outgoing (tx) queue.  Basically...
  *   5) Unlock the struct once it's linked in
  *   6) Return
  *
+ * Note that this is only used when doing queue-based transmitting;
+ * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased.
+ *
  */
-int aim_tx_enqueue(struct aim_session_t *sess,
-                  struct command_tx_struct *newpacket)
+int aim_tx_enqueue__queuebased(struct aim_session_t *sess,
+                              struct command_tx_struct *newpacket)
 {
   struct command_tx_struct *cur;
-  struct command_tx_struct *newpacket_copy = NULL;
 
   if (newpacket->conn == NULL) {
-      faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion,  defaulting to BOS\n");
+      faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
       newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
   }
  
-  newpacket_copy = (struct command_tx_struct *) malloc (sizeof(struct command_tx_struct));
-  memcpy(newpacket_copy, newpacket, sizeof(struct command_tx_struct));
-
   /* assign seqnum */
-  newpacket_copy->seqnum = aim_get_next_txseqnum(newpacket_copy->conn);
+  newpacket->seqnum = aim_get_next_txseqnum(newpacket->conn);
   /* set some more fields */
-  newpacket_copy->lock = 1; /* lock */
-  newpacket_copy->sent = 0; /* not sent yet */
-  newpacket_copy->next = NULL; /* always last */
+  newpacket->lock = 1; /* lock */
+  newpacket->sent = 0; /* not sent yet */
+  newpacket->next = NULL; /* always last */
 
   /* see overhead note in aim_rxqueue counterpart */
   if (sess->queue_outgoing == NULL) {
-    sess->queue_outgoing = newpacket_copy;
+    sess->queue_outgoing = newpacket;
   } else {
     for (cur = sess->queue_outgoing;
         cur->next;
         cur = cur->next)
       ;
-    cur->next = newpacket_copy;
+    cur->next = newpacket;
   }
 
-  newpacket_copy->lock = 0; /* unlock so it can be sent */
+  newpacket->lock = 0; /* unlock so it can be sent */
 
 #if debug == 2
   faimdprintf(2, "calling aim_tx_printqueue()\n");
@@ -63,6 +95,41 @@ int aim_tx_enqueue(struct aim_session_t *sess,
   return 0;
 }
 
+/*
+ * aim_tx_enqueue__immediate()
+ *
+ * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
+ * the whole queue mess when you want immediate writes to happen.
+ *
+ * Basically the same as its __queuebased couterpart, however
+ * instead of doing a list append, it just calls aim_tx_sendframe()
+ * right here. 
+ * 
+ */
+int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
+{
+  if (newpacket->conn == NULL) {
+    faimdprintf(1, "aim_tx_enqueue: ERROR: packet has no connection\n");
+    if (newpacket->data)
+      free(newpacket->data);
+    free(newpacket);
+    return -1;
+  }
+
+  newpacket->seqnum = aim_get_next_txseqnum(newpacket->conn);
+
+  newpacket->lock = 1; /* lock */
+  newpacket->sent = 0; /* not sent yet */
+
+  aim_tx_sendframe(newpacket);
+
+  if (newpacket->data)
+    free(newpacket->data);
+  free(newpacket);
+
+  return 0;
+}
+
 /* 
  *  aim_get_next_txseqnum()
  *
@@ -74,7 +141,12 @@ int aim_tx_enqueue(struct aim_session_t *sess,
  */
 u_int aim_get_next_txseqnum(struct aim_conn_t *conn)
 {
-  return ( ++conn->seqnum );
+  u_int ret;
+  
+  faim_mutex_lock(&conn->seqnum_lock);
+  ret = ++conn->seqnum;
+  faim_mutex_unlock(&conn->seqnum_lock);
+  return ret;
 }
 
 /*
@@ -134,10 +206,72 @@ int aim_tx_printqueue(struct aim_session_t *sess)
  *    9) Step to next struct in list and go back to 1.
  *
  */
+int aim_tx_sendframe(struct command_tx_struct *cur)
+{
+  u_char *curPacket;
+
+  if (!cur)
+    return -1; /* fatal */
+
+  cur->lock = 1; /* lock the struct */
+
+  /* allocate full-packet buffer */
+  curPacket = (char *) malloc(cur->commandlen + 6);
+      
+  /* command byte */
+  curPacket[0] = 0x2a;
+      
+  /* type/family byte */
+  curPacket[1] = cur->type;
+      
+  /* bytes 3+4: word: FLAP sequence number */
+  aimutil_put16(curPacket+2, cur->seqnum);
+
+  /* bytes 5+6: word: SNAC len */
+  aimutil_put16(curPacket+4, cur->commandlen);
+      
+  /* bytes 7 and on: raw: SNAC data */  /* XXX: ye gods! get rid of this! */
+  memcpy(&(curPacket[6]), cur->data, cur->commandlen);
+      
+  /* full image of raw packet data now in curPacket */
+  faim_mutex_lock(&cur->conn->active);
+  if ( (u_int)write(cur->conn->fd, curPacket, (cur->commandlen + 6)) != (cur->commandlen + 6)) {
+    faim_mutex_unlock(&cur->conn->active);
+    printf("\nWARNING: Error in sending packet 0x%4x -- will try again next time\n\n", cur->seqnum);
+    cur->sent = 0; /* mark it unsent */
+    return 0; /* bail out -- continuable error */
+  } else {
+    faimdprintf(2, "\nSENT 0x%4x\n\n", cur->seqnum);
+    
+    cur->sent = 1; /* mark the struct as sent */
+    cur->conn->lastactivity = time(NULL);
+  }
+  faim_mutex_unlock(&cur->conn->active);
+
+#if debug > 2
+  faimdprintf(2, "\nPacket:");
+  for (i = 0; i < (cur->commandlen + 6); i++) {
+    if ((i % 8) == 0) {
+      faimdprintf(2, "\n\t");
+    }
+    if (curPacket[i] >= ' ' && curPacket[i]<127) {
+      faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
+    } else {
+      faimdprintf(2, "0x%2x ", curPacket[i]);
+    }
+  }
+  faimdprintf(2, "\n");
+#endif
+  cur->lock = 0; /* unlock the struct */
+  free(curPacket); /* free up full-packet buffer */
+
+  return 1; /* success */
+}
+
 int aim_tx_flushqueue(struct aim_session_t *sess)
 {
   struct command_tx_struct *cur;
-  u_char *curPacket = NULL;
+   
 #if debug > 1
   int i = 0;
 #endif
@@ -158,54 +292,9 @@ int aim_tx_flushqueue(struct aim_session_t *sess)
        /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
        sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
       }
-      
-      cur->lock = 1; /* lock the struct */
-      
-      /* allocate full-packet buffer */
-      curPacket = (char *) malloc(cur->commandlen + 6);
-      
-      /* command byte */
-      curPacket[0] = 0x2a;
-      
-      /* type/family byte */
-      curPacket[1] = cur->type;
-      
-      /* bytes 3+4: word: FLAP sequence number */
-      aimutil_put16(curPacket+2, cur->seqnum);
 
-      /* bytes 5+6: word: SNAC len */
-      aimutil_put16(curPacket+4, cur->commandlen);
-      
-      /* bytes 7 and on: raw: SNAC data */
-      memcpy(&(curPacket[6]), cur->data, cur->commandlen);
-      
-      /* full image of raw packet data now in curPacket */
-      if ( (u_int)write(cur->conn->fd, curPacket, (cur->commandlen + 6)) != (cur->commandlen + 6)) {
-       printf("\nWARNING: Error in sending packet 0x%4x -- will try again next time\n\n", cur->seqnum);
-       cur->sent = 0; /* mark it unsent */
-       continue; /* bail out */
-      } else {
-       faimdprintf(2, "\nSENT 0x%4x\n\n", cur->seqnum);
-
-       cur->sent = 1; /* mark the struct as sent */
-       cur->conn->lastactivity = time(NULL);
-      }
-#if debug > 2
-      faimdprintf(2, "\nPacket:");
-      for (i = 0; i < (cur->commandlen + 6); i++) {
-       if ((i % 8) == 0) {
-         faimdprintf(2, "\n\t");
-       }
-       if (curPacket[i] >= ' ' && curPacket[i]<127) {
-         faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
-       } else {
-         faimdprintf(2, "0x%2x ", curPacket[i]);
-       }
-      }
-      faimdprintf(2, "\n");
-#endif
-      cur->lock = 0; /* unlock the struct */
-      free(curPacket); /* free up full-packet buffer */
+      if (aim_tx_sendframe(cur) == -1)
+       break;
     }
   }
 
This page took 0.036184 seconds and 4 git commands to generate.