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 |
// ConnectionImpl.cpp: implementation of the CConnectionImpl class.
|
|
18 |
//
|
|
19 |
//////////////////////////////////////////////////////////////////////
|
|
20 |
|
|
21 |
#include "stdafx.h"
|
|
22 |
#include "ConnectionImpl.h"
|
|
23 |
#include "RegistryImpl.h"
|
|
24 |
#include "ServerManager.h"
|
|
25 |
|
|
26 |
#ifdef _DEBUG
|
|
27 |
extern BOOL gDoLogging;
|
|
28 |
#endif
|
|
29 |
|
366
|
30 |
//#define LOG_CONNECTION
|
60
|
31 |
#if defined(LOG_CONNECTION) && defined(_DEBUG)
|
|
32 |
#define TCDEBUGOPEN() if (gDoLogging) { this->m_DebugLog->WaitForAccess(); }
|
|
33 |
#define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(this->m_DebugLogMsg,"%s", s); this->m_DebugLog->log(this->m_DebugLogMsg); }
|
|
34 |
#define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(this->m_DebugLogMsg, s, a1); this->m_DebugLog->log(this->m_DebugLogMsg); }
|
|
35 |
#define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(this->m_DebugLogMsg, s, a1, a2); this->m_DebugLog->log(this->m_DebugLogMsg); }
|
|
36 |
#define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(this->m_DebugLogMsg, s, a1, a2, a3); this->m_DebugLog->log(this->m_DebugLogMsg); }
|
|
37 |
#define TCDEBUGLOGA4(s, a1, a2, a3, a4) if (gDoLogging) { sprintf(this->m_DebugLogMsg, s, a1, a2, a3, a4); this->m_DebugLog->log(this->m_DebugLogMsg); }
|
|
38 |
#define TCDEBUGCLOSE() if (gDoLogging) { this->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 TCDEBUGLOGA4(s, a1, a2, a3, a4)
|
|
46 |
#define TCDEBUGCLOSE()
|
|
47 |
#endif
|
|
48 |
|
366
|
49 |
//#define LOG_MPROCESSOR
|
|
50 |
|
60
|
51 |
//////////////////////////////////////////////////////////////////////
|
|
52 |
// Construction/Destruction
|
|
53 |
//////////////////////////////////////////////////////////////////////
|
|
54 |
|
|
55 |
CConnectionImpl::CConnectionImpl()
|
|
56 |
{
|
|
57 |
m_ConnectSettings = NULL;
|
|
58 |
m_ClientList = NULL;
|
|
59 |
m_Status = eDisconnected;
|
|
60 |
m_Registry = NULL;
|
|
61 |
m_ConnectionID = 0;
|
|
62 |
m_OsError = 0;
|
|
63 |
m_BaseComm = NULL;
|
|
64 |
m_BaseProtocol = NULL;
|
|
65 |
m_BaseCommHandle = NULL;
|
|
66 |
m_BaseProtocolHandle = NULL;
|
|
67 |
|
|
68 |
// message processing thread flags and handles
|
|
69 |
m_MessageProcessorState = MP_NONE;
|
|
70 |
m_ExitMessageProcessor = false;
|
|
71 |
m_PauseMessageProcessing = false;
|
|
72 |
m_StartMessageProcessing = false;
|
|
73 |
m_hMessageProcessorExittedEvent = NULL;
|
|
74 |
m_hMessageProcessorStoppedEvent = NULL;
|
|
75 |
m_hMessageProcessorStartedEvent = NULL;
|
|
76 |
m_hMessageProcessorThread = NULL;
|
|
77 |
m_dwMessageProcessorThreadId = 0;
|
|
78 |
|
|
79 |
m_NextRetryTime = m_RetryTimeoutTime = 0;
|
|
80 |
|
|
81 |
m_NextFlushFileTime = 0;
|
|
82 |
}
|
|
83 |
|
|
84 |
CConnectionImpl::CConnectionImpl(ConnectData conData, DWORD connectionId)
|
|
85 |
{
|
|
86 |
#ifdef _DEBUG
|
|
87 |
if (gDoLogging)
|
|
88 |
{
|
366
|
89 |
#if defined(LOG_CONNECTION)
|
60
|
90 |
m_DebugLog = new TCDebugLog("TCF_ConnectionLog", connectionId);
|
366
|
91 |
#else
|
|
92 |
m_DebugLog = NULL;
|
|
93 |
#endif
|
|
94 |
#if defined(LOG_MPROCESSOR)
|
60
|
95 |
m_DebugLog2 = new TCDebugLog("TCF_ProcessorLog", connectionId);
|
366
|
96 |
#else
|
|
97 |
m_DebugLog2 = NULL;
|
|
98 |
#endif
|
60
|
99 |
}
|
|
100 |
else
|
|
101 |
{
|
|
102 |
m_DebugLog = NULL;
|
|
103 |
m_DebugLog2 = NULL;
|
|
104 |
}
|
|
105 |
#else
|
|
106 |
m_DebugLog = NULL;
|
|
107 |
m_DebugLog2 = NULL;
|
|
108 |
#endif
|
|
109 |
|
|
110 |
TCDEBUGOPEN();
|
|
111 |
TCDEBUGLOGA1("CConnectionImpl::CConnectionImpl id = %d\n", connectionId);
|
|
112 |
|
|
113 |
m_ConnectSettings = new ConnectData();
|
|
114 |
|
|
115 |
memcpy(m_ConnectSettings, &conData, sizeof(ConnectData));
|
|
116 |
|
|
117 |
m_ClientList = new ClientList();
|
|
118 |
m_ClientList->clear();
|
|
119 |
m_Status = eDisconnected;
|
|
120 |
m_Registry = new CRegistryImpl(connectionId);
|
|
121 |
m_ConnectionID = connectionId;
|
|
122 |
m_BaseComm = NULL;
|
|
123 |
m_BaseProtocol = NULL;
|
|
124 |
m_BaseCommHandle = NULL;
|
|
125 |
m_BaseProtocolHandle = NULL;
|
|
126 |
|
|
127 |
// message processing thread flags and handles
|
|
128 |
m_MessageProcessorState = MP_NONE;
|
|
129 |
m_ExitMessageProcessor = false;
|
|
130 |
m_PauseMessageProcessing = false;
|
|
131 |
m_StartMessageProcessing = false;
|
|
132 |
|
|
133 |
// create named events
|
|
134 |
char eventName[100];
|
|
135 |
|
|
136 |
sprintf(eventName, "%s%d", MESSAGEPROCESSOR_EXITEVENT_BASENAME, connectionId);
|
|
137 |
m_hMessageProcessorExittedEvent = ::CreateEvent(NULL, FALSE, FALSE, eventName);
|
|
138 |
|
|
139 |
sprintf(eventName, "%s%d", MESSAGEPROCESSOR_STOPEVENT_BASENAME, connectionId);
|
|
140 |
m_hMessageProcessorStoppedEvent = ::CreateEvent(NULL, FALSE, FALSE, eventName);
|
|
141 |
|
|
142 |
sprintf(eventName, "%s%d", MESSAGEPROCESSOR_STARTEVENT_BASENAME, connectionId);
|
|
143 |
m_hMessageProcessorStartedEvent = ::CreateEvent(NULL, FALSE, FALSE, eventName);
|
|
144 |
|
|
145 |
m_hMessageProcessorThread = NULL;
|
|
146 |
m_dwMessageProcessorThreadId = 0;
|
|
147 |
|
|
148 |
m_NextRetryTime = m_RetryTimeoutTime = 0;
|
|
149 |
|
|
150 |
m_NextFlushFileTime = 0;
|
|
151 |
m_OsError = 0;
|
|
152 |
|
|
153 |
TCDEBUGCLOSE();
|
|
154 |
}
|
|
155 |
CConnectionImpl::~CConnectionImpl()
|
|
156 |
{
|
|
157 |
TCDEBUGOPEN();
|
|
158 |
TCDEBUGLOGS("CConnectionImpl::~CConnectionImpl\n");
|
|
159 |
|
|
160 |
// terminate the message processor thread if running
|
|
161 |
|
|
162 |
if (m_hMessageProcessorThread != NULL)
|
|
163 |
{
|
|
164 |
BOOL t = ::TerminateThread(m_hMessageProcessorThread, 0);
|
|
165 |
::CloseHandle(m_hMessageProcessorThread);
|
|
166 |
}
|
|
167 |
|
|
168 |
if (m_hMessageProcessorExittedEvent != NULL)
|
|
169 |
{
|
|
170 |
::CloseHandle(m_hMessageProcessorExittedEvent);
|
|
171 |
}
|
|
172 |
|
|
173 |
if (m_hMessageProcessorStoppedEvent != NULL)
|
|
174 |
{
|
|
175 |
::CloseHandle(m_hMessageProcessorStoppedEvent);
|
|
176 |
}
|
|
177 |
|
|
178 |
if (m_ConnectSettings)
|
|
179 |
delete m_ConnectSettings;
|
|
180 |
|
|
181 |
|
|
182 |
if (m_ClientList)
|
|
183 |
{
|
|
184 |
m_ClientList->clear();
|
|
185 |
delete m_ClientList;
|
|
186 |
}
|
|
187 |
|
|
188 |
if (m_Registry)
|
|
189 |
{
|
|
190 |
delete m_Registry;
|
|
191 |
}
|
|
192 |
|
|
193 |
if (m_BaseComm)
|
|
194 |
{
|
|
195 |
delete m_BaseComm;
|
|
196 |
}
|
|
197 |
|
|
198 |
if (m_BaseCommHandle)
|
|
199 |
{
|
|
200 |
::FreeLibrary(m_BaseCommHandle);
|
|
201 |
}
|
|
202 |
if (m_BaseProtocol)
|
|
203 |
{
|
|
204 |
delete m_BaseProtocol;
|
|
205 |
}
|
|
206 |
|
|
207 |
if (m_BaseProtocolHandle)
|
|
208 |
{
|
|
209 |
::FreeLibrary(m_BaseProtocolHandle);
|
|
210 |
}
|
|
211 |
|
|
212 |
TCDEBUGCLOSE();
|
|
213 |
if (m_DebugLog)
|
|
214 |
delete m_DebugLog;
|
|
215 |
if (m_DebugLog2)
|
|
216 |
delete m_DebugLog2;
|
|
217 |
|
|
218 |
}
|
|
219 |
|
|
220 |
BOOL CConnectionImpl::IsEqual(CConnection* connection)
|
|
221 |
{
|
|
222 |
TCDEBUGOPEN();
|
|
223 |
TCDEBUGLOGS("CConnectionImpl::IsEqual\n");
|
|
224 |
|
|
225 |
BOOL equal = FALSE;
|
|
226 |
|
|
227 |
if (strcmp(m_ConnectSettings->connectType, connection->m_ConnectSettings->connectType) == 0)
|
|
228 |
{
|
|
229 |
if (m_BaseComm)
|
|
230 |
{
|
|
231 |
if (m_BaseComm->IsConnectionEqual(connection->m_ConnectSettings))
|
|
232 |
{
|
|
233 |
equal = TRUE;
|
|
234 |
}
|
|
235 |
}
|
|
236 |
else
|
|
237 |
{
|
|
238 |
equal = TRUE;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
TCDEBUGCLOSE();
|
|
243 |
return equal;
|
|
244 |
}
|
|
245 |
|
|
246 |
BOOL CConnectionImpl::IsEqual(pConnectData pConData)
|
|
247 |
{
|
|
248 |
TCDEBUGOPEN();
|
|
249 |
TCDEBUGLOGS("CConnectionImpl::IsEqual\n");
|
|
250 |
|
|
251 |
BOOL equal = FALSE;
|
|
252 |
|
|
253 |
if (strcmp(m_ConnectSettings->connectType, pConData->connectType) == 0)
|
|
254 |
{
|
|
255 |
if (m_BaseComm)
|
|
256 |
{
|
|
257 |
if (m_BaseComm->IsConnectionEqual(pConData))
|
|
258 |
{
|
|
259 |
equal = TRUE;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
else
|
|
263 |
{
|
|
264 |
equal = TRUE;
|
|
265 |
}
|
|
266 |
}
|
|
267 |
TCDEBUGCLOSE();
|
|
268 |
return equal;
|
|
269 |
}
|
|
270 |
|
|
271 |
long CConnectionImpl::DoConnect()
|
|
272 |
{
|
|
273 |
TCDEBUGOPEN();
|
|
274 |
TCDEBUGLOGS("CConnectionImpl::DoConnect\n");
|
|
275 |
|
|
276 |
long ret = TCAPI_ERR_NONE;
|
|
277 |
|
|
278 |
if (m_BaseComm && m_BaseProtocol)
|
|
279 |
{
|
|
280 |
ret = m_BaseComm->OpenPort();
|
|
281 |
if (ret != TCAPI_ERR_NONE)
|
|
282 |
{
|
|
283 |
m_OsError = m_BaseComm->m_lastCommError;
|
|
284 |
TCDEBUGLOGA1(" m_BaseComm->OpenPort = %d\n", ret);
|
|
285 |
}
|
|
286 |
}
|
|
287 |
else
|
|
288 |
{
|
|
289 |
ret = TCAPI_ERR_UNKNOWN_MEDIA_TYPE;
|
|
290 |
}
|
|
291 |
|
|
292 |
if (ret == TCAPI_ERR_NONE)
|
|
293 |
{
|
|
294 |
m_Status = eConnected;
|
|
295 |
|
|
296 |
TCDEBUGCLOSE();
|
|
297 |
|
|
298 |
StartProcessing();
|
|
299 |
}
|
|
300 |
else
|
|
301 |
{
|
|
302 |
// if (m_BaseComm != NULL)
|
|
303 |
// {
|
|
304 |
// delete m_BaseComm;
|
|
305 |
// m_BaseComm = NULL;
|
|
306 |
// }
|
|
307 |
TCDEBUGCLOSE();
|
|
308 |
}
|
|
309 |
return ret;
|
|
310 |
}
|
|
311 |
|
|
312 |
long CConnectionImpl::DoDisconnect()
|
|
313 |
{
|
|
314 |
TCDEBUGOPEN();
|
|
315 |
TCDEBUGLOGS("CConnectionImpl::DoDisconnect\n");
|
|
316 |
|
|
317 |
long ret = TCAPI_ERR_NONE;
|
|
318 |
if (IsConnected())
|
|
319 |
{
|
|
320 |
ret = m_BaseComm->ClosePort();
|
|
321 |
// delete m_BaseComm;
|
|
322 |
// m_BaseComm = NULL;
|
|
323 |
}
|
|
324 |
m_Status = eDisconnected;
|
|
325 |
|
|
326 |
TCDEBUGCLOSE();
|
|
327 |
return ret;
|
|
328 |
}
|
|
329 |
|
|
330 |
BOOL CConnectionImpl::AddClient(CClient* client)
|
|
331 |
{
|
|
332 |
TCDEBUGOPEN();
|
|
333 |
TCDEBUGLOGS("CConnectionImpl::AddClient\n");
|
|
334 |
|
|
335 |
BOOL ok = TRUE;
|
|
336 |
|
|
337 |
m_ClientList->push_back(client);
|
|
338 |
|
|
339 |
TCDEBUGCLOSE();
|
|
340 |
return ok;
|
|
341 |
}
|
|
342 |
|
|
343 |
long CConnectionImpl::DoSendMessage(long encodeOption, BYTE protocolVersion, BOOL useMsgId, BYTE msgId, DWORD msgLength, BYTE* pMsg)
|
|
344 |
{
|
|
345 |
TCDEBUGOPEN();
|
|
346 |
TCDEBUGLOGS("CConnectionImpl::DoSendMessage\n");
|
|
347 |
|
|
348 |
long err = TCAPI_ERR_NONE;
|
|
349 |
if (IsRetryInProgress())
|
|
350 |
{
|
|
351 |
err = TCAPI_ERR_COMM_RETRY_IN_PROGRESS;
|
|
352 |
}
|
|
353 |
else if (IsRetryTimedOut())
|
|
354 |
{
|
|
355 |
err = TCAPI_ERR_COMM_TIMEOUT;
|
|
356 |
}
|
|
357 |
else if (m_Status == eConnected)
|
|
358 |
{
|
|
359 |
BYTE* encodedMessage = new BYTE[msgLength + 40]; // add enough for header (msgLength may be 0)
|
|
360 |
// if msgLength == 0, then encodeOption SHOULD be ENCODE_FORMAT since com expects to send something!
|
|
361 |
if (encodeOption == ENCODE_FORMAT)
|
|
362 |
{
|
|
363 |
#ifdef _DEBUG
|
|
364 |
char msg[200]; msg[0] = '\0';
|
|
365 |
int len = (msgLength > 30) ? 30 : msgLength;
|
|
366 |
for (int i = 0; i < len; i ++)
|
|
367 |
{
|
|
368 |
sprintf(msg, "%s%02.2x ", msg, pMsg[i]);
|
|
369 |
}
|
|
370 |
sprintf(msg, "%s\n", msg);
|
|
371 |
TCDEBUGLOGS(msg);
|
|
372 |
#endif
|
|
373 |
// msgLength maybe 0 and pMsg maybe NULL (we're not sending a raw message, just a protocol header)
|
|
374 |
msgLength = m_BaseProtocol->EncodeMessage(pMsg, msgLength, protocolVersion, msgId, encodedMessage, msgLength+40);
|
|
375 |
#ifdef _DEBUG
|
|
376 |
msg[0] = '\0';
|
|
377 |
len = (msgLength > 30) ? 30 : msgLength;
|
|
378 |
for (i = 0; i < len; i ++)
|
|
379 |
{
|
|
380 |
sprintf(msg, "%s%02.2x ", msg, encodedMessage[i]);
|
|
381 |
}
|
|
382 |
sprintf(msg, "%s\n", msg);
|
|
383 |
TCDEBUGLOGS(msg);
|
|
384 |
#endif
|
|
385 |
err = m_BaseComm->SendDataToPort(msgLength, encodedMessage);
|
|
386 |
}
|
|
387 |
else
|
|
388 |
{
|
|
389 |
#ifdef _DEBUG
|
|
390 |
char msg[200]; msg[0] = '\0';
|
|
391 |
int len = (msgLength > 30) ? 30 : msgLength;
|
|
392 |
for (int i = 0; i < len; i ++)
|
|
393 |
{
|
|
394 |
sprintf(msg, "%s%02.2x ", msg, pMsg[i]);
|
|
395 |
}
|
|
396 |
sprintf(msg, "%s\n", msg);
|
|
397 |
TCDEBUGLOGS(msg);
|
|
398 |
#endif
|
|
399 |
// msgLength != 0 and pMsg != NULL
|
|
400 |
err = m_BaseComm->SendDataToPort(msgLength, pMsg);
|
|
401 |
}
|
|
402 |
delete[] encodedMessage;
|
|
403 |
|
|
404 |
TCDEBUGLOGS("CConnectionImpl::DoSendMessage done\n");
|
|
405 |
if (err == TCAPI_ERR_COMM_ERROR)
|
|
406 |
{
|
366
|
407 |
// EnterRetryPeriod(err, true, m_BaseComm->m_lastCommError);
|
60
|
408 |
m_OsError = m_BaseComm->m_lastCommError;
|
|
409 |
}
|
|
410 |
}
|
|
411 |
else
|
|
412 |
{
|
|
413 |
err = TCAPI_ERR_MEDIA_NOT_OPEN;
|
|
414 |
}
|
|
415 |
|
|
416 |
TCDEBUGLOGA1("CConnectionImpl::DoSendMessage err = %d\n", err);
|
|
417 |
TCDEBUGCLOSE();
|
|
418 |
return err;
|
|
419 |
}
|
|
420 |
|
|
421 |
long CConnectionImpl::DoRetryProcessing()
|
|
422 |
{
|
|
423 |
long err = TCAPI_ERR_NONE;
|
|
424 |
|
|
425 |
// if not connected
|
|
426 |
// return no error
|
|
427 |
if (m_BaseComm == NULL /*|| m_BaseComm->IsConnected() == false*/)
|
|
428 |
return TCAPI_ERR_MEDIA_NOT_OPEN;
|
|
429 |
|
|
430 |
// if retry not in progress && retry not timed out
|
|
431 |
// return no error
|
|
432 |
if (!IsRetryInProgress() && !IsRetryTimedOut())
|
|
433 |
return TCAPI_ERR_NONE;
|
|
434 |
|
|
435 |
// TCDEBUGOPEN();
|
|
436 |
// TCDEBUGLOGS("CConnectionImpl::DoRetryProcessing\n");
|
|
437 |
// TCDEBUGCLOSE();
|
|
438 |
// if retry timeout flag already set
|
|
439 |
// return timeout error
|
|
440 |
if (IsRetryTimedOut())
|
|
441 |
return TCAPI_ERR_COMM_TIMEOUT;
|
|
442 |
|
|
443 |
// get current time
|
|
444 |
time_t ctime;
|
|
445 |
time(&ctime);
|
|
446 |
// if retry timeout period has expired
|
|
447 |
if (ctime >= m_RetryTimeoutTime)
|
|
448 |
{
|
|
449 |
TCDEBUGOPEN();
|
|
450 |
TCDEBUGLOGS("CConnectionImpl::DoRetryProcessing retry timeout\n");
|
|
451 |
TCDEBUGCLOSE();
|
|
452 |
// send timeout error to all clients
|
|
453 |
NotifyClientsCommError(TCAPI_ERR_COMM_TIMEOUT);
|
|
454 |
// close comm port
|
|
455 |
m_BaseComm->ClosePort();
|
|
456 |
// set retry timeout flag
|
|
457 |
SetRetryTimedOut();
|
|
458 |
// return retry timeout error
|
|
459 |
err = TCAPI_ERR_COMM_TIMEOUT;
|
|
460 |
}
|
|
461 |
// else if retry time has passed
|
|
462 |
else if (ctime >= m_NextRetryTime)
|
|
463 |
{
|
|
464 |
TCDEBUGOPEN();
|
|
465 |
TCDEBUGLOGS("CConnectionImpl::DoRetryProcessing retry time\n");
|
|
466 |
TCDEBUGCLOSE();
|
|
467 |
// close comm port
|
|
468 |
// reopen comm port
|
|
469 |
m_BaseComm->ClosePort();
|
|
470 |
int openErr = m_BaseComm->OpenPort();
|
|
471 |
// if comm error
|
|
472 |
if (openErr != TCAPI_ERR_NONE)
|
|
473 |
{
|
|
474 |
// set next retry time
|
|
475 |
// return comm error
|
|
476 |
m_NextRetryTime = ctime + m_ConnectSettings->retryInterval;
|
|
477 |
err = TCAPI_ERR_COMM_RETRY_IN_PROGRESS;
|
|
478 |
m_OsError = m_BaseComm->m_lastCommError;
|
|
479 |
}
|
|
480 |
else
|
|
481 |
{
|
|
482 |
TCDEBUGOPEN();
|
|
483 |
TCDEBUGLOGS("CConnectionImpl::DoRetryProcessing reconnected\n");
|
|
484 |
TCDEBUGCLOSE();
|
|
485 |
// send reconnect warning to all clients
|
|
486 |
NotifyClientsCommError(TCAPI_INFO_COMM_RECONNECTED);
|
|
487 |
// set connected
|
|
488 |
SetConnected();
|
|
489 |
err = TCAPI_ERR_NONE;
|
|
490 |
}
|
|
491 |
}
|
|
492 |
else // still in retry
|
|
493 |
{
|
|
494 |
err = TCAPI_ERR_COMM_RETRY_IN_PROGRESS;
|
|
495 |
}
|
|
496 |
|
|
497 |
|
|
498 |
// TCDEBUGOPEN();
|
|
499 |
// TCDEBUGLOGA1("CConnectionImpl::DoRetryProcessing err = %d\n", err);
|
|
500 |
// TCDEBUGCLOSE();
|
|
501 |
return err;
|
|
502 |
}
|
|
503 |
long CConnectionImpl::EnterRetryPeriod(long commErr, bool passOsErr, DWORD osErr)
|
|
504 |
{
|
|
505 |
TCDEBUGOPEN();
|
366
|
506 |
TCDEBUGLOGA3("CConnectionImpl::EnterRetryPeriod commErr=%d passOsErr=%d osErr=%d\n", commErr, passOsErr, osErr);
|
60
|
507 |
TCDEBUGCLOSE();
|
|
508 |
|
|
509 |
long err = TCAPI_ERR_NONE;
|
|
510 |
|
|
511 |
// set next retry time
|
|
512 |
time_t ctime;
|
|
513 |
time(&ctime);
|
|
514 |
m_NextRetryTime = ctime + m_ConnectSettings->retryInterval;
|
|
515 |
// set retry timeout time
|
|
516 |
m_RetryTimeoutTime = ctime + m_ConnectSettings->retryTimeout;
|
|
517 |
// send comm error to all clients
|
|
518 |
NotifyClientsCommError(commErr, passOsErr, osErr);
|
|
519 |
// set retry in progress flag
|
|
520 |
SetRetryInProgress();
|
|
521 |
|
|
522 |
return err;
|
|
523 |
}
|
|
524 |
|
|
525 |
BOOL CConnectionImpl::RemoveClient(CClient* client)
|
|
526 |
{
|
|
527 |
TCDEBUGOPEN();
|
|
528 |
TCDEBUGLOGS("CConnectionImpl::RemoveClient\n");
|
|
529 |
|
|
530 |
BOOL found = FALSE;
|
|
531 |
|
|
532 |
if (m_ClientList->size() != 0)
|
|
533 |
{
|
|
534 |
ClientList::iterator iter;
|
|
535 |
for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++)
|
|
536 |
{
|
|
537 |
if ((*iter)->GetClientId() == client->GetClientId())
|
|
538 |
{
|
|
539 |
m_ClientList->erase(iter);
|
|
540 |
found = TRUE;
|
|
541 |
break;
|
|
542 |
}
|
|
543 |
}
|
|
544 |
}
|
|
545 |
|
|
546 |
TCDEBUGCLOSE();
|
|
547 |
return found;
|
|
548 |
}
|
|
549 |
|
|
550 |
BOOL CConnectionImpl::ExitProcessing()
|
|
551 |
{
|
|
552 |
TCDEBUGOPEN();
|
|
553 |
TCDEBUGLOGS("CConnectionImpl::ExitProcessing\n");
|
|
554 |
|
|
555 |
// exit the messageprocessing thread
|
|
556 |
if (m_hMessageProcessorThread != NULL)
|
|
557 |
{
|
|
558 |
m_MessageProcessorState = MP_EXIT;
|
|
559 |
|
|
560 |
m_StartMessageProcessing = false;
|
|
561 |
m_PauseMessageProcessing = true;
|
|
562 |
m_ExitMessageProcessor = true;
|
|
563 |
DWORD waitStatus = ::WaitForSingleObject(m_hMessageProcessorExittedEvent, MESSAGEPROCESSOR_EVENTWAIT_TIMEOUT);
|
|
564 |
TCDEBUGLOGA1("CConnectionImpl::ExitProcessing waitStatus=%x\n", waitStatus);
|
|
565 |
::CloseHandle(m_hMessageProcessorThread);
|
|
566 |
m_hMessageProcessorThread = NULL;
|
|
567 |
}
|
|
568 |
|
|
569 |
TCDEBUGCLOSE();
|
|
570 |
return TRUE;
|
|
571 |
}
|
|
572 |
|
|
573 |
BOOL CConnectionImpl::StartProcessing()
|
|
574 |
{
|
|
575 |
TCDEBUGOPEN();
|
|
576 |
TCDEBUGLOGS("CConnectionImpl::StartProcessing\n");
|
|
577 |
|
|
578 |
// starts processing thread
|
|
579 |
if (m_hMessageProcessorThread == NULL)
|
|
580 |
{
|
|
581 |
m_MessageProcessorState = MP_PAUSE;
|
|
582 |
|
|
583 |
m_ExitMessageProcessor = false;
|
|
584 |
m_StartMessageProcessing = false;
|
|
585 |
m_PauseMessageProcessing = false;
|
|
586 |
// TODO: create thread
|
|
587 |
m_hMessageProcessorThread = ::CreateThread(
|
|
588 |
NULL,
|
|
589 |
0,
|
|
590 |
(LPTHREAD_START_ROUTINE) MessageProcessor,
|
|
591 |
this,
|
|
592 |
0,
|
|
593 |
&m_dwMessageProcessorThreadId);
|
|
594 |
}
|
|
595 |
|
|
596 |
TCDEBUGCLOSE();
|
|
597 |
return PauseProcessing();//RestartProcessing();
|
|
598 |
}
|
|
599 |
|
|
600 |
BOOL CConnectionImpl::PauseProcessing()
|
|
601 |
{
|
|
602 |
TCDEBUGOPEN();
|
|
603 |
TCDEBUGLOGS("CConnectionImpl::PauseProcessing\n");
|
|
604 |
|
|
605 |
// tells the processing thread to pause
|
|
606 |
if (m_hMessageProcessorThread != NULL)
|
|
607 |
{
|
|
608 |
m_MessageProcessorState = MP_PAUSE;
|
|
609 |
|
|
610 |
m_ExitMessageProcessor = false;
|
|
611 |
m_StartMessageProcessing = false;
|
|
612 |
m_PauseMessageProcessing = true;
|
|
613 |
DWORD waitStatus = ::WaitForSingleObject(m_hMessageProcessorStoppedEvent, MESSAGEPROCESSOR_EVENTWAIT_TIMEOUT);
|
|
614 |
TCDEBUGLOGA1("CConnectionImpl::PauseProcessing waitStatus=%x\n", waitStatus);
|
|
615 |
}
|
|
616 |
|
|
617 |
TCDEBUGCLOSE();
|
|
618 |
return TRUE;
|
|
619 |
}
|
|
620 |
|
|
621 |
BOOL CConnectionImpl::RestartProcessing()
|
|
622 |
{
|
|
623 |
TCDEBUGOPEN();
|
|
624 |
TCDEBUGLOGS("CConnectionImpl::RestartProcessing\n");
|
|
625 |
|
|
626 |
// tell the processing thread to restart
|
|
627 |
if (m_hMessageProcessorThread != NULL)
|
|
628 |
{
|
|
629 |
m_MessageProcessorState = MP_START;
|
|
630 |
|
|
631 |
m_ExitMessageProcessor = false;
|
|
632 |
m_StartMessageProcessing = true;
|
|
633 |
m_PauseMessageProcessing = false;
|
|
634 |
DWORD waitStatus = ::WaitForSingleObject(m_hMessageProcessorStartedEvent, MESSAGEPROCESSOR_EVENTWAIT_TIMEOUT);
|
|
635 |
TCDEBUGLOGA1("CConnectionImpl::RestartProcessing waitStatus=%x\n", waitStatus);
|
|
636 |
}
|
|
637 |
|
|
638 |
TCDEBUGCLOSE();
|
|
639 |
return TRUE;
|
|
640 |
}
|
|
641 |
|
|
642 |
BOOL CConnectionImpl::RemoveClientFromRegistry(CClient* client)
|
|
643 |
{
|
|
644 |
TCDEBUGOPEN();
|
|
645 |
TCDEBUGLOGS("CConnectionImpl::RemoveClientFromRegistry\n");
|
|
646 |
TCDEBUGCLOSE();
|
|
647 |
|
|
648 |
return m_Registry->RemoveClient(client);
|
|
649 |
}
|
|
650 |
|
|
651 |
BOOL CConnectionImpl::AddClientToRegistry(CClient* client, long numberIds, BYTE* ids)
|
|
652 |
{
|
|
653 |
TCDEBUGOPEN();
|
|
654 |
TCDEBUGLOGS("CConnectionImpl::AddClientToRegistry\n");
|
|
655 |
TCDEBUGCLOSE();
|
|
656 |
|
|
657 |
return m_Registry->AddClient(client, numberIds, ids);
|
|
658 |
}
|
|
659 |
|
|
660 |
void CConnectionImpl::NotifyClientsCommError(long tcfError, bool passOsError, DWORD osError)
|
|
661 |
{
|
|
662 |
// TCDEBUGOPEN();
|
|
663 |
// TCDEBUGLOGS("CConnectionImpl::NotifyClientsCommError\n");
|
|
664 |
// TCDEBUGCLOSE();
|
|
665 |
|
|
666 |
if (m_ClientList->size() != 0)
|
|
667 |
{
|
|
668 |
ClientList::iterator iter;
|
|
669 |
for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++)
|
|
670 |
{
|
|
671 |
CErrorMonitor* errorMonitor = (*iter)->m_ErrorMonitor;
|
|
672 |
errorMonitor->PutError(tcfError, passOsError, osError);
|
|
673 |
}
|
|
674 |
}
|
|
675 |
}
|
|
676 |
BOOL CConnectionImpl::HasVersion()
|
|
677 |
{
|
|
678 |
BOOL found = FALSE;
|
|
679 |
|
|
680 |
if (m_BaseComm && m_BaseComm->HasVersion())
|
|
681 |
found = TRUE;
|
|
682 |
|
|
683 |
return found;
|
|
684 |
}
|
|
685 |
void CConnectionImpl::GetVersion(char* version)
|
|
686 |
{
|
|
687 |
if (HasVersion()) {
|
|
688 |
m_BaseComm->GetVersion(version);
|
|
689 |
}
|
|
690 |
}
|
|
691 |
|
|
692 |
void CConnectionImpl::UnLockAllDestinations()
|
|
693 |
{
|
|
694 |
if (m_ClientList->size() != 0)
|
|
695 |
{
|
|
696 |
ClientList::iterator iter;
|
|
697 |
for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++)
|
|
698 |
{
|
|
699 |
CInputStream* inputStream = (*iter)->m_InputStream;
|
|
700 |
CMessageFile* file = (*iter)->m_MessageFile;
|
|
701 |
if (inputStream != NULL)
|
|
702 |
{
|
|
703 |
inputStream->UnLockStream();
|
|
704 |
}
|
|
705 |
else if (file != NULL)
|
|
706 |
{
|
|
707 |
file->UnLockMessageFile();
|
|
708 |
}
|
|
709 |
}
|
|
710 |
}
|
|
711 |
}
|
|
712 |
|
|
713 |
#if defined(LOG_MPROCESSOR) && defined(_DEBUG)
|
|
714 |
#define MPLOGOPEN() if (gDoLogging) { pThis->m_DebugLog2->WaitForAccess(); }
|
|
715 |
#define MPLOGS(s) if (gDoLogging) { sprintf(pThis->m_DebugLogMsg2,"%s", s); pThis->m_DebugLog2->log(pThis->m_DebugLogMsg2); }
|
|
716 |
#define MPLOGA1(s, a1) if (gDoLogging) { sprintf(pThis->m_DebugLogMsg2, s, a1); pThis->m_DebugLog2->log(pThis->m_DebugLogMsg2); }
|
|
717 |
#define MPLOGA2(s, a1, a2) if (gDoLogging) { sprintf(pThis->m_DebugLogMsg2, s, a1, a2); pThis->m_DebugLog2->log(pThis->m_DebugLogMsg2); }
|
|
718 |
#define MPLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(pThis->m_DebugLogMsg2, s, a1, a2, a3); pThis->m_DebugLog2->log(pThis->m_DebugLogMsg2); }
|
|
719 |
#define MPLOGCLOSE() if (gDoLogging) { pThis->m_DebugLog2->ReleaseAccess(); }
|
|
720 |
#else
|
|
721 |
#define MPLOGOPEN()
|
|
722 |
#define MPLOGS(s)
|
|
723 |
#define MPLOGA1(s, a1)
|
|
724 |
#define MPLOGA2(s, a1, a2)
|
|
725 |
#define MPLOGA3(s, a1, a2, a3)
|
|
726 |
#define MPLOGCLOSE()
|
|
727 |
#endif
|
|
728 |
|
|
729 |
DWORD WINAPI CConnectionImpl::MessageProcessor(LPVOID lpParam)
|
|
730 |
{
|
|
731 |
CConnectionImpl* pThis = (CConnectionImpl*)lpParam;
|
|
732 |
|
|
733 |
MPLOGOPEN();
|
|
734 |
MPLOGS("MessageProcessor start thread\n");
|
|
735 |
|
|
736 |
bool processing = false;
|
|
737 |
long err = TCAPI_ERR_NONE;
|
|
738 |
DWORD pollSize = 0;
|
|
739 |
|
|
740 |
while (pThis->m_MessageProcessorState != MP_EXIT)
|
|
741 |
{
|
|
742 |
if (pThis->m_MessageProcessorState == MP_PAUSE)
|
|
743 |
{
|
|
744 |
MPLOGS("MessageProcessor pause\n");
|
|
745 |
|
|
746 |
processing = false;
|
|
747 |
pThis->m_PauseMessageProcessing = false;
|
|
748 |
pThis->m_MessageProcessorState = MP_NONE;
|
|
749 |
BOOL ok = ::SetEvent(pThis->m_hMessageProcessorStoppedEvent);
|
|
750 |
}
|
|
751 |
|
|
752 |
if (pThis->IsRetryInProgress())
|
|
753 |
err = pThis->DoRetryProcessing();
|
|
754 |
else if (pThis->IsRetryTimedOut())
|
|
755 |
err = TCAPI_ERR_COMM_TIMEOUT;
|
|
756 |
|
|
757 |
if (processing && err == TCAPI_ERR_NONE)
|
|
758 |
{
|
|
759 |
if (pThis->m_BaseComm && pThis->m_BaseComm->IsConnected())
|
|
760 |
{
|
|
761 |
err = pThis->m_BaseComm->PollPort(pollSize);
|
|
762 |
MPLOGA2("MessageProcessor PollPort = %d pollsize = %d\n", err, pollSize);
|
|
763 |
if (err != TCAPI_ERR_NONE)
|
|
764 |
{
|
|
765 |
MPLOGA2("MessageProcessor err = %d osError = %d\n", err, pThis->m_BaseComm->m_lastCommError);
|
366
|
766 |
// pThis->EnterRetryPeriod(err, true, pThis->m_BaseComm->m_lastCommError);
|
60
|
767 |
}
|
|
768 |
else
|
|
769 |
{
|
|
770 |
if (pollSize == 0)
|
|
771 |
{
|
|
772 |
Sleep(1);
|
|
773 |
}
|
|
774 |
else
|
|
775 |
{
|
|
776 |
long numberProcessed = 0;
|
|
777 |
// MPLOGA1("MessageProcessor ProcessBuffer pRegistry = %x\n", pThis->m_Registry);
|
|
778 |
err = pThis->m_BaseComm->ProcessBuffer(pThis, pThis->m_Registry, numberProcessed);
|
|
779 |
|
|
780 |
MPLOGA2("MessageProcessor ProcessBuffer err = %d number = %d\n", err, numberProcessed);
|
|
781 |
|
|
782 |
if (err == TCAPI_ERR_COMM_ERROR)
|
|
783 |
{
|
|
784 |
// for this error we have os error, but we probably caught this in PollPort already
|
366
|
785 |
// pThis->EnterRetryPeriod(err, true, pThis->m_BaseComm->m_lastCommError);
|
60
|
786 |
}
|
|
787 |
else if (err != TCAPI_ERR_NONE)
|
|
788 |
{
|
|
789 |
// all clients already notified in ProcessBuffer
|
|
790 |
err = TCAPI_ERR_NONE;
|
|
791 |
}
|
|
792 |
pThis->UnLockAllDestinations(); // unlock all input streams, if they became locked during AddMessage()
|
|
793 |
// Sleep(1);
|
|
794 |
}
|
|
795 |
}
|
|
796 |
// MPLOGS("MessageProcessor FlushAllClientMessageFiles\n");
|
|
797 |
pThis->FlushAllClientMessageFiles();
|
|
798 |
}
|
|
799 |
else
|
|
800 |
{
|
|
801 |
// basecom not connected
|
|
802 |
Sleep(1);
|
|
803 |
}
|
|
804 |
}
|
|
805 |
else
|
|
806 |
{
|
|
807 |
// processing is not being done
|
|
808 |
Sleep(1);
|
|
809 |
}
|
|
810 |
if (pThis->m_MessageProcessorState == MP_START)
|
|
811 |
{
|
|
812 |
MPLOGS("MessageProcessor start\n");
|
|
813 |
|
|
814 |
processing = true;
|
|
815 |
pThis->m_StartMessageProcessing = false;
|
|
816 |
pThis->m_MessageProcessorState = MP_PROCESSING;
|
|
817 |
BOOL ok = ::SetEvent(pThis->m_hMessageProcessorStartedEvent);
|
|
818 |
}
|
|
819 |
}
|
|
820 |
// signal we're stopping
|
|
821 |
pThis->m_ExitMessageProcessor = false;
|
|
822 |
pThis->m_MessageProcessorState = MP_NONE;
|
|
823 |
::SetEvent(pThis->m_hMessageProcessorExittedEvent);
|
|
824 |
|
|
825 |
MPLOGS("MessageProcessor exit thread\n");
|
|
826 |
MPLOGCLOSE();
|
|
827 |
|
|
828 |
return 0;
|
|
829 |
}
|
|
830 |
|
|
831 |
void CConnectionImpl::FlushAllClientMessageFiles()
|
|
832 |
{
|
|
833 |
DWORD cTick = GetTickCount();
|
|
834 |
|
|
835 |
// MPLOGA2("CConnectionImpl::FlushAllClientMessageFiles cTick=%d m_NextFlushFileTime=%d\n", cTick, m_NextFlushFileTime);
|
|
836 |
|
|
837 |
if (cTick > m_NextFlushFileTime)
|
|
838 |
{
|
|
839 |
// MPLOGS("CConnectionImpl::FlushAllClientMessageFiles flush timeout\n");
|
|
840 |
if (m_ClientList->size() != 0)
|
|
841 |
{
|
|
842 |
ClientList::iterator iter;
|
|
843 |
for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++)
|
|
844 |
{
|
|
845 |
CMessageFile* file = (*iter)->m_MessageFile;
|
|
846 |
if (file != NULL)
|
|
847 |
{
|
|
848 |
// MPLOGS("CConnectionImpl::FlushAllClientMessageFiles flush client\n");
|
|
849 |
file->FlushFile();
|
|
850 |
}
|
|
851 |
}
|
|
852 |
}
|
|
853 |
m_NextFlushFileTime = GetTickCount() + FLUSH_TIME;
|
|
854 |
}
|
|
855 |
}
|
|
856 |
|
|
857 |
BOOL CConnectionImpl::CreateCommProtocols(const char* commPath, const char* protPath)
|
|
858 |
{
|
|
859 |
BOOL loaded = FALSE;
|
|
860 |
|
|
861 |
TCDEBUGOPEN();
|
|
862 |
TCDEBUGLOGS("CConnectionImpl::CreateCommProtocols\n");
|
|
863 |
|
|
864 |
TCDEBUGLOGA2(" commPath=%s protPath=%s\n", commPath, protPath);
|
|
865 |
|
|
866 |
m_BaseCommHandle = ::LoadLibrary(commPath);
|
|
867 |
m_BaseProtocolHandle = ::LoadLibrary(protPath);
|
|
868 |
if (m_BaseCommHandle == NULL || m_BaseProtocolHandle == NULL)
|
|
869 |
{
|
|
870 |
TCDEBUGLOGA2(" error loading library, m_BaseCommHandle=%x m_BaseProtocolHandle=%x\n", m_BaseCommHandle, m_BaseProtocolHandle);
|
|
871 |
if (m_BaseCommHandle) ::FreeLibrary(m_BaseCommHandle); m_BaseCommHandle = NULL;
|
|
872 |
if (m_BaseProtocolHandle) ::FreeLibrary(m_BaseProtocolHandle); m_BaseProtocolHandle = NULL;
|
|
873 |
|
|
874 |
}
|
|
875 |
else
|
|
876 |
{
|
|
877 |
COMMCREATE lpCommFn = (COMMCREATE)::GetProcAddress(m_BaseCommHandle, COMMCREATE_FNNAME);
|
|
878 |
PROTOCOLCREATE lpProtFn = (PROTOCOLCREATE)::GetProcAddress(m_BaseProtocolHandle, PROTOCOLCREATE_FNNAME);
|
|
879 |
if (lpCommFn == NULL || lpProtFn == NULL)
|
|
880 |
{
|
|
881 |
TCDEBUGLOGA2(" error finding function, lpCommFn=%x lpProtFn=%x\n", lpCommFn, lpProtFn);
|
|
882 |
if (m_BaseCommHandle) ::FreeLibrary(m_BaseCommHandle); m_BaseCommHandle = NULL;
|
|
883 |
if (m_BaseProtocolHandle) ::FreeLibrary(m_BaseProtocolHandle); m_BaseProtocolHandle = NULL;
|
|
884 |
}
|
|
885 |
else
|
|
886 |
{
|
|
887 |
m_BaseProtocol = lpProtFn();
|
|
888 |
if (m_BaseProtocol == NULL)
|
|
889 |
{
|
|
890 |
TCDEBUGLOGA1(" error creating protocol, m_BaseProtocol=%x\n", m_BaseProtocol);
|
|
891 |
if (m_BaseCommHandle) ::FreeLibrary(m_BaseCommHandle); m_BaseCommHandle = NULL;
|
|
892 |
if (m_BaseProtocolHandle) ::FreeLibrary(m_BaseProtocolHandle); m_BaseProtocolHandle = NULL;
|
|
893 |
}
|
|
894 |
else
|
|
895 |
{
|
|
896 |
m_BaseComm = lpCommFn(m_ConnectSettings, m_ConnectionID, m_BaseProtocol);
|
|
897 |
if (m_BaseComm == NULL)
|
|
898 |
{
|
|
899 |
TCDEBUGLOGA1(" error creating comm, m_BaseComm=%x\n", m_BaseComm);
|
|
900 |
if (m_BaseProtocol) delete m_BaseProtocol; m_BaseProtocol = NULL;
|
|
901 |
|
|
902 |
if (m_BaseCommHandle) ::FreeLibrary(m_BaseCommHandle); m_BaseCommHandle = NULL;
|
|
903 |
if (m_BaseProtocolHandle) ::FreeLibrary(m_BaseProtocolHandle); m_BaseProtocolHandle = NULL;
|
|
904 |
}
|
|
905 |
else
|
|
906 |
{
|
|
907 |
loaded = TRUE;
|
|
908 |
TCDEBUGLOGA4(" created class, m_BaseComm=%x m_BaseProtocol=%x m_BaseCommHandle=%x m_BaseProtocolHandle=%x\n", m_BaseComm, m_BaseProtocol, m_BaseCommHandle, m_BaseProtocolHandle);
|
|
909 |
}
|
|
910 |
}
|
|
911 |
}
|
|
912 |
}
|
|
913 |
|
|
914 |
TCDEBUGCLOSE();
|
|
915 |
return loaded;
|
|
916 |
}
|