chiark / gitweb /
WIP - incoming message processing
[innduct.git] / backends / innduct.c
index 1b8875ca4c252ff82b483b8967d42fbb1cfe9bcc..65417c91aaac29eae95b4b3a135e138c00ebb9b3 100644 (file)
@@ -76,10 +76,11 @@ static const char *remote_host;
 struct Article {
   char *mid;
   int midlen;
-  int checked;
+  int checked, sentbody;
+  fd and offset for blanking token or mid;
 };
 
-#define CONNBUFSZ 16384
+#define CONNIOVS 128
 
 #define CN "<%d> "
 
@@ -88,21 +89,22 @@ typedef struct Conn Conn;
 typedef enum {
   Malloc, Const, Artdata;
 } XmitKind;
+
 typedef struct {
   XmitKind kind;
   union {
     char *malloc_tofree;
+    ARTHANDLE *sm_art;
   } info;
 } XmitDetails;
 
 struct Conn {
   ISNODE(Conn);
   int fd, max_queue, stream;
-  LIST(Article) queue; /* not yet told peer, or said TAKETHIS */
-  LIST(Article) sent; /* offered, in xmit, or transmitted waiting reply */
-  Article send; /* partially transmitted */
-  struct iovec *xmit;
-  XmitDetails *xmitd;
+  LIST(Article) queue; /* not yet told peer, or CHECK said send it */
+  LIST(Article) sent; /* offered/transmitted - in xmit or waiting reply */
+  struct iovec xmit[CONNIOVS];
+  XmitDetails xmitd[CONNIOVS];
   int xmitu;
 };
 
@@ -246,7 +248,7 @@ static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
   LIST_ADDHEAD(idle, conn);
   notice(CN "connected %s", conn->fd, conn->stream ? "streaming" : "plain");
   connect_attempt_discard();
-  process_queue();
+  check_master_queue();
   return 0;
 
  x:
