connectivity/com.nokia.tcf/native/TCFNative/TCFServer/MessageFile.cpp
author fturovic <frank.turovich@nokia.com>
Mon, 29 Jun 2009 16:17:23 -0500
changeset 326 ab7c9b8bd24f
parent 60 9d2210c8eed2
permissions -rw-r--r--
bug 9343 fixed broken link
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
// MessageFile.cpp: implementation of the CMessageFile 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 "ServerClient.h"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    23
#include "MessageFile.h"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    24
#include "ServerManager.h"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    25
#include "TCErrorConstants.h"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    26
#include <stdio.h>
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    27
#include <sys/stat.h>
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    28
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    29
//#define USE_TEXT_FILE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    30
#ifdef USE_TEXT_FILE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    31
#define WRITE_MODE		"w+t"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    32
#define APPEND_MODE		"a+t"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    33
#else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    34
#define WRITE_MODE		"w+b"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    35
#define APPEND_MODE		"a+b"
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    36
#endif
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    37
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    38
#define FLUSH_FREQ	2000	// # messages to write between flushing (if buffered)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    39
#ifdef _DEBUG
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    40
extern BOOL gDoLogging;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    41
#endif
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    42
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    43
//#define LOG_MESSAGEFILE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    44
#if defined(LOG_MESSAGEFILE) && defined(_DEBUG)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    45
extern CServerManager* gManager;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    46
#define TCDEBUGOPEN() if (gDoLogging) { gManager->m_DebugLog->WaitForAccess(); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    47
#define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); gManager->m_DebugLog->log(TCDebugMsg); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    48
#define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); gManager->m_DebugLog->log(TCDebugMsg); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    49
#define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); gManager->m_DebugLog->log(TCDebugMsg); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    50
#define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); gManager->m_DebugLog->log(TCDebugMsg); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    51
#define TCDEBUGCLOSE() if (gDoLogging) { gManager->m_DebugLog->ReleaseAccess(); }
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    52
#else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    53
#define TCDEBUGOPEN()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    54
#define TCDEBUGLOGS(s)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    55
#define TCDEBUGLOGA1(s, a1)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    56
#define TCDEBUGLOGA2(s, a1, a2)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    57
#define TCDEBUGLOGA3(s, a1, a2, a3)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    58
#define TCDEBUGCLOSE()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    59
#endif
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    60
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    61
//////////////////////////////////////////////////////////////////////
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    62
// Construction/Destruction
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    63
//////////////////////////////////////////////////////////////////////
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    64
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    65
CMessageFile::CMessageFile(CHAR* pFilePath, long inClientID)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    66
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    67
	m_ClientID = inClientID;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    68
	m_FileLocked = FALSE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    69
	strncpy(m_FilePath, pFilePath, MAX_FILEPATH);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    70
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    71
	char toString[30];
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    72
	sprintf(toString, "%s%04.4d", MESSAGEFILE_MUTEX_BASENAME, m_ClientID);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    73
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    74
	m_Mutex.Open(toString, MESSAGEFILE_MUTEX_TIMEOUT);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    75
	m_hFile = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    76
	m_numWritten = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    77
	m_numWrittenSinceLastFlush = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    78
	m_Open = FALSE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    79
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    80
#ifdef LOG_FILE_PERFORMANCE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    81
	perfFileName="c:\\tcf\\serverfileperf.txt";
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    82
	fpLog = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    83
	numLogged=0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    84
