Upload.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:5k
源码类别:
P2P编程
开发平台:
Visual C++
- // Upload.cpp: implementation of the CUpload class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "Upload.h"
- #include "DownloaderFeedback.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CUpload::CUpload(CDownloaderFeedback* pDownloaderFeedback,
- CConnection* pconnection,
- CChoker* pchoker,
- CStorageWrapper* pstorage,
- long lMaxSliceLength,
- long lMaxRatePeriod,
- long lFudge):m_Measure(lMaxRatePeriod, lFudge)
- {
- m_pconnection = pconnection;
- m_pstorage = pstorage;
- m_lMaxSliceLength = lMaxSliceLength;
- m_lMaxRatePeriod = lMaxRatePeriod;
- m_bChoked = true;
- m_bInterested = false;
- if (m_pstorage->do_I_have_anything())
- {
- m_pconnection->send_bitfield(m_pstorage->get_have_list());
- }
- // test. send all have bitfield to get the peer send request.
- /**
- vector<bool> vHave;
- for (int i=0; i<47; i++)
- vHave.push_back(true);
- m_pconnection->send_bitfield(vHave);
- //*/
- // for feedback
- m_pDownloaderFeedback = pDownloaderFeedback;
- m_pconnection->GetIP(m_lAddr, m_sPort);
- }
- CUpload::~CUpload()
- {
- }
- void CUpload::got_not_interested()
- {
- if (m_bInterested)
- {
- m_bInterested = false;
- m_vBuffer.erase(m_vBuffer.begin(), m_vBuffer.end());
- }
- // ShowMessage(m_pconnection->m_pEConnection, "got_not_interested()");
- m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, "got_not_interested()");
- }
- void CUpload::got_interested()
- {
- if (!m_bInterested)
- {
- m_bInterested = true;
- }
- //** test.
- unchoke();
- // ShowMessage(m_pconnection->m_pEConnection, "got_interested()");
- m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, "got_interested()");
- }
- void CUpload::got_request(long index, long begin, long length)
- {
- if (!m_bInterested || length > m_lMaxSliceLength)
- {
- // assert(false);
- m_pconnection->Close();
- return;
- }
- if (!m_bChoked)
- {
- char szText[100] = {0};
- sprintf(szText, "got_request() : index = %d, begin = %d, length = %d request.size = %d", index, begin, length, m_vBuffer.size());
- // ShowMessage(m_pconnection->m_pEConnection, szText);
- m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
- m_vBuffer.push_back(CRequestsItem(index, begin, length));
- if (!flushed())
- return;
- }
- }
- bool CUpload::flushed()
- {
- while(m_vBuffer.size() > 0 && m_pconnection->IsFlush())
- {
- // string strIp = m_pconnection->GetIP();
- long index = m_vBuffer[0].m_index;
- long begin = m_vBuffer[0].m_begin;
- long length = m_vBuffer[0].m_length;
- m_vBuffer.erase(m_vBuffer.begin());
- if (length <= 0)
- {
- assert(false);
- continue;
- }
- if (!m_pstorage->do_I_have(index))
- continue;
- char* pbuf = new char[length];
- memstream membuf;
- membuf.write(pbuf, length);
- delete pbuf;
- if (!m_pstorage->get_piece(index, membuf, begin, length))
- {
- // assert(false);
- m_pconnection->Close();
- return false;
- }
- m_Measure.update_rate(length);
- m_pconnection->send_piece(index, begin, membuf);
- #ifdef _DEBUG
- char szText[100] = {0};
- sprintf(szText, "send_piece() : index = %d, begin = %d, length = %d", index, begin, membuf.size());
- // ShowMessage(m_pconnection->m_pEConnection, szText);
- m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
- #endif
- }
- return true;
- }
- void CUpload::got_cancel(long index, long begin, long length)
- {
- // assert(false);
- bool bfind = false;
- for (int i=0; i<m_vBuffer.size(); i++)
- {
- if (m_vBuffer[i].m_begin == begin && m_vBuffer[i].m_index == index && m_vBuffer[i].m_length == length)
- {
- m_vBuffer.erase(m_vBuffer.begin() + i);
- bfind = true;
- break;
- }
- }
- char szText[100] = {0};
- sprintf(szText, "got_cancel() : index = %d, begin = %d, length = %d", index, begin, length);
- // ShowMessage(m_pconnection->m_pEConnection, szText);
- m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
- // assert(bfind);
- }
- void CUpload::choke()
- {
- if (!m_bChoked)
- {
- m_bChoked = true;
- m_vBuffer.erase(m_vBuffer.begin(), m_vBuffer.end());
- m_pconnection->send_choke();
- }
- }
- void CUpload::unchoke()
- {
- if (m_bChoked)
- {
- m_bChoked = false;
- m_pconnection->send_unchoke();
- }
- }
- bool CUpload::is_choked()
- {
- return m_bChoked;
- }
- bool CUpload::is_interested()
- {
- return m_bInterested;
- }
- bool CUpload::has_queries()
- {
- return m_vBuffer.size() > 0;
- }
- long CUpload::get_rate()
- {
- return m_Measure.get_rate();
- }
- long CUpload::get_total()
- {
- return m_Measure.get_total();
- }
- CUploader::CUploader(CDownloaderFeedback* pDownloaderFeedback, CStorageWrapper* pstorage, CChoker* pchoker,long lMaxSliceLength, long lMaxRatePeriod, long lFudge)
- {
- m_pChoker = pchoker;
- m_pstorage = pstorage;
- m_lMaxSliceLength = lMaxSliceLength;
- m_lMaxRatePeriod = lMaxRatePeriod;
- m_lFudge = lFudge;
- m_pDownloaderFeedback = pDownloaderFeedback;
- }
- CUpload* CUploader::make_upload(CConnection* pconnection)
- {
- return new CUpload(m_pDownloaderFeedback, pconnection, m_pChoker, m_pstorage, m_lMaxSliceLength, m_lMaxRatePeriod, m_lFudge);
- }