libjuice_rs/agent/
handler.rs1use crate::agent::State;
2
3#[derive(Default)]
18pub struct Handler {
19 on_state_change: Option<Box<dyn FnMut(State) + Send + 'static>>,
21 on_candidate: Option<Box<dyn FnMut(String) + Send + 'static>>,
23 on_gathering_done: Option<Box<dyn FnMut() + Send + 'static>>,
25 on_recv: Option<Box<dyn FnMut(&[u8]) + Send + 'static>>,
27}
28
29impl Handler {
30 pub fn state_handler<F>(mut self, f: F) -> Self
32 where
33 F: FnMut(State),
34 F: Send + Sync + 'static,
35 {
36 self.on_state_change = Some(Box::new(f));
37 self
38 }
39
40 pub fn candidate_handler<F>(mut self, f: F) -> Self
42 where
43 F: FnMut(String),
44 F: Send + 'static,
45 {
46 self.on_candidate = Some(Box::new(f));
47 self
48 }
49
50 pub fn gathering_done_handler<F>(mut self, f: F) -> Self
52 where
53 F: FnMut(),
54 F: Send + 'static,
55 {
56 self.on_gathering_done = Some(Box::new(f));
57 self
58 }
59
60 pub fn recv_handler<F>(mut self, f: F) -> Self
62 where
63 F: FnMut(&[u8]),
64 F: Send + 'static,
65 {
66 self.on_recv = Some(Box::new(f));
67 self
68 }
69
70 pub(crate) fn on_state_changed(&mut self, state: State) {
71 if let Some(f) = &mut self.on_state_change {
72 f(state)
73 }
74 }
75
76 pub(crate) fn on_candidate(&mut self, candidate: String) {
77 if let Some(f) = &mut self.on_candidate {
78 f(candidate)
79 }
80 }
81
82 pub(crate) fn on_gathering_done(&mut self) {
83 if let Some(f) = &mut self.on_gathering_done {
84 f()
85 }
86 }
87
88 pub(crate) fn on_recv(&mut self, packet: &[u8]) {
89 if let Some(f) = &mut self.on_recv {
90 f(packet)
91 }
92 }
93}