Skip to content

Commit

Permalink
chore: Improve status code checks
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Jun 28, 2024
1 parent bdd60d1 commit f4b7337
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/internal_modules/roc_audio/fanout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "roc_audio/fanout.h"
#include "roc_audio/sample_spec_to_str.h"
#include "roc_core/panic.h"
#include "roc_status/code_to_str.h"

namespace roc {
namespace audio {
Expand Down Expand Up @@ -53,7 +54,14 @@ status::StatusCode Fanout::write(Frame& in_frame) {
for (IFrameWriter* writer = frame_writers_.front(); writer != NULL;
writer = frame_writers_.nextof(*writer)) {
const status::StatusCode code = writer->write(in_frame);

if (code != status::StatusOK) {
// These codes can be returned only from read().
roc_panic_if_msg(
code == status::StatusPart || code == status::StatusDrain,
"fanout loop: unexpected status from write operation: status=%s",
status::code_to_str(code));

return code;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_pipeline/receiver_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ status::StatusCode ReceiverLoop::read(audio::Frame& frame,
// invokes process_subframe_imp() and process_task_imp()
const status::StatusCode code = process_subframes_and_tasks(frame, duration, mode);

roc_panic_if_msg(code <= status::NoStatus || code >= status::MaxStatus,
"receiver loop: invalid status code %d", code);

if (code != status::StatusOK && code != status::StatusPart) {
return code;
}
Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_pipeline/receiver_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ status::StatusCode ReceiverSession::read(audio::Frame& frame,

const status::StatusCode code = frame_reader_->read(frame, duration, mode);

roc_panic_if_msg(code <= status::NoStatus || code >= status::MaxStatus,
"receiver session: invalid status code %d", code);

// Failure happened. Remember error to return it from next refresh() call.
// Return StatusEnd to be excluded from mixing.
// We don't return error from read() because we don't want the whole
Expand Down
12 changes: 11 additions & 1 deletion src/internal_modules/roc_pipeline/receiver_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ status::StatusCode ReceiverSource::refresh(core::nanoseconds_t current_time,

const status::StatusCode code = slot->refresh(current_time, slot_deadline);
if (code != status::StatusOK) {
roc_log(LogError, "receiver source: failed to refresh slot: status=%s",
status::code_to_str(code));
return code;
}

Expand Down Expand Up @@ -206,7 +208,15 @@ status::StatusCode ReceiverSource::read(audio::Frame& frame,
audio::FrameReadMode mode) {
roc_panic_if(init_status_ != status::StatusOK);

return frame_reader_->read(frame, duration, mode);
const status::StatusCode code = frame_reader_->read(frame, duration, mode);

if (code != status::StatusOK && code != status::StatusPart
&& code != status::StatusDrain) {
roc_log(LogError, "receiver source: failed to read frame: status=%s",
status::code_to_str(code));
}

return code;
}

} // namespace pipeline
Expand Down
8 changes: 7 additions & 1 deletion src/internal_modules/roc_pipeline/sender_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@ status::StatusCode SenderLoop::write(audio::Frame& frame) {
}

// invokes process_subframe_imp() and process_task_imp()
return process_subframes_and_tasks(frame, frame.duration(), audio::ModeHard);
const status::StatusCode code =
process_subframes_and_tasks(frame, frame.duration(), audio::ModeHard);

roc_panic_if_msg(code <= status::NoStatus || code >= status::MaxStatus,
"sender loop: invalid status code %d", code);

return code;
}

core::nanoseconds_t SenderLoop::timestamp_imp() const {
Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_pipeline/sender_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ status::StatusCode SenderSession::write(audio::Frame& frame) {

const status::StatusCode code = frame_writer_->write(frame);

roc_panic_if_msg(code <= status::NoStatus || code >= status::MaxStatus,
"sender session: invalid status code %d", code);

// If error happens, save it to return later from refresh(), which allows
// SenderSlot to handle it.
if (code != status::StatusOK) {
Expand Down
11 changes: 10 additions & 1 deletion src/internal_modules/roc_pipeline/sender_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ status::StatusCode SenderSink::refresh(core::nanoseconds_t current_time,

const status::StatusCode code = slot->refresh(current_time, slot_deadline);
if (code != status::StatusOK) {
roc_log(LogError, "sender sink: failed to refresh slot: status=%s",
status::code_to_str(code));
return code;
}

Expand Down Expand Up @@ -186,7 +188,14 @@ bool SenderSink::has_clock() const {
status::StatusCode SenderSink::write(audio::Frame& frame) {
roc_panic_if(init_status_ != status::StatusOK);

return frame_writer_->write(frame);
const status::StatusCode code = frame_writer_->write(frame);

if (code != status::StatusOK) {
roc_log(LogError, "sender sink: failed to write frame: status=%s",
status::code_to_str(code));
}

return code;
}

} // namespace pipeline
Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_sndio/pump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ status::StatusCode Pump::run() {
}
}

roc_panic_if_msg(code <= status::NoStatus || code >= status::MaxStatus,
"pump: invalid status code %d", code);

if (code == status::StatusEnd) {
code = status::StatusOK; // EOF is fine
}
Expand Down
1 change: 1 addition & 0 deletions src/internal_modules/roc_status/code_to_str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace status {
const char* code_to_str(StatusCode code) {
switch (code) {
case NoStatus:
case MaxStatus:
break;
case StatusOK:
return "OK";
Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_status/status_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ enum StatusCode {
//! Example: trying to push packet for an interface that does not support
//! it, trying to connect using a protocol that doesn't support it.
StatusBadOperation,

//! Maximum enum value.
MaxStatus,
};

} // namespace status
Expand Down

0 comments on commit f4b7337

Please sign in to comment.