OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::Utils::TCPConsole Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/TCPConsole.hpp>

Collaboration diagram for Slic3r::Utils::TCPConsole:

Public Member Functions

 TCPConsole ()
 TCPConsole (const std::string &host_name, const std::string &port_name)
 ~TCPConsole ()=default
void set_defaults ()
void set_write_timeout (std::chrono::steady_clock::duration timeout)
void set_read_timeout (std::chrono::steady_clock::duration timeout)
void set_tcp_queue_delay (std::chrono::steady_clock::duration delay)
void set_line_delimiter (const std::string &newline)
void set_command_done_string (const std::string &done_string)
void set_remote (const std::string &host_name, const std::string &port_name)
bool enqueue_cmd (const SerialMessage &cmd)
bool run_queue ()
std::string error_message () const

Private Types

using tcp = boost::asio::ip::tcp

Private Member Functions

void handle_connect (const boost::system::error_code &ec)
void handle_read (const boost::system::error_code &ec, std::size_t bytes_transferred)
void handle_write (const boost::system::error_code &ec, std::size_t bytes_transferred, SerialMessageType messageType)
void transmit_next_command ()
void wait_next_line ()
std::string extract_next_line ()
void set_deadline_in (std::chrono::steady_clock::duration)
bool is_deadline_over () const

Private Attributes

std::string m_host_name
std::string m_port_name
std::string m_newline
std::string m_done_string
std::chrono::steady_clock::duration m_connect_timeout
std::chrono::steady_clock::duration m_write_timeout
std::chrono::steady_clock::duration m_read_timeout
std::chrono::steady_clock::duration m_tcp_queue_delay
std::deque< SerialMessage > m_cmd_queue
boost::asio::io_context m_io_context
tcp::resolver m_resolver
tcp::socket m_socket
boost::asio::streambuf m_recv_buffer
std::string m_send_buffer
bool m_is_connected
boost::system::error_code m_error_code
std::chrono::steady_clock::time_point m_deadline

Detailed Description

Member Typedef Documentation

◆ tcp

using Slic3r::Utils::TCPConsole::tcp = boost::asio::ip::tcp
private

Constructor & Destructor Documentation

◆ TCPConsole() [1/2]

Slic3r::Utils::TCPConsole::TCPConsole ( )
inline
tcp::resolver m_resolver
Definition TCPConsole.hpp:99
void set_defaults()
Definition TCPConsole.hpp:31
tcp::socket m_socket
Definition TCPConsole.hpp:100
boost::asio::io_context m_io_context
Definition TCPConsole.hpp:98

References m_io_context, m_resolver, m_socket, and set_defaults().

Here is the call graph for this function:

◆ TCPConsole() [2/2]

Slic3r::Utils::TCPConsole::TCPConsole ( const std::string & host_name,
const std::string & port_name )
inline
28 { set_defaults(); set_remote(host_name, port_name); }
void set_remote(const std::string &host_name, const std::string &port_name)
Definition TCPConsole.hpp:60

References m_io_context, m_resolver, m_socket, set_defaults(), and set_remote().

Here is the call graph for this function:

◆ ~TCPConsole()

Slic3r::Utils::TCPConsole::~TCPConsole ( )
default

Member Function Documentation

◆ enqueue_cmd()

bool Slic3r::Utils::TCPConsole::enqueue_cmd ( const SerialMessage & cmd)
inline
66 {
67 // TODO: Add multithread protection to queue
68 m_cmd_queue.push_back(cmd);
69 return true;
70 }
std::deque< SerialMessage > m_cmd_queue
Definition TCPConsole.hpp:96

References m_cmd_queue.

Referenced by Slic3r::MKS::start_print(), and Slic3r::MKS::test().

Here is the caller graph for this function:

◆ error_message()

std::string Slic3r::Utils::TCPConsole::error_message ( ) const
inline
73{ return m_error_code.message(); }
boost::system::error_code m_error_code
Definition TCPConsole.hpp:105

References m_error_code.

Referenced by Slic3r::MKS::start_print(), and Slic3r::MKS::test().

Here is the caller graph for this function:

◆ extract_next_line()

