author | fturovic <frank.turovich@nokia.com> |
Thu, 05 Aug 2010 09:52:02 -0500 | |
changeset 1766 | 040623849d55 |
parent 1481 | c7f22cc57d44 |
child 1991 | 1c752f14a126 |
permissions | -rw-r--r-- |
60 | 1 |
/* |
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
// ClientManager.cpp: implementation of the CClientManager class. |
|
18 |
// |
|
19 |
////////////////////////////////////////////////////////////////////// |
|
20 |
||
21 |
#include "stdafx.h" |
|
22 |
#include "ClientManager.h" |
|
23 |
#include "TCErrorConstants.h" |
|
24 |
#include "resource.h" |
|
25 |
#include <stdio.h> |
|
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
26 |
#ifdef _PSAPI_PRESENT |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
27 |
#include <psapi.h> |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
28 |
#endif |
60 | 29 |
|
30 |
#ifdef _DEBUG |
|
31 |
extern BOOL gDoLogging; |
|
32 |
extern char TCDebugMsg[100]; |
|
33 |
#define TCDEBUGOPEN() if (gDoLogging) m_DebugLog->WaitForAccess(); |
|
34 |
#define TCDEBUGLOGS(s) if (gDoLogging) sprintf(TCDebugMsg,"%s", s); if (gDoLogging) m_DebugLog->log(TCDebugMsg); |
|
35 |
#define TCDEBUGLOGA1(s, a1) if (gDoLogging) sprintf(TCDebugMsg, s, a1); if (gDoLogging) m_DebugLog->log(TCDebugMsg); |
|
36 |
#define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) sprintf(TCDebugMsg, s, a1, a2); if (gDoLogging) m_DebugLog->log(TCDebugMsg); |
|
37 |
#define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) sprintf(TCDebugMsg, s, a1, a2, a3); if (gDoLogging) m_DebugLog->log(TCDebugMsg); |
|
38 |
#define TCDEBUGCLOSE() if (gDoLogging) m_DebugLog->ReleaseAccess(); |
|
39 |
#else |
|
40 |
#define TCDEBUGOPEN() |
|
41 |
#define TCDEBUGLOGS(s) |
|
42 |
#define TCDEBUGLOGA1(s, a1) |
|
43 |
#define TCDEBUGLOGA2(s, a1, a2) |
|
44 |
#define TCDEBUGLOGA3(s, a1, a2, a3) |
|
45 |
#define TCDEBUGCLOSE() |
|
46 |
#endif |
|
47 |
||
48 |
#ifdef _DEBUG |
|
49 |
static char* GetErrorText(DWORD inError); |
|
50 |
#endif |
|
51 |
||
52 |
////////////////////////////////////////////////////////////////////// |
|
53 |
// Construction/Destruction |
|
54 |
////////////////////////////////////////////////////////////////////// |
|
55 |
CClientManager::CClientManager() |
|
56 |
{ |
|
57 |
m_Server = NULL; |
|
58 |
m_StreamList = NULL; |
|
59 |
m_ErrorMonitorList = NULL; |
|
60 |
m_DebugLog = NULL; |
|
61 |
m_DllLocation = NULL; |
|
62 |
m_hServer = NULL; |
|
63 |
m_hServerThread = NULL; |
|
64 |
m_Version[0] = NULL; |
|
65 |
m_ServerLockFile = NULL; |
|
66 |
m_ServerExeFile = NULL; |
|
67 |
} |
|
68 |
CClientManager::CClientManager(HINSTANCE hinstDLL) |
|
69 |
{ |
|
70 |
#ifdef _DEBUG |
|
71 |
if (gDoLogging) |
|
72 |
m_DebugLog = new TCDebugLog("TCF_ClientLog", ::GetCurrentProcessId()); |
|
73 |
else |
|
74 |
m_DebugLog = NULL; |
|
75 |
||
76 |
#else |
|
77 |
m_DebugLog = NULL; |
|
78 |
#endif |
|
79 |
TCDEBUGOPEN(); |
|
80 |
TCDEBUGLOGS("CClientManager::CClientManager\n"); |
|
81 |
||
82 |
m_Server = new CServerCommand(); |
|
83 |
||
84 |
// lock server access (it might be running) |
|
85 |
m_Server->WaitforServerPipeAccess(); |
|
86 |
||
87 |
m_StreamList = new InputStreamList(); |
|
88 |
m_StreamList->clear(); |
|
89 |
||
90 |
m_ErrorMonitorList = new ErrorMonitorList(); |
|
91 |
m_ErrorMonitorList->clear(); |
|
92 |
||
93 |
m_DllLocation = new char[MAX_DLLPATHNAME]; |
|
94 |
::GetModuleFileName(hinstDLL, m_DllLocation, MAX_DLLPATHNAME); |
|
95 |
||
96 |
char exeDirectory[MAX_DLLPATHNAME] = {0}; |
|
97 |
strncpy(exeDirectory, m_DllLocation, MAX_DLLPATHNAME); |
|
98 |
size_t len = strlen(exeDirectory); |
|
99 |
// remove file |
|
100 |
for (int i = len-1; i > 0; i--) |
|
101 |
{ |
|
102 |
if (exeDirectory[i] == PATH_DELIMITER) |
|
103 |
break; |
|
104 |
} |
|
105 |
exeDirectory[i] = NULL; |
|
106 |
||
107 |
m_ServerExeFile = new char[MAX_DLLPATHNAME]; |
|
108 |
sprintf(m_ServerExeFile, "\"%s%c%s\"", exeDirectory, PATH_DELIMITER, SERVER_PROCESS_NAME); |
|
109 |
||
110 |
m_ServerLockFile = new char[MAX_DLLPATHNAME]; |
|
111 |
sprintf(m_ServerLockFile, "%s%c%s", exeDirectory, PATH_DELIMITER, SERVER_LOCKFILE_NAME); |
|
112 |
||
113 |
char name[100]; |
|
114 |
sprintf(name, "%s%ld", ERRORMONITORLIST_MUTEX_BASENAME, ::GetCurrentProcessId()); |
|
115 |
m_ErrorMonitorListMutex.Open(name, ERRORMONITORLIST_MUTEX_TIMEOUT); |
|
116 |
||
117 |
sprintf(name, "%s%ld", INPUTSTREAMLIST_MUTEX_BASENAME, ::GetCurrentProcessId()); |
|
118 |
m_StreamListMutex.Open(name, INPUTSTREAMLIST_MUTEX_TIMEOUT); |
|
119 |
||
120 |
m_hServer = NULL; |
|
121 |
m_hServerThread = NULL; |
|
122 |
||
123 |
// release server access |
|
124 |
m_Server->ReleaseServerPipeAccess(); |
|
125 |
int ret = ::LoadString(hinstDLL, IDS_VERSION, m_Version, MAX_VERSION_STRING); |
|
126 |
||
127 |
TCDEBUGCLOSE(); |
|
128 |
} |
|
129 |
||
130 |
CClientManager::~CClientManager() |
|
131 |
{ |
|
132 |
TCDEBUGOPEN(); |
|
133 |
TCDEBUGLOGS("CClientManager::~CClientManager\n"); |
|
134 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
135 |
||
136 |
if (m_Server) |
|
137 |
{ |
|
138 |
delete m_Server; |
|
139 |
m_Server = NULL; |
|
140 |
} |
|
141 |
||
142 |
WaitForStreamListAccess(); |
|
143 |
TCDEBUGLOGA1("CClientManager::~CClientManager: erasing stream list size=%d\n", InputStreamListSize()); |
|
144 |
if (m_StreamList) |
|
145 |
{ |
|
146 |
InputStreamList::iterator iter; |
|
147 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
148 |
{ |
|
149 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list next 1\n"); |
|
150 |
// m_StreamList->erase(iter); |
|
151 |
delete *iter; |
|
152 |
// TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list next 2\n"); |
|
153 |
} |
|
154 |
m_StreamList->clear(); |
|
155 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list done 1\n"); |
|
156 |
delete m_StreamList; |
|
157 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list done 2\n"); |
|
158 |
} |
|
159 |
ReleaseStreamListAccess(); |
|
160 |
m_StreamListMutex.Close(); |
|
161 |
||
162 |
WaitForErrorMonitorListAccess(); |
|
163 |
TCDEBUGLOGA1("CClientManager::~CClientManager: erasing monitor list size=%d\n", ErrorMonitorListSize()); |
|
164 |
if (m_ErrorMonitorList) |
|
165 |
{ |
|
166 |
ErrorMonitorList::iterator iter; |
|
167 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
168 |
{ |
|
169 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list next 1\n"); |
|
170 |
// m_ErrorMonitorList->erase(iter); |
|
171 |
delete *iter; |
|
172 |
// TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list next 1\n"); |
|
173 |
} |
|
174 |
m_ErrorMonitorList->clear(); |
|
175 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list done 1\n"); |
|
176 |
delete m_ErrorMonitorList; |
|
177 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list done 2\n"); |
|
178 |
} |
|
179 |
ReleaseErrorMonitorListAccess(); |
|
180 |
m_ErrorMonitorListMutex.Close(); |
|
181 |
||
182 |
if (m_DllLocation) |
|
183 |
{ |
|
184 |
delete[] m_DllLocation; |
|
185 |
m_DllLocation = NULL; |
|
186 |
} |
|
187 |
||
188 |
if (m_ServerLockFile) |
|
189 |
{ |
|
190 |
delete[] m_ServerLockFile; |
|
191 |
m_ServerLockFile = NULL; |
|
192 |
} |
|
193 |
||
194 |
if (m_ServerExeFile) |
|
195 |
{ |
|
196 |
delete[] m_ServerExeFile; |
|
197 |
m_ServerExeFile = NULL; |
|
198 |
} |
|
199 |
TCDEBUGLOGS("CClientManager::~CClientManager: closing log\n"); |
|
200 |
TCDEBUGCLOSE(); |
|
201 |
if (m_DebugLog) |
|
202 |
{ |
|
203 |
delete m_DebugLog; |
|
204 |
m_DebugLog = NULL; |
|
205 |
} |
|
206 |
} |
|
1481
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
207 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
208 |
#ifdef _PSAPI_PRESENT |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
209 |
void |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
210 |
CClientManager::FindOrCreateRunningServer() |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
211 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
212 |
TCDEBUGOPEN(); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
213 |
TCDEBUGLOGS("CClientManager::FindOrCreateRunningServer: searching for existing TCFServer.exe\n"); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
214 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
215 |
// Directory where TCFServer.exe lives |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
216 |
char exeDirectory[MAX_DLLPATHNAME] = {0}; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
217 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
218 |
BOOL foundRunningServer = FALSE; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
219 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
220 |
// Get the list of process identifiers. |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
221 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
222 |
DWORD aProcesses[1024], cbNeeded, cProcesses; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
223 |
unsigned int i; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
224 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
225 |
if ( EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
226 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
227 |
// Calculate how many process identifiers were returned. |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
228 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
229 |
cProcesses = cbNeeded / sizeof(DWORD); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
230 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
231 |
// Find existing TCFServer |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
232 |
for ( i = 0; i < cProcesses; i++ ) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
233 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
234 |
if( aProcesses[i] != 0 ) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
235 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
236 |
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
237 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
238 |
// Get a handle to the process. |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
239 |
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
240 |
PROCESS_VM_READ, |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
241 |
FALSE, aProcesses[i] ); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
242 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
243 |
// Get the process name. |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
244 |
if (NULL != hProcess ) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
245 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
246 |
HMODULE hMod; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
247 |
DWORD cbNeeded; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
248 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
249 |
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
250 |
&cbNeeded) ) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
251 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
252 |
if (GetModuleBaseNameA( hProcess, hMod, szProcessName, |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
253 |
sizeof(szProcessName)/sizeof(char) )) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
254 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
255 |
if (strcmp(SERVER_PROCESS_NAME, szProcessName) == 0) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
256 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
257 |
// get the full path |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
258 |
GetModuleFileNameExA(hProcess, hMod, exeDirectory, |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
259 |
sizeof(exeDirectory)/sizeof(char) ); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
260 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
261 |
TCDEBUGLOGA1("Found TCFServer at %s\n", exeDirectory); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
262 |
CloseHandle( hProcess ); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
263 |
foundRunningServer = TRUE; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
264 |
break; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
265 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
266 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
267 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
268 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
269 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
270 |
CloseHandle( hProcess ); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
271 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
272 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
273 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
274 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
275 |
if (!foundRunningServer) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
276 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
277 |
// start the one next to the DLL |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
278 |
strncpy(exeDirectory, m_DllLocation, strlen(m_DllLocation) + 1); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
279 |
size_t len = strlen(exeDirectory); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
280 |
// remove file |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
281 |
for (int i = len-1; i > 0; i--) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
282 |
{ |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
283 |
if (exeDirectory[i] == PATH_DELIMITER) |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
284 |
break; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
285 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
286 |
exeDirectory[i] = NULL; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
287 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
288 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
289 |
m_ServerExeFile = new char[MAX_DLLPATHNAME]; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
290 |
sprintf(m_ServerExeFile, "\"%s%c%s\"", exeDirectory, PATH_DELIMITER, SERVER_PROCESS_NAME); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
291 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
292 |
m_ServerLockFile = new char[MAX_DLLPATHNAME]; |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
293 |
sprintf(m_ServerLockFile, "%s%c%s", exeDirectory, PATH_DELIMITER, SERVER_LOCKFILE_NAME); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
294 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
295 |
TCDEBUGCLOSE(); |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
296 |
|
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
297 |
} |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
298 |
#endif |
c7f22cc57d44
Commit some changes from Ed, also for USB, return different error for h/w comm error as opposed to where there is no error reported by windows, but the bytes did not get sent to the device (not responding)
Chad Peckham <chad.peckham@nokia.com>
parents:
914
diff
changeset
|
299 |
|
60 | 300 |
CErrorMonitor* |
301 |
CClientManager::FindErrorMonitor(long inClientId) |
|
302 |
{ |
|
303 |
CErrorMonitor* errorMonitor = NULL; |
|
304 |
ErrorMonitorList::iterator iter; |
|
305 |
||
306 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
307 |
{ |
|
308 |
if ((*iter)->IsThisClient(inClientId)) |
|
309 |
{ |
|
310 |
errorMonitor = *iter; |
|
311 |
break; |
|
312 |
} |
|
313 |
} |
|
314 |
return errorMonitor; |
|
315 |
} |
|
316 |
long CClientManager::ErrorMonitorListSize() |
|
317 |
{ |
|
318 |
long size = m_ErrorMonitorList->size(); |
|
319 |
||
320 |
return size; |
|
321 |
} |
|
322 |
void CClientManager::AddErrorMonitor(CErrorMonitor* monitor) |
|
323 |
{ |
|
324 |
m_ErrorMonitorList->push_back(monitor); |
|
325 |
} |
|
326 |
void CClientManager::RemoveErrorMonitor(CErrorMonitor* monitor) |
|
327 |
{ |
|
328 |
ErrorMonitorList::iterator iter; |
|
329 |
||
330 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
331 |
{ |
|
332 |
if ((*iter)->IsThisClient(monitor->GetClientId())) |
|
333 |
{ |
|
334 |
m_ErrorMonitorList->erase(iter); |
|
335 |
break; |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
CInputStream* |
|
341 |
CClientManager::FindInputStream(long inClientId) |
|
342 |
{ |
|
343 |
CInputStream* inputStream = NULL; |
|
344 |
InputStreamList::iterator iter; |
|
345 |
||
346 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
347 |
{ |
|
348 |
if ((*iter)->IsThisClient(inClientId)) |
|
349 |
// if ((*iter).IsThisClient(inClientId)) |
|
350 |
{ |
|
351 |
inputStream = *iter; |
|
352 |
// inputStream = iter; |
|
353 |
break; |
|
354 |
} |
|
355 |
} |
|
356 |
return inputStream; |
|
357 |
} |
|
358 |
long CClientManager::InputStreamListSize() |
|
359 |
{ |
|
360 |
long size = m_StreamList->size(); |
|
361 |
||
362 |
return size; |
|
363 |
} |
|
364 |
void CClientManager::AddInputStream(CInputStream* stream) |
|
365 |
{ |
|
366 |
m_StreamList->push_back(stream); |
|
367 |
// m_StreamList->push_back(*stream); |
|
368 |
||
369 |
} |
|
370 |
void CClientManager::RemoveInputStream(CInputStream* stream) |
|
371 |
{ |
|
372 |
InputStreamList::iterator iter; |
|
373 |
||
374 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
375 |
{ |
|
376 |
if ((*iter)->IsThisClient(stream->GetClientId())) |
|
377 |
// if ((*iter).IsThisClient(stream->GetClientId())) |
|
378 |
{ |
|
379 |
m_StreamList->erase(iter); |
|
380 |
break; |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
BOOL CClientManager::StartServer(pServerProcessData pData) |
|
386 |
{ |
|
387 |
TCDEBUGLOGA1("CClientManager::StartServer numRefs = %d\n",pData->numRefs); |
|
388 |
||
389 |
BOOL serverStarted = FALSE; |
|
390 |
// server is ref counted |
|
391 |
// refcount = 0 => server is not running |
|
392 |
// refcount > 0 => server already started by some other process |
|
393 |
if (pData->numRefs == 0) |
|
394 |
{ |
|
395 |
// server not running |
|
396 |
// get exe location |
|
397 |
char exeLocation[MAX_DLLPATHNAME] = {0}; |
|
398 |
strncpy(exeLocation, m_DllLocation, MAX_DLLPATHNAME); |
|
399 |
size_t len = strlen(exeLocation); |
|
400 |
// remove file |
|
401 |
for (int i = len-1; i > 0; i--) |
|
402 |
{ |
|
403 |
if (exeLocation[i] == PATH_DELIMITER) |
|
404 |
break; |
|
405 |
} |
|
406 |
exeLocation[i] = NULL; |
|
407 |
char quotedLocation[MAX_DLLPATHNAME] = {0}; |
|
408 |
sprintf(quotedLocation, "\"%s%c%s\"", exeLocation, PATH_DELIMITER, SERVER_PROCESS_NAME); |
|
409 |
||
410 |
TCDEBUGLOGA1(" exeLocation=%s\n", quotedLocation); |
|
411 |
||
412 |
// create process |
|
413 |
STARTUPINFO si; |
|
414 |
memset(&si,0,sizeof(si)); |
|
415 |
si.cb = sizeof(si); |
|
416 |
memset(&pData->serverProcess, 0, sizeof(pData->serverProcess)); |
|
417 |
pData->serverProcess.hProcess = NULL; |
|
418 |
if (!::CreateProcess( |
|
419 |
NULL, // module location |
|
420 |
quotedLocation, // command line |
|
421 |
NULL, // process attributes |
|
422 |
NULL, // thread attributes |
|
423 |
FALSE, // inherit our handles |
|
424 |
CREATE_NO_WINDOW, // no window |
|
425 |
NULL, // use our environment |
|
426 |
NULL, // user our current directory |
|
427 |
&si, // startup info |
|
428 |
&pData->serverProcess)) // process info |
|
429 |
{ |
|
430 |
// TODO: error creating process |
|
431 |
} |
|
432 |
else |
|
433 |
{ |
|
434 |
// we are the creator so save handles for later |
|
435 |
m_hServer = pData->serverProcess.hProcess; |
|
436 |
m_hServerThread = pData->serverProcess.hThread; |
|
437 |
// add a refcount |
|
438 |
pData->numRefs++; |
|
439 |
serverStarted = TRUE; |
|
440 |
} |
|
441 |
} |
|
442 |
else |
|
443 |
{ |
|
444 |
// already running |
|
445 |
// add a refcount and open our process handle to it |
|
446 |
pData->numRefs++; |
|
447 |
m_hServer = ::OpenProcess(0, FALSE, pData->serverProcess.dwProcessId); |
|
448 |
serverStarted = TRUE; |
|
449 |
} |
|
450 |
TCDEBUGLOGA1("CClientManager::StartServer serverStarted=%d\n", serverStarted); |
|
451 |
return serverStarted; |
|
452 |
} |
|
453 |
||
454 |
BOOL CClientManager::StopServer(pServerProcessData pData) |
|
455 |
{ |
|
456 |
TCDEBUGLOGS("CClientManager::StopServer\n"); |
|
457 |
||
458 |
BOOL serverStopped = FALSE; |
|
459 |
||
460 |
if (pData->serverProcess.hProcess == NULL || pData->numRefs <= 0) |
|
461 |
{ |
|
462 |
serverStopped = TRUE; |
|
463 |
} |
|
464 |
else |
|
465 |
{ |
|
466 |
TCDEBUGLOGA1(" numRefs = %d\n",pData->numRefs); |
|
467 |
||
468 |
// substract ref count |
|
469 |
pData->numRefs--; |
|
470 |
// if refcount == 0 then really stop the server process |
|
471 |
if (pData->numRefs <= 0) |
|
472 |
{ |
|
473 |
// last client process is closing |
|
474 |
// tell server to exit |
|
475 |
ServerCommandData cmdrsp; |
|
476 |
cmdrsp.command = eCmdExit; |
|
477 |
TCDEBUGLOGS(" SendCommand eCmdExit\n"); |
|
478 |
m_Server->SendCommand(&cmdrsp); |
|
479 |
TCDEBUGLOGS(" GetResponse eExit\n"); |
|
480 |
m_Server->GetResponse(&cmdrsp); |
|
481 |
// wait for process to exit |
|
482 |
TCDEBUGLOGS(" WaitForSingleObject start\n"); |
|
483 |
WaitForSingleObject(m_hServer, 10000L /*INFINITE*/); |
|
484 |
TCDEBUGLOGS(" WaitForSingleObject found\n"); |
|
485 |
||
486 |
if (m_hServer != NULL) |
|
487 |
CloseHandle(m_hServer); |
|
488 |
||
489 |
if (m_hServerThread != NULL) |
|
490 |
CloseHandle(m_hServerThread); |
|
491 |
} |
|
492 |
else |
|
493 |
{ |
|
494 |
// just close our handle to server process |
|
495 |
if (m_hServer != NULL) |
|
496 |
CloseHandle(m_hServer); |
|
497 |
||
498 |
if (m_hServerThread != NULL) |
|
499 |
CloseHandle(m_hServerThread); |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
TCDEBUGLOGS("CClientManager::StopServer end\n"); |
|
504 |
return TRUE; |
|
505 |
} |
|
506 |
long CClientManager::StartServer() |
|
507 |
{ |
|
508 |
long ret = TCAPI_ERR_NONE; |
|
509 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
510 |
||
511 |
TCDEBUGLOGA3("CClientManager::StartServer this = %x m_hServer = %x numRefs = %d\n", this, m_hServer, pData->numRefs); |
|
512 |
// TCDEBUGLOGA1(" mgrRefs = %d\n", m_MgrServerRef); |
|
513 |
||
514 |
BOOL serverStarted = FALSE; |
|
515 |
// server is ref counted |
|
516 |
// refcount = 0 => server is not running |
|
517 |
// refcount > 0 => server already started by some other process |
|
518 |
||
519 |
// terminate the TCFServer if it is already running |
|
520 |
TerminateServerThroughLockFile(pData); |
|
521 |
||
522 |
if (pData->numRefs == 0) |
|
523 |
{ |
|
524 |
// server not running |
|
525 |
TCDEBUGLOGA1(" TCFServer exe =%s\n", m_ServerExeFile); |
|
526 |
TCDEBUGLOGA1(" TCFServer lock=%s\n", m_ServerLockFile); |
|
527 |
||
528 |
||
529 |
// create process |
|
530 |
STARTUPINFO si; |
|
531 |
memset(&si,0,sizeof(si)); |
|
532 |
si.cb = sizeof(si); |
|
533 |
memset(&pData->serverProcess, 0, sizeof(pData->serverProcess)); |
|
534 |
pData->serverProcess.hProcess = NULL; |
|
535 |
if (!::CreateProcess( |
|
536 |
NULL, // module location |
|
537 |
m_ServerExeFile, // command line |
|
538 |
NULL, // process attributes |
|
539 |
NULL, // thread attributes |
|
540 |
FALSE, // inherit our handles |
|
541 |
CREATE_NO_WINDOW, // no window |
|
542 |
NULL, // use our environment |
|
543 |
NULL, // user our current directory |
|
544 |
&si, // startup info |
|
545 |
&pData->serverProcess)) // process info |
|
546 |
{ |
|
547 |
// TODO: error creating process |
|
548 |
} |
|
549 |
else |
|
550 |
{ |
|
551 |
// we are the creator so save handles for later |
|
552 |
m_hServer = pData->serverProcess.hProcess; |
|
553 |
m_hServerThread = pData->serverProcess.hThread; |
|
554 |
// add a refcount |
|
555 |
pData->numRefs++; |
|
556 |
serverStarted = TRUE; |
|
557 |
TCDEBUGLOGA3("CClientManager::StartServer created m_hServer = %x processId = %d numRefs = %d\n", m_hServer, pData->serverProcess.dwProcessId, pData->numRefs); |
|
558 |
||
559 |
// create lock file and save process ID |
|
560 |
TCDEBUGLOGS("CClientManager::StartServer CreateLockFile\n"); |
|
561 |
CreateLockFile(pData->serverProcess.dwProcessId); |
|
562 |
} |
|
563 |
} |
|
564 |
else |
|
565 |
{ |
|
566 |
// already running |
|
567 |
// add a refcount and open our process handle to it only if we haven't opened it already |
|
568 |
pData->numRefs++; |
|
569 |
if (m_hServer == NULL) |
|
570 |
m_hServer = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pData->serverProcess.dwProcessId); |
|
571 |
if (m_hServer == 0) |
|
572 |
{ |
|
573 |
TCDEBUGLOGA1("CClientManager::StartServer opened m_hServer null error=%d\n", ::GetLastError()); |
|
574 |
} |
|
575 |
m_hServerThread = NULL; // only creator gets real thread handle |
|
576 |
serverStarted = TRUE; |
|
577 |
TCDEBUGLOGA3("CClientManager::StartServer opened m_hServer = %x processId = %d numRefs = %d\n", m_hServer, pData->serverProcess.dwProcessId, pData->numRefs); |
|
578 |
// save our process id to lock file |
|
579 |
AppendToLockFile(pData->serverProcess.dwProcessId); |
|
580 |
} |
|
581 |
if (serverStarted) |
|
582 |
m_ServerRunning = TRUE; |
|
583 |
||
584 |
TCDEBUGLOGA1("CClientManager::StartServer end numRefs = %d\n", pData->numRefs); |
|
585 |
return ret; |
|
586 |
} |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
587 |
BOOL CClientManager::IsTCFServerActive(DWORD processId) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
588 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
589 |
HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processId); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
590 |
if (h) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
591 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
592 |
// is it really still alive? |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
593 |
DWORD exitCode = -5; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
594 |
BOOL exitCall = ::GetExitCodeProcess(h, &exitCode); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
595 |
::CloseHandle(h); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
596 |
if (exitCall == TRUE && exitCode != STILL_ACTIVE) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
597 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
598 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
599 |
return FALSE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
600 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
601 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
602 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
603 |
// TCFServer is still active |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
604 |
return TRUE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
605 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
606 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
607 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
608 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
609 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
610 |
return FALSE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
611 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
612 |
} |
60 | 613 |
long CClientManager::StopServer() |
614 |
{ |
|
615 |
long ret = TCAPI_ERR_NONE; |
|
616 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
617 |
||
618 |
TCDEBUGLOGA3("CClientManager::StopServer this = %x m_hServer = %x numRefs = %d\n", this, m_hServer, pData->numRefs); |
|
619 |
||
620 |
BOOL serverStopped = FALSE; |
|
621 |
||
622 |
if (pData->serverProcess.hProcess == NULL || pData->numRefs <= 0) |
|
623 |
{ |
|
624 |
TCDEBUGLOGS("CClientManager::StopServer hProcess NULL or numRefs <= 0\n"); |
|
625 |
serverStopped = TRUE; |
|
626 |
} |
|
627 |
#if (0) |
|
628 |
else if (m_hServer == NULL) |
|
629 |
{ |
|
630 |
// we've already closed our handle to server |
|
631 |
// don't close it again |
|
632 |
TCDEBUGLOGS("CClientManager::StopServer m_hServer null\n"); |
|
633 |
} |
|
634 |
#endif |
|
635 |
else |
|
636 |
{ |
|
637 |
// substract ref count |
|
638 |
pData->numRefs--; |
|
639 |
if (pData->numRefs < 0) pData->numRefs = 0; |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
640 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
641 |
bool sendStop = true; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
642 |
if (!IsTCFServerActive(pData->serverProcess.dwProcessId)) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
643 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
644 |
sendStop = false; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
645 |
pData->numRefs = 0; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
646 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
647 |
|
60 | 648 |
// if refcount == 0 then really stop the server process |
649 |
if (pData->numRefs == 0) |
|
650 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
651 |
if (sendStop) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
652 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
653 |
// last client process is closing |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
654 |
// tell server to exit |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
655 |
ServerCommandData cmdrsp; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
656 |
cmdrsp.command = eCmdExit; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
657 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
658 |
TCDEBUGLOGS(" SendCommand eCmdExit\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
659 |
m_Server->SendCommand(&cmdrsp); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
660 |
TCDEBUGLOGS(" GetResponse eExit\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
661 |
m_Server->GetResponse(&cmdrsp); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
662 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
663 |
// wait for process to exit |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
664 |
TCDEBUGLOGS(" WaitForSingleObject start\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
665 |
DWORD waitErr = ::WaitForSingleObject(m_hServer, 10000L /*INFINITE*/); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
666 |
TCDEBUGLOGA1("CClientManager::StopServer WaitForSingleObject = %d\n", waitErr); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
667 |
} |
60 | 668 |
|
669 |
// now close our handle to server process |
|
670 |
if (m_hServer != NULL) |
|
671 |
{ |
|
672 |
CloseHandle(m_hServer); |
|
673 |
m_hServer = NULL; |
|
674 |
} |
|
675 |
||
676 |
if (m_hServerThread != NULL) |
|
677 |
{ |
|
678 |
CloseHandle(m_hServerThread); |
|
679 |
m_hServerThread = NULL; |
|
680 |
} |
|
681 |
serverStopped = TRUE; |
|
682 |
||
683 |
// delete lock file |
|
684 |
TCDEBUGLOGS("CClientManager::StopServer DeleteLockFile\n"); |
|
685 |
DeleteLockFile(); |
|
686 |
} |
|
687 |
else |
|
688 |
{ |
|
689 |
// just close our handle to server process |
|
690 |
||
691 |
if (m_hServer != NULL) |
|
692 |
{ |
|
693 |
CloseHandle(m_hServer); |
|
694 |
m_hServer = NULL; |
|
695 |
} |
|
696 |
||
697 |
if (m_hServerThread != NULL) |
|
698 |
{ |
|
699 |
CloseHandle(m_hServerThread); |
|
700 |
m_hServerThread = NULL; |
|
701 |
} |
|
702 |
DeleteFromLockFile(pData->serverProcess.dwProcessId); |
|
703 |
} |
|
704 |
} |
|
705 |
if (serverStopped) |
|
706 |
m_ServerRunning = FALSE; |
|
707 |
||
708 |
TCDEBUGLOGA1("CClientManager::StopServer end numRefs = %d\n", pData->numRefs); |
|
709 |
return ret; |
|
710 |
} |
|
711 |
||
712 |
BOOL CClientManager::IsServerRunning() |
|
713 |
{ |
|
714 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
715 |
if (IsTCFServerActive(pData->serverProcess.dwProcessId)) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
716 |
{ |
60 | 717 |
return TRUE; |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
718 |
} |
60 | 719 |
else |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
720 |
{ |
60 | 721 |
return FALSE; |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
722 |
} |
60 | 723 |
} |
724 |
||
725 |
void CClientManager::CreateLockFile(DWORD processId) |
|
726 |
{ |
|
727 |
if (m_ServerLockFile != NULL) |
|
728 |
{ |
|
729 |
FILE* f = fopen(m_ServerLockFile, "wt"); |
|
730 |
TCDEBUGLOGA1("CClientManager::CreateLockFile f=%x\n", f); |
|
731 |
||
732 |
if (f) |
|
733 |
{ |
|
734 |
DWORD callingProcessId = ::GetCurrentProcessId(); |
|
735 |
TCDEBUGLOGA2("CClientManager::CreateLockFile callingProcessId=%d processId=%d\n", callingProcessId, processId); |
|
736 |
fprintf(f, "%ld %ld\n", callingProcessId, processId); |
|
737 |
fclose(f); |
|
738 |
} |
|
739 |
else |
|
740 |
{ |
|
741 |
DWORD err = ::GetLastError(); |
|
742 |
TCDEBUGLOGA2("CClientManager::CreateLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
743 |
} |
|
744 |
} |
|
745 |
} |
|
746 |
void CClientManager::AppendToLockFile(DWORD processId) |
|
747 |
{ |
|
748 |
if (m_ServerLockFile != NULL) |
|
749 |
{ |
|
750 |
FILE* f = fopen(m_ServerLockFile, "at"); |
|
751 |
TCDEBUGLOGA1("CClientManager::AppendToLockFile f=%x\n", f); |
|
752 |
||
753 |
if (f) |
|
754 |
{ |
|
755 |
DWORD callingProcessId = ::GetCurrentProcessId(); |
|
756 |
TCDEBUGLOGA2("CClientManager::AppendToLockFile callingProcessId=%d processId=%d\n", callingProcessId, processId); |
|
757 |
fprintf(f, "%ld %ld\n", callingProcessId, processId); |
|
758 |
fclose(f); |
|
759 |
} |
|
760 |
else |
|
761 |
{ |
|
762 |
DWORD err = ::GetLastError(); |
|
763 |
TCDEBUGLOGA2("CClientManager::AppendToLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
764 |
} |
|
765 |
} |
|
766 |
} |
|
767 |
void CClientManager::DeleteLockFile() |
|
768 |
{ |
|
769 |
if (m_ServerLockFile != NULL) |
|
770 |
{ |
|
771 |
TCDEBUGLOGS("CClientManager::DeleteLockFile\n"); |
|
772 |
::remove(m_ServerLockFile); |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
void CClientManager::DeleteFromLockFile(DWORD serverProcessId) |
|
777 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
778 |
DWORD creatorIds[10]; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
779 |
DWORD serverIds[10]; |
60 | 780 |
int numIds = 0; |
781 |
||
782 |
DWORD ourProcessId = ::GetCurrentProcessId(); |
|
783 |
||
784 |
if (m_ServerLockFile != NULL) |
|
785 |
{ |
|
786 |
DWORD attr = ::GetFileAttributes(m_ServerLockFile); |
|
787 |
TCDEBUGLOGA1("CClientManager::DeleteFromLockFile attr=%x\n", attr); |
|
788 |
||
789 |
if (attr != 0xffffffff) // error |
|
790 |
{ |
|
791 |
// file exists |
|
792 |
// read the process Ids from it |
|
793 |
||
794 |
FILE *f = fopen(m_ServerLockFile, "rt"); |
|
795 |
TCDEBUGLOGA1("CClientManager::DeleteFromLockFile f=%x\n", f); |
|
796 |
if (f) |
|
797 |
{ |
|
798 |
BOOL done = FALSE; |
|
799 |
while (!done) |
|
800 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
801 |
DWORD creatorId = 0xffffffff; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
802 |
DWORD serverId = 0xffffffff; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
803 |
int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId); |
60 | 804 |
if (n == 2) |
805 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
806 |
TCDEBUGLOGA3("CClientManager::DeleteFromLockFile numIds=%d creatorId=%d serverId=%d\n", numIds, creatorId, serverId); |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
807 |
if (creatorId != ourProcessId || serverId != serverProcessId) |
60 | 808 |
{ |
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
809 |
creatorIds[numIds] = creatorId; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
810 |
serverIds[numIds] = serverId; |
60 | 811 |
numIds++; |
812 |
if (numIds > 9) |
|
813 |
done = TRUE; |
|
814 |
} |
|
815 |
} |
|
816 |
else |
|
817 |
{ |
|
818 |
done = TRUE; |
|
819 |
} |
|
820 |
} |
|
821 |
fclose(f); |
|
822 |
} |
|
823 |
||
824 |
// now rewrite lock file without us |
|
825 |
::remove(m_ServerLockFile); |
|
826 |
if (numIds > 0) |
|
827 |
{ |
|
828 |
f = fopen(m_ServerLockFile, "wt"); |
|
829 |
if (f) |
|
830 |
{ |
|
831 |
for (int i = 0; i < numIds; i++) |
|
832 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
833 |
fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]); |
60 | 834 |
} |
835 |
fclose(f); |
|
836 |
} |
|
837 |
} |
|
838 |
} |
|
839 |
} |
|
840 |
} |
|
841 |
||
842 |
// Currently assumes there is only ONE TCFServer, but multiple client processes (that use that server) |
|
843 |
// we should not have more than a few Carbide processes connecting to the same TCFServer |
|
844 |
void CClientManager::TerminateServerThroughLockFile(pServerProcessData pData) |
|
845 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
846 |
DWORD creatorIds[10]; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
847 |
DWORD serverIds[10]; |
60 | 848 |
BOOL liveCaller[10]; |
849 |
int numIds = 0; |
|
850 |
if (m_ServerLockFile != NULL) |
|
851 |
{ |
|
852 |
DWORD attr = ::GetFileAttributes(m_ServerLockFile); |
|
853 |
TCDEBUGLOGA1("CClientManager::TerminateServerThroughLockFile attr=%x\n", attr); |
|
854 |
||
855 |
if (attr != 0xffffffff) // error |
|
856 |
{ |
|
857 |
// file exists |
|
858 |
// read the process Ids from it |
|
859 |
||
860 |
FILE *f = fopen(m_ServerLockFile, "rt"); |
|
861 |
TCDEBUGLOGA1("CClientManager::TerminateServerThroughLockFile f=%x\n", f); |
|
862 |
if (f) |
|
863 |
{ |
|
864 |
BOOL done = FALSE; |
|
865 |
while (!done) |
|
866 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
867 |
DWORD creatorId = 0xffffffff; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
868 |
DWORD serverId = 0xffffffff; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
869 |
int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId); |
60 | 870 |
if (n == 2) |
871 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
872 |
TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile n=%d creatorId=%d serverId=%d\n", n, creatorId, serverId); |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
873 |
creatorIds[numIds] = creatorId; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
874 |
serverIds[numIds] = serverId; |
60 | 875 |
numIds++; |
876 |
if (numIds > 9) |
|
877 |
done = TRUE; |
|
878 |
} |
|
879 |
else |
|
880 |
{ |
|
881 |
done = TRUE; |
|
882 |
} |
|
883 |
} |
|
884 |
fclose(f); |
|
885 |
||
886 |
int numDeadCallers = 0; |
|
887 |
for (int i = 0; i < numIds; i++) |
|
888 |
{ |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
889 |
HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, creatorIds[i]); |
60 | 890 |
if (h) |
891 |
{ |
|
892 |
// calling process is still alive |
|
893 |
liveCaller[i] = TRUE; |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
894 |
DWORD exitCode = -5; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
895 |
BOOL exitCall = ::GetExitCodeProcess(h, &exitCode); |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
896 |
DWORD id = ::GetCurrentProcessId(); |
60 | 897 |
::CloseHandle(h); |
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
898 |
TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d alive exitCall=%d currentId=%d\n", creatorIds[i], exitCall, id); |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
899 |
if (exitCall == TRUE && exitCode != STILL_ACTIVE) |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
900 |
{ |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
901 |
liveCaller[i] = FALSE; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
902 |
numDeadCallers++; |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
903 |
} |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
904 |
{ |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
905 |
TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile exitCode=%d still_active=%d\n", exitCode, STILL_ACTIVE); |
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
906 |
} |
60 | 907 |
} |
908 |
else |
|
909 |
{ |
|
910 |
liveCaller[i] = FALSE; |
|
911 |
numDeadCallers++; |
|
912 |
DWORD err = ::GetLastError(); |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
913 |
TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d dead err=%d:%s\n", creatorIds[i], err, GetErrorText(err)); |
60 | 914 |
} |
915 |
} |
|
916 |
if (numDeadCallers == numIds) |
|
917 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
918 |
// All clients of this TCFServer are dead |
60 | 919 |
// terminate the TCFServer, and delete lock file |
920 |
pData->numRefs = 0; |
|
921 |
::remove(m_ServerLockFile); |
|
505
6de8d9cfdda1
modify how we determine if the TCFServer process creator is still alive - this will help in killing orphan TCFServer processes on startup
Chad Peckham <chad.peckham@nokia.com>
parents:
60
diff
changeset
|
922 |
HANDLE h = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, serverIds[0]); |
60 | 923 |
if (h) |
924 |
{ |
|
925 |
BOOL ret = ::TerminateProcess(h, -1); |
|
926 |
if (ret == 0) |
|
927 |
{ |
|
928 |
DWORD err = ::GetLastError(); |
|
929 |
TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile TerminateProcess=%d:%s\n", err, GetErrorText(err)); |
|
930 |
} |
|
931 |
::CloseHandle(h); |
|
932 |
} |
|
933 |
} |
|
934 |
else |
|
935 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
936 |
// some java clients are still alive |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
937 |
// check to see if TCFServer is still alive |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
938 |
if (IsTCFServerActive(serverIds[0])) |
60 | 939 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
940 |
// TCFServer is still active |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
941 |
// leave TCFServer running, recreate lock file and save live callers |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
942 |
::remove(m_ServerLockFile); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
943 |
f = fopen(m_ServerLockFile, "wt"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
944 |
if (f) |
60 | 945 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
946 |
for (int i = 0; i < numIds; i++) |
60 | 947 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
948 |
if (liveCaller[i]) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
949 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
950 |
fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
951 |
} |
60 | 952 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
953 |
fclose(f); |
60 | 954 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
955 |
pData->numRefs -= numDeadCallers; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
956 |
if (pData->numRefs < 0) pData->numRefs = 0; |
60 | 957 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
958 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
959 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
960 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
961 |
pData->numRefs = 0; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
962 |
::remove(m_ServerLockFile); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
963 |
} |
60 | 964 |
} |
965 |
} |
|
966 |
else |
|
967 |
{ |
|
968 |
// error opening lock file |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
969 |
// perhaps the user deleted it, |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
970 |
// if so, we assume he has also deleted the TCFServer as we now have no way of verifying if the |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
971 |
// process is dead. |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
972 |
pData->numRefs = 0; |
60 | 973 |
DWORD err = ::GetLastError(); |
974 |
TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
975 |
} |
|
976 |
} |
|
977 |
} |
|
978 |
} |
|
979 |
#ifdef _DEBUG |
|
980 |
static char* GetErrorText(DWORD inError) |
|
981 |
{ |
|
982 |
static char msg[256]; |
|
983 |
FormatMessage( |
|
984 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
|
985 |
NULL, |
|
986 |
inError, |
|
987 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
988 |
(LPTSTR) &msg, |
|
989 |
sizeof(msg) - 1, |
|
990 |
NULL); |
|
991 |
||
992 |
return msg; |
|
993 |
} |
|
994 |
#else |
|
995 |
static char* GetErrorText(DWORD inError) |
|
996 |
{ |
|
997 |
return NULL; |
|
998 |
} |
|
999 |
#endif |