mirror of
https://github.com/BotChain-Robots/rpc.git
synced 2026-03-09 23:12:27 +01:00
Fix race condition on m_tag_to_queue_map
This commit is contained in:
@@ -4,11 +4,11 @@
|
|||||||
|
|
||||||
#ifndef STRING_H
|
#ifndef STRING_H
|
||||||
#define STRING_H
|
#define STRING_H
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cctype>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
inline std::vector<std::string> split(const std::string &str, const char delimiter) {
|
inline std::vector<std::string> split(const std::string &str, const char delimiter) {
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
@@ -22,15 +22,15 @@ inline std::vector<std::string> split(const std::string &str, const char delimit
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_integer(const std::string& s) {
|
inline bool is_integer(const std::string &s) {
|
||||||
if (s.empty()) return false;
|
if (s.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
size_t start = 0;
|
size_t start = 0;
|
||||||
if (s[0] == '-' || s[0] == '+')
|
if (s[0] == '-' || s[0] == '+')
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
return start < s.size() &&
|
return start < s.size() && std::all_of(s.begin() + start, s.end(), ::isdigit);
|
||||||
std::all_of(s.begin() + start, s.end(), ::isdigit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // STRING_H
|
#endif // STRING_H
|
||||||
|
|||||||
@@ -66,9 +66,10 @@ std::optional<SizeAndSource> MessagingInterface::recv(uint8_t *buffer, const siz
|
|||||||
{tag, std::make_unique<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
|
{tag, std::make_unique<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
|
||||||
PER_TAG_MAX_QUEUE_SIZE)});
|
PER_TAG_MAX_QUEUE_SIZE)});
|
||||||
}
|
}
|
||||||
|
const auto &queue = m_tag_to_queue_map[tag];
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
const auto data = m_tag_to_queue_map[tag]->dequeue(MAX_RECV_WAIT_TIME);
|
const auto data = queue->dequeue(MAX_RECV_WAIT_TIME);
|
||||||
|
|
||||||
if (!data.has_value()) {
|
if (!data.has_value()) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -139,10 +140,10 @@ void MessagingInterface::handle_recv() {
|
|||||||
std::make_unique<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
|
std::make_unique<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
|
||||||
PER_TAG_MAX_QUEUE_SIZE)});
|
PER_TAG_MAX_QUEUE_SIZE)});
|
||||||
}
|
}
|
||||||
|
const auto &queue = m_tag_to_queue_map[mpi_message->tag()];
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
m_tag_to_queue_map[mpi_message->tag()]->enqueue(std::move(data.value()),
|
queue->enqueue(std::move(data.value()), MAX_WAIT_TIME_TAG_ENQUEUE);
|
||||||
MAX_WAIT_TIME_TAG_ENQUEUE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -314,9 +314,9 @@ std::optional<mDNSRobotModule> mDNSDiscoveryService::parse_response(uint8_t *buf
|
|||||||
if (split_string[0] == CONNECTED_MODULES_STR) {
|
if (split_string[0] == CONNECTED_MODULES_STR) {
|
||||||
for (const auto connected_modules = split(split_string[1], ',');
|
for (const auto connected_modules = split(split_string[1], ',');
|
||||||
const auto &module_id : connected_modules) {
|
const auto &module_id : connected_modules) {
|
||||||
if (is_integer(module_id)) {
|
if (is_integer(module_id)) {
|
||||||
response.connected_module_ids.emplace_back(stoi(module_id));
|
response.connected_module_ids.emplace_back(stoi(module_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user