mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-03 10:17:58 -05:00
ws_connect(), query_boxes_info() and start_print() set expires_after() deadlines, then made synchronous Beast calls. Beast only applies those deadlines to async operations, so none of them fired. A printer that accepted the websocket and then stopped answering blocked the caller forever. That froze the GUI thread when the Creality send dialog opened or the sidebar synced filaments, and left uploads stuck after the file arrived. Run every websocket call as an async operation on the local io_context, so the deadlines apply. run_until_done() and the per-address connect loop move from MoonrakerPrinterAgent.cpp into AsioUtils.hpp, and both files share them. Each resolved address gets its own connect deadline, and start_print() gets a reply deadline for its commands. A start-print timeout shows the translated timeout message that HTTP timeouts already use, now shared through PrintHost::timed_out_message(), and other websocket errors drop Boost's source location. The websocket port and deadlines move into a protected ws_settings member, so the tests can use a local port and short deadlines. The new tests run CrealityPrint against a fake printer that goes silent before or after the handshake.
172 lines
5.7 KiB
C++
172 lines
5.7 KiB
C++
#ifndef slic3r_PrintHost_hpp_
|
|
#define slic3r_PrintHost_hpp_
|
|
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <string>
|
|
#include <functional>
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <utility>
|
|
#include <wx/string.h>
|
|
|
|
#include <libslic3r/enum_bitmask.hpp>
|
|
#include "Http.hpp"
|
|
#include <map>
|
|
class wxArrayString;
|
|
|
|
namespace Slic3r {
|
|
|
|
class DynamicPrintConfig;
|
|
|
|
enum class PrintHostPostUploadAction {
|
|
None,
|
|
StartPrint,
|
|
StartSimulation,
|
|
QueuePrint
|
|
};
|
|
using PrintHostPostUploadActions = enum_bitmask<PrintHostPostUploadAction>;
|
|
ENABLE_ENUM_BITMASK_OPERATORS(PrintHostPostUploadAction);
|
|
|
|
struct PrintHostUpload
|
|
{
|
|
bool use_3mf { false };
|
|
boost::filesystem::path source_path;
|
|
boost::filesystem::path upload_path;
|
|
|
|
std::string group;
|
|
std::string storage;
|
|
|
|
PrintHostPostUploadAction post_action { PrintHostPostUploadAction::None };
|
|
|
|
// Some extended parameters for different upload methods.
|
|
std::map<std::string, std::string> extended_info;
|
|
|
|
// Safe accessor for an extended_info entry; returns `def` when the key is absent.
|
|
std::string extended(const std::string &key, const std::string &def = {}) const
|
|
{
|
|
auto it = extended_info.find(key);
|
|
return it != extended_info.end() ? it->second : def;
|
|
}
|
|
};
|
|
|
|
class PrintHost
|
|
{
|
|
public:
|
|
virtual ~PrintHost();
|
|
|
|
typedef Http::ProgressFn ProgressFn;
|
|
typedef std::function<void(wxString /* error */)> ErrorFn;
|
|
typedef std::function<void(wxString /* tag */, wxString /* status */)> InfoFn;
|
|
|
|
virtual const char* get_name() const = 0;
|
|
|
|
virtual bool test(wxString &curl_msg) const = 0;
|
|
virtual wxString get_test_ok_msg () const = 0;
|
|
virtual wxString get_test_failed_msg (wxString &msg) const = 0;
|
|
virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, InfoFn info_fn) const = 0;
|
|
virtual bool has_auto_discovery() const = 0;
|
|
virtual bool can_test() const = 0;
|
|
virtual PrintHostPostUploadActions get_post_upload_actions() const = 0;
|
|
// A print host usually does not support multiple printers, with the exception of Repetier server.
|
|
virtual bool supports_multiple_printers() const { return false; }
|
|
virtual std::string get_host() const = 0;
|
|
/**
|
|
* Get the serial number for connecting to the printer.
|
|
* For Elegoo CC2, the device details connection to the printer requires the serial number.
|
|
* Other print hosts do not need to implement this interface, and it returns an empty string by default.
|
|
*/
|
|
virtual std::string get_sn() const { return ""; }
|
|
|
|
// Support for Repetier server multiple groups & printers. Not supported by other print hosts.
|
|
// Returns false if not supported. May throw HostNetworkError.
|
|
virtual bool get_groups(wxArrayString & /* groups */) const { return false; }
|
|
virtual bool get_printers(wxArrayString & /* printers */) const { return false; }
|
|
// Support for PrusaLink uploading to different storage. Not supported by other print hosts.
|
|
// Returns false if not supported or fail.
|
|
virtual bool get_storage(wxArrayString& /*storage_path*/, wxArrayString& /*storage_name*/) const { return false; }
|
|
|
|
static PrintHost* get_print_host(DynamicPrintConfig *config);
|
|
static std::string get_print_host_webui(DynamicPrintConfig *config);
|
|
// Reads the "err" field of a JSON reply, 0 when absent. Returns -1 when the body is not valid JSON.
|
|
static int get_err_code_from_body(const std::string &body);
|
|
|
|
//Support for cloud webui login
|
|
virtual bool is_cloud() const { return false; }
|
|
virtual bool is_logged_in() const { return false; }
|
|
virtual void log_out() const {}
|
|
virtual bool get_login_url(wxString& auth_url) const { return false; }
|
|
|
|
protected:
|
|
virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const;
|
|
static wxString timed_out_message();
|
|
};
|
|
|
|
|
|
struct PrintHostJob
|
|
{
|
|
PrintHostUpload upload_data;
|
|
std::unique_ptr<PrintHost> printhost;
|
|
bool switch_to_device_tab{false};
|
|
bool cancelled = false;
|
|
|
|
PrintHostJob() {}
|
|
PrintHostJob(const PrintHostJob&) = delete;
|
|
PrintHostJob(PrintHostJob &&other)
|
|
: upload_data(std::move(other.upload_data))
|
|
, printhost(std::move(other.printhost))
|
|
, switch_to_device_tab(other.switch_to_device_tab)
|
|
, cancelled(other.cancelled)
|
|
{}
|
|
|
|
PrintHostJob(DynamicPrintConfig *config)
|
|
: printhost(PrintHost::get_print_host(config))
|
|
{}
|
|
|
|
PrintHostJob& operator=(const PrintHostJob&) = delete;
|
|
PrintHostJob& operator=(PrintHostJob &&other)
|
|
{
|
|
upload_data = std::move(other.upload_data);
|
|
printhost = std::move(other.printhost);
|
|
switch_to_device_tab = other.switch_to_device_tab;
|
|
cancelled = other.cancelled;
|
|
return *this;
|
|
}
|
|
|
|
bool empty() const { return !printhost; }
|
|
operator bool() const { return !!printhost; }
|
|
};
|
|
|
|
|
|
namespace GUI { class PrintHostQueueDialog; }
|
|
|
|
class PrintHostJobQueue
|
|
{
|
|
public:
|
|
PrintHostJobQueue(GUI::PrintHostQueueDialog *queue_dialog);
|
|
PrintHostJobQueue(const PrintHostJobQueue &) = delete;
|
|
PrintHostJobQueue(PrintHostJobQueue &&other) = delete;
|
|
~PrintHostJobQueue();
|
|
|
|
PrintHostJobQueue& operator=(const PrintHostJobQueue &) = delete;
|
|
PrintHostJobQueue& operator=(PrintHostJobQueue &&other) = delete;
|
|
|
|
void enqueue(PrintHostJob job);
|
|
void cancel(size_t id);
|
|
|
|
// Uploads the job, firing UploadStarted and a matching UploadFinished. An exception thrown by
|
|
// the upload is reported through error_fn and makes the upload fail.
|
|
static bool upload_job(PrintHostJob &job, PrintHost::ProgressFn progress_fn, PrintHost::ErrorFn error_fn, PrintHost::InfoFn info_fn);
|
|
|
|
private:
|
|
struct priv;
|
|
std::shared_ptr<priv> p;
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|