Add timeout/errors management, multiple fixes

This commit is contained in:
Sergey Kovalev 2020-12-29 04:50:52 +07:00
parent bb928f6ef7
commit fbe90b5534
4 changed files with 68 additions and 35 deletions

View file

@ -45,7 +45,7 @@ namespace Slic3r {
bool ret = console.run_queue(); bool ret = console.run_queue();
if (!ret) { if (!ret) {
msg = console.error_message(); msg = wxString::FromUTF8(console.error_message().c_str());
} }
return ret; return ret;
@ -98,25 +98,18 @@ namespace Slic3r {
BOOST_LOG_TRIVIAL(error) << boost::format("MKS: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body; BOOST_LOG_TRIVIAL(error) << boost::format("MKS: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body;
error_fn(format_error(body, error, status)); error_fn(format_error(body, error, status));
res = false; res = false;
}) })
.on_progress([&](Http::Progress progress, bool& cancel) { .on_progress([&](Http::Progress progress, bool& cancel) {
prorgess_fn(std::move(progress), cancel); prorgess_fn(std::move(progress), cancel);
if (cancel) { if (cancel) {
// Upload was canceled // Upload was canceled
BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled"; BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled";
res = false; res = false;
} }
}) }).perform_sync();
.perform_sync();
if (res && upload_data.start_print) {
wxString msg;
if (!start_print(msg, upload_data.upload_path.string())) {
error_fn(wxString("Can't start printing: ") + msg);
}
}
return res; return res;
} }
std::string MKS::get_upload_url(const std::string& filename) const std::string MKS::get_upload_url(const std::string& filename) const
@ -141,7 +134,7 @@ namespace Slic3r {
bool ret = console.run_queue(); bool ret = console.run_queue();
if (!ret) { if (!ret) {
msg = console.error_message(); msg = wxString::FromUTF8(console.error_message().c_str());
} }
return ret; return ret;

View file

@ -33,7 +33,6 @@ namespace Slic3r {
std::string console_port; std::string console_port;
std::string get_upload_url(const std::string& filename) const; std::string get_upload_url(const std::string& filename) const;
std::string timestamp_str() const;
bool start_print(wxString& msg, const std::string& filename) const; bool start_print(wxString& msg, const std::string& filename) const;
int get_err_code_from_body(const std::string& body) const; int get_err_code_from_body(const std::string& body) const;
}; };

View file

