-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02ce4da
commit ffbeb71
Showing
17 changed files
with
458 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
use tokio::{ | ||
net::{tcp::OwnedWriteHalf, TcpStream}, | ||
runtime::{Builder, Runtime}, | ||
sync::{mpsc, RwLock}, | ||
task::JoinHandle, | ||
}; | ||
|
||
use crate::{error::BasicDummyError, packet::Packet, worker_pool::receiver::receiver_fn}; | ||
|
||
lazy_static! { | ||
static ref DUMMY_RUNTIME:Runtime = Builder::new_multi_thread() | ||
.enable_time() | ||
.worker_threads(10) | ||
// no timer! | ||
.build() | ||
.expect("creat block runtime error"); | ||
|
||
} | ||
|
||
pub enum BasicDummyAPI { | ||
WriteHalf(Box<dyn FnOnce(&mut OwnedWriteHalf) + 'static + Sync + Send>), | ||
} | ||
|
||
pub enum DummyVersion { | ||
Version1_15, | ||
Version1_16, | ||
} | ||
|
||
pub enum TriggerFnWhenPacket { | ||
All, | ||
On(Packet), | ||
} | ||
|
||
pub struct BasicDummy { | ||
pub dummy_name: String, | ||
pub domain: String, | ||
pub packet_version: u32, | ||
pub client_version: u32, | ||
pub net_handle: Option<JoinHandle<()>>, | ||
basic_dummy_api_tx: async_channel::Sender<BasicDummyAPI>, | ||
basic_dummy_api_rx: async_channel::Receiver<BasicDummyAPI>, | ||
when_receiver_fn: HashMap<TriggerFnWhenPacket, Vec<Box<dyn FnOnce() + Send + Sync + 'static>>>, | ||
} | ||
|
||
impl BasicDummy { | ||
pub fn new(dummy_name: &str, domain: &str, version: DummyVersion) -> Arc<RwLock<Self>> { | ||
let (basic_dummy_api_tx, basic_dummy_api_rx) = async_channel::bounded(10); | ||
match version { | ||
DummyVersion::Version1_15 => Arc::new(RwLock::new(Self { | ||
dummy_name: dummy_name.to_string(), | ||
domain: domain.to_string(), | ||
packet_version: 4, | ||
client_version: 163, | ||
net_handle: None, | ||
when_receiver_fn: HashMap::new(), | ||
basic_dummy_api_tx, | ||
basic_dummy_api_rx, | ||
})), | ||
DummyVersion::Version1_16 => todo!(), | ||
} | ||
} | ||
|
||
pub async fn write_half_fn( | ||
&self, | ||
clouse: Box<dyn FnOnce(&mut OwnedWriteHalf) + 'static + Sync + Send>, | ||
) -> Result<(), async_channel::SendError<BasicDummyAPI>> { | ||
self.basic_dummy_api_tx | ||
.send(BasicDummyAPI::WriteHalf(clouse)) | ||
.await?; | ||
Ok(()) | ||
} | ||
pub async fn connect_to_relay( | ||
dummy: Arc<RwLock<Self>>, | ||
addr: &str, | ||
) -> Result<(), BasicDummyError> { | ||
match TcpStream::connect(&addr).await { | ||
Ok(stream) => { | ||
let api_rx = dummy.read().await.basic_dummy_api_rx.clone(); | ||
dummy.write().await.net_handle = | ||
Some(DUMMY_RUNTIME.spawn(basic_dummy_fn(stream, dummy.clone(), api_rx))); | ||
Ok(()) | ||
} | ||
Err(_) => Err(BasicDummyError::JoinRelayError("connect error")), | ||
} | ||
} | ||
} | ||
|
||
async fn basic_dummy_fn( | ||
stream: TcpStream, | ||
dummy: Arc<RwLock<BasicDummy>>, | ||
api_rx: async_channel::Receiver<BasicDummyAPI>, | ||
) { | ||
let (mut read_half, mut write_half) = stream.into_split(); | ||
|
||
loop { | ||
tokio::select! { | ||
recv = receiver_fn(&mut read_half) => { | ||
match recv { | ||
Ok(packet) => { | ||
for (fn_type ,function) in dummy.write().await.when_receiver_fn.iter(){ | ||
|
||
} | ||
continue; | ||
} | ||
Err(_) => {break;} | ||
}; | ||
} | ||
|
||
Ok(api) = api_rx.recv() => { | ||
match api { | ||
BasicDummyAPI::WriteHalf(f) => f(&mut write_half), | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::sync::Arc; | ||
|
||
use log::info; | ||
use tokio::sync::{broadcast, mpsc}; | ||
|
||
use crate::relay_manager::relay::SharedRelayRoom; | ||
|
||
#[derive(Clone)] | ||
pub struct Event { | ||
pub event_name: String, | ||
pub event_type: EventType, | ||
} | ||
impl Event { | ||
pub fn new(event_name: &str, event_type: EventType) -> Self { | ||
Self { | ||
event_name:event_name.to_string(), | ||
event_type, | ||
} | ||
} | ||
} | ||
#[derive(Clone)] | ||
pub enum EventType { | ||
NewRoomAndHostOk(Arc<SharedRelayRoom>), | ||
} | ||
|
||
lazy_static! { | ||
pub static ref EVENT_CHANNEL: (async_channel::Sender<Event>, async_channel::Receiver<Event>) = | ||
async_channel::unbounded(); | ||
pub static ref EVENT_CHANNEL_MULTIPLE: (broadcast::Sender<Event>, broadcast::Receiver<Event>) = | ||
broadcast::channel(100); | ||
} | ||
|
||
pub fn init_event_system() -> anyhow::Result<()> { | ||
info!("事件系统正在初始化"); | ||
|
||
tokio::spawn(async move { | ||
match EVENT_CHANNEL.1.recv().await { | ||
Ok(event) => EVENT_CHANNEL_MULTIPLE.0.send(event), | ||
Err(_) => todo!(), | ||
} | ||
}); | ||
Ok(()) | ||
} |
Oops, something went wrong.