Log request number. More configurable keepalive with pdu/bw limits.
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc-thread.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-pdu-assoc-thread.cpp,v 1.6 2002-10-09 12:50:26 adam Exp $
6  */
7
8 #ifdef WIN32
9 #define USE_THREADS 1
10 #endif
11
12 #if YAZ_POSIX_THREADS
13 #define USE_THREADS 1
14 #endif
15
16 #if USE_THREADS
17
18 #ifdef WIN32
19 #include <process.h>
20 #else
21 #include <pthread.h>
22 #include <unistd.h>
23 #endif
24
25 #include <errno.h>
26 #include <yaz/log.h>
27 #include <yaz/tcpip.h>
28
29 #include <yaz++/pdu-assoc.h>
30 #include <yaz++/socket-manager.h>
31
32 Yaz_PDU_AssocThread::Yaz_PDU_AssocThread(
33     IYazSocketObservable *socketObservable)
34     : Yaz_PDU_Assoc(socketObservable)
35 {
36     
37 }
38
39 #ifdef WIN32
40 void __cdecl
41 #else
42 void *
43 #endif 
44 events(void *p)
45 {
46     Yaz_SocketManager *s = (Yaz_SocketManager *) p;
47     
48     yaz_log (LOG_LOG, "thread started");
49     while (s->processEvent() > 0)
50         ;
51     yaz_log (LOG_LOG, "thread finished");
52 #ifdef WIN32
53 #else
54     return 0;
55 #endif
56 }
57
58 void Yaz_PDU_AssocThread::childNotify(COMSTACK cs)
59 {
60     Yaz_SocketManager *socket_observable = new Yaz_SocketManager;
61     Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable, cs);
62
63     new_observable->m_next = m_children;
64     m_children = new_observable;
65     new_observable->m_parent = this;
66
67     /// Clone PDU Observer
68     new_observable->m_PDU_Observer =
69         m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs));
70 #ifdef WIN32
71     long t_id;
72     t_id = _beginthread (events, 0, socket_observable);
73     if (t_id == -1)
74     {
75         yaz_log (LOG_FATAL|LOG_ERRNO, "_beginthread failed");
76         exit (1);
77     }
78 #else
79     pthread_t tid;
80
81     int id = pthread_create (&tid, 0, events, socket_observable);
82     if (id)
83         yaz_log (LOG_ERRNO|LOG_FATAL, "pthread_create returned id=%d", id);
84     else
85         pthread_detach (tid);
86 #endif
87 }
88 #endif