-
Notifications
You must be signed in to change notification settings - Fork 0
resolving medium level issue[do not merge] #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,17 +40,23 @@ template <typename Derived> | |
| class CommandInterface | ||
| { | ||
| public: | ||
| CommandInterface() = default; | ||
| CommandInterface() : mResponseReceived(false) {} | ||
|
|
||
| bool sendCommand(std::string command, std::string &response) | ||
| { | ||
| Derived &derived = static_cast<Derived &>(*this); | ||
| if (derived.send(command)) | ||
| { | ||
| std::unique_lock<std::mutex> lock(mResponseMutex); | ||
| mResponseCondition.wait_for(lock, std::chrono::seconds(5)); | ||
| response = mLastResponse; | ||
| return true; | ||
| mResponseReceived = false; | ||
| bool gotResponse = mResponseCondition.wait_for(lock, std::chrono::seconds(5), | ||
| [this]() { return mResponseReceived; }); | ||
|
|
||
| if (gotResponse) | ||
| { | ||
| response = mLastResponse; | ||
| return true; | ||
| } | ||
| } | ||
|
Comment on lines
45
to
60
|
||
|
|
||
| return false; | ||
|
|
@@ -63,6 +69,7 @@ class CommandInterface | |
| { | ||
| std::lock_guard<std::mutex> lock(mResponseMutex); | ||
| mLastResponse = message; | ||
| mResponseReceived = true; | ||
| mResponseCondition.notify_one(); | ||
| } | ||
|
|
||
|
|
@@ -71,6 +78,7 @@ class CommandInterface | |
| CommandInterface &operator=(const CommandInterface &) = delete; | ||
|
|
||
| std::string mLastResponse; | ||
| bool mResponseReceived; | ||
| std::mutex mResponseMutex; | ||
| std::condition_variable mResponseCondition; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,7 @@ class JsonWrap | |
|
|
||
| uint32_t getUint32(const char *name, bool &err) | ||
| { | ||
| uint32_t res; | ||
| uint32_t res = 0; | ||
| cJSON *itm = cJSON_GetObjectItem(mPtr, name); | ||
| if (!itm || !cJSON_IsNumber(itm)) | ||
| { | ||
|
Comment on lines
+83
to
86
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In sendCommand(), mResponseReceived is reset after derived.send(command) has already been issued. If the response arrives quickly (on another thread) between derived.send() returning and acquiring mResponseMutex, onMessage() can set mResponseReceived=true and notify, but this code then sets it back to false and waits until timeout (missed signal). Reset the flag (or use a monotonically increasing request/response token) before sending, and only wait with the predicate after the send is initiated.