std::string Slic3r::Utils::TCPConsole::extract_next_line ( )
private
72{
73 char linebuf[1024];
74 std::istream is(&m_recv_buffer);
75 is.getline(linebuf, sizeof(linebuf));
76 return is.good() ? linebuf : std::string{};
77}
boost::asio::streambuf m_recv_buffer
Definition TCPConsole.hpp:101

References m_recv_buffer.

Referenced by handle_read().

Here is the caller graph for this function:

◆ handle_connect()

void Slic3r::Utils::TCPConsole::handle_connect ( const boost::system::error_code & ec)
private
135{
136 m_error_code = ec;
137
138 if (ec) {
139 BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't connect to %1%:%2%: %3%")
142 % ec.message();
143
144 m_io_context.stop();
145 }
146 else {
147 m_is_connected = true;
148 BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%")
150 % m_port_name;
151
153 }
154}
std::string m_host_name
Definition TCPConsole.hpp:87
std::string m_port_name
Definition TCPConsole.hpp:88
void transmit_next_command()
Definition TCPConsole.cpp:28
bool m_is_connected
Definition TCPConsole.hpp:104

References m_error_code, m_host_name, m_io_context, m_is_connected, m_port_name, and transmit_next_command().

Referenced by run_queue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_read()

void Slic3r::Utils::TCPConsole::handle_read ( const boost::system::error_code & ec,
std::size_t bytes_transferred )
private
82{
83 m_error_code = ec;
84
85 if (ec) {
86 BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't read from %1%:%2%: %3%")
89 % ec.message();
90
91 m_io_context.stop();
92 }
93 else {
94 std::string line = extract_next_line();
95 boost::trim(line);
96
97 BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: received '%3%' from %1%:%2%")
100 % line;
101
102 boost::to_lower(line);
103
104 if (line == m_done_string)
106 else
108 }
109}
void wait_next_line()
Definition TCPConsole.cpp:59
std::string m_done_string
Definition TCPConsole.hpp:90
std::string extract_next_line()
Definition TCPConsole.cpp:71

References extract_next_line(), m_done_string, m_error_code, m_host_name, m_io_context, m_port_name, transmit_next_command(), and wait_next_line().

Referenced by wait_next_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_write()

void Slic3r::Utils::TCPConsole::handle_write ( const boost::system::error_code & ec,
std::size_t bytes_transferred,
SerialMessageType messageType )
private
114{
115 m_error_code = ec;
116 if (ec) {
117 BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't write to %1%:%2%: %3%")
120 % ec.message();
121
122 m_io_context.stop();
123 }
124 else {
125 if(messageType == Command) {
127 }
128 else {
130 }
131 }
132}
@ Command
Definition SerialMessageType.hpp:6

References Slic3r::Utils::Command, m_error_code, m_host_name, m_io_context, m_port_name, transmit_next_command(), and wait_next_line().

Referenced by transmit_next_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_deadline_over()

bool Slic3r::Utils::TCPConsole::is_deadline_over ( ) const
private
161{
162 return m_deadline < std::chrono::steady_clock::now();
163}
std::chrono::steady_clock::time_point m_deadline
Definition TCPConsole.hpp:106

References m_deadline.

Referenced by run_queue().

Here is the caller graph for this function:

◆ run_queue()

