TrackerDownloadCommand.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "TrackerDownloadCommand.h"
  23. #include "TrackerUpdateCommand.h"
  24. #include "ChunkedEncoding.h"
  25. #include "DlRetryEx.h"
  26. #include "message.h"
  27. #include "MetaFileUtil.h"
  28. #include "DlAbortEx.h"
  29. TrackerDownloadCommand::TrackerDownloadCommand(int cuid, Request* req,
  30. TorrentDownloadEngine* e,
  31. const Socket* s)
  32. :AbstractCommand(cuid, req, e, s), len(0) {
  33. resSize = 256;
  34. res = new char[resSize];
  35. ChunkedEncoding* ce = new ChunkedEncoding();
  36. transferEncodings["chunked"] = ce;
  37. }
  38. TrackerDownloadCommand::~TrackerDownloadCommand() {
  39. delete [] res;
  40. for(map<string, TransferEncoding*>::iterator itr = transferEncodings.begin(); itr != transferEncodings.end(); itr++) {
  41. delete((*itr).second);
  42. }
  43. }
  44. bool TrackerDownloadCommand::executeInternal(Segment seg) {
  45. TransferEncoding* te = NULL;
  46. if(transferEncoding.size()) {
  47. te = getTransferEncoding(transferEncoding);
  48. assert(te != NULL);
  49. }
  50. int bufSize = 4096;
  51. char buf[bufSize];
  52. socket->readData(buf, bufSize);
  53. if(te != NULL) {
  54. int infbufSize = 4096;
  55. char infbuf[infbufSize];
  56. te->inflate(infbuf, infbufSize, buf, bufSize);
  57. if(len+infbufSize >= resSize) {
  58. expandBuffer(len+infbufSize);
  59. }
  60. memcpy(res+len, infbuf, infbufSize);
  61. len += infbufSize;
  62. } else {
  63. if(len+bufSize >= resSize) {
  64. expandBuffer(len+bufSize);
  65. }
  66. memcpy(res+len, buf, bufSize);
  67. len += bufSize;
  68. }
  69. if(e->segmentMan->totalSize != 0 && bufSize == 0) {
  70. throw new DlRetryEx(EX_GOT_EOF);
  71. }
  72. if(te != NULL && te->finished()
  73. || te == NULL && len == e->segmentMan->totalSize
  74. || bufSize == 0) {
  75. if(te != NULL) te->end();
  76. logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  77. MetaEntry* entry = MetaFileUtil::bdecoding(res, len);
  78. e->commands.push(new TrackerUpdateCommand(cuid, req, (TorrentDownloadEngine*)e, entry));
  79. return true;
  80. } else {
  81. e->commands.push(this);
  82. return false;
  83. }
  84. }
  85. TransferEncoding* TrackerDownloadCommand::getTransferEncoding(string name) {
  86. return transferEncodings[name];
  87. }
  88. void TrackerDownloadCommand::expandBuffer(int newSize) {
  89. char* newbuf = new char[newSize];
  90. memcpy(newbuf, res, len);
  91. delete [] res;
  92. res = newbuf;
  93. resSize = newSize;
  94. }