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 crate::{nb, SocketAddr, TcpClientStack, TcpFullStack, UdpClientStack, UdpFullStack};
use core::cell::RefCell;
pub struct SharableStack<T> {
stack: RefCell<T>,
}
impl<T> SharableStack<T> {
pub fn new(stack: T) -> Self {
SharableStack {
stack: RefCell::new(stack),
}
}
pub fn acquire(&self) -> SharedStack<T> {
SharedStack { stack: &self.stack }
}
}
pub struct SharedStack<'a, T> {
stack: &'a RefCell<T>,
}
macro_rules! forward {
($func:ident($($v:ident: $IT:ty),*) -> $T:ty) => {
fn $func(&mut self, $($v: $IT),*) -> $T {
self.stack.borrow_mut().$func($($v),*)
}
}
}
impl<'a, T> UdpClientStack for SharedStack<'a, T>
where
T: UdpClientStack,
{
type Error = T::Error;
type UdpSocket = T::UdpSocket;
forward! {socket() -> Result<Self::UdpSocket, Self::Error>}
forward! {connect(socket: &mut Self::UdpSocket, address: SocketAddr) -> Result<(), Self::Error>}
forward! {send(socket: &mut Self::UdpSocket, data: &[u8]) -> Result<(), nb::Error<<T as UdpClientStack>::Error>>}
forward! {receive(socket: &mut Self::UdpSocket, data: &mut [u8]) -> Result<(usize, SocketAddr), nb::Error<<T as UdpClientStack>::Error>>}
forward! {close(socket: Self::UdpSocket) -> Result<(), Self::Error>}
}
impl<'a, T> UdpFullStack for SharedStack<'a, T>
where
T: UdpFullStack,
{
forward! {bind(socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error>}
forward! {send_to(socket: &mut Self::UdpSocket, remote: SocketAddr, buffer: &[u8]) -> Result<(), nb::Error<<T as UdpClientStack>::Error>>}
}
impl<'a, T> TcpClientStack for SharedStack<'a, T>
where
T: TcpClientStack,
{
type TcpSocket = T::TcpSocket;
type Error = T::Error;
forward! {socket() -> Result<Self::TcpSocket, Self::Error>}
forward! {connect(socket: &mut Self::TcpSocket, address: SocketAddr) -> Result<(), nb::Error<<T as TcpClientStack>::Error>>}
forward! {send(socket: &mut Self::TcpSocket, data: &[u8]) -> Result<usize, nb::Error<<T as TcpClientStack>::Error>>}
forward! {receive(socket: &mut Self::TcpSocket, data: &mut [u8]) -> Result<usize, nb::Error<<T as TcpClientStack>::Error>>}
forward! {is_connected(socket: &Self::TcpSocket) -> Result<bool, Self::Error>}
forward! {close(socket: Self::TcpSocket) -> Result<(), Self::Error>}
}
impl<'a, T> TcpFullStack for SharedStack<'a, T>
where
T: TcpFullStack,
{
forward! {bind(socket: &mut Self::TcpSocket, port: u16) -> Result<(), <T as TcpClientStack>::Error>}
forward! {listen(socket: &mut Self::TcpSocket) -> Result<(), <T as TcpClientStack>::Error>}
forward! {accept(socket: &mut Self::TcpSocket) -> Result<(<T as TcpClientStack>::TcpSocket, SocketAddr), nb::Error<<T as TcpClientStack>::Error>>}
}