Simplify code

This commit is contained in:
johannesd3 2021-02-13 15:38:05 +01:00 committed by Johannesd3
parent 27f308b82f
commit f9c0e26f6d

View file

@ -449,7 +449,7 @@ enum ReceivedData {
async fn audio_file_fetch_receive_data( async fn audio_file_fetch_receive_data(
shared: Arc<AudioFileShared>, shared: Arc<AudioFileShared>,
file_data_tx: mpsc::UnboundedSender<ReceivedData>, file_data_tx: mpsc::UnboundedSender<ReceivedData>,
data_rx: ChannelData, mut data_rx: ChannelData,
initial_data_offset: usize, initial_data_offset: usize,
initial_request_length: usize, initial_request_length: usize,
request_sent_time: Instant, request_sent_time: Instant,
@ -465,49 +465,44 @@ async fn audio_file_fetch_receive_data(
.number_of_open_requests .number_of_open_requests
.fetch_add(1, atomic::Ordering::SeqCst); .fetch_add(1, atomic::Ordering::SeqCst);
enum TryFoldErr { let result = loop {
ChannelError, let data = match data_rx.next().await {
FinishEarly, Some(Ok(data)) => data,
} Some(Err(e)) => break Err(e),
None => break Ok(()),
};
let result = data_rx
.map_err(|_| TryFoldErr::ChannelError)
.try_for_each(|data| {
if measure_ping_time { if measure_ping_time {
let duration = Instant::now() - request_sent_time; let duration = Instant::now() - request_sent_time;
let duration_ms: u64; let duration_ms: u64;
if 0.001 * (duration.as_millis() as f64) if 0.001 * (duration.as_millis() as f64) > MAXIMUM_ASSUMED_PING_TIME_SECONDS {
> MAXIMUM_ASSUMED_PING_TIME_SECONDS
{
duration_ms = (MAXIMUM_ASSUMED_PING_TIME_SECONDS * 1000.0) as u64; duration_ms = (MAXIMUM_ASSUMED_PING_TIME_SECONDS * 1000.0) as u64;
} else { } else {
duration_ms = duration.as_millis() as u64; duration_ms = duration.as_millis() as u64;
} }
let _ = file_data_tx let _ = file_data_tx.send(ReceivedData::ResponseTimeMs(duration_ms as usize));
.send(ReceivedData::ResponseTimeMs(duration_ms as usize));
measure_ping_time = false; measure_ping_time = false;
} }
let data_size = data.len(); let data_size = data.len();
let _ = file_data_tx let _ = file_data_tx.send(ReceivedData::Data(PartialFileData {
.send(ReceivedData::Data(PartialFileData {
offset: data_offset, offset: data_offset,
data: data, data: data,
})); }));
data_offset += data_size; data_offset += data_size;
if request_length < data_size { if request_length < data_size {
warn!("Data receiver for range {} (+{}) received more data from server than requested.", initial_data_offset, initial_request_length); warn!(
"Data receiver for range {} (+{}) received more data from server than requested.",
initial_data_offset, initial_request_length
);
request_length = 0; request_length = 0;
} else { } else {
request_length -= data_size; request_length -= data_size;
} }
future::ready(if request_length == 0 { if request_length == 0 {
Err(TryFoldErr::FinishEarly) break Ok(());
} else { }
Ok(()) };
})
})
.await;
if request_length > 0 { if request_length > 0 {
let missing_range = Range::new(data_offset, request_length); let missing_range = Range::new(data_offset, request_length);
@ -521,7 +516,7 @@ async fn audio_file_fetch_receive_data(
.number_of_open_requests .number_of_open_requests
.fetch_sub(1, atomic::Ordering::SeqCst); .fetch_sub(1, atomic::Ordering::SeqCst);
if let Err(TryFoldErr::ChannelError) = result { if result.is_err() {
warn!( warn!(
"Error from channel for data receiver for range {} (+{}).", "Error from channel for data receiver for range {} (+{}).",
initial_data_offset, initial_request_length initial_data_offset, initial_request_length