win32-name-event-objects-20040228
[openafs.git] / src / WINNT / afsadmsvr / TaAfsAdmSvrClientPing.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "TaAfsAdmSvrClientInternal.h"
16 #include <WINNT/AfsAppLib.h>
17
18
19 /*
20  * VARIABLES __________________________________________________________________
21  *
22  */
23
24 static struct
25    {
26    HANDLE hPingThread;
27    DWORD *adwClients;
28    size_t cdwClients;
29
30    HANDLE hCallbackThread;
31    size_t cReqCallback;
32    } l;
33
34
35 /*
36  * PROTOTYPES _________________________________________________________________
37  *
38  */
39
40 DWORD WINAPI ClientPingThread (LPVOID lp);
41
42 DWORD WINAPI ClientCallbackThread (LPVOID lp);
43
44
45 /*
46  * ROUTINES ___________________________________________________________________
47  *
48  */
49
50 void StartPingThread (DWORD idClient)
51 {
52    asc_Enter();
53
54    for (size_t ii = 0; ii < l.cdwClients; ++ii)
55       {
56       if (!l.adwClients[ ii ])
57          break;
58       }
59    if (REALLOC (l.adwClients, l.cdwClients, 1+ii, 1))
60       {
61       l.adwClients[ ii ] = idClient;
62       }
63
64    if (!l.hPingThread)
65       {
66       DWORD dwThreadID;
67       l.hPingThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)ClientPingThread, (LPVOID)0, 0, &dwThreadID);
68       }
69
70    asc_Leave();
71 }
72
73
74 void StopPingThread (DWORD idClient)
75 {
76    asc_Enter();
77
78    for (size_t ii = 0; ii < l.cdwClients; ++ii)
79       {
80       if (l.adwClients[ ii ] == idClient)
81          l.adwClients[ ii ] = 0;
82       }
83
84    asc_Leave();
85 }
86
87
88 DWORD WINAPI ClientPingThread (LPVOID lp)
89 {
90    for (;;)
91       {
92       Sleep (csecAFSADMSVR_CLIENT_PING * 1000L);  // server adds race allowance
93
94       asc_Enter();
95
96       for (size_t ii = 0; ii < l.cdwClients; ++ii)
97          {
98          DWORD idClient;
99          if ((idClient = l.adwClients[ ii ]) == 0)
100             continue;
101
102          asc_Leave();
103
104          RpcTryExcept
105             {
106             ULONG status;
107             if (!AfsAdmSvr_Ping (idClient, &status))
108                {
109                if (status == ERROR_INVALID_HANDLE) // we've been disconnected!
110                   StopPingThread (idClient);
111                }
112             }
113          RpcExcept(1)
114             ;
115          RpcEndExcept
116
117          asc_Enter();
118          }
119
120       asc_Leave();
121       }
122
123    l.hPingThread = NULL;
124    return 0;
125 }
126
127
128 void StartCallbackThread (void)
129 {
130    asc_Enter();
131    if ((++l.cReqCallback) == 1)
132       {
133       DWORD dwThreadID;
134       l.hCallbackThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)ClientCallbackThread, (LPVOID)0, 0, &dwThreadID);
135       }
136    asc_Leave();
137 }
138
139
140 void StopCallbackThread (void)
141 {
142    asc_Enter();
143    if (!(l.cReqCallback) || !(--l.cReqCallback))
144       {
145       if (l.hCallbackThread)
146          {
147          TerminateThread (l.hCallbackThread, 0);
148          l.hCallbackThread = NULL;
149          }
150       }
151    asc_Leave();
152 }
153
154
155 DWORD WINAPI ClientCallbackThread (LPVOID lp)
156 {
157    // The callback thread's task is simple: it initiates a particular
158    // RPC, which never returns. (Well, actually, it will return if the
159    // server shuts down.) By leaving a thread active, the server has a
160    // context in which to perform callback calls.
161    //
162    RpcTryExcept
163       {
164       AfsAdmSvr_CallbackHost();
165       }
166    RpcExcept(1)
167       ;
168    RpcEndExcept
169
170    l.hCallbackThread = NULL;
171    return 0;
172 }
173