mirror of
https://github.com/kunkundi/crossdesk-server.git
synced 2026-10-25 17:21:59 +08:00
[fix] fix silent offline join failures and Windows min/max macro build errors
This commit is contained in:
1 parent
db8f32ccce
commit
ebdc48ace6
5 files changed
+107
-12
No files matched your search
@@ -121,7 +121,7 @@ size_t QuerySizeParam(const std::map<std::string, std::string>& params,
|
||||
if (!ParseSize(it->second, &value)) {
|
||||
return fallback;
|
||||
}
|
||||
return std::min(value, max_value);
|
||||
return (std::min)(value, max_value);
|
||||
}
|
||||
|
||||
std::string QueryStringParam(const std::map<std::string, std::string>& params,
|
||||
@@ -224,7 +224,7 @@ void ApplyDevicePage(std::vector<OnlineDeviceInfo>* devices, size_t offset,
|
||||
}
|
||||
|
||||
const size_t available = devices->size() - offset;
|
||||
const size_t count = std::min(limit, available);
|
||||
const size_t count = (std::min)(limit, available);
|
||||
std::vector<OnlineDeviceInfo> page(devices->begin() + offset,
|
||||
devices->begin() + offset + count);
|
||||
devices->swap(page);
|
||||
@@ -754,7 +754,7 @@ AdminHttpResponse AdminController::HandleDisconnect(
|
||||
bool persisted = false;
|
||||
if (db_) {
|
||||
for (const auto& session : db_->ListRemoteControlSessions(
|
||||
static_cast<size_t>(std::numeric_limits<int>::max()), 0,
|
||||
static_cast<size_t>((std::numeric_limits<int>::max)()), 0,
|
||||
transmission_id)) {
|
||||
if (session.transmission_id == transmission_id) {
|
||||
persisted = true;
|
||||
@@ -788,7 +788,7 @@ nlohmann::json AdminController::BuildStats(size_t online_device_fallback) const
|
||||
size_t active_connection_count =
|
||||
transmission_ ? transmission_->GetActiveConnectionCount() : 0;
|
||||
if (db_) {
|
||||
active_connection_count = std::max(
|
||||
active_connection_count = (std::max)(
|
||||
active_connection_count,
|
||||
static_cast<size_t>(db_->CountActiveRemoteControlConnections()));
|
||||
}
|
||||
|
||||
@@ -238,6 +238,22 @@ bool SignalNegotiation::join_transmission(websocketpp::connection_hdl hdl,
|
||||
int ret = device_db_manager_->VerifyDevice(transmission_id, password);
|
||||
|
||||
if (0 == ret) {
|
||||
std::string host_id =
|
||||
transmission_manager_->GetHostIdOfTransmission(transmission_id);
|
||||
websocketpp::connection_hdl host_hdl =
|
||||
transmission_manager_->GetWsHandle(host_id);
|
||||
|
||||
if (host_id.empty() || host_hdl.expired()) {
|
||||
LOG_WARN("Remote [{}] is unavailable, cannot join transmission",
|
||||
transmission_id.c_str());
|
||||
json message = {{"type", "user_join_transmission"},
|
||||
{"transmission_id", transmission_id},
|
||||
{"status", "failed"},
|
||||
{"reason", "Remote unavailable"}};
|
||||
send_msg_(hdl, message);
|
||||
return true;
|
||||
}
|
||||
|
||||
transmission_manager_->BindGuestToTransmission(user_id, transmission_id);
|
||||
|
||||
json message = {{"type", "user_join_transmission"},
|
||||
@@ -245,12 +261,7 @@ bool SignalNegotiation::join_transmission(websocketpp::connection_hdl hdl,
|
||||
{"user_id", user_id},
|
||||
{"status", "success"}};
|
||||
|
||||
std::string host_id =
|
||||
transmission_manager_->GetHostIdOfTransmission(transmission_id);
|
||||
|
||||
if (!host_id.empty()) {
|
||||
send_msg_(transmission_manager_->GetWsHandle(host_id), message);
|
||||
}
|
||||
send_msg_(host_hdl, message);
|
||||
} else if (-1 == ret) {
|
||||
LOG_ERROR("Password incorrect for transmission id [{}]",
|
||||
transmission_id.c_str());
|
||||
|
||||
@@ -147,7 +147,7 @@ std::vector<std::string> TransmissionManager::GetAllUserIdOfTransmission(
|
||||
|
||||
std::vector<TransmissionSnapshot> TransmissionManager::GetTransmissionSnapshots() {
|
||||
size_t ignored_count = 0;
|
||||
return GetTransmissionSnapshots(std::numeric_limits<size_t>::max(), 0, "",
|
||||
return GetTransmissionSnapshots((std::numeric_limits<size_t>::max)(), 0, "",
|
||||
&ignored_count);
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ std::vector<TransmissionSnapshot> TransmissionManager::GetTransmissionSnapshots(
|
||||
std::lock_guard<std::recursive_mutex> lock(ws_hdl_alive_checker_mutex_);
|
||||
std::vector<TransmissionSnapshot> result;
|
||||
if (limit > 0) {
|
||||
result.reserve(std::min(limit, transmission_host_id_list_.size()));
|
||||
result.reserve((std::min)(limit, transmission_host_id_list_.size()));
|
||||
}
|
||||
|
||||
size_t matched_count = 0;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "signal_negotiation.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "device_db_manager.h"
|
||||
#include "transmission_manager.h"
|
||||
|
||||
int main() {
|
||||
int failures = 0;
|
||||
|
||||
auto expect = [&failures](bool condition, const std::string& message) {
|
||||
if (!condition) {
|
||||
std::cerr << "FAIL: " << message << std::endl;
|
||||
++failures;
|
||||
}
|
||||
};
|
||||
|
||||
const auto db_path =
|
||||
std::filesystem::temp_directory_path() /
|
||||
("crossdesk_signal_negotiation_test_" +
|
||||
std::to_string(std::chrono::steady_clock::now()
|
||||
.time_since_epoch()
|
||||
.count()) +
|
||||
".db");
|
||||
|
||||
{
|
||||
DeviceDBManager db(db_path.string());
|
||||
DeviceCredential offline_host = db.AddDevice("", "");
|
||||
expect(!offline_host.device_id.empty(), "test host device is registered");
|
||||
expect(!offline_host.password.empty(), "test host password is registered");
|
||||
|
||||
auto transmission = std::make_shared<TransmissionManager>();
|
||||
SignalNegotiation negotiation(transmission, &db);
|
||||
|
||||
std::vector<json> sent_messages;
|
||||
auto requester_connection = std::make_shared<int>(1);
|
||||
websocketpp::connection_hdl requester_hdl(requester_connection);
|
||||
negotiation.SetSendMsgCallback(
|
||||
[&](websocketpp::connection_hdl, json message) {
|
||||
sent_messages.push_back(message);
|
||||
});
|
||||
|
||||
json request = {
|
||||
{"type", "join_transmission"},
|
||||
{"user_id", "C-controller"},
|
||||
{"transmission_id", offline_host.device_id + "@" +
|
||||
offline_host.password},
|
||||
};
|
||||
|
||||
negotiation.join_transmission(requester_hdl, request);
|
||||
|
||||
expect(sent_messages.size() == 1,
|
||||
"offline host join sends one failure response to requester");
|
||||
if (!sent_messages.empty()) {
|
||||
expect(sent_messages[0].value("type", "") == "user_join_transmission",
|
||||
"offline host join response has user_join_transmission type");
|
||||
expect(sent_messages[0].value("status", "") == "failed",
|
||||
"offline host join response fails");
|
||||
expect(sent_messages[0].value("reason", "") == "Remote unavailable",
|
||||
"offline host join response reports remote unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::remove(db_path);
|
||||
|
||||
if (failures > 0) {
|
||||
std::cerr << failures << " failure(s)" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -87,6 +87,13 @@ target("transmission_manager_test")
|
||||
add_files("tests/transmission_manager_test.cpp")
|
||||
add_includedirs("src", "src/log")
|
||||
|
||||
target("signal_negotiation_test")
|
||||
set_kind("binary")
|
||||
set_default(false)
|
||||
add_deps("negotiation")
|
||||
add_files("tests/signal_negotiation_test.cpp")
|
||||
add_includedirs("src", "src/device_db_manager", "src/log")
|
||||
|
||||
target("admin_auth_test")
|
||||
set_kind("binary")
|
||||
set_default(false)
|
||||
|
||||
Reference in new issue
Block a user