11use anyhow:: Context as _;
2+ use crossbeam_channel:: { Receiver , Sender } ;
23use parking_lot:: RwLock ;
34use puffin:: { FrameSinkId , GlobalProfiler , ScopeCollection } ;
45use std:: {
@@ -23,6 +24,7 @@ pub struct Server {
2324 join_handle : Option < std:: thread:: JoinHandle < ( ) > > ,
2425 num_clients : Arc < AtomicUsize > ,
2526 sink_remove : fn ( FrameSinkId ) -> ( ) ,
27+ wait_client : Receiver < ( ) > ,
2628}
2729
2830impl Server {
@@ -247,6 +249,7 @@ impl Server {
247249 // but `crossbeam_channel` will continue until the channel is empty.
248250 let ( tx, rx) : ( crossbeam_channel:: Sender < Arc < puffin:: FrameData > > , _ ) =
249251 crossbeam_channel:: unbounded ( ) ;
252+ let ( client_connected, wait_client) = crossbeam_channel:: bounded ( 0 ) ;
250253
251254 let clients = Arc :: new ( RwLock :: new ( Vec :: new ( ) ) ) ;
252255 let num_clients = Arc :: new ( AtomicUsize :: default ( ) ) ;
@@ -275,12 +278,13 @@ impl Server {
275278 let _handle_accept = std:: thread:: Builder :: new ( )
276279 . name ( "pf-server-client" . to_owned ( ) )
277280 . spawn ( move || {
281+ let rdv_channel_client = client_connected;
278282 let ps_connection = PuffinServerConnection {
279283 tcp_listener,
280284 clients : clients. clone ( ) ,
281285 num_clients : num_clients_wait,
282286 } ;
283- if let Err ( err) = ps_connection. accept_new_clients ( ) {
287+ if let Err ( err) = ps_connection. accept_new_clients ( & rdv_channel_client ) {
284288 log:: warn!( "pf-server-client failure: {err}" ) ;
285289 }
286290 } )
@@ -296,13 +300,23 @@ impl Server {
296300 join_handle : Some ( join_handle) ,
297301 num_clients,
298302 sink_remove,
303+ wait_client,
299304 } )
300305 }
301306
302307 /// Number of clients currently connected.
303308 pub fn num_clients ( & self ) -> usize {
304309 self . num_clients . load ( Ordering :: SeqCst )
305310 }
311+
312+ /// Block thread to wait at least a puffin client.
313+ ///
314+ /// # Errors
315+ /// Return error from channel.
316+ pub fn wait_client ( & self ) -> Result < ( ) , crossbeam_channel:: RecvError > {
317+ log:: info!( "Wait puffin_http client" ) ;
318+ self . wait_client . recv ( )
319+ }
306320}
307321
308322impl Drop for Server {
@@ -346,7 +360,7 @@ struct PuffinServerConnection {
346360 num_clients : Arc < AtomicUsize > ,
347361}
348362impl PuffinServerConnection {
349- fn accept_new_clients ( & self ) -> anyhow:: Result < ( ) > {
363+ fn accept_new_clients ( & self , rdv_channel : & Sender < ( ) > ) -> anyhow:: Result < ( ) > {
350364 loop {
351365 match self . tcp_listener . accept ( ) {
352366 Ok ( ( tcp_stream, client_addr) ) => {
@@ -373,6 +387,7 @@ impl PuffinServerConnection {
373387 } ) ;
374388 self . num_clients
375389 . store ( self . clients . read ( ) . len ( ) , Ordering :: SeqCst ) ;
390+ rdv_channel. send ( ( ) ) ?;
376391 }
377392 Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: WouldBlock => {
378393 // Nothing to do for now. Continue looping
0 commit comments