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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{
builders,
errors::{RpcError, RpcResult},
io::MsgReader,
};
use async_std::{future, sync::Arc, task};
use capnp::traits::FromPointerReader;
use socket2::{Domain, Type};
use std::{
future::Future,
io::Result,
path::{Path, PathBuf},
sync::atomic::{AtomicBool, Ordering},
thread,
time::Duration,
};
pub use socket2::{SockAddr, Socket};
pub fn default_socket_path() -> PathBuf {
PathBuf::from("/run/user/1000/qrpc.socket")
}
pub struct RpcSocket {
inner: Socket,
_addr: SockAddr,
run: AtomicBool,
listening: AtomicBool,
timeout: Duration,
}
impl RpcSocket {
fn new_socket<P: AsRef<Path>>(path: P) -> Result<(Socket, SockAddr)> {
let addr = SockAddr::unix(path)?;
let socket = Socket::new(
Domain::unix(),
Type::seqpacket(),
None,
)?;
Ok((socket, addr))
}
pub fn new<P: AsRef<Path>>(path: P) -> Result<Arc<Self>> {
Self::with_duration(path, Duration::from_secs(5))
}
pub fn create<P, F>(path: P, handle_connection: F) -> Result<Arc<Self>>
where
P: AsRef<Path>,
F: Fn(Socket, SockAddr) + Send + Sync + 'static,
{
let (inner, _addr) = Self::new_socket(path)?;
inner.bind(&_addr)?;
inner.listen(32)?;
let arc = Arc::new(Self {
inner,
_addr,
timeout: Duration::from_secs(5),
run: AtomicBool::from(true),
listening: AtomicBool::from(true),
});
let arc2 = Arc::clone(&arc);
thread::spawn(move || {
while let Ok((sock, addr)) = arc2.inner.accept() {
handle_connection(sock, addr);
}
});
Ok(arc)
}
pub fn with_duration<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<Arc<Self>> {
let (inner, _addr) = Self::new_socket(path)?;
inner.connect(&_addr)?;
Ok(Arc::new(Self {
inner,
_addr,
timeout,
run: AtomicBool::from(true),
listening: AtomicBool::from(false),
}))
}
pub fn listen<F>(self: &Arc<Self>, fut: F)
where
F: Future<Output = ()> + Send + 'static,
{
self.listening.fetch_or(true, Ordering::Relaxed);
task::spawn(fut);
}
pub async fn send_msg<'s, F: 'static, T, S, M: 's>(
self: &'s Arc<Self>,
target: S,
msg: Vec<u8>,
handle: F,
) -> RpcResult<T>
where
F: Fn(MsgReader<'s, M>) -> RpcResult<T> + Send,
S: Into<String>,
M: FromPointerReader<'s>,
{
let msg = builders::_internal::to(target.into(), msg);
self.inner.send(&msg).unwrap();
let _self = Arc::clone(self);
self.with_timeout(async move {
let (_, buf) = builders::_internal::from(&_self.inner);
MsgReader::new(buf).map(|ok| handle(ok))
})
.await?
.map_err(|_| RpcError::Other("Serialisation failure!".into()))?
}
pub fn running(&self) -> bool {
self.run.load(Ordering::Relaxed)
}
pub fn listening(&self) -> bool {
self.listening.load(Ordering::Relaxed)
}
async fn with_timeout<T, F>(&self, fut: F) -> RpcResult<T>
where
F: Future<Output = T> + Send + 'static,
{
future::timeout(self.timeout.clone(), fut)
.await
.map_err(|_| RpcError::Timeout)
}
}