Peer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef _D_PEER_H_
  23. #define _D_PEER_H_
  24. #include "common.h"
  25. #include "BitfieldMan.h"
  26. #include <string.h>
  27. #include <string>
  28. using namespace std;
  29. #define PEER_ID_LENGTH 20
  30. class Peer {
  31. public:
  32. int entryId;
  33. string ipaddr;
  34. int port;
  35. bool amChocking;
  36. bool amInterested;
  37. bool peerChoking;
  38. bool peerInterested;
  39. int tryCount;
  40. int error;
  41. int cuid;
  42. private:
  43. char peerId[PEER_ID_LENGTH];
  44. //unsigned char* bitfield;
  45. BitfieldMan* bitfield;
  46. //int bitfieldLength;
  47. long long int peerUpload;
  48. long long int peerDownload;
  49. int pieceLength;
  50. long long int totalLength;
  51. public:
  52. Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
  53. entryId(0), ipaddr(ipaddr), port(port),
  54. amChocking(true), amInterested(false),
  55. peerChoking(true), peerInterested(false),
  56. tryCount(0), error(0), cuid(0),
  57. bitfield(NULL),// bitfieldLength(0),
  58. peerUpload(0), peerDownload(0),
  59. pieceLength(pieceLength), totalLength(totalLength) {
  60. this->bitfield = new BitfieldMan(pieceLength, totalLength);
  61. }
  62. ~Peer() {
  63. if(bitfield != NULL) {
  64. delete /*[]*/ bitfield;
  65. }
  66. }
  67. void setPeerId(const char* peerId) {
  68. memcpy(this->peerId, peerId, PEER_ID_LENGTH);
  69. }
  70. const char* getPeerId() const { return this->peerId; }
  71. void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
  72. this->bitfield->setBitfield(bitfield, bitfieldLength);
  73. /*
  74. if(this->bitfield != NULL) {
  75. delete [] this->bitfield;
  76. }
  77. this->bitfieldLength = bitfieldLength;
  78. this->bitfield = new unsigned char[this->bitfieldLength];
  79. memcpy(this->bitfield, bitfield, this->bitfieldLength);
  80. */
  81. }
  82. const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
  83. int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
  84. /*
  85. void initBitfield(int pieces);
  86. */
  87. /**
  88. * operation = 1: set index-th bit 1
  89. * operation = 0: set index-th bit 0
  90. */
  91. void updateBitfield(int index, int operation);
  92. void addPeerUpload(int size) { peerUpload += size; }
  93. void setPeerUpload(long long int size) { peerUpload = size; }
  94. long long int getPeerUpload() const { return peerUpload; }
  95. void addPeerDownload(int size) { peerDownload += size; }
  96. void setPeerDownload(long long int size) { peerDownload = size; }
  97. long long int getPeerDownload() const { return peerDownload; }
  98. bool shouldChoke() const;
  99. bool hasPiece(int index) const;
  100. bool isSeeder() const;
  101. static Peer* nullPeer;
  102. };
  103. #endif // _D_PEER_H_