Skip to content

Commit 0c089bd

Browse files
committed
Factor out duplicated code in data_receiver
1 parent 6349f7a commit 0c089bd

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/data_receiver.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void data_receiver::close_stream() {
7878
cancel_all_registered();
7979
}
8080

81-
template <class T>
82-
double data_receiver::pull_sample_typed(T *buffer, uint32_t buffer_elements, double timeout) {
81+
sample_p lsl::data_receiver::try_get_next_sample(double timeout)
82+
{
8383
if (conn_.lost())
8484
throw lost_error("The stream read by this outlet has been lost. To recover, you need to "
8585
"re-resolve the source and re-create the inlet.");
@@ -89,18 +89,25 @@ double data_receiver::pull_sample_typed(T *buffer, uint32_t buffer_elements, dou
8989
check_thread_start_ = false;
9090
}
9191
// get the sample with timeout
92-
if (sample_p s = sample_queue_.pop_sample(timeout)) {
92+
if (sample_p s = sample_queue_.pop_sample(timeout))
93+
return s;
94+
else if (conn_.lost())
95+
throw lost_error("The stream read by this inlet has been lost. To recover, you need to "
96+
"re-resolve the source and re-create the inlet.");
97+
return nullptr;
98+
}
99+
100+
101+
template <class T>
102+
double data_receiver::pull_sample_typed(T *buffer, uint32_t buffer_elements, double timeout) {
103+
if(sample_p s = try_get_next_sample(timeout))
104+
{
93105
if (buffer_elements != conn_.type_info().channel_count())
94106
throw std::range_error("The number of buffer elements provided does not match the "
95107
"number of channels in the sample.");
96108
s->retrieve_typed(buffer);
97109
return s->timestamp;
98-
} else {
99-
if (conn_.lost())
100-
throw lost_error("The stream read by this inlet has been lost. To recover, you need to "
101-
"re-resolve the source and re-create the inlet.");
102-
return 0.0;
103-
}
110+
} else return 0.0;
104111
}
105112

106113
template double data_receiver::pull_sample_typed<char>(char *, uint32_t, double);
@@ -112,27 +119,14 @@ template double data_receiver::pull_sample_typed<double>(double *, uint32_t, dou
112119
template double data_receiver::pull_sample_typed<std::string>(std::string *, uint32_t, double);
113120

114121
double data_receiver::pull_sample_untyped(void *buffer, int buffer_bytes, double timeout) {
115-
if (conn_.lost())
116-
throw lost_error("The stream read by this inlet has been lost. To recover, you need to "
117-
"re-resolve the source and re-create the inlet.");
118-
// start data thread implicitly if necessary
119-
if (check_thread_start_ && !data_thread_.joinable()) {
120-
data_thread_ = std::thread(&data_receiver::data_thread, this);
121-
check_thread_start_ = false;
122-
}
123-
// get the sample with timeout
124-
if (sample_p s = sample_queue_.pop_sample(timeout)) {
122+
if(sample_p s = try_get_next_sample(timeout)) {
125123
if (buffer_bytes != conn_.type_info().sample_bytes())
126124
throw std::range_error("The size of the provided buffer does not match the number of "
127125
"bytes in the sample.");
128126
s->retrieve_untyped(buffer);
129127
return s->timestamp;
130-
} else {
131-
if (conn_.lost())
132-
throw lost_error("The stream read by this inlet has been lost. To recover, you need to "
133-
"re-resolve the source and re-create the inlet.");
134-
return 0.0;
135128
}
129+
else return 0.0;
136130
}
137131

138132

src/data_receiver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class data_receiver : public cancellable_registry {
8181
/// The data reader thread.
8282
void data_thread();
8383

84+
sample_p try_get_next_sample(double timeout);
85+
8486
/// the underlying connection
8587
inlet_connection &conn_;
8688

0 commit comments

Comments
 (0)