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();
if (!ret) {
msg = console.error_message();
msg = wxString::FromUTF8(console.error_message().c_str());
}
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;
error_fn(format_error(body, error, status));
res = false;
})
.on_progress([&](Http::Progress progress, bool& cancel) {
prorgess_fn(std::move(progress), cancel);
if (cancel) {
// Upload was canceled
BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled";
res = false;
}
})
.perform_sync();
})
.on_progress([&](Http::Progress progress, bool& cancel) {
prorgess_fn(std::move(progress), cancel);
if (cancel) {
// Upload was canceled
BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled";
res = false;
}
}).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
@ -141,7 +134,7 @@ namespace Slic3r {
bool ret = console.run_queue();
if (!ret) {
msg = console.error_message();
msg = wxString::FromUTF8(console.error_message().c_str());
}
return ret;

View File

@ -33,7 +33,6 @@ namespace Slic3r {
std::string console_port;
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;
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 Utils {
const char* default_newline = "\n";
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)
TCPConsole::TCPConsole() : resolver_(io_context_), socket_(io_context_)
{
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()
{
@ -47,17 +47,20 @@ namespace Slic3r {
% port_name_
% cmd;
auto data = boost::asio::buffer(cmd + newline_);
send_buffer_ = cmd + newline_;
set_deadline_in(write_timeout_);
boost::asio::async_write(
socket_,
data,
boost::asio::buffer(send_buffer_),
boost::bind(&TCPConsole::handle_write, this, _1, _2)
);
}
void TCPConsole::wait_next_line()
{
set_deadline_in(read_timeout_);
boost::asio::async_read_until(
socket_,
recv_buffer_,
@ -145,6 +148,7 @@ namespace Slic3r {
io_context_.stop();
}
else {
is_connected_ = true;
BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%")
% host_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()
{
auto now = boost::chrono::steady_clock::now();
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_);
@ -165,16 +182,21 @@ namespace Slic3r {
boost::bind(&TCPConsole::handle_connect, this, _1)
);
// TODO: Add error and timeout processing
io_context_.restart();
while (!io_context_.stopped()) {
BOOST_LOG_TRIVIAL(debug) << ".\n";
// Loop until we get any reasonable result. Negative result is also result.
// TODO: Rewrite to more graceful way using deadlime_timer
bool timeout = false;
while (!(timeout = is_deadline_over()) && !io_context_.stopped()) {
if (error_code_) {
io_context_.stop();
}
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_.close();

View File

@ -4,6 +4,7 @@
#include <string>
#include <list>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/streambuf.hpp>
@ -19,6 +20,15 @@ namespace Slic3r {
TCPConsole(const std::string& host_name, const std::string& port_name);
~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) {
newline_ = newline;
}
@ -52,10 +62,16 @@ namespace Slic3r {
void wait_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 port_name_;
std::string newline_;
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_;
@ -63,8 +79,11 @@ namespace Slic3r {
tcp::resolver resolver_;
tcp::socket socket_;
boost::asio::streambuf recv_buffer_;
std::string send_buffer_;
bool is_connected_;
boost::system::error_code error_code_;
boost::chrono::steady_clock::time_point deadline_;
};
} // Utils