@ -20,17 +20,17 @@ using boost::asio::ip::tcp;
namespace Slic3r { namespace Slic3r {
namespace Utils { namespace Utils {
const char* default_newline = "\n"; TCPConsole::TCPConsole() : resolver_(io_context_), socket_(io_context_)
const char* default_done_string = "ok";
TCPConsole::TCPConsole() : resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string) {}
TCPConsole::TCPConsole(const std::string& host_name, const std::string& port_name) :
resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string)
{ {
set_remote(host_name, port_name); set_defaults();
} }
TCPConsole::TCPConsole(const std::string& host_name, const std::string& port_name) :
resolver_(io_context_), socket_(io_context_)
{
set_defaults();
set_remote(host_name, port_name);
}
void TCPConsole::transmit_next_command() void TCPConsole::transmit_next_command()
{ {
@ -47,17 +47,20 @@ namespace Slic3r {
% port_name_ % port_name_
% cmd; % cmd;
auto data = boost::asio::buffer(cmd + newline_);
send_buffer_ = cmd + newline_;
set_deadline_in(write_timeout_);
boost::asio::async_write( boost::asio::async_write(
socket_, socket_,
data, boost::asio::buffer(send_buffer_),
boost::bind(&TCPConsole::handle_write, this, _1, _2) boost::bind(&TCPConsole::handle_write, this, _1, _2)
); );
} }
void TCPConsole::wait_next_line() void TCPConsole::wait_next_line()
{ {
set_deadline_in(read_timeout_);
boost::asio::async_read_until( boost::asio::async_read_until(
socket_, socket_,
recv_buffer_, recv_buffer_,
@ -145,6 +148,7 @@ namespace Slic3r {
io_context_.stop(); io_context_.stop();
} }
else { else {
is_connected_ = true;
BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%") BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%")
% host_name_ % host_name_
% port_name_; % port_name_;
@ -154,10 +158,23 @@ namespace Slic3r {
} }
} }
void TCPConsole::set_deadline_in(boost::chrono::steady_clock::duration d)
{
deadline_ = boost::chrono::steady_clock::now() + d;
}
bool TCPConsole::is_deadline_over()
{
return deadline_ < boost::chrono::steady_clock::now();
}
bool TCPConsole::run_queue() bool TCPConsole::run_queue()
{ {
auto now = boost::chrono::steady_clock::now();
try { try {
// TODO: Add more resets and initializations after previous run // TODO: Add more resets and initializations after previous run (reset() method?..)
set_deadline_in(connect_timeout_);
is_connected_ = false;
io_context_.restart();
auto endpoints = resolver_.resolve(host_name_, port_name_); auto endpoints = resolver_.resolve(host_name_, port_name_);
@ -165,16 +182,21 @@ namespace Slic3r {
boost::bind(&TCPConsole::handle_connect, this, _1) boost::bind(&TCPConsole::handle_connect, this, _1)
); );
// TODO: Add error and timeout processing // Loop until we get any reasonable result. Negative result is also result.
io_context_.restart(); // TODO: Rewrite to more graceful way using deadlime_timer
while (!io_context_.stopped()) { bool timeout = false;
BOOST_LOG_TRIVIAL(debug) << ".\n"; while (!(timeout = is_deadline_over()) && !io_context_.stopped()) {
if (error_code_) { if (error_code_) {
io_context_.stop(); io_context_.stop();
} }
io_context_.run_for(boost::asio::chrono::milliseconds(100)); io_context_.run_for(boost::asio::chrono::milliseconds(100));
} }
// Override error message if timeout is set
if (timeout) {
error_code_ = make_error_code(boost::asio::error::timed_out);
}
// Socket is not closed automatically by boost // Socket is not closed automatically by boost
socket_.close(); socket_.close();

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <list> #include <list>
#include <boost/system/error_code.hpp> #include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/asio/streambuf.hpp> #include <boost/asio/streambuf.hpp>
@ -19,6 +20,15 @@ namespace Slic3r {
TCPConsole(const std::string& host_name, const std::string& port_name); TCPConsole(const std::string& host_name, const std::string& port_name);
~TCPConsole() {} ~TCPConsole() {}
void set_defaults()
{
newline_ = "\n";
done_string_ = "ok";
connect_timeout_ = boost::chrono::milliseconds(5000);
write_timeout_ = boost::chrono::milliseconds(10000);
read_timeout_ = boost::chrono::milliseconds(10000);
}
void set_line_delimiter(const std::string& newline) { void set_line_delimiter(const std::string& newline) {
newline_ = newline; newline_ = newline;
} }
@ -52,10 +62,16 @@ namespace Slic3r {
void wait_next_line(); void wait_next_line();
std::string extract_next_line(); std::string extract_next_line();
void set_deadline_in(boost::chrono::steady_clock::duration);
bool is_deadline_over();
std::string host_name_; std::string host_name_;
std::string port_name_; std::string port_name_;
std::string newline_; std::string newline_;
std::string done_string_; std::string done_string_;
boost::chrono::steady_clock::duration connect_timeout_;
boost::chrono::steady_clock::duration write_timeout_;
boost::chrono::steady_clock::duration read_timeout_;
std::list<std::string> cmd_queue_; std::list<std::string> cmd_queue_;
@ -63,8 +79,11 @@ namespace Slic3r {
tcp::resolver resolver_; tcp::resolver resolver_;
tcp::socket socket_; tcp::socket socket_;
boost::asio::streambuf recv_buffer_; boost::asio::streambuf recv_buffer_;
std::string send_buffer_;
bool is_connected_;
boost::system::error_code error_code_; boost::system::error_code error_code_;
boost::chrono::steady_clock::time_point deadline_;
}; };
} // Utils } // Utils