From 14ed8d1853f5c59689db46600ce105714b968f28 Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Tue, 21 Feb 2023 13:03:34 +0800 Subject: [PATCH] dht: fix build wrong Signed-off-by: Xiaobo Liu --- Cargo.toml | 4 ++-- src/dht.rs | 9 +++++---- src/errors.rs | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a42490d..3c4562b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ description = "A sniffer of DHT" [dependencies] env_logger = "0.7" log = "0.4" -thiserror = "1.0" +thiserror = "1" anyhow = "1.0" rand = "0.7" sha1 = "0.6" @@ -17,5 +17,5 @@ async-std = { version = "1.6", features = ["unstable"] } lru-cache = "0.1" structopt = "0.3" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +serde_json = "1" bencode = { path = "bencode" } diff --git a/src/dht.rs b/src/dht.rs index b816aa3..d332d55 100644 --- a/src/dht.rs +++ b/src/dht.rs @@ -1,9 +1,10 @@ use std::collections::HashMap; use std::time::Duration; +use async_std::channel::{Receiver, Sender}; use async_std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; -use async_std::sync::{channel, Arc, Receiver, Sender}; -use async_std::task; +use async_std::sync::Arc; +use async_std::{channel, task}; use bencode::Value; use log::{debug, info}; use rand::prelude::*; @@ -50,7 +51,7 @@ impl DHT { let sock = UdpSocket::bind(self.laddr.as_ref()).await?; self.socket = Arc::new(Some(sock)); - let (tx, rx) = channel(self.peers); + let (tx, rx) = channel::bounded(self.peers); self.start_message_handler(tx); self.start_join(); @@ -240,7 +241,7 @@ impl DHT { info!("channel is full, skip."); } else { let ac = self.summarize(v, addr)?; - tx.send(ac).await; + tx.send(ac).await.map_err(|e| Error::Send(e.into_inner()))?; } Ok(()) diff --git a/src/errors.rs b/src/errors.rs index a44bc2e..40977c2 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,4 @@ +use crate::Message; use async_std::io::Error as AsyncIoError; use bencode::Error as BencodeError; use thiserror::Error as ThisError; @@ -6,10 +7,16 @@ use thiserror::Error as ThisError; pub enum Error { #[error(transparent)] AsyncIo(#[from] AsyncIoError), + #[error(transparent)] Bencode(#[from] BencodeError), + + #[error("send fail, message: {0:?}")] + Send(Message), + #[error("bencode dict not found '{0}'")] DictNotFound(String), + #[error("{0}")] Other(String), }