CUploadFile类和CUploadFiles类

本文详细介绍了Shareaza软件中的上传机制,包括CUploadFile类如何管理上传任务和文件碎片,以及CUploadFiles类如何组织和操作这些上传文件。通过具体的类实现与方法,展示了文件上传过程中的关键操作。
摘要由CSDN通过智能技术生成

//
// UploadFile.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza 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.
//
// Shareaza 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 Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#if !defined(AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_)
#define AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_

#pragma once

#include "FileFragments.hpp"

class CUploadTransfer;

/*

每个CUploadFile中有好多个UploadTranfer对象指针,

active的那个是正在上传的任务。

*/


class CUploadFile
{

// Construction

public:

 CUploadFile(CUploadTransfer* pUpload, SHA1* pSHA1,
  LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize);

 virtual ~CUploadFile();

// Attributes

public:

 IN_ADDR   m_pAddress;

 BOOL   m_bSHA1;

 SHA1   m_pSHA1;
 
 CString   m_sName;
 
 CString   m_sPath;
 
 QWORD   m_nSize;

public:

 DWORD   m_nRequests;

 FF::SimpleFragmentList m_oFragments;

public:

 BOOL   m_bSelected;

protected: // 里面都是CUploadTranfser的指针

 CPtrList  m_pTransfers;

// Operations

public:

 void    Add(CUploadTransfer* pUpload);

 BOOL    Remove(CUploadTransfer* pUpload);

 CUploadTransfer* GetActive() const;

public:

 void    AddFragment(QWORD nOffset, QWORD nLength);

 void    Remove();

// Inlines

public:

 //UploadTransfer链表为空了则返回true

 inline BOOL IsEmpty() const
 {
  return m_pTransfers.GetCount() == 0;
 }

};

#endif // !defined(AFX_UPLOADFILE_H__D2D194F0_8528_482F_A4DA_DE1D9C496335__INCLUDED_)

//
// UploadFile.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza 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.
//
// Shareaza 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 Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "UploadFile.h"
#include "UploadTransfer.h"
#include "FragmentedFile.h"
#include "Statistics.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//
// CUploadFile construction

//根据参数设置各个属性的值,创建当前CUploadFile对象

CUploadFile::CUploadFile(CUploadTransfer* pUpload, SHA1* pSHA1,
       LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize)
: m_oFragments( nSize )
{
 m_pAddress = pUpload->m_pHost.sin_addr;

 m_sName  = pszName;

 m_sPath  = pszPath;

 m_nSize  = nSize;

 if ( m_bSHA1 = ( pSHA1 != NULL ) )
 {
  m_pSHA1 = *pSHA1;
 }

 m_nRequests  = 0;

 m_bSelected  = FALSE;

 m_pTransfers.AddTail( pUpload );
}

CUploadFile::~CUploadFile()
{
}

//
// CUploadFile transfer operations

//将参数pUpload加入到m_pTransfers链表的末尾

void CUploadFile::Add(CUploadTransfer* pUpload)
{
 if ( m_pTransfers.Find( pUpload ) == NULL )
 {
  m_pTransfers.AddTail( pUpload );
 }
}

//在m_pTranfers链表中删除参数指定的uploadTransfer对象

//如果没有找到,返回false,如果找到了删除之,如果为空了返回true

BOOL CUploadFile::Remove(CUploadTransfer* pUpload)
{
 POSITION pos = m_pTransfers.Find( pUpload );

 if ( pos == NULL )
 {
  return FALSE;
 }

 m_pTransfers.RemoveAt( pos );

 return IsEmpty();
}

//找到当前UploadTranfer中的活跃Tranfser对象

CUploadTransfer* CUploadFile::GetActive() const
{
 if ( IsEmpty() )
 {
  return NULL;
 }

 for ( POSITION pos = m_pTransfers.GetHeadPosition() ; pos ; )
 {
  CUploadTransfer* pUpload =
   (CUploadTransfer*)m_pTransfers.GetNext( pos );

  //所谓活跃,值得就是CUploadTransfer的m_nState状态不是upsNull

  if ( pUpload->m_nState != upsNull )
  {
   return pUpload;
  }
 }

 return (CUploadTransfer*)m_pTransfers.GetTail();
}

//调用transfer链表中每个transfer的Remove方法完成删除工作

void CUploadFile::Remove()
{
 for ( POSITION pos = m_pTransfers.GetHeadPosition() ; pos ; )
 {
  CUploadTransfer* pUpload =
   (CUploadTransfer*)m_pTransfers.GetNext( pos );

  pUpload->Remove();
 }
}

//
// CUploadFile fragments

//如果 m_oFragments为空了,说明上传一个文件结束了,完成文件数目增加1

//然后在 m_oFragments中增加一个片段,偏移量为nOffset,长度为nLength

void CUploadFile::AddFragment(QWORD nOffset, QWORD nLength)
{
 if ( m_oFragments.empty() )
 {
  Statistics.Current.Uploads.Files++;
 }

    m_oFragments.insert( FF::SimpleFragment( nOffset, nOffset + nLength ) );
}

//
// UploadFiles.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza 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.
//
// Shareaza 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 Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#if !defined(AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_)
#define AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_

