1
0
mirror of https://github.com/asamy/ctorrent synced 2025-10-05 23:52:41 +02:00

minor improvements

This commit is contained in:
A. Samy
2015-12-03 04:15:13 +00:00
parent 15dc879971
commit cb9b0d04ca
7 changed files with 28 additions and 8 deletions

View File

@@ -91,7 +91,7 @@ protected:
return true;
}
void internalWriteString(const std::string&);
void internalWriteString(const std::string &);
private:
DataBuffer<char> m_buffer;

View File

@@ -252,10 +252,19 @@ void Peer::handleMessage(MessageType messageType, InputMessage in)
m_queue.erase(it);
delete piece;
} else {
piece->blocks[blockIndex].size = payloadSize;
piece->blocks[blockIndex].data = in.getBuffer(payloadSize);
PieceBlock *block = &piece->blocks[blockIndex];
uint8_t *payload = in.getBuffer(payloadSize);
if (block->rpos != 0) {
memcpy(block->data, payload, std::max(payloadSize, block->size - block->rpos));
free(payload);
block->rpos += payloadSize;
} else {
block->size = block->rpos = payloadSize;
block->data = payload;
++piece->currentBlocks;
}
if (++piece->currentBlocks == piece->numBlocks) {
if (piece->currentBlocks == piece->numBlocks) {
DataBuffer<uint8_t> pieceData;
pieceData.reserve(piece->numBlocks * maxRequestSize); // just a prediction could be bit less
for (size_t x = 0; x < piece->numBlocks; ++x)

View File

@@ -100,9 +100,10 @@ protected:
private:
struct PieceBlock {
size_t size;
size_t rpos;
uint8_t *data;
PieceBlock() { data = nullptr; size = 0; }
PieceBlock() { data = nullptr; size = rpos = 0; }
~PieceBlock() { delete []data; }
};

View File

@@ -200,7 +200,7 @@ void TorrentFileManagerImpl::scan_file(const TorrentFile &f)
FILE *fpp = cf->fp;
if (bufPos > cf->info.length) {
if (fread(&buf[bufPos - cf->info.length], 1, cf->info.length, fpp) != cf->info.length)
if (fread(&buf[bufPos - cf->info.length], 1, bufPos, fpp) != cf->info.length)
return;
bufPos -= cf->info.length;

View File

@@ -96,7 +96,7 @@ static void print_stats(Torrent *t)
printc(COL_YELLOW, "%.2f Mbps (%zd / %zd downloaded %zd hash miss, %zd wasted - %.2f seconds left) ",
t->downloadSpeed(), t->downloadedBytes(), meta->totalSize(),
t->hashMisses(), t->wastedBytes(), t->eta());
printc(COL_YELLOW, "[ %d/%d pieces %d peers active]\n",
printc(COL_YELLOW, "[ %d/%d pieces %d peers active ]\n",
fm->completedPieces(), fm->totalPieces(), t->activePeers());
}

View File

@@ -50,6 +50,16 @@ public:
m_bits = new uint8_t[m_size];
memset(m_bits, 0x00, m_size);
}
void resize(size_t size)
{
uint8_t *bits = new uint8_t[size];
if (!bits)
return;
memcpy(bits, m_bits, size);
m_bits = bits;
m_size = size;
}
// simply modulus by 8 but since this is a bitset lets keep this all
// bit relevant

View File

@@ -173,7 +173,7 @@ void SchedulerImpl::thread()
// Ugly hack for newly added events
bool cont = m_condition.wait_until(m, std::chrono::system_clock::now()
+ std::chrono::milliseconds(1), [this] () { return events.top().expired(); });
+ std::chrono::milliseconds(5), [this] () { return events.top().expired(); });
if (m_stopped)
break;
else if (!cont) {