fn start_thread<B: BlockT + 'static>(
	protocol_sender: Sender<FromNetworkMsg<B>>,
	network_port: NetworkPort<B>,
	config: NetworkConfiguration,
	registered: RegisteredProtocol<Message<B>>,
) -> Result<((oneshot::Sender<()>, thread::JoinHandle<()>), Arc<Mutex<NetworkService<Message<B>>>>, PeersetHandle), Error> {
	// Start the main service.
	let (service, peerset) = match start_service(config, registered) {
		Ok((service, peerset)) => (Arc::new(Mutex::new(service)), peerset),
		Err(err) => {
			warn!("Error starting network: {}", err);
			return Err(err.into())
		},
	};

	let (close_tx, close_rx) = oneshot::channel();
	let service_clone = service.clone();
	let mut runtime = RuntimeBuilder::new().name_prefix("libp2p-").build()?;
	let peerset_clone = peerset.clone();
	let thread = thread::Builder::new().name("network".to_string()).spawn(move || {
		let fut = run_thread(protocol_sender, service_clone, network_port, peerset_clone)
			.select(close_rx.then(|_| Ok(())))
			.map(|(val, _)| val)
			.map_err(|(err,_ )| err);

		// Note that we use `block_on` and not `block_on_all` because we want to kill the thread
		// instantly if `close_rx` receives something.
		match runtime.block_on(fut) {
			Ok(()) => debug!(target: "sub-libp2p", "Networking thread finished"),
			Err(err) => error!(target: "sub-libp2p", "Error while running libp2p: {:?}", err),
		};
	})?;

	Ok(((close_tx, thread), service, peerset))
}
/// Starts the background thread that handles the networking.
Starts the background thread that handles the networking.
Starts the background thread that handles the networking .
fn start_thread<B: BlockT + 'static>(
	protocol_sender: Sender<FromNetworkMsg<B>>,
	network_port: NetworkPort<B>,
	config: NetworkConfiguration,
	registered: RegisteredProtocol<Message<B>>,
) -> Result<((oneshot::Sender<()>, thread::JoinHandle<()>), Arc<Mutex<NetworkService<Message<B>>>>, PeersetHandle), Error> {
	// Start the main service.
	let (service, peerset) = match start_service(config, registered) {
		Ok((service, peerset)) => (Arc::new(Mutex::new(service)), peerset),
		Err(err) => {
			warn!("Error starting network: {}", err);
			return Err(err.into())
		},
	};
	let (close_tx, close_rx) = oneshot::channel();
	let service_clone = service.clone();
	let mut runtime = RuntimeBuilder::new().name_prefix("libp2p-").build()?;
	let peerset_clone = peerset.clone();
	let thread = thread::Builder::new().name("network".to_string()).spawn(move || {
		let fut = run_thread(protocol_sender, service_clone, network_port, peerset_clone)
			.select(close_rx.then(|_| Ok(())))
			.map(|(val, _)| val)
			.map_err(|(err,_ )| err);
		// Note that we use `block_on` and not `block_on_all` because we want to kill the thread
		// instantly if `close_rx` receives something.
		match runtime.block_on(fut) {
			Ok(()) => debug!(target: "sub-libp2p", "Networking thread finished"),
			Err(err) => error!(target: "sub-libp2p", "Error while running libp2p: {:?}", err),
		};
	})?;
	Ok(((close_tx, thread), service, peerset))
}
fn start_thread < B : BlockT + ' static > ( protocol_sender : Sender < FromNetworkMsg < B > > , network_port : NetworkPort < B > , config : NetworkConfiguration , registered : RegisteredProtocol < Message < B > > , ) -> Result < ( ( oneshot :: Sender < ( ) > , thread :: JoinHandle < ( ) > ) , Arc < Mutex < NetworkService < Message < B > > > > , PeersetHandle ) , Error > { // Start the main service. let ( service , peerset ) = match start_service ( config , registered ) { Ok ( ( service , peerset ) ) => ( Arc :: new ( Mutex :: new ( service ) ) , peerset ) , Err ( err ) => { warn ! ( " " err ) ; return Err ( err . into ( ) ) } , } ; let ( close_tx , close_rx ) = oneshot :: channel ( ) ; let service_clone = service . clone ( ) ; let mut runtime = RuntimeBuilder :: new ( ) . name_prefix ( " " ) . build ( ) ? ; let peerset_clone = peerset . clone ( ) ; let thread = thread :: Builder :: new ( ) . name ( " " . to_string ( ) ) . spawn ( move | | { let fut = run_thread ( protocol_sender , service_clone , network_port , peerset_clone ) . select ( close_rx . then ( | _ | Ok ( ( ) ) ) ) . map ( | ( val , _ ) | val ) . map_err ( | ( err , _ ) | err ) ; // Note that we use `block_on` and not `block_on_all` because we want to kill the thread // instantly if `close_rx` receives something. match runtime . block_on ( fut ) { Ok ( ( ) ) => debug ! ( target " " " " ) , Err ( err ) => error ! ( target " " " " err ) , } ; } ) ? ; Ok ( ( ( close_tx , thread ) , service , peerset ) ) }
Starts the background thread that handles the networking.
Starts the background thread that handles the networking .
protocol_sender
Sender<FromNetworkMsg<B>>
network_port
NetworkPort<B>
config
NetworkConfiguration
registered
RegisteredProtocol<Message<B>>
protocol_sender
Sender<FromNetworkMsg<B>>
network_port
NetworkPort<B>
config
NetworkConfiguration
registered
RegisteredProtocol<Message<B>>