Request queue.
[ir-tcl-moved-to-github.git] / queue.c
diff --git a/queue.c b/queue.c
index 29c2b65..1d416d2 100644 (file)
--- a/queue.c
+++ b/queue.c
@@ -6,7 +6,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: queue.c,v $
- * Revision 1.1  1995-07-28 10:28:39  adam
+ * Revision 1.2  1995-08-03 13:23:01  adam
+ * Request queue.
+ *
+ * Revision 1.1  1995/07/28  10:28:39  adam
  * First work on request queue.
  *
  */
@@ -29,11 +32,10 @@ void *ir_tcl_malloc (size_t size)
     return p;
 }
 
-int ir_tcl_send (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
-                 const char *msg)
+int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
+                      const char *msg)
 {
     IrTcl_Request **rp;
-    int empty;
 
     if (!z_APDU (p->odr_out, &apdu, 0))
     {
@@ -43,35 +45,43 @@ int ir_tcl_send (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
         return TCL_ERROR;
     }
     rp = &p->request_queue;
-    empty = *rp ? 0 : 1;
     while (*rp)
         rp = &(*rp)->next;
     *rp = ir_tcl_malloc (sizeof(**rp));
     (*rp)->next = NULL;
-    (*rp)->state = IR_TCL_R_Queue;
     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
+    odr_setbuf (p->odr_out, NULL, 0, 1);
     odr_reset (p->odr_out);
-    if (empty)
+    if (p->state == IR_TCL_R_Idle)
     {
-        int r;
-
-        r = cs_put (p->cs_link, (*rp)->buf_out, (*rp)->len_out);
-        if (r < 0)
+        if (ir_tcl_send_q (p, *rp, msg) == TCL_ERROR)
         {
             sprintf (interp->result, "cs_put failed in %s", msg);
             return TCL_ERROR;
         } 
-        else if (r == 1)
-        {
-            ir_select_add_write (cs_fileno (p->cs_link), p);
-            logf (LOG_DEBUG, "Send part of %s", msg);
-            (*rp)->state = IR_TCL_R_Writing;
-        }
-        else
-        {
-            logf (LOG_DEBUG, "Send %s (%d bytes)", msg, (*rp)->len_out);
-            (*rp)->state = IR_TCL_R_Waiting;
-        }
+    }
+    return TCL_OK;
+}
+
+int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rp, const char *msg)
+{
+    int r;
+
+    r = cs_put (p->cs_link, rp->buf_out, rp->len_out);
+    if (r < 0)
+        return TCL_ERROR;
+    else if (r == 1)
+    {
+        ir_select_add_write (cs_fileno (p->cs_link), p);
+        logf (LOG_DEBUG, "Send part of %s", msg);
+        p->state = IR_TCL_R_Writing;
+    }
+    else
+    {
+        logf (LOG_DEBUG, "Send %s (%d bytes)", msg, rp->len_out);
+        p->state = IR_TCL_R_Waiting;
+        free (rp->buf_out);
+        rp->buf_out = NULL;
     }
     return TCL_OK;
 }