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
use std::sync::{Mutex,Arc};
use std::sync::mpsc::{channel,Sender,Receiver,SendError};
#[derive(Clone)]
pub struct MultiSender<T: Send> {
clients: Arc<Mutex<Vec<Sender<T>>>>
}
unsafe impl<T: Send> Sync for MultiSender<T> {}
impl<T: Send+Clone> MultiSender<T> {
pub fn new() -> MultiSender<T> {
MultiSender::<T> {
clients: Arc::new(Mutex::new(Vec::new()))
}
}
pub fn receiver(&mut self) -> Receiver<T> {
let (cast_tx, cast_rx) = channel();
let mut clients = self.clients.lock().unwrap();
clients.push(cast_tx);
cast_rx
}
pub fn send(&self, msg: T) {
let clients = self.clients.lock().unwrap();
for client in clients.iter() {
client.send(msg.clone());
}
}
pub fn send_one(&self, i: usize, msg: T) -> Result<(), SendError<T>> {
let clients = self.clients.lock().unwrap();
clients[i].send(msg.clone())
}
#[allow(dead_code)]
pub fn disconnect_all(&mut self) {
let mut clients = self.clients.lock().unwrap();
*clients = Vec::new();
}
}