#endif
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    85
	OPENFILEPERF();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    86
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    87
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    88
CMessageFile::~CMessageFile()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    89
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    90
	CLOSEFILEPERF();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    91
	Close();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    92
	m_Mutex.Close();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    93
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    94
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    95
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    96
long CMessageFile::AddMessage(DWORD inLength, BYTE* inMessage)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    97
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    98
	long err = TCAPI_ERR_NONE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
    99
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   100
	BOOL gotIt = WaitForAccess(); // will lock on first access only
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   101
	size_t lenWritten = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   102
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   103
#ifdef USE_TEXT_FILE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   104
	lenWritten = inLength;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   105
	int textStart = 20;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   106
	if (inMessage[9] == 0x5a) textStart = 64;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   107
	for (int i = 0; i < textStart; i++)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   108
		fprintf(m_hFile, "%02.2X ", inMessage[i]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   109
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   110
	for (i = textStart; i < (int)inLength; i++)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   111
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   112
		if (isprint(inMessage[i]))
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   113
			fprintf(m_hFile, "%c", inMessage[i]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   114
		else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   115
			fprintf(m_hFile, "%02.2X ", inMessage[i]);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   116
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   117
	fprintf(m_hFile,"\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   118
#else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   119
	lenWritten = fwrite(inMessage, 1, inLength, m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   120
#endif
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   121
	if (lenWritten < inLength)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   122
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   123
		err = TCAPI_ERR_WRITING_FILE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   124
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   125
	else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   126
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   127
		m_numWrittenSinceLastFlush++;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   128
		m_numWritten++;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   129
		if ((m_numWrittenSinceLastFlush % FLUSH_FREQ) == 0)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   130
		{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   131
			FlushFile(TRUE);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   132
		}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   133
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   134
	LOGFILEPERF("AddMessage\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   135
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   136
	// no ReleaseAccess this is done by Connection when all bytes processed in buffer and
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   137
	//   calls all clients unlock method
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   138
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   139
	return err;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   140
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   141
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   142
long CMessageFile::Open()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   143
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   144
	TCDEBUGOPEN();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   145
	long err = TCAPI_ERR_NONE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   146
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   147
	TCDEBUGLOGS("CMessageFile::Open\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   148
	WaitForAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   149
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   150
	if (m_hFile)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   151
		fclose(m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   152
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   153
	m_hFile = _fsopen(m_FilePath, APPEND_MODE, _SH_DENYNO);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   154
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   155
	if (m_hFile == NULL)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   156
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   157
		err = TCAPI_ERR_FILE_DOES_NOT_EXIST;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   158
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   159
	else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   160
		m_Open = TRUE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   161
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   162
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   163
	ReleaseAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   164
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   165
	TCDEBUGCLOSE();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   166
	return err;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   167
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   168
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   169
long CMessageFile::Close()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   170
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   171
	TCDEBUGOPEN();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   172
	long err = TCAPI_ERR_NONE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   173
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   174
	TCDEBUGLOGA1("CMessageFile::Close numWritten=%d\n", m_numWritten);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   175
	WaitForAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   176
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   177
	if (m_hFile)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   178
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   179
		fclose(m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   180
		m_hFile = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   181
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   182
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   183
	ReleaseAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   184
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   185
	TCDEBUGCLOSE();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   186
	return err;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   187
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   188
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   189
long CMessageFile::ClearFile()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   190
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   191
	TCDEBUGOPEN();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   192
	long err = TCAPI_ERR_NONE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   193
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   194
	TCDEBUGLOGA2("CMessageFile::ClearFile m_hFile=%x numWritten=%d\n", m_hFile, m_numWritten);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   195
	WaitForAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   196
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   197
	if (m_hFile)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   198
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   199
		fclose(m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   200
		m_hFile = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   201
		m_Open = FALSE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   202
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   203
	m_numWritten = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   204
	m_numWrittenSinceLastFlush = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   205
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   206
	m_hFile = _fsopen(m_FilePath, WRITE_MODE, _SH_DENYNO);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   207
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   208
	if (m_hFile == NULL)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   209
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   210
		err = TCAPI_ERR_FILE_DOES_NOT_EXIST;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   211
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   212
	else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   213
		m_Open = TRUE;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   214
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   215
	LOGFILEPERF("ClearFile\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   216
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   217
	TCDEBUGLOGA1("CMessageFile::ClearFile m_hFile=%x\n", m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   218
	ReleaseAccess();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   219
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   220
	TCDEBUGCLOSE();
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   221
	return err;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   222
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   223
void CMessageFile::FlushFile(BOOL numberTimeOut)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   224
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   225
	if (m_hFile && m_numWrittenSinceLastFlush > 0)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   226
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   227
		fflush(m_hFile);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   228
		m_numWrittenSinceLastFlush = 0;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   229
		if (numberTimeOut)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   230
			LOGFILEPERF("FlushFile <number>\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   231
		else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   232
			LOGFILEPERF("FlushFile <time>\n");
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   233
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   234
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   235
#ifdef LOG_FILE_PERFORMANCE
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   236
void CMessageFile::logPerf(char* msg)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   237
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   238
	if (fpLog)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   239
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   240
		SYSTEMTIME sTime;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   241
		GetLocalTime(&sTime);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   242
		fprintf(fpLog, 
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   243
			"%02.2d%02.2d-%02.2d:%02.2d:%02.2d.%03.3d: %s",
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   244
			sTime.wDay, sTime.wMonth, sTime.wHour, sTime.wMinute, sTime.wSecond, sTime.wMilliseconds,
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   245
			msg);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   246
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   247
		numLogged++;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   248
		if ((numLogged % 1000) == 0)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   249
			fflush(fpLog);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   250
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   251
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   252
void CMessageFile::openPerf()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   253
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   254
	struct _stat buf;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   255
	char* dirname = "c:\\tcf";
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   256
	int result = _stat(dirname, &buf);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   257
	if (result == 0) // exists
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   258
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   259
		if (fpLog == NULL)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   260
			fpLog = _fsopen(perfFileName, "at", _SH_DENYNO);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   261
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   262
	else
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   263
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   264
		fpLog = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   265
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   266
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   267
void CMessageFile::closePerf()
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   268
{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   269
	if (fpLog)
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   270
	{
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   271
		fflush(fpLog);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   272
		fclose(fpLog);
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   273
	}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   274
	fpLog = NULL;
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   275
}
9d2210c8eed2 Commit changes for 2.1.0
chpeckha
parents:
diff changeset
   276
#endif