Upload.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:5k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // Upload.cpp: implementation of the CUpload class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "testbt.h"
  6. #include "Upload.h"
  7. #include "DownloaderFeedback.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CUpload::CUpload(CDownloaderFeedback* pDownloaderFeedback,
  17. CConnection* pconnection,
  18. CChoker* pchoker,
  19. CStorageWrapper* pstorage,
  20. long lMaxSliceLength,
  21. long lMaxRatePeriod,
  22. long lFudge):m_Measure(lMaxRatePeriod, lFudge)
  23. {
  24. m_pconnection = pconnection;
  25. m_pstorage = pstorage;
  26. m_lMaxSliceLength = lMaxSliceLength;
  27. m_lMaxRatePeriod = lMaxRatePeriod;
  28. m_bChoked = true;
  29. m_bInterested = false;
  30. if (m_pstorage->do_I_have_anything())
  31. {
  32. m_pconnection->send_bitfield(m_pstorage->get_have_list());
  33. }
  34. // test. send all have bitfield to get the peer send request.
  35. /**
  36. vector<bool> vHave;
  37. for (int i=0; i<47; i++)
  38. vHave.push_back(true);
  39. m_pconnection->send_bitfield(vHave);
  40. //*/
  41. // for feedback
  42. m_pDownloaderFeedback = pDownloaderFeedback;
  43. m_pconnection->GetIP(m_lAddr, m_sPort);
  44. }
  45. CUpload::~CUpload()
  46. {
  47. }
  48. void CUpload::got_not_interested()
  49. {
  50. if (m_bInterested)
  51. {
  52. m_bInterested = false;
  53. m_vBuffer.erase(m_vBuffer.begin(), m_vBuffer.end());
  54. }
  55. // ShowMessage(m_pconnection->m_pEConnection, "got_not_interested()");
  56. m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, "got_not_interested()");
  57. }
  58. void CUpload::got_interested()
  59. {
  60. if (!m_bInterested)
  61. {
  62. m_bInterested = true;
  63. }
  64. //** test.
  65. unchoke();
  66. // ShowMessage(m_pconnection->m_pEConnection, "got_interested()");
  67. m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, "got_interested()");
  68. }
  69. void CUpload::got_request(long index, long begin, long length)
  70. {
  71. if (!m_bInterested || length > m_lMaxSliceLength)
  72. {
  73. // assert(false);
  74. m_pconnection->Close();
  75. return;
  76. }
  77. if (!m_bChoked)
  78. {
  79. char szText[100] = {0};
  80. sprintf(szText, "got_request() : index = %d, begin = %d, length = %d request.size = %d", index, begin, length, m_vBuffer.size());
  81. // ShowMessage(m_pconnection->m_pEConnection, szText);
  82. m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
  83. m_vBuffer.push_back(CRequestsItem(index, begin, length));
  84. if (!flushed())
  85. return;
  86. }
  87. }
  88. bool CUpload::flushed()
  89. {
  90. while(m_vBuffer.size() > 0 && m_pconnection->IsFlush())
  91. {
  92. // string strIp = m_pconnection->GetIP();
  93. long index = m_vBuffer[0].m_index;
  94. long begin = m_vBuffer[0].m_begin;
  95. long length = m_vBuffer[0].m_length;
  96. m_vBuffer.erase(m_vBuffer.begin());
  97. if (length <= 0)
  98. {
  99. assert(false);
  100. continue;
  101. }
  102. if (!m_pstorage->do_I_have(index))
  103. continue;
  104. char* pbuf = new char[length];
  105. memstream membuf;
  106. membuf.write(pbuf, length);
  107. delete pbuf;
  108. if (!m_pstorage->get_piece(index, membuf, begin, length))
  109. {
  110. // assert(false);
  111. m_pconnection->Close();
  112. return false;
  113. }
  114. m_Measure.update_rate(length);
  115. m_pconnection->send_piece(index, begin, membuf);
  116. #ifdef _DEBUG
  117. char szText[100] = {0};
  118. sprintf(szText, "send_piece() : index = %d, begin = %d, length = %d", index, begin, membuf.size());
  119. // ShowMessage(m_pconnection->m_pEConnection, szText);
  120. m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
  121. #endif
  122. }
  123. return true;
  124. }
  125. void CUpload::got_cancel(long index, long begin, long length)
  126. {
  127. // assert(false);
  128. bool bfind = false;
  129. for (int i=0; i<m_vBuffer.size(); i++)
  130. {
  131. if (m_vBuffer[i].m_begin == begin && m_vBuffer[i].m_index == index && m_vBuffer[i].m_length == length)
  132. {
  133. m_vBuffer.erase(m_vBuffer.begin() + i);
  134. bfind = true;
  135. break;
  136. }
  137. }
  138. char szText[100] = {0};
  139. sprintf(szText, "got_cancel() : index = %d, begin = %d, length = %d", index, begin, length);
  140. // ShowMessage(m_pconnection->m_pEConnection, szText);
  141. m_pDownloaderFeedback->ShowSocketMessage(m_lAddr, m_sPort, szText);
  142. // assert(bfind);
  143. }
  144. void CUpload::choke()
  145. {
  146. if (!m_bChoked)
  147. {
  148. m_bChoked = true;
  149. m_vBuffer.erase(m_vBuffer.begin(), m_vBuffer.end());
  150. m_pconnection->send_choke();
  151. }
  152. }
  153. void CUpload::unchoke()
  154. {
  155. if (m_bChoked)
  156. {
  157. m_bChoked = false;
  158. m_pconnection->send_unchoke();
  159. }
  160. }
  161. bool CUpload::is_choked()
  162. {
  163. return m_bChoked;
  164. }
  165. bool CUpload::is_interested()
  166. {
  167. return m_bInterested;
  168. }
  169. bool CUpload::has_queries()
  170. {
  171. return m_vBuffer.size() > 0;
  172. }
  173. long CUpload::get_rate()
  174. {
  175. return m_Measure.get_rate();
  176. }
  177. long CUpload::get_total()
  178. {
  179. return m_Measure.get_total();
  180. }
  181. CUploader::CUploader(CDownloaderFeedback* pDownloaderFeedback, CStorageWrapper* pstorage, CChoker* pchoker,long lMaxSliceLength, long lMaxRatePeriod, long lFudge)
  182. {
  183. m_pChoker = pchoker;
  184. m_pstorage = pstorage;
  185. m_lMaxSliceLength = lMaxSliceLength;
  186. m_lMaxRatePeriod = lMaxRatePeriod;
  187. m_lFudge = lFudge;
  188. m_pDownloaderFeedback = pDownloaderFeedback;
  189. }
  190. CUpload* CUploader::make_upload(CConnection* pconnection)
  191. {
  192. return new CUpload(m_pDownloaderFeedback, pconnection, m_pChoker, m_pstorage, m_lMaxSliceLength, m_lMaxRatePeriod, m_lFudge);
  193. }