Peer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #define DEFAULT_LATENCY 1500
  31. class Peer {
  32. public:
  33. int entryId;
  34. string ipaddr;
  35. int port;
  36. bool amChoking;
  37. bool amInterested;
  38. bool peerChoking;
  39. bool peerInterested;
  40. int tryCount;
  41. int error;
  42. int cuid;
  43. bool chokingRequired;
  44. bool optUnchoking;
  45. private:
  46. char peerId[PEER_ID_LENGTH];
  47. BitfieldMan* bitfield;
  48. bool fastExtensionEnabled;
  49. // allowed fast indexes that peer has sent by Allowed Fast message
  50. Integers fastSet;
  51. long long int peerUpload;
  52. long long int peerDownload;
  53. int pieceLength;
  54. int deltaUpload;
  55. int deltaDownload;
  56. int latency;
  57. public:
  58. Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
  59. entryId(0), ipaddr(ipaddr), port(port),
  60. amChoking(true), amInterested(false),
  61. peerChoking(true), peerInterested(false),
  62. tryCount(0), error(0), cuid(0),
  63. chokingRequired(true), optUnchoking(false),
  64. bitfield(NULL),
  65. fastExtensionEnabled(false),
  66. peerUpload(0), peerDownload(0),
  67. pieceLength(pieceLength),
  68. deltaUpload(0), deltaDownload(0),
  69. latency(DEFAULT_LATENCY) {
  70. this->bitfield = new BitfieldMan(pieceLength, totalLength);
  71. }
  72. ~Peer() {
  73. if(bitfield != NULL) {
  74. delete bitfield;
  75. }
  76. }
  77. void resetStatus();
  78. void addDeltaUpload(int length) {
  79. this->deltaUpload += length;
  80. }
  81. void resetDeltaUpload() { this->deltaUpload = 0; }
  82. int getDeltaUpload() const { return this->deltaUpload; }
  83. void addDeltaDownload(int length) {
  84. this->deltaDownload += length;
  85. }
  86. void resetDeltaDownload() { this->deltaDownload = 0; }
  87. int getDeltaDownload() const { return this->deltaDownload; }
  88. void setPeerId(const char* peerId) {
  89. memcpy(this->peerId, peerId, PEER_ID_LENGTH);
  90. }
  91. const char* getPeerId() const { return this->peerId; }
  92. void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
  93. this->bitfield->setBitfield(bitfield, bitfieldLength);
  94. }
  95. const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
  96. int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
  97. void setAllBitfield();
  98. /**
  99. * operation = 1: set index-th bit 1
  100. * operation = 0: set index-th bit 0
  101. */
  102. void updateBitfield(int index, int operation);
  103. void addPeerUpload(int size) {
  104. peerUpload += size;
  105. addDeltaUpload(size);
  106. }
  107. void setPeerUpload(long long int size) { peerUpload = size; }
  108. long long int getPeerUpload() const { return peerUpload; }
  109. void addPeerDownload(int size) {
  110. peerDownload += size;
  111. addDeltaDownload(size);
  112. }
  113. void setPeerDownload(long long int size) { peerDownload = size; }
  114. long long int getPeerDownload() const { return peerDownload; }
  115. void setFastExtensionEnabled(bool enabled) {
  116. fastExtensionEnabled = enabled;
  117. }
  118. bool isFastExtensionEnabled() const { return fastExtensionEnabled; }
  119. void addFastSetIndex(int index);
  120. const Integers& getFastSet() const { return fastSet; }
  121. bool isInFastSet(int index) const;
  122. int countFastSet() const { return fastSet.size(); }
  123. bool shouldBeChoking() const;
  124. bool hasPiece(int index) const;
  125. bool isSeeder() const;
  126. void updateLatency(int latency);
  127. int getLatency() const { return latency; }
  128. static Peer* nullPeer;
  129. };
  130. #endif // _D_PEER_H_