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