AllowedFastMessage.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "AllowedFastMessage.h"
  23. #include "PeerInteraction.h"
  24. #include "PeerMessageUtil.h"
  25. #include "Util.h"
  26. #include "DlAbortEx.h"
  27. AllowedFastMessage* AllowedFastMessage::create(const char* data, int dataLength) {
  28. if(dataLength != 5) {
  29. throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "allowed fast", dataLength, 5);
  30. }
  31. int id = PeerMessageUtil::getId(data);
  32. if(id != ID) {
  33. throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
  34. id, "allowed fast", ID);
  35. }
  36. AllowedFastMessage* allowedFastMessage = new AllowedFastMessage();
  37. allowedFastMessage->setIndex(PeerMessageUtil::getIntParam(data, 1));
  38. return allowedFastMessage;
  39. }
  40. void AllowedFastMessage::receivedAction() {
  41. if(!peer->isFastExtensionEnabled()) {
  42. throw new DlAbortEx("%s received while fast extension is disabled",
  43. toString().c_str());
  44. }
  45. peer->addFastSetIndex(index);
  46. }
  47. const char* AllowedFastMessage::getMessage() {
  48. if(!inProgress) {
  49. /**
  50. * len --- 5, 4bytes
  51. * id --- 17, 1byte
  52. * piece index --- index, 4bytes
  53. * total: 9bytes
  54. */
  55. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, ID);
  56. PeerMessageUtil::setIntParam(&msg[5], index);
  57. }
  58. return msg;
  59. }
  60. int AllowedFastMessage::getMessageLength() {
  61. return sizeof(msg);
  62. }
  63. void AllowedFastMessage::onSendComplete() {
  64. peerInteraction->addFastSetIndex(index);
  65. }
  66. void AllowedFastMessage::check() const {
  67. PeerMessageUtil::checkIndex(index, pieces);
  68. }
  69. string AllowedFastMessage::toString() const {
  70. return "allowed fast index="+Util::itos(index);
  71. }