00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "vmsBtDownloadImpl.h"
00008 #include "vmsBtSessionImpl.h"
00009 #include "vmsBtDownloadPeerInfoListImpl.h"
00010 #include "vmsBtDownloadPeerInfoImpl.h"
00011
00012 vmsBtDownloadImpl::vmsBtDownloadImpl(void)
00013 {
00014 m_iDownloadSpeedLimit = -1;
00015 m_pTorrent = NULL;
00016 m_peerList.m_dld = this;
00017 }
00018
00019 vmsBtDownloadImpl::~vmsBtDownloadImpl(void)
00020 {
00021 if (m_pTorrent)
00022 m_pTorrent->Release ();
00023 }
00024
00025 vmsBtDownloadState vmsBtDownloadImpl::GetState ()
00026 {
00027 if (m_handle.is_valid () == false)
00028 return BTDS_QUEUED;
00029
00030
00031
00032 switch (m_handle.status ().state)
00033 {
00034 case torrent_status::queued_for_checking:
00035 return BTDS_QUEUED;
00036
00037 case torrent_status::checking_files:
00038 return BTDS_CHECKING_FILES;
00039
00040 case torrent_status::connecting_to_tracker:
00041 return BTDS_CONNECTING_TRACKER;
00042
00043 case torrent_status::downloading:
00044 return BTDS_DOWNLOADING;
00045
00046 case torrent_status::finished:
00047 return BTDS_FINISHED;
00048
00049 case torrent_status::seeding:
00050 return BTDS_SEEDING;
00051
00052 case torrent_status::allocating:
00053 return BTDS_ALLOCATING;
00054 }
00055
00056 assert (FALSE);
00057 throw 0;
00058 }
00059
00060 BOOL vmsBtDownloadImpl::IsPaused ()
00061 {
00062 if (m_handle.is_valid () == false)
00063 return FALSE;
00064
00065 return m_handle.status ().paused;
00066 }
00067
00068 void vmsBtDownloadImpl::get_SavePath (LPSTR pszRes)
00069 {
00070 if (m_handle.is_valid () == false) {
00071 *pszRes = 0;
00072 return;
00073 }
00074
00075 strcpy (pszRes, m_handle.save_path ().string ().c_str ());
00076 }
00077
00078 float vmsBtDownloadImpl::get_PercentDone ()
00079 {
00080 if (m_handle.is_valid () == false)
00081 return 0;
00082
00083 return (float)
00084 ((double)get_TotalDownloadedBytesCount () / m_pTorrent->get_TotalFilesSize () * 100.0);
00085
00086 }
00087
00088 int vmsBtDownloadImpl::get_PiecesProgressMap (bool* pbPieces, int *pnCompletedPieces)
00089 {
00090 if (m_handle.is_valid () == false) {
00091 if (pnCompletedPieces != NULL)
00092 *pnCompletedPieces = 0;
00093 return 0;
00094 }
00095
00096
00097
00098 if (pnCompletedPieces != NULL)
00099 *pnCompletedPieces = m_handle.status ().num_pieces;
00100
00101 const std::vector<bool>* pieces = m_handle.status ().pieces;
00102
00103 if (pieces && pbPieces)
00104 {
00105 for (size_t i = 0; i < pieces->size (); i++)
00106 pbPieces [i] = pieces->at (i);
00107 }
00108
00109 return pieces ? pieces->size () : 0;
00110 }
00111
00112 int vmsBtDownloadImpl::get_DownloadConnectionCount ()
00113 {
00114 if (m_handle.is_valid () == false)
00115 return 0;
00116
00117
00118
00119 std::vector <peer_info> v;
00120 m_handle.get_peer_info (v);
00121
00122 int n = 0;
00123
00124 for (size_t i = 0; i < v.size (); i++)
00125 {
00126 if ((v [i].flags & peer_info::interesting) != 0 &&
00127 (v [i].flags & peer_info::queued) == 0)
00128 n++;
00129 }
00130
00131 return n;
00132 }
00133
00134 UINT64 vmsBtDownloadImpl::get_TotalDownloadedBytesCount ()
00135 {
00136 if (m_handle.is_valid () == false)
00137 return 0;
00138
00139 return m_handle.status ().total_done;
00140 }
00141
00142 UINT vmsBtDownloadImpl::GetDownloadSpeed ()
00143 {
00144 if (m_handle.is_valid () == false)
00145 return 0;
00146
00147 return (UINT)m_handle.status ().download_payload_rate;
00148 }
00149
00150 UINT vmsBtDownloadImpl::GetUploadSpeed ()
00151 {
00152 if (m_handle.is_valid () == false)
00153 return 0;
00154
00155 return (UINT)m_handle.status ().upload_payload_rate;
00156 }
00157
00158 void vmsBtDownloadImpl::Pause ()
00159 {
00160 if (m_handle.is_valid () == false)
00161 return;
00162
00163 m_handle.pause ();
00164 }
00165
00166 void vmsBtDownloadImpl::Resume ()
00167 {
00168 if (m_handle.is_valid () == false)
00169 return;
00170
00171 m_handle.resume ();
00172 }
00173
00174 void vmsBtDownloadImpl::SetDownloadLimit (int limit)
00175 {
00176 if (m_handle.is_valid () == false)
00177 return;
00178
00179 m_handle.set_download_limit (limit);
00180 }
00181
00182 int vmsBtDownloadImpl::GetDownloadLimit ()
00183 {
00184 return m_iDownloadSpeedLimit;
00185 }
00186
00187 vmsBtFile* vmsBtDownloadImpl::get_Torrent ()
00188 {
00189 return m_pTorrent;
00190 }
00191
00192 int vmsBtDownloadImpl::get_ConnectionCount ()
00193 {
00194 if (m_handle.is_valid () == false)
00195 return 0;
00196
00197 return m_handle.status ().num_peers;
00198 }
00199
00200 void vmsBtDownloadImpl::set_ConnectionLimit (int limit)
00201 {
00202 if (m_handle.is_valid () == false)
00203 return;
00204
00205 m_handle.set_max_connections (limit);
00206 }
00207
00208 BOOL vmsBtDownloadImpl::get_FastResumeData (LPBYTE pbRes, DWORD dwSize, DWORD *pdwDataSize)
00209 {
00210 if (m_handle.is_valid () == false)
00211 return FALSE;
00212
00213
00214
00215 entry e = m_handle.write_resume_data ();
00216
00217 std::vector <char> v;
00218 bencode (std::back_inserter (v), e);
00219
00220 *pdwDataSize = v.size ();
00221
00222 if (pbRes == NULL)
00223 return TRUE;
00224
00225 if (dwSize < v.size ())
00226 return FALSE;
00227
00228 for (size_t i = 0; i < v.size (); i++)
00229 *pbRes++ = v [i];
00230
00231 return TRUE;
00232 }
00233
00234 void vmsBtDownloadImpl::set_TrackerLogin (LPCSTR pszUser, LPCSTR pszPassword)
00235 {
00236 if (m_handle.is_valid () == false)
00237 return;
00238
00239 m_handle.set_tracker_login (pszUser, pszPassword);
00240 }
00241
00242 BOOL vmsBtDownloadImpl::MoveToFolder (LPCSTR pszNewFolder)
00243 {
00244 if (m_handle.is_valid () == false)
00245 return FALSE;
00246
00247
00248 char sz [MAX_PATH];
00249 strcpy (sz, pszNewFolder);
00250 LPSTR psz = sz;
00251 while (*++psz) if (*psz == '\\') *psz = '/';
00252 return m_handle.move_storage (sz);
00253 }
00254
00255 void vmsBtDownloadImpl::check_handle_is_valid ()
00256 {
00257 if (m_handle.is_valid () == false)
00258 vmsBtSessionImpl::Instance ()->RestoreDownloadHandle (this);
00259 assert (m_handle.is_valid ());
00260 }
00261
00262 int vmsBtDownloadImpl::get_NextAnnounceInterval ()
00263 {
00264 if (m_handle.is_valid () == false)
00265 return -1;
00266
00267 return m_handle.status ().next_announce.total_seconds ();
00268 }
00269
00270 bool vmsBtDownloadImpl::is_PieceCompleted (int nIndex)
00271 {
00272 if (m_handle.is_valid () == false)
00273 return false;
00274
00275 return m_handle.status ().pieces->at (nIndex);
00276 }
00277
00278 void vmsBtDownloadImpl::get_CurrentTracker (LPSTR pszRes)
00279 {
00280 if (m_handle.is_valid () == false) {
00281 *pszRes = 0;
00282 return;
00283 }
00284
00285 torrent_status s = m_handle.status ();
00286 LPCSTR pszT = s.current_tracker.c_str ();
00287 if (pszT == NULL || *pszT == 0)
00288 pszT = m_handle.get_torrent_info ().trackers () [0].url.c_str ();
00289 if (pszT)
00290 strcpy (pszRes, pszT);
00291 else
00292 *pszRes = 0;
00293 }
00294
00295 UINT64 vmsBtDownloadImpl::get_TotalUploadedByteCount ()
00296 {
00297 if (m_handle.is_valid () == false)
00298 return 0;
00299
00300 return m_handle.status ().total_payload_upload;
00301 }
00302
00303 void vmsBtDownloadImpl::get_PeersStat (int *pnPeersConnected, int *pnSeedsTotal, int *pnLeechersTotal, int *pnSeedsConnected)
00304 {
00305 if (m_handle.is_valid () == false) {
00306 *pnPeersConnected = 0;
00307 *pnSeedsTotal = 0;
00308 *pnLeechersTotal = 0;
00309 *pnSeedsConnected = 0;
00310 return;
00311 }
00312
00313 torrent_status s = m_handle.status ();
00314 *pnPeersConnected = s.num_peers;
00315 *pnSeedsTotal = s.num_complete;
00316 *pnLeechersTotal = s.num_incomplete;
00317 *pnSeedsConnected = s.num_seeds;
00318 }
00319
00320 UINT64 vmsBtDownloadImpl::get_WastedByteCount ()
00321 {
00322 if (m_handle.is_valid () == false)
00323 return 0;
00324
00325 torrent_status s = m_handle.status ();
00326 return s.total_failed_bytes + s.total_redundant_bytes;
00327 }
00328
00329 double vmsBtDownloadImpl::get_ShareRating ()
00330 {
00331 if (m_handle.is_valid () == false)
00332 return 0;
00333
00334 torrent_status s = m_handle.status ();
00335 if (s.total_payload_download == 0)
00336 return 0;
00337 return (double)s.total_payload_upload / s.total_payload_download;
00338 }
00339
00340 vmsBtDownloadPeerInfoList* vmsBtDownloadImpl::get_PeerInfoList ()
00341 {
00342 if (m_handle.is_valid () == false)
00343 return NULL;
00344 return &m_peerList;
00345 }
00346
00347 void vmsBtDownloadImpl::get_FileProgress (float *p)
00348 {
00349 if (m_handle.is_valid () == false)
00350 return;
00351
00352
00353 std::vector<float> v;
00354 m_handle.file_progress (v);
00355 for (size_t i = 0; i < v.size (); i++)
00356 p [i] = v [i];
00357 }
00358
00359 BOOL vmsBtDownloadImpl::is_HandleValid ()
00360 {
00361 return m_handle.is_valid ();
00362 }
00363
00364 void vmsBtDownloadImpl::OnTrackerAlert (LPCSTR pszMsg)
00365 {
00366 if (m_handle.trackers ().size () == 1 && m_pTorrent->m_torrent->trackers ().size () == 1)
00367 return;
00368
00369 std::vector <announce_entry> v = m_handle.trackers ();
00370
00371 for (size_t i = 0; i < v.size (); i++)
00372 {
00373 if (strstr (pszMsg, v [i].url.c_str ()) != NULL)
00374 {
00375 v.erase (v.begin () + i);
00376 if (v.size () != 0)
00377 {
00378 m_handle.replace_trackers (v);
00379 m_handle.force_reannounce ();
00380 return;
00381 }
00382 }
00383 }
00384
00385 m_handle.replace_trackers (m_pTorrent->m_torrent->trackers ());
00386
00387
00388 }
00389
00390 int vmsBtDownloadImpl::get_CurrentTaskProgress ()
00391 {
00392 return m_handle.status ().progress * 100;
00393 }