bool Slic3r::Utils::TCPConsole::run_queue ( )
166{
167 try {
168 // TODO: Add more resets and initializations after previous run (reset() method?..)
170 m_is_connected = false;
171 m_io_context.restart();
172
173 auto endpoints = m_resolver.resolve(m_host_name, m_port_name);
174
175 m_socket.async_connect(endpoints->endpoint(),
176 boost::bind(&TCPConsole::handle_connect, this, boost::placeholders::_1)
177 );
178
179 // Loop until we get any reasonable result. Negative result is also result.
180 // TODO: Rewrite to more graceful way using deadlime_timer
181 bool timeout = false;
182 while (!(timeout = is_deadline_over()) && !m_io_context.stopped()) {
183 if (m_error_code) {
184 m_io_context.stop();
185 }
186 m_io_context.run_for(boost::asio::chrono::milliseconds(100));
187 }
188
189 // Override error message if timeout is set
190 if (timeout)
191 m_error_code = make_error_code(boost::asio::error::timed_out);
192
193 // Socket is not closed automatically by boost
194 m_socket.close();
195
196 if (m_error_code) {
197 // We expect that message is logged in handler
198 return false;
199 }
200
201 // It's expected to have empty queue after successful exchange
202 if (!m_cmd_queue.empty()) {
203 BOOST_LOG_TRIVIAL(error) << "TCPConsole: command queue is not empty after end of exchange";
204 return false;
205 }
206 }
207 catch (std::exception& e)
208 {
209 BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Exception while talking with %1%:%2%: %3%")
212 % e.what();
213
214 return false;
215 }
216
217 return true;
218}
void set_deadline_in(std::chrono::steady_clock::duration)
Definition TCPConsole.cpp:156
void handle_connect(const boost::system::error_code &ec)
Definition TCPConsole.cpp:134
std::chrono::steady_clock::duration m_connect_timeout
Definition TCPConsole.hpp:91
bool is_deadline_over() const
Definition TCPConsole.cpp:160

References handle_connect(), is_deadline_over(), m_cmd_queue, m_connect_timeout, m_error_code, m_host_name, m_io_context, m_is_connected, m_port_name, m_resolver, m_socket, and set_deadline_in().

Referenced by Slic3r::MKS::start_print(), and Slic3r::MKS::test().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_command_done_string()

void Slic3r::Utils::TCPConsole::set_command_done_string ( const std::string & done_string)
inline
56 {
57 m_done_string = done_string;
58 }

References m_done_string.

◆ set_deadline_in()

void Slic3r::Utils::TCPConsole::set_deadline_in ( std::chrono::steady_clock::duration d)
private
157{
158 m_deadline = std::chrono::steady_clock::now() + d;
159}

References m_deadline.

Referenced by run_queue(), transmit_next_command(), and wait_next_line().

Here is the caller graph for this function:

◆ set_defaults()

void Slic3r::Utils::TCPConsole::set_defaults ( )
inline
32 {
33 m_newline = "\n";
34 m_done_string = "ok";
35 m_connect_timeout = std::chrono::milliseconds(5000);
36 m_write_timeout = std::chrono::milliseconds(10000);
37 m_read_timeout = std::chrono::milliseconds(10000);
38 m_tcp_queue_delay = std::chrono::milliseconds(0);
39 }
std::chrono::steady_clock::duration m_tcp_queue_delay
Definition TCPConsole.hpp:94
std::chrono::steady_clock::duration m_read_timeout
Definition TCPConsole.hpp:93
std::chrono::steady_clock::duration m_write_timeout
Definition TCPConsole.hpp:92
std::string m_newline
Definition TCPConsole.hpp:89

References m_connect_timeout, m_done_string, m_newline, m_read_timeout, m_tcp_queue_delay, and m_write_timeout.

Referenced by TCPConsole(), and TCPConsole().

Here is the caller graph for this function:

◆ set_line_delimiter()

void Slic3r::Utils::TCPConsole::set_line_delimiter ( const std::string & newline)
inline
53 {
54 m_newline = newline;
55 }

References m_newline.

◆ set_read_timeout()

void Slic3r::Utils::TCPConsole::set_read_timeout ( std::chrono::steady_clock::duration timeout)
inline
45 {
46 m_read_timeout = timeout;
47 }

References m_read_timeout.

◆ set_remote()

void Slic3r::Utils::TCPConsole::set_remote ( const std::string & host_name,
const std::string & port_name )
inline
61 {
62 m_host_name = host_name;
63 m_port_name = port_name;
64 }

References m_host_name, and m_port_name.

Referenced by TCPConsole().

Here is the caller graph for this function:

◆ set_tcp_queue_delay()

void Slic3r::Utils::TCPConsole::set_tcp_queue_delay ( std::chrono::steady_clock::duration delay)
inline
49 {
50 m_tcp_queue_delay = delay;
51 }

References m_tcp_queue_delay.

◆ set_write_timeout()

void Slic3r::Utils::TCPConsole::set_write_timeout ( std::chrono::steady_clock::duration timeout)
inline
41 {
42 m_write_timeout = timeout;
43 }

References m_write_timeout.

