author | fturovic <frank.turovich@nokia.com> |
Wed, 13 Oct 2010 11:00:18 -0500 | |
changeset 2151 | fab48956482f |
parent 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 |
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
27 |
#include "psapi.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
|
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 |
||
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
113 |
|
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
114 |
#ifdef _PSAPI_PRESENT |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
115 |
FindOrCreateRunningServer(); |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
116 |
#endif |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
117 |
|
60 | 118 |
char name[100]; |
119 |
sprintf(name, "%s%ld", ERRORMONITORLIST_MUTEX_BASENAME, ::GetCurrentProcessId()); |
|
120 |
m_ErrorMonitorListMutex.Open(name, ERRORMONITORLIST_MUTEX_TIMEOUT); |
|
121 |
||
122 |
sprintf(name, "%s%ld", INPUTSTREAMLIST_MUTEX_BASENAME, ::GetCurrentProcessId()); |
|
123 |
m_StreamListMutex.Open(name, INPUTSTREAMLIST_MUTEX_TIMEOUT); |
|
124 |
||
125 |
m_hServer = NULL; |
|
126 |
m_hServerThread = NULL; |
|
127 |
||
128 |
// release server access |
|
129 |
m_Server->ReleaseServerPipeAccess(); |
|
130 |
int ret = ::LoadString(hinstDLL, IDS_VERSION, m_Version, MAX_VERSION_STRING); |
|
131 |
||
132 |
TCDEBUGCLOSE(); |
|
133 |
} |
|
134 |
||
135 |
CClientManager::~CClientManager() |
|
136 |
{ |
|
137 |
TCDEBUGOPEN(); |
|
138 |
TCDEBUGLOGS("CClientManager::~CClientManager\n"); |
|
139 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
140 |
||
141 |
if (m_Server) |
|
142 |
{ |
|
143 |
delete m_Server; |
|
144 |
m_Server = NULL; |
|
145 |
} |
|
146 |
||
147 |
WaitForStreamListAccess(); |
|
148 |
TCDEBUGLOGA1("CClientManager::~CClientManager: erasing stream list size=%d\n", InputStreamListSize()); |
|
149 |
if (m_StreamList) |
|
150 |
{ |
|
151 |
InputStreamList::iterator iter; |
|
152 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
153 |
{ |
|
154 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list next 1\n"); |
|
155 |
// m_StreamList->erase(iter); |
|
156 |
delete *iter; |
|
157 |
// TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list next 2\n"); |
|
158 |
} |
|
159 |
m_StreamList->clear(); |
|
160 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list done 1\n"); |
|
161 |
delete m_StreamList; |
|
162 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing stream list done 2\n"); |
|
163 |
} |
|
164 |
ReleaseStreamListAccess(); |
|
165 |
m_StreamListMutex.Close(); |
|
166 |
||
167 |
WaitForErrorMonitorListAccess(); |
|
168 |
TCDEBUGLOGA1("CClientManager::~CClientManager: erasing monitor list size=%d\n", ErrorMonitorListSize()); |
|
169 |
if (m_ErrorMonitorList) |
|
170 |
{ |
|
171 |
ErrorMonitorList::iterator iter; |
|
172 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
173 |
{ |
|
174 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list next 1\n"); |
|
175 |
// m_ErrorMonitorList->erase(iter); |
|
176 |
delete *iter; |
|
177 |
// TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list next 1\n"); |
|
178 |
} |
|
179 |
m_ErrorMonitorList->clear(); |
|
180 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list done 1\n"); |
|
181 |
delete m_ErrorMonitorList; |
|
182 |
TCDEBUGLOGS("CClientManager::~CClientManager: erasing monitor list done 2\n"); |
|
183 |
} |
|
184 |
ReleaseErrorMonitorListAccess(); |
|
185 |
m_ErrorMonitorListMutex.Close(); |
|
186 |
||
187 |
if (m_DllLocation) |
|
188 |
{ |
|
189 |
delete[] m_DllLocation; |
|
190 |
m_DllLocation = NULL; |
|
191 |
} |
|
192 |
||
193 |
if (m_ServerLockFile) |
|
194 |
{ |
|
195 |
delete[] m_ServerLockFile; |
|
196 |
m_ServerLockFile = NULL; |
|
197 |
} |
|
198 |
||
199 |
if (m_ServerExeFile) |
|
200 |
{ |
|
201 |
delete[] m_ServerExeFile; |
|
202 |
m_ServerExeFile = NULL; |
|
203 |
} |
|
204 |
TCDEBUGLOGS("CClientManager::~CClientManager: closing log\n"); |
|
205 |
TCDEBUGCLOSE(); |
|
206 |
if (m_DebugLog) |
|
207 |
{ |
|
208 |
delete m_DebugLog; |
|
209 |
m_DebugLog = NULL; |
|
210 |
} |
|
211 |
} |
|
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
|
212 |
|
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 |
#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
|
214 |
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
|
215 |
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
|
216 |
{ |
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 |
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
|
218 |
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
|
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 |
// 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
|
221 |
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
|
222 |
|
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 |
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
|
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 |
// 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
|
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 |
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
|
228 |
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
|
229 |
|
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 |
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
|
231 |
{ |
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 |
// 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
|
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 |
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
|
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 |
// 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
|
237 |
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
|
238 |
{ |
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 |
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
|
240 |
{ |
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 |
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
|
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 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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
|
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 |
// 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
|
249 |
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
|
250 |
{ |
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 |
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
|
252 |
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
|
253 |
|
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 |
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
|
255 |
&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
|
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 |
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
|
258 |
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
|
259 |
{ |
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 |
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
|
261 |
{ |
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 |
// 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
|
263 |
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
|
264 |
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
|
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 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
} |
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 |
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
|
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 |
} |
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 |
} |
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 |
|
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 |
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
|
281 |
{ |
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 |
// 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
|
283 |
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
|
284 |
} |
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
285 |
size_t len = strlen(exeDirectory); |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
286 |
// remove file |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
287 |
int j; |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
288 |
for (j = len-1; j > 0; j--) |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
289 |
{ |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
290 |
if (exeDirectory[j] == PATH_DELIMITER) |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
291 |
break; |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
292 |
} |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
293 |
exeDirectory[j] = NULL; |
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
|
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 |
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
|
296 |
sprintf(m_ServerExeFile, "\"%s%c%s\"", exeDirectory, PATH_DELIMITER, SERVER_PROCESS_NAME); |
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
297 |
TCDEBUGLOGA1("exeFile at %s\n", m_ServerExeFile); |
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
|
298 |
|
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 |
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
|
300 |
sprintf(m_ServerLockFile, "%s%c%s", exeDirectory, PATH_DELIMITER, SERVER_LOCKFILE_NAME); |
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
301 |
TCDEBUGLOGA1("lockFile at %s\n", m_ServerLockFile); |
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
302 |
|
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
303 |
|
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
|
304 |
|
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
|
305 |
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
|
306 |
|
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
|
307 |
} |
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
|
308 |
#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
|
309 |
|
60 | 310 |
CErrorMonitor* |
311 |
CClientManager::FindErrorMonitor(long inClientId) |
|
312 |
{ |
|
313 |
CErrorMonitor* errorMonitor = NULL; |
|
314 |
ErrorMonitorList::iterator iter; |
|
315 |
||
316 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
317 |
{ |
|
318 |
if ((*iter)->IsThisClient(inClientId)) |
|
319 |
{ |
|
320 |
errorMonitor = *iter; |
|
321 |
break; |
|
322 |
} |
|
323 |
} |
|
324 |
return errorMonitor; |
|
325 |
} |
|
326 |
long CClientManager::ErrorMonitorListSize() |
|
327 |
{ |
|
328 |
long size = m_ErrorMonitorList->size(); |
|
329 |
||
330 |
return size; |
|
331 |
} |
|
332 |
void CClientManager::AddErrorMonitor(CErrorMonitor* monitor) |
|
333 |
{ |
|
334 |
m_ErrorMonitorList->push_back(monitor); |
|
335 |
} |
|
336 |
void CClientManager::RemoveErrorMonitor(CErrorMonitor* monitor) |
|
337 |
{ |
|
338 |
ErrorMonitorList::iterator iter; |
|
339 |
||
340 |
for (iter = m_ErrorMonitorList->begin(); iter != m_ErrorMonitorList->end(); iter++) |
|
341 |
{ |
|
342 |
if ((*iter)->IsThisClient(monitor->GetClientId())) |
|
343 |
{ |
|
344 |
m_ErrorMonitorList->erase(iter); |
|
345 |
break; |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
CInputStream* |
|
351 |
CClientManager::FindInputStream(long inClientId) |
|
352 |
{ |
|
353 |
CInputStream* inputStream = NULL; |
|
354 |
InputStreamList::iterator iter; |
|
355 |
||
356 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
357 |
{ |
|
358 |
if ((*iter)->IsThisClient(inClientId)) |
|
359 |
// if ((*iter).IsThisClient(inClientId)) |
|
360 |
{ |
|
361 |
inputStream = *iter; |
|
362 |
// inputStream = iter; |
|
363 |
break; |
|
364 |
} |
|
365 |
} |
|
366 |
return inputStream; |
|
367 |
} |
|
368 |
long CClientManager::InputStreamListSize() |
|
369 |
{ |
|
370 |
long size = m_StreamList->size(); |
|
371 |
||
372 |
return size; |
|
373 |
} |
|
374 |
void CClientManager::AddInputStream(CInputStream* stream) |
|
375 |
{ |
|
376 |
m_StreamList->push_back(stream); |
|
377 |
// m_StreamList->push_back(*stream); |
|
378 |
||
379 |
} |
|
380 |
void CClientManager::RemoveInputStream(CInputStream* stream) |
|
381 |
{ |
|
382 |
InputStreamList::iterator iter; |
|
383 |
||
384 |
for (iter = m_StreamList->begin(); iter != m_StreamList->end(); iter++) |
|
385 |
{ |
|
386 |
if ((*iter)->IsThisClient(stream->GetClientId())) |
|
387 |
// if ((*iter).IsThisClient(stream->GetClientId())) |
|
388 |
{ |
|
389 |
m_StreamList->erase(iter); |
|
390 |
break; |
|
391 |
} |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
BOOL CClientManager::StartServer(pServerProcessData pData) |
|
396 |
{ |
|
397 |
TCDEBUGLOGA1("CClientManager::StartServer numRefs = %d\n",pData->numRefs); |
|
398 |
||
399 |
BOOL serverStarted = FALSE; |
|
400 |
// server is ref counted |
|
401 |
// refcount = 0 => server is not running |
|
402 |
// refcount > 0 => server already started by some other process |
|
403 |
if (pData->numRefs == 0) |
|
404 |
{ |
|
405 |
// server not running |
|
406 |
// get exe location |
|
407 |
char exeLocation[MAX_DLLPATHNAME] = {0}; |
|
408 |
strncpy(exeLocation, m_DllLocation, MAX_DLLPATHNAME); |
|
409 |
size_t len = strlen(exeLocation); |
|
410 |
// remove file |
|
411 |
for (int i = len-1; i > 0; i--) |
|
412 |
{ |
|
413 |
if (exeLocation[i] == PATH_DELIMITER) |
|
414 |
break; |
|
415 |
} |
|
416 |
exeLocation[i] = NULL; |
|
417 |
char quotedLocation[MAX_DLLPATHNAME] = {0}; |
|
418 |
sprintf(quotedLocation, "\"%s%c%s\"", exeLocation, PATH_DELIMITER, SERVER_PROCESS_NAME); |
|
419 |
||
420 |
TCDEBUGLOGA1(" exeLocation=%s\n", quotedLocation); |
|
421 |
||
422 |
// create process |
|
423 |
STARTUPINFO si; |
|
424 |
memset(&si,0,sizeof(si)); |
|
425 |
si.cb = sizeof(si); |
|
426 |
memset(&pData->serverProcess, 0, sizeof(pData->serverProcess)); |
|
427 |
pData->serverProcess.hProcess = NULL; |
|
428 |
if (!::CreateProcess( |
|
429 |
NULL, // module location |
|
430 |
quotedLocation, // command line |
|
431 |
NULL, // process attributes |
|
432 |
NULL, // thread attributes |
|
433 |
FALSE, // inherit our handles |
|
434 |
CREATE_NO_WINDOW, // no window |
|
435 |
NULL, // use our environment |
|
436 |
NULL, // user our current directory |
|
437 |
&si, // startup info |
|
438 |
&pData->serverProcess)) // process info |
|
439 |
{ |
|
440 |
// TODO: error creating process |
|
441 |
} |
|
442 |
else |
|
443 |
{ |
|
444 |
// we are the creator so save handles for later |
|
445 |
m_hServer = pData->serverProcess.hProcess; |
|
446 |
m_hServerThread = pData->serverProcess.hThread; |
|
447 |
// add a refcount |
|
448 |
pData->numRefs++; |
|
449 |
serverStarted = TRUE; |
|
450 |
} |
|
451 |
} |
|
452 |
else |
|
453 |
{ |
|
454 |
// already running |
|
455 |
// add a refcount and open our process handle to it |
|
456 |
pData->numRefs++; |
|
457 |
m_hServer = ::OpenProcess(0, FALSE, pData->serverProcess.dwProcessId); |
|
458 |
serverStarted = TRUE; |
|
459 |
} |
|
460 |
TCDEBUGLOGA1("CClientManager::StartServer serverStarted=%d\n", serverStarted); |
|
461 |
return serverStarted; |
|
462 |
} |
|
463 |
||
464 |
BOOL CClientManager::StopServer(pServerProcessData pData) |
|
465 |
{ |
|
466 |
TCDEBUGLOGS("CClientManager::StopServer\n"); |
|
467 |
||
468 |
BOOL serverStopped = FALSE; |
|
469 |
||
470 |
if (pData->serverProcess.hProcess == NULL || pData->numRefs <= 0) |
|
471 |
{ |
|
472 |
serverStopped = TRUE; |
|
473 |
} |
|
474 |
else |
|
475 |
{ |
|
476 |
TCDEBUGLOGA1(" numRefs = %d\n",pData->numRefs); |
|
477 |
||
478 |
// substract ref count |
|
479 |
pData->numRefs--; |
|
480 |
// if refcount == 0 then really stop the server process |
|
481 |
if (pData->numRefs <= 0) |
|
482 |
{ |
|
483 |
// last client process is closing |
|
484 |
// tell server to exit |
|
485 |
ServerCommandData cmdrsp; |
|
486 |
cmdrsp.command = eCmdExit; |
|
487 |
TCDEBUGLOGS(" SendCommand eCmdExit\n"); |
|
488 |
m_Server->SendCommand(&cmdrsp); |
|
489 |
TCDEBUGLOGS(" GetResponse eExit\n"); |
|
490 |
m_Server->GetResponse(&cmdrsp); |
|
491 |
// wait for process to exit |
|
492 |
TCDEBUGLOGS(" WaitForSingleObject start\n"); |
|
493 |
WaitForSingleObject(m_hServer, 10000L /*INFINITE*/); |
|
494 |
TCDEBUGLOGS(" WaitForSingleObject found\n"); |
|
495 |
||
496 |
if (m_hServer != NULL) |
|
497 |
CloseHandle(m_hServer); |
|
498 |
||
499 |
if (m_hServerThread != NULL) |
|
500 |
CloseHandle(m_hServerThread); |
|
501 |
} |
|
502 |
else |
|
503 |
{ |
|
504 |
// just close our handle to server process |
|
505 |
if (m_hServer != NULL) |
|
506 |
CloseHandle(m_hServer); |
|
507 |
||
508 |
if (m_hServerThread != NULL) |
|
509 |
CloseHandle(m_hServerThread); |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
TCDEBUGLOGS("CClientManager::StopServer end\n"); |
|
514 |
return TRUE; |
|
515 |
} |
|
516 |
long CClientManager::StartServer() |
|
517 |
{ |
|
518 |
long ret = TCAPI_ERR_NONE; |
|
519 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
520 |
||
521 |
TCDEBUGLOGA3("CClientManager::StartServer this = %x m_hServer = %x numRefs = %d\n", this, m_hServer, pData->numRefs); |
|
522 |
// TCDEBUGLOGA1(" mgrRefs = %d\n", m_MgrServerRef); |
|
523 |
||
524 |
BOOL serverStarted = FALSE; |
|
525 |
// server is ref counted |
|
526 |
// refcount = 0 => server is not running |
|
527 |
// refcount > 0 => server already started by some other process |
|
528 |
||
529 |
// terminate the TCFServer if it is already running |
|
530 |
TerminateServerThroughLockFile(pData); |
|
531 |
||
532 |
if (pData->numRefs == 0) |
|
533 |
{ |
|
534 |
// server not running |
|
535 |
TCDEBUGLOGA1(" TCFServer exe =%s\n", m_ServerExeFile); |
|
536 |
TCDEBUGLOGA1(" TCFServer lock=%s\n", m_ServerLockFile); |
|
537 |
||
538 |
||
539 |
// create process |
|
540 |
STARTUPINFO si; |
|
541 |
memset(&si,0,sizeof(si)); |
|
542 |
si.cb = sizeof(si); |
|
543 |
memset(&pData->serverProcess, 0, sizeof(pData->serverProcess)); |
|
544 |
pData->serverProcess.hProcess = NULL; |
|
545 |
if (!::CreateProcess( |
|
546 |
NULL, // module location |
|
547 |
m_ServerExeFile, // command line |
|
548 |
NULL, // process attributes |
|
549 |
NULL, // thread attributes |
|
550 |
FALSE, // inherit our handles |
|
551 |
CREATE_NO_WINDOW, // no window |
|
552 |
NULL, // use our environment |
|
553 |
NULL, // user our current directory |
|
554 |
&si, // startup info |
|
555 |
&pData->serverProcess)) // process info |
|
556 |
{ |
|
557 |
// TODO: error creating process |
|
558 |
} |
|
559 |
else |
|
560 |
{ |
|
561 |
// we are the creator so save handles for later |
|
562 |
m_hServer = pData->serverProcess.hProcess; |
|
563 |
m_hServerThread = pData->serverProcess.hThread; |
|
564 |
// add a refcount |
|
565 |
pData->numRefs++; |
|
566 |
serverStarted = TRUE; |
|
567 |
TCDEBUGLOGA3("CClientManager::StartServer created m_hServer = %x processId = %d numRefs = %d\n", m_hServer, pData->serverProcess.dwProcessId, pData->numRefs); |
|
568 |
||
569 |
// create lock file and save process ID |
|
570 |
TCDEBUGLOGS("CClientManager::StartServer CreateLockFile\n"); |
|
571 |
CreateLockFile(pData->serverProcess.dwProcessId); |
|
572 |
} |
|
573 |
} |
|
574 |
else |
|
575 |
{ |
|
576 |
// already running |
|
577 |
// add a refcount and open our process handle to it only if we haven't opened it already |
|
578 |
pData->numRefs++; |
|
579 |
if (m_hServer == NULL) |
|
580 |
m_hServer = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pData->serverProcess.dwProcessId); |
|
581 |
if (m_hServer == 0) |
|
582 |
{ |
|
583 |
TCDEBUGLOGA1("CClientManager::StartServer opened m_hServer null error=%d\n", ::GetLastError()); |
|
584 |
} |
|
585 |
m_hServerThread = NULL; // only creator gets real thread handle |
|
586 |
serverStarted = TRUE; |
|
587 |
TCDEBUGLOGA3("CClientManager::StartServer opened m_hServer = %x processId = %d numRefs = %d\n", m_hServer, pData->serverProcess.dwProcessId, pData->numRefs); |
|
588 |
// save our process id to lock file |
|
589 |
AppendToLockFile(pData->serverProcess.dwProcessId); |
|
590 |
} |
|
591 |
if (serverStarted) |
|
592 |
m_ServerRunning = TRUE; |
|
593 |
||
594 |
TCDEBUGLOGA1("CClientManager::StartServer end numRefs = %d\n", pData->numRefs); |
|
595 |
return ret; |
|
596 |
} |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
597 |
BOOL CClientManager::IsTCFServerActive(DWORD processId) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
598 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
599 |
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
|
600 |
if (h) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
601 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
602 |
// is it really still alive? |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
603 |
DWORD exitCode = -5; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
604 |
BOOL exitCall = ::GetExitCodeProcess(h, &exitCode); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
605 |
::CloseHandle(h); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
606 |
if (exitCall == TRUE && exitCode != STILL_ACTIVE) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
607 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
608 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
609 |
return FALSE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
610 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
611 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
612 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
613 |
// TCFServer is still active |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
614 |
return TRUE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
615 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
616 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
617 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
618 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
619 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
620 |
return FALSE; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
621 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
622 |
} |
60 | 623 |
long CClientManager::StopServer() |
624 |
{ |
|
625 |
long ret = TCAPI_ERR_NONE; |
|
626 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
627 |
||
628 |
TCDEBUGLOGA3("CClientManager::StopServer this = %x m_hServer = %x numRefs = %d\n", this, m_hServer, pData->numRefs); |
|
629 |
||
630 |
BOOL serverStopped = FALSE; |
|
631 |
||
632 |
if (pData->serverProcess.hProcess == NULL || pData->numRefs <= 0) |
|
633 |
{ |
|
634 |
TCDEBUGLOGS("CClientManager::StopServer hProcess NULL or numRefs <= 0\n"); |
|
635 |
serverStopped = TRUE; |
|
636 |
} |
|
637 |
#if (0) |
|
638 |
else if (m_hServer == NULL) |
|
639 |
{ |
|
640 |
// we've already closed our handle to server |
|
641 |
// don't close it again |
|
642 |
TCDEBUGLOGS("CClientManager::StopServer m_hServer null\n"); |
|
643 |
} |
|
644 |
#endif |
|
645 |
else |
|
646 |
{ |
|
647 |
// substract ref count |
|
648 |
pData->numRefs--; |
|
649 |
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
|
650 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
651 |
bool sendStop = true; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
652 |
if (!IsTCFServerActive(pData->serverProcess.dwProcessId)) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
653 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
654 |
sendStop = false; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
655 |
pData->numRefs = 0; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
656 |
} |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
657 |
|
60 | 658 |
// if refcount == 0 then really stop the server process |
659 |
if (pData->numRefs == 0) |
|
660 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
661 |
if (sendStop) |
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 |
// last client process is closing |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
664 |
// tell server to exit |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
665 |
ServerCommandData cmdrsp; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
666 |
cmdrsp.command = eCmdExit; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
667 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
668 |
TCDEBUGLOGS(" SendCommand eCmdExit\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
669 |
m_Server->SendCommand(&cmdrsp); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
670 |
TCDEBUGLOGS(" GetResponse eExit\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
671 |
m_Server->GetResponse(&cmdrsp); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
672 |
|
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
673 |
// wait for process to exit |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
674 |
TCDEBUGLOGS(" WaitForSingleObject start\n"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
675 |
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
|
676 |
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
|
677 |
} |
60 | 678 |
|
679 |
// now close our handle to server process |
|
680 |
if (m_hServer != NULL) |
|
681 |
{ |
|
682 |
CloseHandle(m_hServer); |
|
683 |
m_hServer = NULL; |
|
684 |
} |
|
685 |
||
686 |
if (m_hServerThread != NULL) |
|
687 |
{ |
|
688 |
CloseHandle(m_hServerThread); |
|
689 |
m_hServerThread = NULL; |
|
690 |
} |
|
691 |
serverStopped = TRUE; |
|
692 |
||
693 |
// delete lock file |
|
694 |
TCDEBUGLOGS("CClientManager::StopServer DeleteLockFile\n"); |
|
695 |
DeleteLockFile(); |
|
696 |
} |
|
697 |
else |
|
698 |
{ |
|
699 |
// just close our handle to server process |
|
700 |
||
701 |
if (m_hServer != NULL) |
|
702 |
{ |
|
703 |
CloseHandle(m_hServer); |
|
704 |
m_hServer = NULL; |
|
705 |
} |
|
706 |
||
707 |
if (m_hServerThread != NULL) |
|
708 |
{ |
|
709 |
CloseHandle(m_hServerThread); |
|
710 |
m_hServerThread = NULL; |
|
711 |
} |
|
712 |
DeleteFromLockFile(pData->serverProcess.dwProcessId); |
|
713 |
} |
|
714 |
} |
|
715 |
if (serverStopped) |
|
716 |
m_ServerRunning = FALSE; |
|
717 |
||
718 |
TCDEBUGLOGA1("CClientManager::StopServer end numRefs = %d\n", pData->numRefs); |
|
719 |
return ret; |
|
720 |
} |
|
721 |
||
722 |
BOOL CClientManager::IsServerRunning() |
|
723 |
{ |
|
724 |
pServerProcessData pData = m_Server->GetProcessPtr(); |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
725 |
if (IsTCFServerActive(pData->serverProcess.dwProcessId)) |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
726 |
{ |
60 | 727 |
return TRUE; |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
728 |
} |
60 | 729 |
else |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
730 |
{ |
60 | 731 |
return FALSE; |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
732 |
} |
60 | 733 |
} |
734 |
||
735 |
void CClientManager::CreateLockFile(DWORD processId) |
|
736 |
{ |
|
737 |
if (m_ServerLockFile != NULL) |
|
738 |
{ |
|
739 |
FILE* f = fopen(m_ServerLockFile, "wt"); |
|
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
740 |
TCDEBUGLOGA2("CClientManager::CreateLockFile f=%x, lockFile=%s\n", f, m_ServerLockFile); |
60 | 741 |
|
742 |
if (f) |
|
743 |
{ |
|
744 |
DWORD callingProcessId = ::GetCurrentProcessId(); |
|
745 |
TCDEBUGLOGA2("CClientManager::CreateLockFile callingProcessId=%d processId=%d\n", callingProcessId, processId); |
|
746 |
fprintf(f, "%ld %ld\n", callingProcessId, processId); |
|
747 |
fclose(f); |
|
748 |
} |
|
749 |
else |
|
750 |
{ |
|
751 |
DWORD err = ::GetLastError(); |
|
752 |
TCDEBUGLOGA2("CClientManager::CreateLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
753 |
} |
|
754 |
} |
|
755 |
} |
|
756 |
void CClientManager::AppendToLockFile(DWORD processId) |
|
757 |
{ |
|
758 |
if (m_ServerLockFile != NULL) |
|
759 |
{ |
|
760 |
FILE* f = fopen(m_ServerLockFile, "at"); |
|
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
761 |
TCDEBUGLOGA2("CClientManager::AppendToLockFile f=%x, lockFile=%s\n", f, m_ServerLockFile); |
60 | 762 |
|
763 |
if (f) |
|
764 |
{ |
|
765 |
DWORD callingProcessId = ::GetCurrentProcessId(); |
|
766 |
TCDEBUGLOGA2("CClientManager::AppendToLockFile callingProcessId=%d processId=%d\n", callingProcessId, processId); |
|
767 |
fprintf(f, "%ld %ld\n", callingProcessId, processId); |
|
768 |
fclose(f); |
|
769 |
} |
|
770 |
else |
|
771 |
{ |
|
772 |
DWORD err = ::GetLastError(); |
|
773 |
TCDEBUGLOGA2("CClientManager::AppendToLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
774 |
} |
|
775 |
} |
|
776 |
} |
|
777 |
void CClientManager::DeleteLockFile() |
|
778 |
{ |
|
779 |
if (m_ServerLockFile != NULL) |
|
780 |
{ |
|
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
781 |
TCDEBUGLOGA1("CClientManager::DeleteLockFile lockFile=%s\n", m_ServerLockFile); |
60 | 782 |
::remove(m_ServerLockFile); |
783 |
} |
|
784 |
} |
|
785 |
||
786 |
void CClientManager::DeleteFromLockFile(DWORD serverProcessId) |
|
787 |
{ |
|
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
|
788 |
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
|
789 |
DWORD serverIds[10]; |
60 | 790 |
int numIds = 0; |
791 |
||
792 |
DWORD ourProcessId = ::GetCurrentProcessId(); |
|
793 |
||
794 |
if (m_ServerLockFile != NULL) |
|
795 |
{ |
|
796 |
DWORD attr = ::GetFileAttributes(m_ServerLockFile); |
|
1991
1c752f14a126
add Ed's changes to allow multiple Carbide instances to use same TCFServer
Chad Peckham <chad.peckham@nokia.com>
parents:
1481
diff
changeset
|
797 |
TCDEBUGLOGA2("CClientManager::DeleteFromLockFile attr=%x, lockFile=%s\n", attr, m_ServerLockFile); |
60 | 798 |
|
799 |
if (attr != 0xffffffff) // error |
|
800 |
{ |
|
801 |
// file exists |
|
802 |
// read the process Ids from it |
|
803 |
||
804 |
FILE *f = fopen(m_ServerLockFile, "rt"); |
|
805 |
TCDEBUGLOGA1("CClientManager::DeleteFromLockFile f=%x\n", f); |
|
806 |
if (f) |
|
807 |
{ |
|
808 |
BOOL done = FALSE; |
|
809 |
while (!done) |
|
810 |
{ |
|
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
|
811 |
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
|
812 |
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
|
813 |
int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId); |
60 | 814 |
if (n == 2) |
815 |
{ |
|
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
|
816 |
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
|
817 |
if (creatorId != ourProcessId || serverId != serverProcessId) |
60 | 818 |
{ |
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
|
819 |
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
|
820 |
serverIds[numIds] = serverId; |
60 | 821 |
numIds++; |
822 |
if (numIds > 9) |
|
823 |
done = TRUE; |
|
824 |
} |
|
825 |
} |
|
826 |
else |
|
827 |
{ |
|
828 |
done = TRUE; |
|
829 |
} |
|
830 |
} |
|
831 |
fclose(f); |
|
832 |
} |
|
833 |
||
834 |
// now rewrite lock file without us |
|
835 |
::remove(m_ServerLockFile); |
|
836 |
if (numIds > 0) |
|
837 |
{ |
|
838 |
f = fopen(m_ServerLockFile, "wt"); |
|
839 |
if (f) |
|
840 |
{ |
|
841 |
for (int i = 0; i < numIds; i++) |
|
842 |
{ |
|
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
|
843 |
fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]); |
60 | 844 |
} |
845 |
fclose(f); |
|
846 |
} |
|
847 |
} |
|
848 |
} |
|
849 |
} |
|
850 |
} |
|
851 |
||
852 |
// Currently assumes there is only ONE TCFServer, but multiple client processes (that use that server) |
|
853 |
// we should not have more than a few Carbide processes connecting to the same TCFServer |
|
854 |
void CClientManager::TerminateServerThroughLockFile(pServerProcessData pData) |
|
855 |
{ |
|
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
|
856 |
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
|
857 |
DWORD serverIds[10]; |
60 | 858 |
BOOL liveCaller[10]; |
859 |
int numIds = 0; |
|
860 |
if (m_ServerLockFile != NULL) |
|
861 |
{ |
|
862 |
DWORD attr = ::GetFileAttributes(m_ServerLockFile); |
|
863 |
TCDEBUGLOGA1("CClientManager::TerminateServerThroughLockFile attr=%x\n", attr); |
|
864 |
||
865 |
if (attr != 0xffffffff) // error |
|
866 |
{ |
|
867 |
// file exists |
|
868 |
// read the process Ids from it |
|
869 |
||
870 |
FILE *f = fopen(m_ServerLockFile, "rt"); |
|
871 |
TCDEBUGLOGA1("CClientManager::TerminateServerThroughLockFile f=%x\n", f); |
|
872 |
if (f) |
|
873 |
{ |
|
874 |
BOOL done = FALSE; |
|
875 |
while (!done) |
|
876 |
{ |
|
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
|
877 |
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
|
878 |
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
|
879 |
int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId); |
60 | 880 |
if (n == 2) |
881 |
{ |
|
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
|
882 |
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
|
883 |
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
|
884 |
serverIds[numIds] = serverId; |
60 | 885 |
numIds++; |
886 |
if (numIds > 9) |
|
887 |
done = TRUE; |
|
888 |
} |
|
889 |
else |
|
890 |
{ |
|
891 |
done = TRUE; |
|
892 |
} |
|
893 |
} |
|
894 |
fclose(f); |
|
895 |
||
896 |
int numDeadCallers = 0; |
|
897 |
for (int i = 0; i < numIds; i++) |
|
898 |
{ |
|
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
|
899 |
HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, creatorIds[i]); |
60 | 900 |
if (h) |
901 |
{ |
|
902 |
// calling process is still alive |
|
903 |
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
|
904 |
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
|
905 |
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
|
906 |
DWORD id = ::GetCurrentProcessId(); |
60 | 907 |
::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
|
908 |
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
|
909 |
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
|
910 |
{ |
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
|
911 |
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
|
912 |
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
|
913 |
} |
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
|
914 |
{ |
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
|
915 |
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
|
916 |
} |
60 | 917 |
} |
918 |
else |
|
919 |
{ |
|
920 |
liveCaller[i] = FALSE; |
|
921 |
numDeadCallers++; |
|
922 |
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
|
923 |
TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d dead err=%d:%s\n", creatorIds[i], err, GetErrorText(err)); |
60 | 924 |
} |
925 |
} |
|
926 |
if (numDeadCallers == numIds) |
|
927 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
928 |
// All clients of this TCFServer are dead |
60 | 929 |
// terminate the TCFServer, and delete lock file |
930 |
pData->numRefs = 0; |
|
931 |
::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
|
932 |
HANDLE h = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, serverIds[0]); |
60 | 933 |
if (h) |
934 |
{ |
|
935 |
BOOL ret = ::TerminateProcess(h, -1); |
|
936 |
if (ret == 0) |
|
937 |
{ |
|
938 |
DWORD err = ::GetLastError(); |
|
939 |
TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile TerminateProcess=%d:%s\n", err, GetErrorText(err)); |
|
940 |
} |
|
941 |
::CloseHandle(h); |
|
942 |
} |
|
943 |
} |
|
944 |
else |
|
945 |
{ |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
946 |
// some java clients are still alive |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
947 |
// 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
|
948 |
if (IsTCFServerActive(serverIds[0])) |
60 | 949 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
950 |
// TCFServer is still active |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
951 |
// 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
|
952 |
::remove(m_ServerLockFile); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
953 |
f = fopen(m_ServerLockFile, "wt"); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
954 |
if (f) |
60 | 955 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
956 |
for (int i = 0; i < numIds; i++) |
60 | 957 |
{ |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
958 |
if (liveCaller[i]) |
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 |
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
|
961 |
} |
60 | 962 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
963 |
fclose(f); |
60 | 964 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
965 |
pData->numRefs -= numDeadCallers; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
966 |
if (pData->numRefs < 0) pData->numRefs = 0; |
60 | 967 |
} |
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
968 |
else |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
969 |
{ |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
970 |
// TCFServer is really dead |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
971 |
pData->numRefs = 0; |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
972 |
::remove(m_ServerLockFile); |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
973 |
} |
60 | 974 |
} |
975 |
} |
|
976 |
else |
|
977 |
{ |
|
978 |
// error opening lock file |
|
914
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
979 |
// perhaps the user deleted it, |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
980 |
// 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
|
981 |
// process is dead. |
46f36a149510
Fix TCFServer life-cycle issues bug 10621
Chad Peckham <chad.peckham@nokia.com>
parents:
505
diff
changeset
|
982 |
pData->numRefs = 0; |
60 | 983 |
DWORD err = ::GetLastError(); |
984 |
TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile fopenErr=%d:%s\n", err, GetErrorText(err)); |
|
985 |
} |
|
986 |
} |
|
987 |
} |
|
988 |
} |
|
989 |
#ifdef _DEBUG |
|
990 |
static char* GetErrorText(DWORD inError) |
|
991 |
{ |
|
992 |
static char msg[256]; |
|
993 |
FormatMessage( |
|
994 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
|
995 |
NULL, |
|
996 |
inError, |
|
997 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
998 |
(LPTSTR) &msg, |
|
999 |
sizeof(msg) - 1, |
|
1000 |
NULL); |
|
1001 |
||
1002 |
return msg; |
|
1003 |
} |
|
1004 |
#else |
|
1005 |
static char* GetErrorText(DWORD inError) |
|
1006 |
{ |
|
1007 |
return NULL; |
|
1008 |
} |
|
1009 |
#endif |