@@ -361,34 +363,48 @@ static void connect_start() {
 
 /*========== overall control of article flow ==========*/
  
+static void conn_check_work(Conn *conn);
+
 static void check_master_queue(void) {
   if (!queue.count)
     return;
 
-  if (working.head) {
-    conn_assign_one_article(&working);
-  } else if (idle.head) {
-    conn_assign_one_article(&idle);
-  } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
-            !connecting_child && !connect_delay) {
-    connect_delay= reconnect_delay_periods;
-    connect_start();
+  Conn *last_assigned=0;
+  for (;;) {
+    if (working.head) {
+      conn_assign_one_article(&working, &last_assigned);
+    } else if (idle.head) {
+      conn_assign_one_article(&idle, &last_assigned);
+    } else if (nconns < maxconns && queue.count >= max_queue_per_conn &&
+              !connecting_child && !connect_delay) {
+      connect_delay= reconnect_delay_periods;
+      connect_start();
+    } else {
+      break;
+    }
   }
+  conn_check_work(last_assigned);
 } 
-
-static int conn_total_queued_articles(Conn *conn) {
-  return conn->sent.count + !!conn->send + conn->queue.count;
-}
  
-static void conn_assign_one_article(LIST(Conn) *connlist) {
+static void conn_assign_one_article(LIST(Conn) *connlist,
+                                   Conn **last_assigned) {
   Conn *conn= connlist->head;
 
   LIST_REMOVE(*connlist, conn);
   Article *art= LIST_REMHEAD(queue);
   LIST_ADDTAIL(conn->queue, art);
   LIST_ADD(*conn_determine_right_list(conn), conn);
-  
-  check_conn_work(conn);
+
+  /* This slightly odd arrangement is so that we call conn_check_work
+   * once after filling the queue for a new connection in
+   * check_master_queue, rather than for each article. */
+  if (conn != *last_assigned && *last_assigned)
+    conn_check_work(*last_assigned);
+  *last_assigned= conn;
+}
+
+static int conn_total_queued_articles(Conn *conn) {
+  return conn->sent.count + conn->queue.count;
 }
 
 static LIST(Conn) *conn_determine_right_list(Conn *conn) { 
@@ -399,42 +415,36 @@ static LIST(Conn) *conn_determine_right_list(Conn *conn) {
   return &working;
 }
 
-static void check_conn_work(Conn *conn)  {
-  void *rp;
+static void *conn_writeable(oop_source *l, int fd, int ev, void *u) {
+  check_conn_work(u);
+  return OOP_CONTINUE;
+}
+  
+static void conn_check_work(Conn *conn)  {
+  void *rp= 0;
   for (;;) {
     conn_make_some_xmits(conn);
+    if (!conn->xmitu) {
+      loop->cancel_fd(loop, conn->fd, OOP_WRITE);
+      return;
+    }
 
     void *rp= conn_write_some_xmits(conn);
-    if (!rp) {
-      loop->cancel_fd(loop, conn->fd, OOP_WRITE);
+    if (rp==OOP_CONTINUE) {
+      loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
       return;
-    } else if (rp==OOP_CONTINUE) {
-      loop->on_fd(loop, conn->fd, OOP_WRITE;)
- else if (rp==OOP_HALT) {
+    } else if (rp==OOP_HALT) {
       return;
+    } else if (!rp) {
+      /* transmitted everything */
+    } else {
+      abort();
     }
-    
-
-      if (
-
-    while (
-    
+  }
 }
  
 /*========== article transmission ==========*/
 
-static void *conn_writeable() {
-  for (;;) {
-    
-    if (!conn->xmitu) {
-      perhaps_transmit_on(conn);
-      if (!conn->xmitu) {
-       unlink from readable;
-       break;
-      }
-    }
-
-
 static void *conn_write_some_xmits(Conn *conn) {
   /* return values:
    *      0:            nothing more to write, no need to call us again
@@ -475,87 +485,140 @@ static void *conn_write_some_xmits(Conn *conn) {
 
 static void conn_make_some_xmits(Conn *conn) {
   for (;;) {
-    if (conn->send) {
-      do something about this article text;
-      continue;
-    }
-
-    if (conn->xmitu+3 > conn->xmita)
-      /* no space for a CHECK even */
+    if (conn->xmitu+5 > CONNIOVS)
       break;
 
     Article *art= LIST_REMHEAD(queue);
     if (!art) break;
 
     if (art->checked || conn->nocheck) {
+      /* actually send it */
+
+      ARTHANDLE *artdata= SMretrieve(somehow);
+      
       if (conn->stream) {
-       XMIT_LITERAL("TAKETHIS ");
-       xmit_noalloc(art->mid, art->midlen);
-       XMIT_LITERAL("\r\n");
+       if (artdata) {
+         XMIT_LITERAL("TAKETHIS ");
+         xmit_noalloc(art->mid, art->midlen);
+         XMIT_LITERAL("\r\n");
+         xmit_artbody(artdata);
+       }
       } else {
        /* we got 235 from IHAVE */
+       if (artdata) {
+         xmit_artbody(artdata);
+       } else {
+         XMIT_LITERAL(".\r\n");
+       }
       }
-      conn->send= art;
+      art->sent= 1;
+      LIST_ADDTAIL(conn->sent, art);
+
     } else {
+      /* check it */
+      
       if (conn->stream)
        XMIT_LITERAL("IHAVE ");
       else
        XMIT_LITERAL("CHECK ");
       xmit_noalloc(art->mid, art->midlen);
       XMIT_LITERAL("\r\n");
+
       LIST_ADDTAIL(conn->sent, art);
     }
   }
 }
-    
-    if (conn->queue.head) {
-      if (conn->queue.checked || conn->nocheck) {
-       
-
-      && conn->xmitu+3 <= xmita) {
-      if (
-      XMIT("
-    if (conn->xmitu < xmita
-      
 
-  if (!queue
-  
+/*========== responses from peer ==========*/
 
+static const oop_rd_style peer_rd_style= {
+  OOP_RD_DELIM_STRIP, '\n',
+  OOP_RD_NUL_FORBID,
+  OOP_RD_SHORTREC_FORBID
+};
 
+static void *peer_rd_ok(oop_source *lp, oop_read *oread, oop_event ev,
+                       const char *errmsg, int errnoval,
+                       const char *data, size_t recsz, void *conn_v) {
+  Conn *conn= conn_v;
 
-    int circ_used= circ_write - circ_read;
-    if (circ_used < 0) circ_used += CONNBUFSZ;
-    writeable_moredata(conn, CONNBUFSZ-1 - circ_used);
-
-    if (conn->circ_read == conn->circ_write)
-      return OOP_CONTINUE;
-
-    struct iovec iov[2];
-    int niov= 1;
-    iov[0].iov_base= conn->circ_buf + conn->circ_read;
-    if (conn->circ_read > conn->circ_write) { /* wrapped */
-      iov[0].iov_len= CONNBUFSZ - conn->circ_read;
-      iov[1].iov_base= conn->circ_buf;
-      iov[1].iov_len= conn->circ_write;
-      if (niov[1].iov_len) niov= 2;
-    } else {
-      iov[0].iov_len= conn->circ_write - conn->circ_read;
-    }
-    ssize_t rs= writev(conn->fd, &iov, niov);
-    if (rs < 0) {
-      
+  if (ev == OOP_RD_EOF) {
+    warn("unexpected EOF from peer");
+    conn_failed(conn);
+    return;
+  }
+  assert(ev == OOP_RD_OK);
+
+  char *ep;
+  unsigned long code= strtoul(data, &ep, 10);
+  if (ep != data+3 || *ep != ' ' || data[0]=='0') {
+    char sanibuf[100];
+    const char *p= data;
+    char *q= sanibuf;
+    *q++= '`';
+    for (;;) {
+      if (q > sanibuf+sizeof(sanibuf)-8) { strcpy(q,"..."); break; }
+      int c= *p++;
+      if (!c) { *q++= '\''; break; }
+      if (c>=' ' && c<=126 && c!='\\') { *q++= c; continue; }
+      sprintf(q,"\\x%02x",c);
+      q += 4;
     }
-    assert(rs > 0);
+    warn("badly formatted response from peer: %s", sanibuf);
+    conn_failed(conn);
+    return;
+  }
 
-    conn->circ_read += rs;
-    if (conn->circ_read > CONNBUFSZ)
-      conn->circ_read -= CONNBUFSZ;
+  if (conn->quitting) {
+    if (code!=205) {
+      warn("peer gave failure response to QUIT: %s", sani);
+      conn_failed(conn);
+      return;
+    }
+    conn close ok;
+    return;
   }
-}
 
+  switch (code) {
+  case 438: /* CHECK says they have it */
+  case 435: /* IHAVE says they have it */
+    ARTICLE_DEALTWITH(1,unwanted);
+    break;
+
+  case 238: /* CHECK says send it */
+  case 335: /* IHAVE says send it */
+    count_checkedwanted++;
+    Article *art= LIST_REMHEAD(conn->sent);
+    art->checked= 1;
+    LIST_ADDTAIL(conn->queue);
+    break;
+
+  case 235: /* IHAVE says thanks */
+  case 239: /* TAKETHIS says thanks */
+    ARTICLE_DEALTWITH(1,accepted);
+    break;
+
+  case 439: /* TAKETHIS says rejected */
+  case 437: /* IHAVE says rejected */
+    ARTICLE_DEALTWITH(1,rejected);
+    break;
+
+  case 431: /* CHECK or TAKETHIS says try later */
+  case 436: /* IHAVE says try later */
+    ARTICLE_DEALTWITH(0,deferred);
+    break;
+
+  case 400: warn("peer has stopped accepting articles: %s", sani); goto failed;
+  case 503: warn("peer timed us out: %s", sani);                   goto failed;
+  default:  warn("peer sent unexpected message: %s", sani);
+  failed:
+    conn_failed(conn);
+    return OOP_CONTINUE;;
+  }
 
+  return OOP_CONTINUE;
+}  
 main {
   ignore sigpipe;
 };