Files
OrcaSlicer/src/slic3r/Utils/CrealityHostDiscovery.cpp
T
HanifKoh 84657ff11e Add Missing Includes Across the Remaining Sources and Tests (#16071)
* Ignore Clipper, libpng, mcut and Boost.Polygon Internals in clang-tidy

Each only works through a wrapper or umbrella header: libslic3r/clipper.hpp or clipper_z.hpp configure Clipper before including it, png.h pulls in libpng's config headers, and Boost.Polygon's headers only compile through polygon.hpp or voronoi.hpp.

* Ignore minilzo's Config Headers in clang-tidy

lzoconf.h and lzodefs.h are internal to minilzo.h, which is what the code includes.

* Add Missing Includes Across the Remaining Sources and Tests

Covers src/slic3r/Utils, src/slic3r/plugin, src/slic3r/Config, src/libvgcode, src/dev-utils, src/OrcaSlicer.cpp and tests/, the directories left after src/slic3r/GUI and src/libslic3r. Generated with clang-tidy misc-include-cleaner. libvgcode's own headers are included by relative path as in the rest of that library, and Catch2 and pybind11 with angle brackets as elsewhere in the repo.

* Make the GUI and Test Headers Compile on Their Own

Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Headers that only compile on one platform, or that nothing built includes, are left alone.

* Keep Windows and nanosvg Setup Ahead of the Added Includes

OrcaSlicer.cpp and several tests set _WIN32_WINNT, WIN32_LEAN_AND_MEAN or NOMINMAX before including Windows.h, and the profile validator defines NANOSVG_IMPLEMENTATION before any libslic3r header. The added includes had landed above those blocks, which broke the Windows build.

* Add the GUI Includes the First Pass Missed

Covers headers that only became editable once they compiled on their own, and wx symbols whose suggested header changed as the clang-tidy ignore list grew after the src/slic3r/GUI pass.

* Keep the Added Test Includes Below the NOMINMAX Guard

test_marchingsquares.cpp and test_texture_displacement.cpp had includes inside #ifndef NOMINMAX, which the tests inherit as defined on Windows from libslic3r, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory, as in #16068.
2026-10-03 13:45:21 +08:00

125 lines
4.0 KiB
C++

#include "CrealityHostDiscovery.hpp"
#include "CrealityPrint.hpp"
#include "cxmdns.h"
#include "Http.hpp"
#include <boost/log/trivial.hpp>
#include <exception>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
namespace Slic3r {
namespace {
// Model capability/name lookups live in CrealityPrint (one table shared
// with the print host) so discovery can't drift out of sync again.
bool is_cfs_capable(const std::string& code)
{
return CrealityPrint::model_supports_multi_color(code);
}
std::string model_name_for(const std::string& code)
{
return CrealityPrint::model_display_name(code);
}
// Extract the device suffix from a service name like
// "_Creality-543324280CDB19._udp.local." and synthesise a hostname-ish label
// (e.g. "K2-DB19" using the last 4 hex of the MAC-derived suffix).
std::string hostname_from_service(const std::string& service_name)
{
auto dash = service_name.find_last_of('-');
if (dash == std::string::npos) return {};
auto dot = service_name.find('.', dash);
if (dot == std::string::npos) return {};
std::string suffix = service_name.substr(dash + 1, dot - dash - 1);
if (suffix.size() >= 4) {
return "K2-" + suffix.substr(suffix.size() - 4);
}
return suffix.empty() ? std::string{} : "K2-" + suffix;
}
// Synchronously probe http://<ip>/info for {model, mac}. Short timeout --
// we don't want one slow host to drag down discovery.
void probe_info(CrealityHost& host)
{
const std::string url = "http://" + host.ip + "/info";
auto http = Http::get(url);
http.timeout_connect(2)
.timeout_max(4)
.on_complete([&host](std::string body, unsigned /*status*/) {
try {
auto j = nlohmann::json::parse(body);
if (j.contains("model") && j["model"].is_string())
host.model_code = j["model"].get<std::string>();
if (j.contains("mac") && j["mac"].is_string())
host.mac = j["mac"].get<std::string>();
if (is_cfs_capable(host.model_code)) {
host.cfs_capable = true;
host.model_name = model_name_for(host.model_code);
}
} catch (const std::exception& e) {
BOOST_LOG_TRIVIAL(warning)
<< "CrealityHostDiscovery: /info parse failed for "
<< host.ip << ": " << e.what();
}
})
.on_error([&host](std::string /*body*/, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: /info GET failed for "
<< host.ip << ": " << error << " (HTTP " << status << ")";
})
.perform_sync();
}
} // namespace
std::vector<CrealityHost> CrealityHostDiscovery::scan(bool probe)
{
const std::vector<std::string> prefixes{ "Creality", "creality" };
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: starting DNS-SD discovery (prefixes: Creality, creality)";
auto raw = cxnet::syncDiscoveryService(prefixes);
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: mDNS returned " << raw.size() << " match(es)";
std::vector<CrealityHost> hosts;
hosts.reserve(raw.size());
// Dedupe by IP -- one printer may announce twice if multi-homed or if
// we capture both IPv4/IPv6 replies.
std::vector<std::string> seen_ips;
for (const auto& m : raw) {
if (m.machineIp.empty()) continue;
if (std::find(seen_ips.begin(), seen_ips.end(), m.machineIp) != seen_ips.end())
continue;
seen_ips.push_back(m.machineIp);
CrealityHost h;
h.ip = m.machineIp;
h.service_name = m.answer;
h.hostname = hostname_from_service(m.answer);
if (probe) {
probe_info(h);
}
hosts.push_back(std::move(h));
}
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: " << hosts.size() << " unique host(s) after dedup";
return hosts;
}
} // namespace Slic3r