filecache.h
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:11k
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: RCSL 1.0/RPSL 1.0
- *
- * Portions Copyright (c) 1995-2002 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
- * Version 1.0 (the "RPSL") available at
- * http://www.helixcommunity.org/content/rpsl unless you have licensed
- * the file under the RealNetworks Community Source License Version 1.0
- * (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.
- *
- * 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 "hxslist.h" // CHXSimpleList
- #include "hxthread.h" // HXMutex
- // In case we want to read data from the cache file and add to the
- // list, this ratio determines the size of the blocks to create.
- #define CAPACITY_BLOCKSIZE_RATIO 8
- class CHXFileCacheObject : public IHXCacheObject
- {
- public:
- /****************************************************************************
- * CHXFileCacheObject::CHXFileCacheObject
- *
- * Constructor
- */
- CHXFileCacheObject(IHXCommonClassFactory* /*IN*/ pClassFactory,
- UINT32 /*IN*/ ulFileLength,
- char* /*IN*/ pFileName);
- /****************************************************************************
- * CHXFileCacheObject::~CHXFileCacheObject
- *
- * Destructor
- */
- ~CHXFileCacheObject();
- // IUnknown COM Interface Methods
- /****************************************************************************
- * IUnknown::AddRef
- *
- * This routine increases the object reference count in a thread safe
- * manner. The reference count is used to manage the lifetime of an object.
- * This method must be explicitly called by the user whenever a new
- * reference to an object is used.
- */
-
- STDMETHOD_(ULONG32,AddRef) (THIS);
-
- /****************************************************************************
- * IUnknown::Release
- *
- * This routine decreases the object reference count in a thread safe
- * manner, and deletes the object if no more references to it exist. It must
- * be called explicitly by the user whenever an object is no longer needed.
- */
- STDMETHOD_(ULONG32,Release) (THIS);
- /****************************************************************************
- * IUnknown::QueryInterface
- *
- * This routine indicates which interfaces this object supports. If a given
- * interface is supported, the object's reference count is incremented, and
- * a reference to that interface is returned. Otherwise a NULL object and
- * error code are returned. This method is called by other objects to
- * discover the functionality of this object.
- */
-
- STDMETHOD(QueryInterface) (THIS_
- REFIID riid,
- void** ppvObj);
-
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::Init
- *
- * Purpose:
- *
- * Associates a cache object with the response object
- * it should notify of operation completeness.
- */
-
- STDMETHOD(Init) (THIS_
- IHXCacheObjectResponse* /*IN*/ pCacheObjectResponse,
- UINT32 /*IN*/ ulCapacity,
- UINT32 /*IN*/ lThreshold);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::GetThreshold
- *
- * Purpose:
- *
- * Obtain the threshold of the cache object.
- */
- STDMETHOD_(UINT32, GetThreshold) (THIS);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::ChangeThreshold
- *
- * Purpose:
- *
- * The object keeps caching data until it is full (exhausts its
- * capacity). Once it is full, it will overwite existing cached data
- * with new data ONLY if the percentage of cached data which has been
- * read from the cache using the ReadBlock() method is greater than a
- * given percentage of Capacity.. This percentage is set using the SetThreshold()
- * method. In case the threshold is exceeded, the oldest added data
- * (the data with the the least offset) will be discarded and the
- * amount of data discarded is so that the remaining cached data just
- * satisfies the threshold condidtion (approximately).
- *
- * This cache object is used in the HTTP/1.0 file system plugin for
- * mobile devices and in this case, the threshold is set to 70%
- * i.e., utilizedDataPercentage = 0.7
- *
- */
- STDMETHOD(ChangeThreshold) (THIS_
- UINT32 /*IN*/ lNewThreshold);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::GetCapacity
- *
- * Purpose:
- *
- * Obtain the capacity in bytes of the cache object.
- */
- STDMETHOD_(UINT32, GetCapacity) (THIS);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::ChangeCapacity
- *
- * Purpose:
- *
- * Changes the capacity of the cache object (in bytes). It is used to
- * increase or decrease the capacity after the cache object has been
- * created and it's capacity set using SetCapacity(). This method can
- * called any number of times. If new capacity is less than old capacity,
- * the oldest data are discarded.
- *
- */
- STDMETHOD(ChangeCapacity) (THIS_
- UINT32 /*IN*/ newByteCount);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::GetUnusedCapacity
- *
- * Purpose:
- *
- * Obtain the unused capacity in bytes of the cache object.
- */
- STDMETHOD_(UINT32, GetUnusedCapacity) (THIS);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::AddBlock
- *
- * Purpose:
- *
- * Adds a block of data to the cache.
- */
- STDMETHOD(AddBlock) (THIS_
- IHXBuffer* /*IN*/ pBlock);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::VerifyBlock
- *
- * Purpose:
- *
- * Verify that a block of data is in the cache.
- */
- STDMETHOD(VerifyBlock) (THIS_
- UINT32 /*IN*/ ulBlockOffset,
- UINT32 /*IN*/ ulBlockLength);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::ReadBlock
- *
- * Purpose:
- *
- * Read a block out of the cache.
- */
- STDMETHOD(ReadBlock) (THIS_
- UINT32 /*IN*/ ulBlockOffset,
- UINT32 /*IN*/ ulBlockLength);
- /************************************************************************
- * Method:
- * IHXCacheObject::Flush
- *
- * Purpose:
- *
- * Flushes all data to the cache file AND releases all data buffers
- * in the memory.After flushing, the object can be used for reading/writing
- * as before.
- */
- STDMETHOD(Flush) (THIS);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::IsFull
- *
- * Purpose:
- *
- * Can the cache object accept any more data for storage?
- */
-
- STDMETHOD_(BOOL, IsFull) (THIS);
- /************************************************************************
- * Method:
- *
- * IHXCacheObject::IsEmpty
- *
- * Purpose:
- *
- * Does the cache object have any data stored?
- */
-
- STDMETHOD_(BOOL, IsEmpty) (THIS);
- private:
- INT32 m_RefCount;
- IHXCacheObjectResponse* m_pCacheObjectResponse;
- IHXCommonClassFactory* m_pClassFactory;
- UINT32 m_ulCapacity;
- UINT32 m_lThreshold;
- UINT32 m_ulUsedCapacity;
- CHXSimpleList* m_pList;
- UINT32 m_ulCurrentWriteOffset;
- IHXBuffer* m_pPendingAddBlock;
- BOOL m_bInAddBlockDone;
- BOOL m_bInReadBlockDone;
- HXMutex* m_pMutex;
- UINT32 m_ulCurrentReadOffset; // Offset of the first byte that has NOT been read.
- UINT32 m_ulFileLength;
- char* m_pFileName;
- UINT32 m_ulFileWriteOffset;
- FILE* m_pCacheFileHandle;
- UINT32 m_ulHighestByteNotRead;
- FILE* m_pFileHandle;
- struct PendingReadInfo
- {
- UINT32 ulOffset;
- UINT32 ulLength;
- IHXBuffer* pBlock;
-
- } m_pPendingReadInfo;
- struct Info
- {
- UINT32 ulOffset; // Offset of the starting byte of the block
- UINT32 ulSize; // Size of the block
- IHXBuffer* pBlock; // The actual block data
- };
-
- // Discards 'byteCount' amount of oldest data. Note that this
- // method is not thread safe. The caller has to take care of
- // locking common data structures before calling this method.
- HX_RESULT _DiscardDataFromHead(UINT32 byteCount, BOOL bWriteToFile);
-
- HX_RESULT _DiscardDataFromTail(UINT32 byteCount, BOOL bWriteToFile);
- // Check if utilizedData has exceeded THRESHOLD. If yes, discard
- // appropriate amount of data. Note that this
- // method is not thread safe. The caller has to take care of
- // locking common data structures before calling this method.
- HX_RESULT _CheckForThresholdCondition();
- HX_RESULT _CopyFromFileToHead(UINT32 ulOffset, UINT32 ulSize);
- HX_RESULT _CopyFromFileToTail(UINT32 ulOffset, UINT32 ulSize);
- HX_RESULT _CopyAllDataToFile();
-
-
- }; // CHXFileCacheObject