mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
http_private: get basic announce route working
This commit is contained in:
parent
b0f89edd30
commit
96e128bb90
7 changed files with 181 additions and 36 deletions
48
aquatic_http_private/src/common.rs
Normal file
48
aquatic_http_private/src/common.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use tokio::sync::{mpsc, oneshot};
|
||||
|
||||
use aquatic_common::CanonicalSocketAddr;
|
||||
use aquatic_http_protocol::{common::InfoHash, response::Response};
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
workers::{common::ChannelAnnounceRequest, socket::db::ValidatedAnnounceRequest},
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
pub struct RequestWorkerIndex(pub usize);
|
||||
|
||||
impl RequestWorkerIndex {
|
||||
pub fn from_info_hash(config: &Config, info_hash: InfoHash) -> Self {
|
||||
Self(info_hash.0[0] as usize % config.request_workers)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ChannelRequestSender(Vec<mpsc::Sender<ChannelAnnounceRequest>>);
|
||||
|
||||
impl ChannelRequestSender {
|
||||
pub fn new(senders: Vec<mpsc::Sender<ChannelAnnounceRequest>>) -> Self {
|
||||
Self(senders)
|
||||
}
|
||||
|
||||
pub async fn send_to(
|
||||
&self,
|
||||
index: RequestWorkerIndex,
|
||||
request: ValidatedAnnounceRequest,
|
||||
source_addr: CanonicalSocketAddr,
|
||||
) -> anyhow::Result<oneshot::Receiver<Response>> {
|
||||
let (response_sender, response_receiver) = oneshot::channel();
|
||||
|
||||
let request = ChannelAnnounceRequest {
|
||||
request,
|
||||
source_addr,
|
||||
response_sender,
|
||||
};
|
||||
|
||||
match self.0[index.0].send(request).await {
|
||||
Ok(()) => Ok(response_receiver),
|
||||
Err(err) => {
|
||||
Err(anyhow::Error::new(err).context("error sending ChannelAnnounceRequest"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue