summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-23 10:47:25 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-23 10:47:25 +0000
commit1fcd640d3b78b7a6c7b5f6cfdaf3e0f0304b3b47 (patch)
tree593a650d60b6bd61774449136daa7e245369881a /src
parent198b87fa3e564f6df1df8cbf961d655a59216d78 (diff)
* AbstractCommand.cc: casted timeout value to long long int.
* ChunkedEncoding.cc: fixed memory leak. * PeerInteractionCommand.cc: casted timeout value to long long int. * SleepCommand.cc: casted timeout value to long long int. * Data.cc: fixed memory leak. * Data.cc: fixed toLLInt(). * BitfieldMan.cc: fixed memory leak. * TorrentMan.cc: initialized storeDir to ".". * TorrentMan.cc: fixed memory leak. * TorrentMan.cc: corrected file paths of splitted files. * PeerAbstractCommand.cc: casted timeout to long long int. * main.cc: added delete(req) and delete(te->diskWriter). * RequestSlot.cc: casted timeout value to long long int. * Request.cc: fixed memory leak. * PendingMessage.cc: make HAVE messages sent only when peer does not have the piece. * Peer.{h,cc}: added hasPiece(int index).
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCommand.cc2
-rw-r--r--src/BitfieldMan.cc31
-rw-r--r--src/ChunkedEncoding.cc1
-rw-r--r--src/Data.cc6
-rw-r--r--src/Peer.cc4
-rw-r--r--src/Peer.h2
-rw-r--r--src/PeerAbstractCommand.cc2
-rw-r--r--src/PeerInteractionCommand.cc2
-rw-r--r--src/PendingMessage.cc4
-rw-r--r--src/Request.cc4
-rw-r--r--src/RequestSlot.cc2
-rw-r--r--src/SleepCommand.cc2
-rw-r--r--src/TorrentMan.cc16
-rw-r--r--src/main.cc5
14 files changed, 37 insertions, 46 deletions
diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc
index 7caa1fa7..942e1a55 100644
--- a/src/AbstractCommand.cc
+++ b/src/AbstractCommand.cc
@@ -62,7 +62,7 @@ bool AbstractCommand::isTimeoutDetected() {
return false;
} else {
long long int elapsed = Util::difftv(now, checkPoint);
- if(elapsed >= e->option->getAsInt(PREF_TIMEOUT)*1000000) {
+ if(elapsed >= e->option->getAsLLInt(PREF_TIMEOUT)*1000000) {
return true;
} else {
return false;
diff --git a/src/BitfieldMan.cc b/src/BitfieldMan.cc
index 66f89205..7eda3fe2 100644
--- a/src/BitfieldMan.cc
+++ b/src/BitfieldMan.cc
@@ -111,6 +111,7 @@ int BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int length)
}
int max = countSetBit(tempBitfield, bitfieldLength);
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength, max);
+ delete [] tempBitfield;
return index;
}
@@ -123,36 +124,8 @@ int BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int le
tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
}
int max = countSetBit(tempBitfield, bitfieldLength);
- /*
- int max = 0;
- for(int i = 0; i < bitfieldLength; i++) {
- unsigned char bit = tempBitfield[i];
- for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
- unsigned char mask = 1 << bs;
- if(bit & mask) {
- max++;
- }
- }
- }
- */
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength, max);
- /*
- int index = -1;
- int nth = 1+(int)(((double)max)*random()/(RAND_MAX+1.0));
- for(int i = 0; i < bitfieldLength && index == -1; i++) {
- unsigned char bit = tempBitfield[i];
- for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
- unsigned char mask = 1 << bs;
- if(bit & mask) {
- nth--;
- if(nth == 0) {
- index = i*8+7-bs;
- break;
- }
- }
- }
- }
- */
+ delete [] tempBitfield;
return index;
}
diff --git a/src/ChunkedEncoding.cc b/src/ChunkedEncoding.cc
index 496f02aa..ba45b3d7 100644
--- a/src/ChunkedEncoding.cc
+++ b/src/ChunkedEncoding.cc
@@ -154,6 +154,7 @@ int ChunkedEncoding::readChunkSize(char** pp) {
temp[exsp-*pp] = '\0';
chunkSize = strtol(temp, NULL, 16);
+ delete [] temp;
if(chunkSize < 0) {
throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
} else if(errno == ERANGE && (chunkSize == LONG_MAX || chunkSize == LONG_MIN)) {
diff --git a/src/Data.cc b/src/Data.cc
index b3d49f02..2fb430b9 100644
--- a/src/Data.cc
+++ b/src/Data.cc
@@ -44,7 +44,9 @@ string Data::toString() const {
char* temp = new char[len+1];
memcpy(temp, data, len);
temp[len] = '\0';
- return string(temp);
+ string str(temp);
+ delete [] temp;
+ return str;
}
}
@@ -68,7 +70,7 @@ long long int Data::toLLInt() const {
if(len == 0) {
return 0;
} else {
- return strtoll(data, NULL, 10);
+ return strtoll(toString().c_str(), NULL, 10);
}
}
diff --git a/src/Peer.cc b/src/Peer.cc
index ece5c267..a517720e 100644
--- a/src/Peer.cc
+++ b/src/Peer.cc
@@ -46,3 +46,7 @@ bool Peer::shouldChoke() const {
return peerDownload >= peerUpload+pieceLength*4;
}
}
+
+bool Peer::hasPiece(int index) const {
+ return bitfield->isBitSet(index);
+}
diff --git a/src/Peer.h b/src/Peer.h
index ab5f62ca..98f5ef59 100644
--- a/src/Peer.h
+++ b/src/Peer.h
@@ -109,6 +109,8 @@ public:
bool shouldChoke() const;
+ bool hasPiece(int index) const;
+
static Peer* nullPeer;
};
diff --git a/src/PeerAbstractCommand.cc b/src/PeerAbstractCommand.cc
index 26d2d376..9de87869 100644
--- a/src/PeerAbstractCommand.cc
+++ b/src/PeerAbstractCommand.cc
@@ -63,7 +63,7 @@ bool PeerAbstractCommand::isTimeoutDetected() {
return false;
} else {
long long int elapsed = Util::difftv(now, checkPoint);
- if(elapsed >= e->option->getAsInt(PREF_TIMEOUT)*1000000) {
+ if(elapsed >= e->option->getAsLLInt(PREF_TIMEOUT)*1000000) {
return true;
} else {
return false;
diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc
index 0b5a2b8a..6c6b3ab5 100644
--- a/src/PeerInteractionCommand.cc
+++ b/src/PeerInteractionCommand.cc
@@ -361,7 +361,7 @@ void PeerInteractionCommand::keepAlive() {
} else {
struct timeval now;
gettimeofday(&now, NULL);
- if(Util::difftv(now, keepAliveCheckPoint) >= 120*1000000) {
+ if(Util::difftv(now, keepAliveCheckPoint) >= (long long int)120*1000000) {
if(pendingMessages.empty()) {
peerConnection->sendKeepAlive();
}
diff --git a/src/PendingMessage.cc b/src/PendingMessage.cc
index 27df5a99..a88027e8 100644
--- a/src/PendingMessage.cc
+++ b/src/PendingMessage.cc
@@ -25,7 +25,9 @@ bool PendingMessage::processMessage() {
bool retval = true;
switch(peerMessageId) {
case PeerMessage::HAVE:
- peerConnection->sendHave(index);
+ if(!peerConnection->getPeer()->hasPiece(index)) {
+ peerConnection->sendHave(index);
+ }
break;
case PeerMessage::BITFIELD:
peerConnection->sendBitfield();
diff --git a/src/Request.cc b/src/Request.cc
index 91bd6d12..bc6e45d8 100644
--- a/src/Request.cc
+++ b/src/Request.cc
@@ -36,7 +36,9 @@ Request::Request():port(0), tryCount(0), isTorrent(false) {
cookieBox = new CookieBox();
}
-Request::~Request() {}
+Request::~Request() {
+ delete cookieBox;
+}
bool Request::setUrl(string url) {
this->url = url;
diff --git a/src/RequestSlot.cc b/src/RequestSlot.cc
index 3729f47d..7793999e 100644
--- a/src/RequestSlot.cc
+++ b/src/RequestSlot.cc
@@ -55,7 +55,7 @@ void RequestSlot::setDispatchedTime() {
bool RequestSlot::isTimeout(int timeoutSec) const {
struct timeval now;
gettimeofday(&now, NULL);
- return Util::difftv(now, dispatchedTime) > timeoutSec*1000000;
+ return Util::difftv(now, dispatchedTime) > ((long long int)timeoutSec)*1000000;
}
bool RequestSlot::isNull(const RequestSlot& requestSlot) {
diff --git a/src/SleepCommand.cc b/src/SleepCommand.cc
index 82fc6dc5..7d757bc3 100644
--- a/src/SleepCommand.cc
+++ b/src/SleepCommand.cc
@@ -36,7 +36,7 @@ SleepCommand::~SleepCommand() {
bool SleepCommand::execute() {
struct timeval now;
gettimeofday(&now, NULL);
- if(Util::difftv(now, checkPoint) >= wait*1000000) {
+ if(Util::difftv(now, checkPoint) >= ((long long int)wait)*1000000) {
engine->commands.push(nextCommand);
nextCommand = NULL;
return true;
diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc
index 923c22a2..8f357bdb 100644
--- a/src/TorrentMan.cc
+++ b/src/TorrentMan.cc
@@ -37,11 +37,12 @@ TorrentMan::TorrentMan():bitfield(NULL),
peerEntryIdCounter(0), cuidCounter(0),
downloadedSize(0), uploadedSize(0),
deltaDownload(0), deltaUpload(0),
+ storeDir("."),
multiFileTopDir(NULL),
interval(DEFAULT_ANNOUNCE_INTERVAL),
minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL),
complete(0), incomplete(0),
- connections(0) {}
+ connections(0), diskWriter(NULL) {}
TorrentMan::~TorrentMan() {
if(bitfield != NULL) {
@@ -53,6 +54,9 @@ TorrentMan::~TorrentMan() {
for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
delete *itr;
}
+ if(diskWriter != NULL) {
+ delete diskWriter;
+ }
}
// TODO do not use this method in application code
@@ -316,7 +320,6 @@ void TorrentMan::setup(string metaInfoFile) {
name = string(basename(basec))+".file";
free(basec);
}
-
List* files = (List*)infoDic->get("files");
if(files == NULL) {
// single-file mode;
@@ -356,7 +359,6 @@ void TorrentMan::setup(string metaInfoFile) {
announce = ((Data*)topDic->get("announce"))->toString();
pieceLength = ((Data*)infoDic->get("piece length"))->toInt();
pieces = totalSize/pieceLength+(totalSize%pieceLength ? 1 : 0);
-
Data* piecesHashData = (Data*)infoDic->get("pieces");
if(piecesHashData->getLen() != pieces*20) {
throw new DlAbortEx("the number of pieces is wrong.");
@@ -388,7 +390,7 @@ string TorrentMan::getPieceHash(int index) const {
}
string TorrentMan::getFilePath() const {
- return (storeDir == "" ? "." : storeDir)+"/"+name;
+ return storeDir+"/"+name;
}
string TorrentMan::getTempFilePath() const {
@@ -499,11 +501,13 @@ void TorrentMan::renameSingleFile() const {
}
void TorrentMan::splitMultiFile() const {
- multiFileTopDir->createDir((storeDir == "" ? "." : storeDir), true);
+ logger->info("creating directories");
+ multiFileTopDir->createDir(storeDir, true);
long long int offset = 0;
+ logger->info("splitting file");
for(MultiFileEntries::const_iterator itr = multiFileEntries.begin();
itr != multiFileEntries.end(); itr++) {
- Util::rangedFileCopy(itr->path, getTempFilePath(), offset, itr->length);
+ Util::rangedFileCopy(storeDir+"/"+itr->path, getTempFilePath(), offset, itr->length);
offset += itr->length;
}
unlink(getTempFilePath().c_str());
diff --git a/src/main.cc b/src/main.cc
index 27987f00..403ff9ea 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -525,7 +525,7 @@ int main(int argc, char* argv[]) {
Requests requests;
for(int i = 1; optind+i-1 < argc; i++) {
for(int s = 1; s <= split; s++) {
- addCommand(split*(i-1)+s, argv[optind+i-1], referer, requests);
+ addCommand(split*(i-1)+s, argv[optind+i-1], referer, requests);
}
}
e->run();
@@ -594,9 +594,10 @@ int main(int argc, char* argv[]) {
} else {
printDownloadAbortMessage();
}
-
+ delete(req);
delete(te->segmentMan);
delete(te->torrentMan);
+ delete(te->diskWriter);
delete(te);
} catch(Exception* ex) {
cerr << ex->getMsg() << endl;