X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Fyaz-pdu-assoc.cpp;h=a816d4881427144a1e4a092445e68833fd6d080e;hb=933909a6cfd5dcacc446f45e7217849b917abefd;hp=fafac448bf46e028a04e87457b0aaeff6aa2f56f;hpb=6104ba97908b1292806a1242b65beb0edbf2314f;p=yazpp-moved-to-github.git diff --git a/src/yaz-pdu-assoc.cpp b/src/yaz-pdu-assoc.cpp index fafac44..a816d48 100644 --- a/src/yaz-pdu-assoc.cpp +++ b/src/yaz-pdu-assoc.cpp @@ -1,10 +1,42 @@ /* - * Copyright (c) 1998-1999, Index Data. + * Copyright (c) 1998-2000, Index Data. * See the file LICENSE for details. - * Sebastian Hammer, Adam Dickmeiss * * $Log: yaz-pdu-assoc.cpp,v $ - * Revision 1.10 2000-08-10 08:42:42 adam + * 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 + * Better high-level server API. + * + * Revision 1.14 2000/09/12 12:09:53 adam + * More work on high-level server. + * + * Revision 1.13 2000/09/08 10:23:42 adam + * Added skeleton of yaz-z-server. + * + * Revision 1.12 2000/09/06 14:23:45 adam + * WIN32 updates. + * + * Revision 1.11 2000/09/04 08:29:22 adam + * Fixed memory leak(s). Added re-use of associations, rather than + * re-init, when maximum number of targets are in use. + * + * Revision 1.10 2000/08/10 08:42:42 adam * Fixes for {set,get}_APDU_log. * * Revision 1.9 1999/12/06 13:52:45 adam @@ -21,7 +53,7 @@ * Revision 1.6 1999/04/20 10:30:05 adam * Implemented various stuff for client and proxy. Updated calls * to ODR to reflect new name parameter. - * + *g * Revision 1.5 1999/04/09 11:46:57 adam * Added object Yaz_Z_Assoc. Much more functional client. * @@ -42,7 +74,7 @@ #include -#include +#include #include #include @@ -70,24 +102,12 @@ IYaz_PDU_Observable *Yaz_PDU_Assoc::clone() return copy; } -Yaz_PDU_Assoc::~Yaz_PDU_Assoc() -{ - destroy(); -} - 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 */) - { - 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) + switch (m_state) { + case Connecting: if (event & YAZ_SOCKET_OBSERVE_READ) { close(); @@ -105,9 +125,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; @@ -117,12 +136,11 @@ void Yaz_PDU_Assoc::socketNotify(int event) return; if (res < 0) { - logf(LOG_FATAL, "cs_listen failed"); + logf(LOG_FATAL|LOG_ERRNO, "cs_listen failed"); return; } if (!(new_line = cs_accept(m_cs))) return; - /* 1. create socket-manager 2. create pdu-assoc 3. create top-level object @@ -130,23 +148,13 @@ void Yaz_PDU_Assoc::socketNotify(int event) 4. start thread */ int fd = cs_fileno(new_line); + logf (m_log, "accept ok fd = %d", fd); cs_fileno(new_line) = -1; - cs_close (new_line); /* potential problem ... */ -#if 1 + cs_close (new_line); childNotify(fd); -#else - Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable); - assoc->m_parent = this; - assoc->m_next = m_children; - m_children = assoc; - - assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc); - socket(fd); -#endif } - } - else if (m_state == Ready) - { + break; + case Ready: if (event & YAZ_SOCKET_OBSERVE_WRITE) { flush_PDU(); @@ -162,7 +170,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. @@ -170,6 +178,7 @@ void Yaz_PDU_Assoc::socketNotify(int event) m_destroyed = &destroyed; m_PDU_Observer->recv_PDU(m_input_buf, res); + m_destroyed = 0; if (destroyed) // it really was destroyed, return now. return; } while (m_cs && cs_more (m_cs)); @@ -178,6 +187,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(); } } @@ -197,7 +216,7 @@ void Yaz_PDU_Assoc::close() m_queue_out = m_queue_out->m_next; delete q_this; } -// free (m_input_buf); + xfree (m_input_buf); m_input_buf = 0; m_input_len = 0; } @@ -339,6 +358,7 @@ void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer, m_socketObservable->addObserver(cs_fileno(cs), this); m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| YAZ_SOCKET_OBSERVE_EXCEPT); + logf (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(cs)); m_state = Listen; } @@ -366,9 +386,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. state is Closed.. } void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd) @@ -389,54 +411,75 @@ void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd) } #if 1 + // 1 = single-threaded + // 0 = multi-threaded + +// Single-threaded... Only useful for non-blocking handlers void Yaz_PDU_Assoc::childNotify(int fd) { - /// Clone PDU Observable (keep socket manager) + // Clone PDU Observable (keep socket manager) IYaz_PDU_Observable *new_observable = clone(); - /// Clone PDU Observer - IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable); + // Clone PDU Observer + IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable, fd); - /// Attach new socket to it + // Attach new socket to it new_observable->socket(observer, fd); } #else -#include -#include +#include -class thread_info { - Yaz_SocketManager *socketManager; - IYaz_PDU_Observable * - -}; +#ifdef WIN32 +#include +#else +#include +#endif -static void *events(void *p) +#ifdef WIN32 +void __cdecl +#else +void * +#endif + events(void *p) { Yaz_SocketManager *s = (Yaz_SocketManager *) p; - + + logf (LOG_LOG, "thread started"); while (s->processEvent() > 0) ; + logf (LOG_LOG, "thread finished"); +#ifdef WIN32 +#else return 0; +#endif } void Yaz_PDU_Assoc::childNotify(int fd) { - Yaz_SocketManager *socket_observable = new Yaz_SocketManager; - IYaz_PDU_Observable *new_observable = clone(); + Yaz_SocketManager *socket_observable = new Yaz_SocketManager; + Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable); - m_socketObservable = 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); - + +#ifdef WIN32 + long t_id; + t_id = _beginthread (events, 0, socket_observable); + if (t_id == -1) + { + logf (LOG_FATAL|LOG_ERRNO, "_beginthread failed"); + exit (1); + } +#else pthread_t type; int id = pthread_create (&type, 0, events, socket_observable); logf (LOG_LOG, "pthread_create returned id=%d", id); +#endif } +// Threads end #endif -