mirror of
https://github.com/BotChain-Robots/rpc.git
synced 2026-03-09 23:12:27 +01:00
Fix mDNS int parse crash, print errno on TCP connection failure
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
void print_errno() {
|
inline void print_errno() {
|
||||||
char errbuf[ERRBUF_SIZE];
|
char errbuf[ERRBUF_SIZE];
|
||||||
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), 0, errbuf, sizeof(errbuf),
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), 0, errbuf, sizeof(errbuf),
|
||||||
NULL);
|
NULL);
|
||||||
@@ -23,7 +23,7 @@ void print_errno() {
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void print_errno() {
|
inline void print_errno() {
|
||||||
spdlog::error("{}", strerror(errno));
|
spdlog::error("{}", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#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;
|
||||||
@@ -20,4 +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) {
|
||||||
|
if (s.empty()) return false;
|
||||||
|
|
||||||
|
size_t start = 0;
|
||||||
|
if (s[0] == '-' || s[0] == '+')
|
||||||
|
start = 1;
|
||||||
|
|
||||||
|
return start < s.size() &&
|
||||||
|
std::all_of(s.begin() + start, s.end(), ::isdigit);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // STRING_H
|
#endif // STRING_H
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "TCPClient.h"
|
#include "TCPClient.h"
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
#include "util/log.h"
|
||||||
|
|
||||||
constexpr auto SLEEP_WHILE_INITIALIZING = std::chrono::milliseconds(250);
|
constexpr auto SLEEP_WHILE_INITIALIZING = std::chrono::milliseconds(250);
|
||||||
constexpr int PORT = 3001;
|
constexpr int PORT = 3001;
|
||||||
@@ -55,7 +56,8 @@ int TCPClient::init() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (connect(this->m_socket, reinterpret_cast<sockaddr *>(&serv_addr), sizeof(serv_addr)) < 0) {
|
if (connect(this->m_socket, reinterpret_cast<sockaddr *>(&serv_addr), sizeof(serv_addr)) < 0) {
|
||||||
spdlog::error("[TCP] Connection failed");
|
spdlog::error("[TCP] Connection failed to connect");
|
||||||
|
print_errno();
|
||||||
deinit();
|
deinit();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -300,17 +300,23 @@ std::optional<mDNSRobotModule> mDNSDiscoveryService::parse_response(uint8_t *buf
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (split_string[0] == MODULE_ID_STR) {
|
if (split_string[0] == MODULE_ID_STR) {
|
||||||
response.id = stoi(split_string[1]);
|
if (is_integer(std::string(split_string[1]))) {
|
||||||
|
response.id = stoi(split_string[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (split_string[0] == MODULE_TYPE_STR) {
|
if (split_string[0] == MODULE_TYPE_STR) {
|
||||||
response.module_type = static_cast<ModuleType>(stoi(split_string[1]));
|
if (is_integer(std::string(split_string[1]))) {
|
||||||
|
response.module_type = static_cast<ModuleType>(stoi(split_string[1]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
response.connected_module_ids.emplace_back(stoi(module_id));
|
if (is_integer(module_id)) {
|
||||||
|
response.connected_module_ids.emplace_back(stoi(module_id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user