diff --git a/lib/silentdragonlitelib.h b/lib/silentdragonlitelib.h index ef856d8..982b2d5 100644 --- a/lib/silentdragonlitelib.h +++ b/lib/silentdragonlitelib.h @@ -14,6 +14,7 @@ extern char * litelib_initialize_existing (bool dangerous,const char* server); extern char * litelib_execute (const char* s, const char* args); extern void litelib_rust_free_string (char* s); extern char * blake3_PW (char* pw); +extern bool litelib_check_server_online (const char* server); #ifdef __cplusplus } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index d78c23d..afb398f 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -245,6 +245,24 @@ pub extern fn litelib_execute(cmd: *const c_char, args: *const c_char) -> *mut c return c_str.into_raw(); } +// Check is Server Connection is fine +#[no_mangle] +pub extern "C" fn litelib_check_server_online(server: *const c_char) -> bool { + let server_str = unsafe { + assert!(!server.is_null()); + + CStr::from_ptr(server).to_string_lossy().into_owned() + }; + + let server = LightClientConfig::get_server_or_default(Some(server_str)); + let result = LightClientConfig::create(server, false); + + match result { + Ok(_) => true, + Err(_) => false, + } +} + /** * Callers that receive string return values from other functions should call this to return the string * back to rust, so it can be freed. Failure to call this function will result in a memory leak diff --git a/src/settings.cpp b/src/settings.cpp index f8cbaa1..5e2470c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -37,16 +37,15 @@ Config Settings::getSettings() { if (server.trimmed().isEmpty()) { server = Settings::getRandomServer(); - QString response = ""; + bool isOnline = false; // make sure existing server in conf is alive, otherwise choose random one try { - char* resp = litelib_initialize_existing(false, server.toStdString().c_str()); - response = litelib_process_response(resp); + bool isOnline = litelib_check_server_online(server.toStdString().c_str()); } catch (const std::exception& e) { qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); } - if (response.toUpper().trimmed() != "OK") { + if (!isOnline) { qDebug() << "Lite server in conf " << server << " is down, getting a random one"; server = Settings::getRandomServer(); s.setValue("connection/server", server); @@ -333,11 +332,10 @@ QString Settings::getRandomServer() { while (tries < servers.size() ) { qDebug() << "Checking if lite server " << server << " is a alive, try=" << tries; - QString response = ""; + bool isOnline = ""; try { - char* resp = litelib_initialize_existing(false, server.toStdString().c_str()); - response = litelib_process_response(resp); + isOnline = litelib_check_server_online(server.toStdString().c_str()); } catch (const std::exception& e) { qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); } @@ -345,7 +343,7 @@ QString Settings::getRandomServer() { // if we see a valid connection, return this server. when the server is alive, //it tries to read the wallet. This causes an error because it cannot find it (SDL only has a non-encrypted wallet.dat during storage processes) // We can use that. - if (response.contains("Error: Cannot read wallet.")) { + if (isOnline) { qDebug() << "Choosing lite server " << server; return server; }