BaseResource.h
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #pragma once
- namespace NPLayer1 {
- class Communicator;
- class BufferMgr;
- class BaseResource {
- public:
- BaseResource(Communicator*);
- virtual ~BaseResource(void);
- void DeleteResFile() {
- Uninit(false);
- };
- // 在虚拟缓冲区寻找空块或者可以替换的块,返回其在虚拟缓冲区的位置
- UINT GetReplacableBlock(const UINT newBlockID, UINT& replacedBlockID);
- // 针对某个NP的区间列表,获取可以下载的区间列表
- void GetDownloadableArray(const IntervalArray& anotherNP, IntervalArray& result);
- P2P_RETURN_TYPE GetBlock(UINT blockID, UINT& blockSize, LPVOID data);
- P2P_RETURN_TYPE PutBlock(UINT blockID, UINT blockSize, PBYTE data);
- void DelBlock(UINT blockID);
- bool FindBlock(UINT blockID) { return allIntervals.FindBlock(blockID);};
- UINT GetMinBlockID() { return allIntervals.GetMinBlockID();};
- UINT GetMaxBlockID() { return allIntervals.GetMaxBlockID();};
- // 复制全部区间列表
- void GetAllIntervals(BlockInterval* targetArray, UINT8& size) ;
- // 发送给TS/NP的区间列表
- void GetDiffIntervals(BlockInterval* targetArray, UINT8& size, bool forTS, bool getInc);
- // 清空发送给TS/NP的增量区间列表
- void ClearDiffIntervals(bool forTS);
- string GetHashCode() { return hashcode;}; // 取得资源的Hash Code
- string GetResName() { return resname;};
- UINT8 GetSPListSize() { return spIPListSize;};
- NormalAddress* GetSPList() { return spIPList;};
- virtual UINT GetPlayingBlock(bool max=true) = 0;
- virtual int GetBufferPercent() = 0;
- virtual bool EnlargeBuffer() = 0;
- virtual void PrintStatus() = 0;
- virtual BOOL SetPlayingBlock(UINT blockID) = 0;
- virtual void SetDefaultCP() = 0;
- protected:
- virtual void Uninit(bool force) = 0;
- void UninitEx();
- P2P_RETURN_TYPE ParseSPList(string strSPList);
- UINT FindBlockIndex(UINT blockID);
- protected:
- string resname; // 资源名
- string hashcode; // 请求资源的MD5码
- NormalAddress* spIPList; // SuperPeer列表的IP地址
- UINT8 spIPListSize; // SuperPeer列表的大小
- UINT* blockIDArray; // 虚拟缓冲区中对应的BlockID
- UINT* blockMapArray; // 虚拟缓冲区与真正缓冲区(BufferMgr)的对应表
- UINT blockMapSize; // 虚拟缓冲区中块的个数
- IntervalArray allIntervals; // 所有已下载块的区间列表
- IntervalArray csIncIntervals; // 发送给TS的增量区间列表(增加的)
- IntervalArray csDecIntervals; // 发送给TS的增量区间列表(减少的)
- IntervalArray p2pIncIntervals; // 发送给NP的增量区间列表(增加的)
- IntervalArray p2pDecIntervals; // 发送给NP的增量区间列表(减少的)
- UINT blocks4Play; // 有一定的块,开始或者继续播放
- BOOL bInited; // 是否已经初始化
- CriticalSection dataLocker; // 数据访问的互斥变量
- BufferMgr* bufferMgr;
- Communicator* comm;
- protected:
- enum {
- // 默认使用最大 DEFAULT_SPACE Bytes 的磁盘空间
- DEFAULT_SPACE = 1048576*20,
- // 每次开始播放前,需要缓冲TIME_FOR_PLAY秒的数据
- TIME_FOR_PLAY = 45,
- // 选择开始播放的位置,在SPUpdate.maxBlockID之前TIME_BEGIN_PLAY秒的块
- TIME_BEGIN_PLAY = 45,
- };
- };
- }