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
use std::convert::TryInto;
use std::fmt;
use crate::service::Service;
use crate::stream::RecvWriteStream;
lazy_static! {
static ref SERVICE: Service = Service::register("origin");
}
pub async fn accept() -> Result<RecvWriteStream, AcceptError> {
SERVICE
.call(&[], |reply: &[u8]| {
if reply.len() < 8 {
return Err(AcceptError::new(0));
}
let error = i16::from_le_bytes(reply[4..6].try_into().unwrap());
if error != 0 {
return Err(AcceptError::new(error));
}
Ok(SERVICE.stream(i32::from_le_bytes(reply[..4].try_into().unwrap())))
})
.await
}
#[derive(Debug)]
pub struct AcceptError {
code: i16,
}
impl AcceptError {
fn new(code: i16) -> Self {
Self { code }
}
pub fn as_i16(&self) -> i16 {
self.code
}
}
impl fmt::Display for AcceptError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.code.fmt(f)
}
}