use crate::binary::struct_trait::session::Session;
use std::error::Error;
use std::marker;
use std::net::TcpStream;
use std::panic;
use std::thread::{Builder, JoinHandle};
type TcpFork<T> = Result<(JoinHandle<()>, T, TcpStream), Box<dyn Error>>;
#[cfg_attr(
doc_cfg,
doc(cfg(any(feature = "transport", feature = "transport_tcp")))
)]
pub fn fork_tcp<S, P>(p: P, address: &str) -> TcpFork<S::Dual>
where
S: Session + 'static,
P: FnOnce(S, TcpStream) -> Result<(), Box<dyn Error>> + marker::Send + 'static,
{
let stream = TcpStream::connect(address)?;
let copy_stream = stream.try_clone()?;
let (there, here) = Session::new();
let other_thread = Builder::new()
.name(String::from(address))
.stack_size(64 * 1024 * 1024)
.spawn(move || {
panic::set_hook(Box::new(|_info| {
}));
match p(there, copy_stream) {
Ok(()) => (),
Err(e) => panic!("{}", e.to_string()),
}
})?;
Ok((other_thread, here, stream))
}