Yet another WIN32 fix for connect notify.
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc.cpp
index 8be67df..0925962 100644 (file)
@@ -3,7 +3,24 @@
  * See the file LICENSE for details.
  * 
  * $Log: yaz-pdu-assoc.cpp,v $
- * Revision 1.16  2000-09-22 09:54:11  heikki
+ * Revision 1.21  2000-11-20 14:17:36  adam
+ * Yet another WIN32 fix for connect notify.
+ *
+ * Revision 1.20  2000/11/20 11:27:33  adam
+ * Fixes for connect operation (timeout and notify fix).
+ *
+ * Revision 1.19  2000/11/01 14:22:59  adam
+ * Added fd parameter for method IYaz_PDU_Observer::clone.
+ *
+ * Revision 1.18  2000/10/24 12:29:57  adam
+ * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
+ * two Yaz_Proxy's (fatal).
+ *
+ * Revision 1.17  2000/10/11 11:58:16  adam
+ * Moved header files to include/yaz++. Switched to libtool and automake.
+ * Configure script creates yaz++-config script.
+ *
+ * Revision 1.16  2000/09/22 09:54:11  heikki
  * minor
  *
  * Revision 1.15  2000/09/21 21:43:20  adam
@@ -60,7 +77,7 @@
 
 #include <assert.h>
 
-#include <yaz-pdu-assoc.h>
+#include <yaz++/yaz-pdu-assoc.h>
 
 #include <yaz/log.h>
 #include <yaz/tcpip.h>
@@ -91,17 +108,10 @@ IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
 void Yaz_PDU_Assoc::socketNotify(int event)
 {
     logf (m_log, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event);
-    if (0 /* m_state == Connected */)
+    switch (m_state)
     {
-       m_state = Ready;
-       m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
-                                        YAZ_SOCKET_OBSERVE_EXCEPT);
-       m_PDU_Observer->connectNotify();
-       flush_PDU();
-    }
-    else if (m_state == Connecting)
-    {
-       if (event & YAZ_SOCKET_OBSERVE_READ)
+    case Connecting:
+       if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_EXCEPT))
        {
            close();
            m_PDU_Observer->failNotify();
@@ -118,9 +128,8 @@ void Yaz_PDU_Assoc::socketNotify(int event)
            m_PDU_Observer->connectNotify();
            flush_PDU();
        }
-    }
-    else if (m_state == Listen)
-    {
+       break;
+    case Listen:
        if (event & YAZ_SOCKET_OBSERVE_READ)
        {
            int res;
@@ -147,9 +156,8 @@ void Yaz_PDU_Assoc::socketNotify(int event)
            cs_close (new_line);
            childNotify(fd);
        }
-    }
-    else if (m_state == Ready)
-    {
+       break;
+    case Ready:
        if (event & YAZ_SOCKET_OBSERVE_WRITE)
        {
            flush_PDU();
@@ -165,7 +173,7 @@ void Yaz_PDU_Assoc::socketNotify(int event)
                {
                    logf (m_log, "Connection closed by peer");
                    close();
-                   m_PDU_Observer->failNotify();
+                   m_PDU_Observer->failNotify(); // problem here..
                    return;
                }
                // lock it, so we know if recv_PDU deletes it.
@@ -182,6 +190,16 @@ void Yaz_PDU_Assoc::socketNotify(int event)
        {
            m_PDU_Observer->timeoutNotify();
        }
+       break;
+    case Closed:
+       logf (m_log, "CLOSING state=%d event was %d", m_state, event);
+       close();
+       m_PDU_Observer->failNotify();
+       break;
+    default:
+       logf (m_log, "Unknown state=%d event was %d", m_state, event);
+       close();
+       m_PDU_Observer->failNotify();
     }
 }
 
@@ -371,9 +389,11 @@ void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
     logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(cs), res);
     m_socketObservable->addObserver(cs_fileno(cs), this);
     m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
-                                          YAZ_SOCKET_OBSERVE_EXCEPT|
-                                          YAZ_SOCKET_OBSERVE_WRITE);
-    m_state = Connecting;
+                                    YAZ_SOCKET_OBSERVE_EXCEPT|
+                                    YAZ_SOCKET_OBSERVE_WRITE);
+    if (res >= 0)
+       m_state = Connecting;
+    // if res < 0, then cs_connect failed immediately -> m_state is Closed..
 }
 
 void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd)
@@ -393,7 +413,7 @@ void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd)
     }
 }
 
-#if 0
+#if 1
  // 1 = single-threaded
  // 0 = multi-threaded
 
@@ -404,14 +424,14 @@ void Yaz_PDU_Assoc::childNotify(int fd)
     IYaz_PDU_Observable *new_observable = clone();
 
     // Clone PDU Observer
-    IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable);
+    IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable, fd);
 
     // Attach new socket to it
     new_observable->socket(observer, fd);
 }
 #else
 
-#include <yaz-socket-manager.h>
+#include <yaz++/yaz-socket-manager.h>
 
 #ifdef WIN32
 #include <process.h>
@@ -444,7 +464,7 @@ void Yaz_PDU_Assoc::childNotify(int fd)
     Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable);
     
     /// Clone PDU Observer
-    IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable);
+    IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable, fd);
     
     /// Attach new socket to it
     new_observable->socket(observer, fd);