mmapdatf.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 838 行 · 第 1/2 页

CPP
838
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: mmapdatf.cpp,v 1.4.42.4 2004/07/09 01:44:27 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
 * 
 * The contents of this file, and the files included with this file,
 * are subject to the current version of the RealNetworks Public
 * Source License (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (the "RCSL") available at
 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
 * will apply. You may also obtain the license terms directly from
 * RealNetworks.  You may not use this file except in compliance with
 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
 * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
 * the rights, obligations and limitations governing use of the
 * contents of the file.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the
 * portions it created.
 * 
 * This file, and the files included with this file, is distributed
 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 * 
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 * 
 * Contributor(s):
 * 
 * ***** END LICENSE BLOCK ***** */

#include "hxcom.h"
#include "hxresult.h"
#include "hxtypes.h"
#include "hlxclib/sys/stat.h"
#include "hlxclib/sys/types.h"
#include "hlxclib/io.h"
#include "hlxclib/fcntl.h"
#include "hlxclib/windows.h"
#include "hxbuffer.h"
#include "mmapdatf.h"
#include "mmapmgr.h"
#include "hxfiles.h"
#include "hxprefs.h"
#include "filespec.h"
#include "filespecutils.h"
#include "platform/win/filetimeutil.h"
#include "hxcore.h"

MemoryMapManager* MemoryMapDataFile::m_zpMMM = NULL;
BOOL MemoryMapDataFile::g_NT = FALSE;

MemoryMapDataFile::MemoryMapDataFile(IUnknown* pContext,
    REF(IUnknown*) pPersistantObject, BOOL bDisableMemoryMappedIO,
    UINT32 ulChunkSize, BOOL bEnableFileLocking)
    : m_lRefCount(0)
    , m_hFile(INVALID_HANDLE_VALUE)
    , m_pFilename(0)
    , m_ulPos(-1)
    , MmapHandle(0)
    , m_ulChunkSize(ulChunkSize)
    , m_ulLastError(HXR_OK)
{
    m_pContext = pContext;
    m_pContext->AddRef();

    if (m_zpMMM == NULL)
    {
	m_zpMMM = new MemoryMapManager(pContext, bDisableMemoryMappedIO,
		m_ulChunkSize);
	OSVERSIONINFO vinfo;
	vinfo.dwOSVersionInfoSize = sizeof(vinfo);
	if (GetVersionEx(&vinfo))
	{
	    if (vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
	    {
		g_NT = TRUE;
	    }
	}
    }

    if (!pPersistantObject)
    {
	pPersistantObject = m_zpMMM;
	pPersistantObject->AddRef();
    }
}

MemoryMapDataFile::~MemoryMapDataFile()
{
    if (m_hFile != INVALID_HANDLE_VALUE)
    {
	CloseHandle(m_hFile);
	m_hFile = INVALID_HANDLE_VALUE;
    }
    if (m_pFilename)
    {
	delete[] m_pFilename;
	m_pFilename = 0;
    }

    HX_RELEASE(m_pContext);
}

/*
 *  IUnknown methods
 */
STDMETHODIMP
MemoryMapDataFile::QueryInterface(REFIID riid,
			void** ppvObj)
{
    if (IsEqualIID(IID_IUnknown, riid))
    {
	AddRef();
	*ppvObj = this;
	return HXR_OK;
    }
    else if(IsEqualIID(IID_IHXDataFile, riid))
    {
	AddRef();
	*ppvObj = (IHXDataFile*)this;
	return HXR_OK;
    }
    *ppvObj = 0;
    return HXR_NOINTERFACE;
}


STDMETHODIMP_(ULONG32)
MemoryMapDataFile::AddRef()
{
    return InterlockedIncrement(&m_lRefCount);
}


STDMETHODIMP_(ULONG32)
MemoryMapDataFile::Release()
{
    if (InterlockedDecrement(&m_lRefCount) > 0)
    {
	return m_lRefCount;
    }
    delete this;
    return 0;
}
    
/*
 *  IHXDataFile methods
 */

/* Bind DataFile Object with FileName */
STDMETHODIMP_(void)
MemoryMapDataFile::Bind(const char* FileName)
{
    /* XXXPM
    if (m_hFile != INVALID_HANDLE_VALUE)
    {
	CloseHandle(m_hFile);
	m_hFile = INVALID_HANDLE_VALUE;
    }*/

    if (m_pFilename)
    {
	delete[] m_pFilename;
	m_pFilename = 0;
    }
    m_pFilename = new char[strlen(FileName) + 1];
    strcpy(m_pFilename, FileName); /* Flawfinder: ignore */
}


/* Creates a datafile using the specified mode
 * uOpenMode --- File Open mode - HX_FILE_READ/HX_FILE_WRITE/HX_FILE_BINARY
 */
STDMETHODIMP
MemoryMapDataFile::Create(UINT16 uOpenMode)
{
    return HXR_NOTIMPL;
}

/*
 *  Open will open a file with the specified mode
 */
STDMETHODIMP
MemoryMapDataFile::Open(UINT16 uOpenMode)
{
    m_ulLastError = HXR_OK;

    if (m_hFile != INVALID_HANDLE_VALUE)
    {
	CloseHandle(m_hFile);
	m_hFile = INVALID_HANDLE_VALUE;
    }
    
    DWORD dwAccess = 0;
    DWORD dwShare = 0;
    DWORD dwCreate = 0;
    DWORD dwFlags = FILE_ATTRIBUTE_NORMAL;
    m_uOpenMode = uOpenMode;
    if (uOpenMode & HX_FILE_WRITE)
    {
        // Set read access so file can be opened read-only simultaneously
        uOpenMode |= HX_FILE_READ;
	dwAccess |= GENERIC_WRITE;
	dwShare |= FILE_SHARE_WRITE;
	if (g_NT)
	{
	    dwShare |= FILE_SHARE_DELETE;
	}
	if (uOpenMode & HX_FILE_NOTRUNC)
	{
	    dwCreate = OPEN_ALWAYS;
	}
	else
	{
	    dwCreate = CREATE_ALWAYS;
	}
    }

    if (uOpenMode & HX_FILE_READ)
    {
	dwAccess |= GENERIC_READ;
	dwShare |= FILE_SHARE_READ;
	if (!(uOpenMode & HX_FILE_WRITE))
	{
	    dwCreate = OPEN_EXISTING;
	}

	IHXPreferences* pPref = NULL;
	if (m_pContext && 
	    (m_pContext->QueryInterface(IID_IHXPreferences, (void**) &pPref) == HXR_OK) &&
	    pPref)
	{
	    IHXBuffer* pBuffer = NULL;
	    if (pPref->ReadPref("FileReadWriteAccess", pBuffer) == HXR_OK &&
		pBuffer &&
		(::atoi((char*) pBuffer->GetBuffer()) == 1))
	    {
		dwShare |= FILE_SHARE_WRITE;

		if (g_NT)
		{
		    dwShare |= FILE_SHARE_DELETE;
		}
	    }

	    HX_RELEASE(pBuffer);
	    HX_RELEASE(pPref);
	}
    }

    /*
     * Nothing to be done for HX_FILE_BINARY
     */
    m_hFile = CreateFile(OS_STRING(m_pFilename), dwAccess, dwShare, NULL,
	    dwCreate, dwFlags, NULL);
    
    if (m_hFile == INVALID_HANDLE_VALUE)
    {
        if (::GetLastError() == ERROR_FILE_NOT_FOUND)
	{
            m_ulLastError = HXR_FAIL;

	    return HXR_FILE_NOT_FOUND;
	}
	/*
	 * Check if we are trying to truncate.  If this is the
	 * case the file is probably mapped and we have to unmap
	 * it.
	 */
	BOOL bReopen = FALSE;
	if ((uOpenMode & (HX_FILE_WRITE|HX_FILE_NOTRUNC)) == HX_FILE_WRITE)
	{
	    _AttemptUnmap();
	    bReopen = TRUE;
	}
	
	/*
	 * Check if we tried to do read only.  Perhaps the file
	 * is mapped read/write.  We'll have to open it like that too.
	 */
	if (!(uOpenMode & HX_FILE_WRITE))
	{
	    dwAccess |= GENERIC_WRITE;
	    dwShare |= FILE_SHARE_WRITE;
	    if (g_NT)
	    {
		dwShare |= FILE_SHARE_DELETE;
	    }
	    dwCreate = OPEN_ALWAYS;
	    bReopen = TRUE;
	}
	if (!bReopen)
	{
            m_ulLastError = HXR_FAIL;
	    return HXR_FAIL;
	}
	m_hFile = CreateFile(OS_STRING(m_pFilename), dwAccess, dwShare, NULL,
		dwCreate, dwFlags, NULL);

	if (m_hFile == INVALID_HANDLE_VALUE)
	{
            m_ulLastError = HXR_FAIL;
	    return HXR_FAIL;
	}
    }
    m_ulPos = 0;

//  On windows we do now want to use memory mapped IO if we are going across
//  a network share. And we only want to disable this for clients, not servers.

    //check to see if we are within the player...

    IHXClientEngine* pEngine = NULL;
    if (HXR_OK == m_pContext->QueryInterface(IID_IHXClientEngine, (void**)&pEngine))
    {
        HX_RELEASE(pEngine);
        CHXDirSpecifier  dirspec(m_pFilename);

        if (!CHXFileSpecUtils::IsDiskLocal( dirspec) || CHXFileSpecUtils::IsDiskEjectable(dirspec) )
        {
            if (MmapHandle)
            {
	        m_zpMMM->CloseMap(MmapHandle);
	        MmapHandle = 0;
            }
            return HXR_OK;
        }
    }
    
    if (MmapHandle)
    {
	m_zpMMM->CloseMap(MmapHandle);
	MmapHandle = 0;
    }
    MmapHandle = m_zpMMM->OpenMap(m_hFile, m_pContext);
    
    return HXR_OK;
}

/*
 * Original Version
 */
    
/*
    if (m_hFile != INVALID_HANDLE_VALUE)
    {
	CloseHandle(m_hFile);
	m_hFile = INVALID_HANDLE_VALUE;
    }
    DWORD dwPlatformSpecific = 0;
    if (g_NT)
    {
	dwPlatformSpecific = FILE_SHARE_DELETE;
    }
    DWORD dwFlags = 0;
    DWORD dwShare = dwPlatformSpecific;
    DWORD dwCreate = 0;
    m_uOpenMode = uOpenMode;
    if (uOpenMode & HX_FILE_WRITE)
    {
	dwShare |= FILE_SHARE_WRITE;
	dwFlags |= GENERIC_WRITE;
	if (uOpenMode & HX_FILE_NOTRUNC)
	{
	    dwCreate |= OPEN_ALWAYS;
	}
	else
	{
	    dwCreate |= CREATE_ALWAYS;
	}
    }
    if (uOpenMode & HX_FILE_READ)
    {
	dwShare |= FILE_SHARE_READ;
	dwFlags |= GENERIC_READ;
 	if (!(uOpenMode & HX_FILE_WRITE))
	{
	    dwCreate |= OPEN_EXISTING;
	}
    }
    if (uOpenMode & HX_FILE_BINARY)
    {
    }

    m_hFile = CreateFile(m_pFilename,
	    GENERIC_READ | GENERIC_WRITE,
	    FILE_SHARE_READ | FILE_SHARE_WRITE | dwPlatformSpecific,
	    NULL,
	    dwCreate,
	    FILE_ATTRIBUTE_NORMAL,
	    NULL);


    if (m_hFile == INVALID_HANDLE_VALUE)
    {
	if (::GetLastError() == ERROR_FILE_NOT_FOUND)
	{
	    return HXR_FAIL;
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?