Platform: Servd: Add address configuration

This commit is contained in:
Kyle Schwarz
2026-09-28 12:02:40 -04:00
parent f310938684
commit 0a67c9cf5d
2 changed files with 44 additions and 16 deletions
+1
View File
@@ -35,6 +35,7 @@ private:
Socket messageSocket; Socket messageSocket;
bool opened = false; bool opened = false;
bool comEnabled = false; bool comEnabled = false;
std::string token;
std::unique_ptr<Socket> dataSocket; std::unique_ptr<Socket> dataSocket;
}; };
+40 -13
View File
@@ -6,9 +6,21 @@
using namespace icsneo; using namespace icsneo;
#define SERVD_VERSION 2 #define SERVD_VERSION 3
static const Address SERVD_ADDRESS = [] {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
const auto envip = std::getenv("LIBICSNEO_SERVD_IP");
const auto envport = std::getenv("LIBICSNEO_SERVD_PORT");
#ifdef _MSC_VER
#pragma warning(pop)
#endif
return Address(envip ? envip : "127.0.0.1", envport ? static_cast<uint16_t>(std::stoi(envport)) : 26741);
}();
static const Address SERVD_ADDRESS = Address("127.0.0.1", 26741);
static const std::string SERVD_VERSION_STR = std::to_string(SERVD_VERSION); static const std::string SERVD_VERSION_STR = std::to_string(SERVD_VERSION);
bool Servd::Enabled() { bool Servd::Enabled() {
@@ -56,10 +68,15 @@ void Servd::Find(std::vector<FoundDevice>& found) {
return; return;
} }
try {
if(std::stoll(response) < SERVD_VERSION) { if(std::stoll(response) < SERVD_VERSION) {
EventManager::GetInstance().add(APIEvent::Type::ServdOutdatedError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdOutdatedError, APIEvent::Severity::Error);
return; return;
} }
} catch (const std::exception&) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
return;
}
response.resize(512); response.resize(512);
const std::string find_request = SERVD_VERSION_STR + " find"; const std::string find_request = SERVD_VERSION_STR + " find";
@@ -70,22 +87,25 @@ void Servd::Find(std::vector<FoundDevice>& found) {
const auto lines = split(response, '\n'); const auto lines = split(response, '\n');
for(auto&& line : lines) { for(auto&& line : lines) {
const auto cols = split(line, ' '); const auto cols = split(line, ' ');
if(cols.size() < 3) { if(cols.size() < 2) {
if(!line.empty()) { if(!line.empty()) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
} }
continue; continue;
} }
const auto& serial = cols[0]; const auto& serial = cols[0];
const auto& ip = cols[1]; if(serial.empty() || serial.size() >= sizeof(FoundDevice::serial)) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
continue;
}
uint16_t port = 0; uint16_t port = 0;
try { try {
port = static_cast<uint16_t>(std::stoi(cols[2])); port = static_cast<uint16_t>(std::stoi(cols[1]));
} catch (const std::exception&) { } catch (const std::exception&) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
continue; continue;
} }
Address address(ip.c_str(), port); Address address(SERVD_ADDRESS.ip().c_str(), port);
auto& newFound = found.emplace_back(); auto& newFound = found.emplace_back();
std::copy(serial.begin(), serial.end(), newFound.serial); std::copy(serial.begin(), serial.end(), newFound.serial);
newFound.makeDriver = [=](device_eventhandler_t err, neodevice_t& forDevice) { newFound.makeDriver = [=](device_eventhandler_t err, neodevice_t& forDevice) {
@@ -130,16 +150,19 @@ bool Servd::open() {
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
return false; return false;
} }
const auto& ip = tokens[0];
uint16_t port = 0; uint16_t port = 0;
try { try {
port = static_cast<uint16_t>(std::stoi(tokens[1])); port = static_cast<uint16_t>(std::stoi(tokens[0]));
} catch (const std::exception&) { } catch (const std::exception&) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
return false; return false;
} }
Address address(ip.c_str(), port); token = tokens[1];
dataSocket->connect(address); Address address(SERVD_ADDRESS.ip().c_str(), port);
if(!dataSocket->connect(address)) {
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
return false;
}
readThread = std::thread(&Servd::read, this); readThread = std::thread(&Servd::read, this);
writeThread = std::thread(&Servd::write, this); writeThread = std::thread(&Servd::write, this);
opened = true; opened = true;
@@ -159,15 +182,14 @@ bool Servd::close() {
writeThread.join(); writeThread.join();
} }
if(isOpen()) { if(isOpen()) {
Address localAddress; const std::string request = SERVD_VERSION_STR + " close " + token;
dataSocket->address(localAddress);
const std::string request = SERVD_VERSION_STR + " close " + localAddress.ip() + " " + std::to_string(localAddress.port());
std::string response; std::string response;
response.resize(1); response.resize(1);
if(!messageSocket.transceive(request, response, std::chrono::milliseconds(5000))) { if(!messageSocket.transceive(request, response, std::chrono::milliseconds(5000))) {
EventManager::GetInstance().add(APIEvent::Type::ServdTransceiveError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdTransceiveError, APIEvent::Severity::Error);
return false; return false;
} }
token.clear();
dataSocket.reset(); dataSocket.reset();
} }
opened = false; opened = false;
@@ -207,7 +229,12 @@ bool Servd::enableCommunication(bool enable, bool& sendMsg) {
EventManager::GetInstance().add(APIEvent::Type::ServdTransceiveError, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::ServdTransceiveError, APIEvent::Severity::Error);
return false; return false;
} }
try {
com = response.empty() ? 0 : std::stoll(response); com = response.empty() ? 0 : std::stoll(response);
} catch (const std::exception&) {
EventManager::GetInstance().add(APIEvent::Type::ServdInvalidResponseError, APIEvent::Severity::Error);
return false;
}
} }
sendMsg = false; sendMsg = false;
if(enable) { if(enable) {