emissary_core/runtime/
mod.rs1use futures::Stream;
22use rand_core::{CryptoRng, RngCore};
23
24use alloc::{boxed::Box, string::String, vec::Vec};
25use core::{
26 fmt,
27 future::Future,
28 net::SocketAddr,
29 pin::Pin,
30 task::{Context, Poll},
31 time::Duration,
32};
33
34#[cfg(test)]
35pub mod mock;
36#[cfg(test)]
37pub mod noop;
38
39pub trait AsyncRead {
40 fn poll_read(
41 self: Pin<&mut Self>,
42 cx: &mut Context<'_>,
43 buf: &mut [u8],
44 ) -> Poll<crate::Result<usize>>;
45}
46
47pub trait AsyncWrite {
48 fn poll_write(
49 self: Pin<&mut Self>,
50 cx: &mut Context<'_>,
51 buf: &[u8],
52 ) -> Poll<crate::Result<usize>>;
53 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<crate::Result<()>>;
54 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<crate::Result<()>>;
55}
56
57pub trait TcpStream: AsyncRead + AsyncWrite + Unpin + Send + Sync + Sized + 'static {
58 fn connect(address: SocketAddr) -> impl Future<Output = Option<Self>> + Send;
60}
61
62pub trait TcpListener<TcpStream>: Unpin + Send + Sized + 'static {
63 fn bind(address: SocketAddr) -> impl Future<Output = Option<Self>>;
64 fn poll_accept(&mut self, cx: &mut Context<'_>) -> Poll<Option<(TcpStream, SocketAddr)>>;
65 fn local_address(&self) -> Option<SocketAddr>;
66}
67
68pub trait UdpSocket: Unpin + Send + Sized + Clone {
69 fn bind(address: SocketAddr) -> impl Future<Output = Option<Self>>;
70 fn send_to(
71 &mut self,
72 buf: &[u8],
73 target: SocketAddr,
74 ) -> impl Future<Output = Option<usize>> + Send;
75 fn recv_from(
76 &mut self,
77 buf: &mut [u8],
78 ) -> impl Future<Output = Option<(usize, SocketAddr)>> + Send;
79 fn local_address(&self) -> Option<SocketAddr>;
80}
81
82pub trait JoinSet<T>: Stream<Item = T> + Unpin + Send {
83 fn is_empty(&self) -> bool;
85
86 fn len(&self) -> usize;
88
89 fn push<F>(&mut self, future: F)
91 where
92 F: Future<Output = T> + Send + 'static,
93 F::Output: Send;
94}
95
96pub trait Instant: fmt::Debug + Copy + Clone + Send + Unpin + Sync {
97 fn elapsed(&self) -> Duration;
99}
100
101pub trait Counter {
102 fn increment(&mut self, value: usize);
103}
104
105pub trait Gauge {
106 fn increment(&mut self, value: usize);
107 fn decrement(&mut self, value: usize);
108}
109
110pub trait Histogram {
111 fn record(&mut self, record: f64);
112}
113
114pub trait MetricsHandle: Clone + Send + Sync + Unpin {
115 fn counter(&self, name: &'static str) -> impl Counter;
116 fn gauge(&self, name: &'static str) -> impl Gauge;
117 fn histogram(&self, name: &'static str) -> impl Histogram;
118}
119
120pub enum MetricType {
122 Counter {
124 name: &'static str,
126
127 description: &'static str,
129 },
130
131 Gauge {
133 name: &'static str,
135
136 description: &'static str,
138 },
139
140 Histogram {
142 name: &'static str,
144
145 description: &'static str,
147
148 buckets: Vec<f64>,
150 },
151}
152
153pub trait Runtime: Clone + Unpin + Send + 'static {
154 type TcpStream: TcpStream;
155 type UdpSocket: UdpSocket;
156 type TcpListener: TcpListener<Self::TcpStream>;
157 type JoinSet<T: Send + 'static>: JoinSet<T>;
158 type MetricsHandle: MetricsHandle;
159 type Instant: Instant;
160 type Timer: Future<Output = ()> + Send + Unpin;
161
162 fn spawn<F>(future: F)
164 where
165 F: Future + Send + 'static,
166 F::Output: Send;
167
168 fn time_since_epoch() -> Duration;
170
171 fn now() -> Self::Instant;
173
174 fn rng() -> impl RngCore + CryptoRng;
176
177 fn join_set<T: Send + 'static>() -> Self::JoinSet<T>;
183
184 fn register_metrics(metrics: Vec<MetricType>, port: Option<u16>) -> Self::MetricsHandle;
189
190 fn timer(duration: Duration) -> Self::Timer;
192
193 fn delay(duration: Duration) -> impl Future<Output = ()> + Send;
195
196 fn gzip_compress(bytes: impl AsRef<[u8]>) -> Option<Vec<u8>>;
198
199 fn gzip_decompress(bytes: impl AsRef<[u8]>) -> Option<Vec<u8>>;
201}
202
203pub trait AddressBook: Unpin + Send + Sync + 'static {
204 fn resolve_base64(&self, host: String) -> Pin<Box<dyn Future<Output = Option<String>> + Send>>;
206
207 fn resolve_base32(&self, host: &str) -> Option<String>;
209}
210
211pub trait Storage: Unpin + Send + Sync + 'static {
212 fn save_to_disk(&self, routers: Vec<(String, Option<Vec<u8>>, crate::Profile)>);
214}