Handshake clone to owned

This commit is contained in:
Igor Katson 2023-12-05 17:01:30 +00:00
parent 41fb3bfd37
commit 9c7cf61e1a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
4 changed files with 56 additions and 21 deletions

View file

@ -23,7 +23,7 @@ pub trait PeerConnectionHandler {
fn on_connected(&self, _connection_time: Duration) {}
fn get_have_bytes(&self) -> u64;
fn serialize_bitfield_message_to_buf(&self, buf: &mut Vec<u8>) -> anyhow::Result<usize>;
fn on_handshake(&self, handshake: Handshake) -> anyhow::Result<()>;
fn on_handshake<B>(&self, handshake: Handshake<B>) -> anyhow::Result<()>;
fn on_extended_handshake(
&self,
extended_handshake: &ExtendedHandshake<ByteBuf>,
@ -120,7 +120,16 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
}
}
pub async fn manage_peer(
pub async fn manage_peer_incoming(
&self,
mut outgoing_chan: tokio::sync::mpsc::UnboundedReceiver<WriterRequest>,
handshake: Handshake<ByteString>,
socket: tokio::net::TcpSocket,
) -> anyhow::Result<()> {
todo!()
}
pub async fn manage_peer_outgoing(
&self,
mut outgoing_chan: tokio::sync::mpsc::UnboundedReceiver<WriterRequest>,
) -> anyhow::Result<()> {

View file

@ -51,7 +51,7 @@ pub(crate) async fn read_metainfo_from_peer(
);
let result_reader = async move { result_rx.await? };
let connection_runner = async move { connection.manage_peer(writer_rx).await };
let connection_runner = async move { connection.manage_peer_outgoing(writer_rx).await };
tokio::select! {
result = result_reader => result,
@ -145,7 +145,7 @@ impl PeerConnectionHandler for Handler {
Ok(0)
}
fn on_handshake(&self, handshake: Handshake) -> anyhow::Result<()> {
fn on_handshake<B>(&self, handshake: Handshake<B>) -> anyhow::Result<()> {
if !handshake.supports_extended() {
anyhow::bail!("this peer does not support extended handshaking, which is a prerequisite to download metadata")
}

View file

@ -404,7 +404,7 @@ impl TorrentStateLive {
.fetch_add(1, Ordering::Relaxed);
let res = tokio::select! {
r = requester => {r}
r = peer_connection.manage_peer(rx) => {r}
r = peer_connection.manage_peer_outgoing(rx) => {r}
};
handler.state.peer_semaphore.add_permits(1);
@ -502,7 +502,7 @@ impl TorrentStateLive {
matches!(self.get_next_needed_piece(handle), Ok(Some(_)))
}
fn set_peer_live(&self, handle: PeerHandle, h: Handshake) {
fn set_peer_live<B>(&self, handle: PeerHandle, h: Handshake<B>) {
let result = self.peers.with_peer_mut(handle, "set_peer_live", |p| {
p.state
.connecting_to_live(Id20(h.peer_id), &self.peers.stats)
@ -771,7 +771,7 @@ impl<'a> PeerConnectionHandler for &'a PeerHandler {
Ok(len)
}
fn on_handshake(&self, handshake: Handshake) -> anyhow::Result<()> {
fn on_handshake<B>(&self, handshake: Handshake<B>) -> anyhow::Result<()> {
self.state.set_peer_live(self.addr, handshake);
Ok(())
}