PeerMessage.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "PeerMessage.h"
  23. #include "Util.h"
  24. void PeerMessage::setBitfield(const unsigned char* bitfield, int bitfieldLength) {
  25. if(this->bitfield != NULL) {
  26. delete [] bitfield;
  27. }
  28. this->bitfieldLength = bitfieldLength;
  29. this->bitfield = new unsigned char[this->bitfieldLength];
  30. memcpy(this->bitfield, bitfield, this->bitfieldLength);
  31. }
  32. void PeerMessage::setBlock(const char* block, int blockLength) {
  33. if(this->block != NULL) {
  34. delete [] block;
  35. }
  36. this->blockLength = blockLength;
  37. this->block = new char[this->blockLength];
  38. memcpy(this->block, block, this->blockLength);
  39. }
  40. string PeerMessage::toString() const {
  41. switch(id) {
  42. case CHOKE:
  43. return "choke";
  44. case UNCHOKE:
  45. return "unchoke";
  46. case INTERESTED:
  47. return "interested";
  48. case NOT_INTERESTED:
  49. return "not interested";
  50. case HAVE:
  51. return "have index="+Util::itos(index);
  52. case BITFIELD:
  53. return "bitfield "+Util::toHex(bitfield, bitfieldLength);
  54. case REQUEST:
  55. return "request index="+Util::itos(index)+", begin="+Util::itos(begin)+
  56. ", length="+Util::itos(length);
  57. case PIECE:
  58. return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
  59. ", length="+Util::itos(blockLength);
  60. case CANCEL:
  61. return "calcel index="+Util::itos(index)+", begin="+Util::itos(begin)+
  62. ", length="+Util::itos(length);
  63. case KEEP_ALIVE:
  64. return "keep alive";
  65. default:
  66. return "unknown";
  67. }
  68. }