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
use std::io;
use futures::Future;
use tokio_core::reactor::Handle;
use config::Config;
pub mod tcprelay;
pub mod udprelay;
pub mod local;
pub mod server;
mod loadbalancing;
mod dns_resolver;
pub mod socks5;
mod utils;
pub type BoxIoFuture<T> = Box<Future<Item = T, Error = io::Error>>;
fn boxed_future<T, E, F>(f: F) -> Box<Future<Item = T, Error = E>>
where F: Future<Item = T, Error = E> + 'static
{
Box::new(f)
}
scoped_thread_local!(static CONTEXT: Context);
pub struct Context {
handle: Handle,
config: Config,
}
impl Context {
#[doc(hidden)]
pub fn new(handle: Handle, config: Config) -> Context {
Context {
handle: handle,
config: config,
}
}
pub fn with<F, R>(f: F) -> R
where F: FnOnce(&Context) -> R
{
CONTEXT.with(f)
}
#[doc(hidden)]
pub fn set<F, R>(ctx: &Context, f: F) -> R
where F: FnOnce() -> R
{
CONTEXT.set(ctx, f)
}
pub fn handle(&self) -> &Handle {
&self.handle
}
pub fn config(&self) -> &Config {
&self.config
}
}