1#![allow(unused_variables)]
2#[cfg(unix)]
3use std::os::unix::net::UnixStream as OsUnixStream;
4use std::{io::Result as IoResult, net, net::SocketAddr, path::PathBuf};
5
6use ntex_io::Io;
7use ntex_service::cfg::SharedCfg;
8
9#[doc(hidden)]
10pub mod internal {
11 pub use tok_io::*;
12}
13
14use crate::channel::{self, Receiver};
15
16mod io;
17
18#[doc(hidden)]
19#[allow(deprecated)]
20pub use self::io::TokioIoBoxed;
21
22pub(crate) struct TcpStream(tok_io::net::TcpStream);
23
24#[cfg(unix)]
25pub(crate) struct UnixStream(tok_io::net::UnixStream);
26
27#[derive(Copy, Clone, Debug)]
29pub struct Reactor;
30
31pub(crate) fn block_on<F: Future<Output = ()>>(fut: F) {
34 if let Ok(hnd) = tok_io::runtime::Handle::try_current() {
35 log::debug!("Use existing tokio runtime and block on future");
36 hnd.block_on(tok_io::task::LocalSet::new().run_until(fut));
37 } else {
38 log::debug!("Create tokio runtime and block on future");
39
40 let rt = tok_io::runtime::Builder::new_current_thread()
41 .enable_all()
42 .build()
43 .unwrap();
44
45 tok_io::task::LocalSet::new().block_on(&rt, fut);
46 }
47}
48
49impl ntex_rt::Driver for Reactor {
50 fn run(&self, _: &ntex_rt::Runtime) -> std::io::Result<()> {
51 panic!("Not supported")
52 }
53
54 fn handle(&self) -> Box<dyn ntex_rt::Notify> {
55 panic!("Not supported")
56 }
57
58 fn clear(&self) {}
59}
60
61impl crate::Reactor for Reactor {
62 fn tcp_connect(&self, addr: SocketAddr, cfg: SharedCfg) -> Receiver<Io> {
63 let (tx, rx) = channel::create();
64 ntex_rt::spawn_detached(async move {
65 let result = async {
66 let sock = tok_io::net::TcpStream::connect(addr).await?;
67 sock.set_nodelay(true)?;
68 Ok(Io::new(TcpStream(sock), cfg))
69 }
70 .await;
71 let _ = tx.send(result);
72 });
73
74 rx
75 }
76
77 fn unix_connect(&self, addr: PathBuf, cfg: SharedCfg) -> Receiver<Io> {
78 #[cfg(unix)]
79 {
80 let (tx, rx) = channel::create();
81 ntex_rt::spawn_detached(async move {
82 let result = async {
83 let sock = tok_io::net::UnixStream::connect(addr).await?;
84 Ok(Io::new(UnixStream(sock), cfg))
85 }
86 .await;
87 let _ = tx.send(result);
88 });
89
90 rx
91 }
92
93 #[cfg(not(unix))]
94 {
95 Receiver::new(Err(std::io::Error::other(
96 "Unix domain sockets are not supported",
97 )))
98 }
99 }
100
101 fn from_tcp_stream(&self, stream: net::TcpStream, cfg: SharedCfg) -> IoResult<Io> {
102 stream.set_nonblocking(true)?;
103 Ok(Io::new(
104 TcpStream(tok_io::net::TcpStream::from_std(stream)?),
105 cfg,
106 ))
107 }
108
109 #[cfg(unix)]
110 fn from_unix_stream(&self, stream: OsUnixStream, cfg: SharedCfg) -> IoResult<Io> {
111 stream.set_nonblocking(true)?;
112 Ok(Io::new(
113 UnixStream(tok_io::net::UnixStream::from_std(stream)?),
114 cfg,
115 ))
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use std::time::Duration;
122
123 use ntex_rt::System;
124
125 use crate::DefaultRuntime;
126
127 #[test]
133 fn busy_duration() {
134 let sleep = Duration::from_millis(20);
135 let hnd = System::new("test", DefaultRuntime).block_on(async move {
136 let hnd = System::current().handle();
137 ntex_rt::spawn(async move { std::thread::sleep(sleep) })
138 .await
139 .unwrap();
140 hnd
141 });
142 assert!(hnd.busy_duration().unwrap() >= sleep);
143 }
144}