◆ transmit_next_command()

void Slic3r::Utils::TCPConsole::transmit_next_command ( )
private
29{
30 if (m_cmd_queue.empty()) {
31 m_io_context.stop();
32 return;
33 }
34
35 std::this_thread::sleep_for(m_tcp_queue_delay);
36
37 SerialMessage cmd = m_cmd_queue.front();
38 m_cmd_queue.pop_front();
39
40 BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: transmitting '%3%' to %1%:%2%")
43 % cmd.message;
44
45 m_send_buffer = cmd.message;
46
47 if (cmd.messageType == Command) {
49 }
50
52 boost::asio::async_write(
54 boost::asio::buffer(m_send_buffer),
55 boost::bind(&TCPConsole::handle_write, this, boost::placeholders::_1, boost::placeholders::_2, cmd.messageType)
56 );
57}
std::string m_send_buffer
Definition TCPConsole.hpp:102
void handle_write(const boost::system::error_code &ec, std::size_t bytes_transferred, SerialMessageType messageType)
Definition TCPConsole.cpp:111

References Slic3r::Utils::Command, handle_write(), m_cmd_queue, m_host_name, m_io_context, m_newline, m_port_name, m_send_buffer, m_socket, m_tcp_queue_delay, m_write_timeout, Slic3r::Utils::SerialMessage::message, Slic3r::Utils::SerialMessage::messageType, and set_deadline_in().

Referenced by handle_connect(), handle_read(), and handle_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait_next_line()

void Slic3r::Utils::TCPConsole::wait_next_line ( )
private
60{
62 boost::asio::async_read_until(
66 boost::bind(&TCPConsole::handle_read, this, boost::placeholders::_1, boost::placeholders::_2)
67 );
68}
void handle_read(const boost::system::error_code &ec, std::size_t bytes_transferred)
Definition TCPConsole.cpp:79

References handle_read(), m_newline, m_read_timeout, m_recv_buffer, m_socket, and set_deadline_in().

Referenced by handle_read(), and handle_write().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_cmd_queue

std::deque<SerialMessage> Slic3r::Utils::TCPConsole::m_cmd_queue
private

◆ m_connect_timeout

std::chrono::steady_clock::duration Slic3r::Utils::TCPConsole::m_connect_timeout
private

Referenced by run_queue(), and set_defaults().

◆ m_deadline

std::chrono::steady_clock::time_point Slic3r::Utils::TCPConsole::m_deadline
private

◆ m_done_string

std::string Slic3r::Utils::TCPConsole::m_done_string
private

◆ m_error_code

boost::system::error_code Slic3r::Utils::TCPConsole::m_error_code
private

◆ m_host_name

std::string Slic3r::Utils::TCPConsole::m_host_name
private

◆ m_io_context

boost::asio::io_context Slic3r::Utils::TCPConsole::m_io_context
private

◆ m_is_connected

bool Slic3r::Utils::TCPConsole::m_is_connected
private

Referenced by handle_connect(), and run_queue().

◆ m_newline

std::string Slic3r::Utils::TCPConsole::m_newline
private

◆ m_port_name

std::string Slic3r::Utils::TCPConsole::m_port_name
private

◆ m_read_timeout

std::chrono::steady_clock::duration Slic3r::Utils::TCPConsole::m_read_timeout
private

◆ m_recv_buffer

boost::asio::streambuf Slic3r::Utils::TCPConsole::m_recv_buffer
private

◆ m_resolver

tcp::resolver Slic3r::Utils::TCPConsole::m_resolver
private

Referenced by TCPConsole(), TCPConsole(), and run_queue().

◆ m_send_buffer

std::string Slic3r::Utils::TCPConsole::m_send_buffer
private

Referenced by transmit_next_command().

◆ m_socket

tcp::socket Slic3r::Utils::TCPConsole::m_socket
private

◆ m_tcp_queue_delay

std::chrono::steady_clock::duration Slic3r::Utils::TCPConsole::m_tcp_queue_delay
private

◆ m_write_timeout

std::chrono::steady_clock::duration Slic3r::Utils::TCPConsole::m_write_timeout
private

The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/TCPConsole.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/TCPConsole.cpp