1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use alloc::sync::{Arc, Weak};
use core::fmt;
use core::sync::atomic::{AtomicUsize, Ordering};
use futures_intrusive::channel::shared::*;
use spin::{Mutex, RwLock};
cfg_if::cfg_if! {
if #[cfg(feature = "role_center")] {
use alloc::collections::BTreeMap;
} else{
use crate::consts::LINKS_LEN;
use fixed_queue::LinearMap;
type BTreeMap<K, V> = LinearMap<K, V, LINKS_LEN>;
}
}
static LINKS: RwLock<BTreeMap<Link, ()>> = RwLock::new(BTreeMap::new());
static UID_SN: AtomicUsize = AtomicUsize::new(1);
#[derive(Debug)]
pub enum ErrorKind {
NotFound,
NotConnected,
TimedOut,
WouldBlock,
InvalidData,
}
pub struct SendResultFuture {
recver: GenericOneshotReceiver<Mutex<()>, Result<(), ErrorKind>>,
}
impl SendResultFuture {
pub fn new() -> (
SendResultFuture,
GenericOneshotSender<Mutex<()>, Result<(), ErrorKind>>,
) {
let (sender, recver) = generic_oneshot_channel();
let result_future = SendResultFuture { recver };
(result_future, sender)
}
pub async fn get_result(self) -> Result<(), ErrorKind> {
self.recver.receive().await.unwrap()
}
}
pub trait Driver {
fn name(&self) -> &str;
fn send(self: Arc<Self>, buf: &[u8]) -> SendResultFuture;
}
#[derive(Clone)]
pub struct Link {
uid: usize,
pub driver: Weak<dyn Driver + Send + Sync>,
}
impl PartialEq for Link {
fn eq(&self, other: &Self) -> bool {
self.uid == other.uid
}
}
impl Eq for Link {}
impl PartialOrd for Link {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.uid.cmp(&other.uid))
}
}
impl Ord for Link {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.uid.cmp(&other.uid)
}
}
impl fmt::Display for Link {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "link{}", self.uid)
}
}
impl fmt::Debug for Link {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(driver) = self.driver.upgrade() {
write!(f, "link{}({})", self.uid, driver.name())
} else {
write!(f, "link{}({})", self.uid, "invalid")
}
}
}
impl Link {
pub fn new(driver: Weak<dyn Driver + Send + Sync>) -> Self {
let link = Link {
uid: UID_SN.fetch_add(1, Ordering::Relaxed),
driver: driver,
};
LINKS.write().insert(link.clone(), ());
link
}
pub fn destroy(&self) {
LINKS.write().remove(self);
crate::net::when_link_disconnect(self);
}
pub fn status(&self) -> Result<(), ErrorKind> {
if let Some(_) = self.driver.upgrade() {
return Ok(());
} else {
return Err(ErrorKind::NotFound);
}
}
pub async fn send(&self, buf: &[u8]) -> Result<(), ErrorKind> {
if let Some(driver) = self.driver.upgrade() {
return driver.send(&buf).get_result().await;
} else {
return Err(ErrorKind::NotFound);
}
}
pub fn broadcast(buf: &[u8]) {
for (link, _) in LINKS.read().iter() {
if let Some(driver) = link.driver.upgrade() {
driver.send(&buf);
} else {
link.destroy();
}
}
}
}