#pragma once

class CUploadFile;

class CUploadTransfer;


/*

 CUploadFiles 管理一个CUploadFile对象构成的数组,

可以将参数CUploadTransfer所在的CUploadFile对象搬到开头,也可以搬到末尾,

还可以寻找和删除CUploadTransfer对应的CUploadFile对象。

*/


class CUploadFiles 
{

// Construction

public:

 CUploadFiles();

 virtual ~CUploadFiles();
 
// Attributes

protected:

 CPtrList m_pList;

// Operations

public:

 void   Clear();

 CUploadFile* GetFile(CUploadTransfer* pUpload, SHA1* pSHA1,
  LPCTSTR pszName, LPCTSTR pszPath, QWORD nSize);

 void   Remove(CUploadTransfer* pTransfer);

 void   MoveToHead(CUploadTransfer* pTransfer);

 void   MoveToTail(CUploadTransfer* pTransfer);
 
// List Access

public:

 //返回m_pList的头结点位置

 inline POSITION GetIterator() const
 {
  return m_pList.GetHeadPosition();
 }

 //返回参数pos位置的CUploadFile对象指针
 
 inline CUploadFile* GetNext(POSITION& pos) const
 {
  return (CUploadFile*)m_pList.GetNext( pos );
 }

 //返回m_pList中的CUploadFile对象个数
 
 inline int GetCount() const
 {
  return m_pList.GetCount();
 }

 //如果参数pFile在当前m_pList中存在,则返回true,否则返回false

 inline BOOL Check(CUploadFile* pFile) const
 {
  return m_pList.Find( pFile ) != NULL;
 }

};

extern CUploadFiles UploadFiles;

#endif // !defined(AFX_UPLOADFILES_H__0F70ABF7_91B7_436B_A235_C3D01DB0D7F6__INCLUDED_)

//
// UploadFiles.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (
www.shareaza.com)
//
// Shareaza 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.
//
// Shareaza 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 Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "UploadFiles.h"
#include "UploadFile.h"
#include "UploadTransfer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CUploadFiles UploadFiles;


//
// CUploadFiles construction

CUploadFiles::CUploadFiles()
{
}

CUploadFiles::~CUploadFiles()
{
 Clear();
}

//
// CUploadFiles clear

//调用m_List中的每个CUploadFile对象的Remove方法完成清除操作

void CUploadFiles::Clear()
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  GetNext( pos )->Remove();
 }
 
 ASSERT( GetCount() == 0 );
}

//
// CUploadFiles file allocation

/*

遍历m_pList链表,如果有一项CUploadFile的地址和参数pUpload的地址相同

而且pFile->m_sPath和参数pszPath也相同,则在UploadFile中加入uploadTransfer

对象pUpload.如果没有找到,则根据参数创建一个新的CUploadFile对象加入到

m_plist的最后

*/

CUploadFile* CUploadFiles::GetFile(CUploadTransfer* pUpload,
           SHA1* pSHA1, LPCTSTR pszName,
           LPCTSTR pszPath, QWORD nSize)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  CUploadFile* pFile = GetNext( pos );

  if ( pFile->m_pAddress.S_un.S_addr
   == pUpload->m_pHost.sin_addr.S_un.S_addr )
  {
   if ( pFile->m_sPath == pszPath )
   {
    pFile->Add( pUpload );

    return pFile;
   }
  }
 }
 
 CUploadFile* pFile = new CUploadFile( pUpload, pSHA1,
  pszName, pszPath, nSize );

 m_pList.AddTail( pFile );
 
 return pFile;
}

//
// CUploadFiles remove an upload trasnfer

/*

遍历m_pList链表,在pUploadFile中删除参数指定的pTransfer

对象,如果pUploadFile为空了,除了参数Transfer外没有别的Transfer了,

则在m_pList中删除pUploadTransfer对象

*/

void CUploadFiles::Remove(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posRemove = pos;

  CUploadFile* pFile = GetNext( pos );
  
  if ( pFile->Remove( pTransfer ) )
  {
   delete pFile;

   m_pList.RemoveAt( posRemove );
  }
 }
}

//
// CUploadFiles move a file with this transfer to the head/tail. (Cheap BT sorting)

/*

遍历m_pList链表,如果

pUploadFile的active是参数Tranfer,则将其移动到m_pList的最前面

*/

void CUploadFiles::MoveToHead(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posThis = pos;

  CUploadFile* pFile = GetNext( pos );

  if ( pFile->GetActive() == pTransfer )
  {
   m_pList.RemoveAt( posThis );

   m_pList.AddHead( pFile );

   return;
  }
 }
}

/*

遍历m_pList链表,如果

pUploadFile的active是参数Tranfer,则将其移动到m_pList的最后面

*/


void CUploadFiles::MoveToTail(CUploadTransfer* pTransfer)
{
 for ( POSITION pos = GetIterator() ; pos ; )
 {
  POSITION posThis = pos;

  CUploadFile* pFile = GetNext( pos );

  if ( pFile->GetActive() == pTransfer )
  {
   m_pList.RemoveAt( posThis );

   m_pList.AddTail( pFile );

   return;
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值