Check the chunk before uploading
This commit is contained in:
parent
df282ae9d8
commit
b26af687b7
4 changed files with 22 additions and 9 deletions
2
Makefile
2
Makefile
|
|
@ -10,4 +10,6 @@ build-release:
|
||||||
cargo build --release
|
cargo build --release
|
||||||
|
|
||||||
install: build-release
|
install: build-release
|
||||||
|
$(MAKE) build-release
|
||||||
|
$(MAKE) sign-release
|
||||||
cp target/release/rqbit "$(HOME)/bin/"
|
cp target/release/rqbit "$(HOME)/bin/"
|
||||||
|
|
@ -116,6 +116,13 @@ impl ChunkTracker {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_chunk_ready_to_upload(&self, chunk: &ChunkInfo) -> bool {
|
||||||
|
self.have
|
||||||
|
.get(chunk.piece_index.get() as usize)
|
||||||
|
.map(|b| *b)
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
// return true if the whole piece is marked downloaded
|
// return true if the whole piece is marked downloaded
|
||||||
pub fn mark_chunk_downloaded<ByteBuf>(
|
pub fn mark_chunk_downloaded<ByteBuf>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
|
|
@ -263,18 +263,22 @@ impl PeerConnection {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tx = self
|
let tx = {
|
||||||
.state
|
let g = self.state.locked.read();
|
||||||
.locked
|
if !g.chunks.is_chunk_ready_to_upload(&chunk_info) {
|
||||||
.read()
|
anyhow::bail!(
|
||||||
.peers
|
"got request for a chunk that is not ready to upload. chunk {:?}",
|
||||||
.clone_tx(peer_handle)
|
&chunk_info
|
||||||
.ok_or_else(|| {
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
g.peers.clone_tx(peer_handle).ok_or_else(|| {
|
||||||
anyhow::anyhow!(
|
anyhow::anyhow!(
|
||||||
"peer {} died, dropping chunk that it requested",
|
"peer {} died, dropping chunk that it requested",
|
||||||
peer_handle
|
peer_handle
|
||||||
)
|
)
|
||||||
})?;
|
})?
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: this is not super efficient as it does copying multiple times.
|
// TODO: this is not super efficient as it does copying multiple times.
|
||||||
// Theoretically, this could be done in the sending code, so that it reads straight into
|
// Theoretically, this could be done in the sending code, so that it reads straight into
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use std::{
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use futures::{stream::FuturesUnordered, StreamExt};
|
use futures::{stream::FuturesUnordered, StreamExt};
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, info, warn};
|
||||||
use parking_lot::{Mutex, RwLock};
|
use parking_lot::{Mutex, RwLock};
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use size_format::SizeFormatterBinary as SF;
|
use size_format::SizeFormatterBinary as SF;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue