Skip to content

Commit 0f0daf1

Browse files
committed
Prevent invalid resolver operations
Throws an exception on some illegal operations (e.g. starting a resolve operation while a continuous resolve operation is in progress, retrieving results before starting a resolve operation)
1 parent 65cb40d commit 0f0daf1

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

include/lsl_cpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ class continuous_resolver {
16681668
std::vector<stream_info> results() {
16691669
lsl_streaminfo buffer[1024];
16701670
return std::vector<stream_info>(
1671-
buffer, buffer + lsl_resolver_results(obj.get(), buffer, sizeof(buffer)));
1671+
buffer, buffer + check_error(lsl_resolver_results(obj.get(), buffer, sizeof(buffer))));
16721672
}
16731673

16741674
/// Move constructor for stream_inlet

src/resolver_impl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ resolver_impl *resolver_impl::create_resolver(
9090

9191
std::vector<stream_info_impl> resolver_impl::resolve_oneshot(
9292
const std::string &query, int minimum, double timeout, double minimum_time) {
93+
if(status == resolver_status::running_continuous)
94+
throw std::logic_error("resolve_oneshot called during continuous operation");
95+
9396
check_query(query);
9497
// reset the IO service & set up the query parameters
9598
io_->restart();
@@ -113,6 +116,8 @@ std::vector<stream_info_impl> resolver_impl::resolve_oneshot(
113116
// start the first wave of resolve packets
114117
next_resolve_wave();
115118

119+
status = resolver_status::started_oneshot;
120+
116121
// run the IO operations until finished
117122
if (!cancelled_) {
118123
io_->run();
@@ -125,6 +130,9 @@ std::vector<stream_info_impl> resolver_impl::resolve_oneshot(
125130
}
126131

127132
void resolver_impl::resolve_continuous(const std::string &query, double forget_after) {
133+
if(status == resolver_status::running_continuous)
134+
throw std::logic_error("resolve_continuous called during another continuous operation");
135+
128136
check_query(query);
129137
// reset the IO service & set up the query parameters
130138
io_->restart();
@@ -139,9 +147,13 @@ void resolver_impl::resolve_continuous(const std::string &query, double forget_a
139147
next_resolve_wave();
140148
// spawn a thread that runs the IO operations
141149
background_io_ = std::make_shared<std::thread>([shared_io = io_]() { shared_io->run(); });
150+
status = resolver_status::running_continuous;
142151
}
143152

144153
std::vector<stream_info_impl> resolver_impl::results(uint32_t max_results) {
154+
if (status == resolver_status::empty)
155+
throw std::logic_error("results() called before starting a resolve operation");
156+
145157
std::vector<stream_info_impl> output;
146158
std::lock_guard<std::mutex> lock(results_mut_);
147159
double expired_before = lsl_clock() - forget_after_;

src/resolver_impl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class resolver_impl : public cancellable_registry {
124124
*/
125125
void cancel();
126126

127+
enum class resolver_status {
128+
empty, started_oneshot, running_continuous
129+
};
130+
127131
private:
128132
/// This function starts a new wave of resolves.
129133
void next_resolve_wave();
@@ -157,6 +161,7 @@ class resolver_impl : public cancellable_registry {
157161
std::atomic<bool> expired_;
158162

159163
// reinitialized for each query
164+
resolver_status status{resolver_status::empty};
160165
/// our current query string
161166
std::string query_;
162167
/// the minimum number of results that we want

0 commit comments

Comments
 (0)