For ZOOM-C, put reconnect handling to separate function. Fix reconnect
[yaz-moved-to-github.git] / src / zoom-c.c
index c7efa2e..935674b 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.85 2006-08-16 22:47:11 adam Exp $
+ * $Id: zoom-c.c,v 1.89 2006-09-19 21:09:44 adam Exp $
  */
 /**
  * \file zoom-c.c
@@ -40,6 +40,9 @@
 #include <sys/select.h>
 #endif
 #ifdef WIN32
+#if FD_SETSIZE < 512
+#define FD_SETSIZE 512
+#endif
 #include <winsock.h>
 #endif
 
@@ -314,6 +317,7 @@ ZOOM_API(ZOOM_connection)
     return c;
 }
 
+
 /* set database names. Take local databases (if set); otherwise
    take databases given in ZURL (if set); otherwise use Default */
 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
@@ -371,6 +375,9 @@ ZOOM_API(void)
     yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
             c, host, portnum);
 
+    set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
+    ZOOM_connection_remove_tasks(c);
+
     if (c->cs)
     {
         yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
@@ -472,8 +479,6 @@ ZOOM_API(void)
     c->async = ZOOM_options_get_bool(c->options, "async", 0);
     yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
  
-    set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
-
     task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
 
     if (!c->async)
@@ -906,6 +911,17 @@ static void do_close(ZOOM_connection c)
     c->state = STATE_IDLE;
 }
 
+static int ZOOM_test_reconnect(ZOOM_connection c)
+{
+    if (!c->reconnect_ok)
+        return 0;
+    do_close(c);
+    c->reconnect_ok = 0;
+    c->tasks->running = 0;
+    ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
+    return 1;
+}
+
 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
                                     int force_sync, int start, int count)
 {
@@ -1200,7 +1216,7 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
                     odr_prepend(c->odr_out, "ZOOM-C",
                                 ireq->implementationName));
     
-    version = odr_strdup(c->odr_out, "$Revision: 1.85 $");
+    version = odr_strdup(c->odr_out, "$Revision: 1.89 $");
     if (strlen(version) > 10)   /* check for unexpanded CVS strings */
         version[strlen(version)-2] = '\0';
     ireq->implementationVersion = 
@@ -3419,13 +3435,7 @@ static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
         break;
     case Z_APDU_close:
         yaz_log(log_api, "%p recv_apdu Close PDU", c);
-        if (c->reconnect_ok)
-        {
-            do_close(c);
-            c->tasks->running = 0;
-            ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
-        }
-        else
+        if (!ZOOM_test_reconnect(c))
         {
             set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
             do_close(c);
@@ -3605,13 +3615,9 @@ static int do_read(ZOOM_connection c)
         return 0;
     if (r <= 0)
     {
-        if (c->reconnect_ok)
+        if (ZOOM_test_reconnect(c))
         {
-            do_close(c);
-            c->reconnect_ok = 0;
             yaz_log(log_details, "%p do_read reconnect read", c);
-            c->tasks->running = 0;
-            ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
         }
         else
         {
@@ -3668,13 +3674,8 @@ static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
     if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
     {
         yaz_log(log_details, "%p do_write_ex write failed", c);
-        if (c->reconnect_ok)
+        if (ZOOM_test_reconnect(c))
         {
-            do_close(c);
-            c->reconnect_ok = 0;
-            yaz_log(log_details, "%p do_write_ex reconnect write", c);
-            c->tasks->running = 0;
-            ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
             return zoom_pending;
         }
         if (c->state == STATE_CONNECTING)
@@ -3853,7 +3854,7 @@ ZOOM_API(int)
     return ZOOM_connection_error_x(c, cp, addinfo, 0);
 }
 
-static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
+static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
 {
     ZOOM_Event event = 0;
     int r = cs_look(c->cs);
@@ -3869,10 +3870,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
     }
     else if (r == CS_CONNECT)
     {
-        int ret;
-        event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
-
-        ret = cs_rcvconnect(c->cs);
+        int ret = ret = cs_rcvconnect(c->cs);
         yaz_log(log_details, "%p ZOOM_connection_do_io "
                 "cs_rcvconnect returned %d", c, ret);
         if (ret == 1)
@@ -3882,10 +3880,10 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
                 c->mask += ZOOM_SELECT_WRITE;
             if (c->cs->io_pending & CS_WANT_READ)
                 c->mask += ZOOM_SELECT_READ;
-            ZOOM_connection_put_event(c, event);
         }
         else if (ret == 0)
         {
+            event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
             ZOOM_connection_put_event(c, event);
             get_cert(c);
             if (c->proto == PROTO_Z3950)
@@ -3904,17 +3902,29 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
         {
             set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
             do_close(c);
-            ZOOM_connection_put_event(c, event);
         }
     }
     else
     {
+        if (mask & ZOOM_SELECT_EXCEPT)
+        {
+            if (ZOOM_test_reconnect(c))
+            {
+                event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
+                ZOOM_connection_put_event(c, event);
+            }
+            else
+            {
+                set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+                do_close(c);
+            }
+            return;
+        }
         if (mask & ZOOM_SELECT_READ)
             do_read(c);
         if (c->cs && (mask & ZOOM_SELECT_WRITE))
             do_write(c);
     }
-    return 1;
 }
 
 ZOOM_API(int)