11use crate :: prelude:: * ;
22use anyhow:: Context ;
3+ use futures:: StreamExt ;
34use nix:: { sys:: time:: TimeValLike , time:: clock_gettime} ;
45use runner_shared:: artifacts:: ExecutionTimestamps ;
56use runner_shared:: fifo:: { Command as FifoCommand , MarkerType } ;
67use runner_shared:: fifo:: { RUNNER_ACK_FIFO , RUNNER_CTL_FIFO } ;
78use std:: cmp:: Ordering ;
89use std:: path:: { Path , PathBuf } ;
910use std:: { collections:: HashSet , time:: Duration } ;
10- use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
11+ use tokio:: io:: AsyncWriteExt ;
1112use tokio:: net:: unix:: pid_t;
1213use tokio:: net:: unix:: pipe:: OpenOptions as TokioPipeOpenOptions ;
1314use tokio:: net:: unix:: pipe:: Receiver as TokioPipeReader ;
1415use tokio:: net:: unix:: pipe:: Sender as TokioPipeSender ;
16+ use tokio_util:: codec:: { FramedRead , LengthDelimitedCodec } ;
1517
1618fn create_fifo < P : AsRef < std:: path:: Path > > ( path : P ) -> anyhow:: Result < ( ) > {
1719 // Remove the previous FIFO (if it exists)
@@ -71,7 +73,7 @@ pub struct FifoBenchmarkData {
7173
7274pub struct RunnerFifo {
7375 ack_fifo : TokioPipeSender ,
74- ctl_fifo : TokioPipeReader ,
76+ ctl_reader : FramedRead < TokioPipeReader , LengthDelimitedCodec > ,
7577}
7678
7779fn get_pipe_open_options ( ) -> TokioPipeOpenOptions {
@@ -94,24 +96,27 @@ impl RunnerFifo {
9496 let ack_fifo = get_pipe_open_options ( ) . open_sender ( ack_path) ?;
9597 let ctl_fifo = get_pipe_open_options ( ) . open_receiver ( ctl_path) ?;
9698
97- Ok ( Self { ctl_fifo, ack_fifo } )
99+ let codec = LengthDelimitedCodec :: builder ( )
100+ . length_field_length ( 4 )
101+ . little_endian ( )
102+ . new_codec ( ) ;
103+ let ctl_reader = FramedRead :: new ( ctl_fifo, codec) ;
104+
105+ Ok ( Self {
106+ ack_fifo,
107+ ctl_reader,
108+ } )
98109 }
99110
100111 pub async fn recv_cmd ( & mut self ) -> anyhow:: Result < FifoCommand > {
101- let mut len_buffer = [ 0u8 ; 4 ] ;
102- self . ctl_fifo . read_exact ( & mut len_buffer) . await ?;
103- let message_len = u32:: from_le_bytes ( len_buffer) as usize ;
104-
105- let mut buffer = vec ! [ 0u8 ; message_len] ;
106- loop {
107- if self . ctl_fifo . read_exact ( & mut buffer) . await . is_ok ( ) {
108- break ;
109- }
110- }
111-
112- let decoded = bincode:: deserialize ( & buffer) . with_context ( || {
113- format ! ( "Failed to deserialize FIFO command (len: {message_len}, data: {buffer:?})" )
114- } ) ?;
112+ let bytes = self
113+ . ctl_reader
114+ . next ( )
115+ . await
116+ . ok_or_else ( || anyhow:: anyhow!( "FIFO stream closed" ) ) ??;
117+
118+ let decoded = bincode:: deserialize ( & bytes)
119+ . with_context ( || format ! ( "Failed to deserialize FIFO command (data: {bytes:?})" ) ) ?;
115120 Ok ( decoded)
116121 }
117122
0 commit comments