mpstthree/transport/tcp/
fork.rs1use crate::binary::struct_trait::session::Session;
9use std::error::Error;
10use std::marker;
11use std::net::TcpStream;
12use std::panic;
13use std::thread::{Builder, JoinHandle};
14
15type TcpFork<T> = Result<(JoinHandle<()>, T, TcpStream), Box<dyn Error>>;
16
17#[cfg_attr(
25 doc_cfg,
26 doc(cfg(any(feature = "transport", feature = "transport_tcp")))
27)]
28pub fn fork_tcp<S, P>(p: P, address: &str) -> TcpFork<S::Dual>
29where
30 S: Session + 'static,
31 P: FnOnce(S, TcpStream) -> Result<(), Box<dyn Error>> + marker::Send + 'static,
32{
33 let stream = TcpStream::connect(address)?;
34 let copy_stream = stream.try_clone()?;
35 let (there, here) = Session::new();
36 let other_thread = Builder::new()
37 .name(String::from(address))
38 .stack_size(64 * 1024 * 1024)
39 .spawn(move || {
40 panic::set_hook(Box::new(|_info| {
41 }));
43 match p(there, copy_stream) {
44 Ok(()) => (),
45 Err(e) => panic!("{}", e.to_string()),
46 }
47 })?;
48 Ok((other_thread, here, stream))
49}