Files
OrcaSlicer/src/slic3r/Utils/TCPConsole.hpp
T
HanifKoh 30902561c0 Stop Exporting Names Through Usings in the Remaining Headers (#16250)
The last headers with a using or namespace alias at namespace scope:

- TCPConsole.hpp imported boost::asio::ip::tcp into Slic3r::Utils for
  two member declarations. The alias is now a private member of the
  class.
- WebSocketClient.hpp declared four namespace aliases and a tcp alias
  at global scope, each used only by the header. The names are spelled
  out.
- Repair.hpp aliased CGAL::Polygon_mesh_processing as PMP in
  Slic3r::tex2color. The three functions that use it declare the alias
  themselves.
- PreciseSeam.hpp, Thumbnails.hpp and MarchingSquares.hpp used a
  using-declaration or directive for one or two spots each; those spots
  are qualified. Thumbnails.hpp's "PNG"sv default argument becomes
  "PNG", which converts to the std::string_view parameter the same way.
- tests/sla_print/sla_test_utils.hpp had "using namespace Slic3r;" and
  tests/filament_group/fg_test_serialization.hpp "using json =
  nlohmann::json;" at global scope. The headers qualify their own names;
  the two SLA test sources get the directive themselves.

Also removed: twelve type aliases in headers that nothing references
(ConflictObjName, CircleSqf, CircleSqd, TRawBuffer, DistanceFunction,
SamePair, ExtruderNozzleInfos, Vec2dEvent, Vec2dsEvent, Vec3dEvent,
t_option, t_optgroups, Plater::fs_path) and a duplicate
fn_ft_job_msg_destroy alias in FileTransferUtils.hpp.
2026-10-08 13:59:12 +08:00

113 lines
3.7 KiB
C++

#ifndef slic3r_Utils_TCPConsole_hpp_
#define slic3r_Utils_TCPConsole_hpp_
#include <chrono>
#include <cstddef>
#include <boost/asio/io_context.hpp>
#include <string>
#include <deque>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/streambuf.hpp>
#include "SerialMessage.hpp"
#include "SerialMessageType.hpp"
namespace Slic3r {
namespace Utils {
// Generic command / response TCP telnet like console class.
// Used by the MKS host to send G-code commands to test connection ("M105") and to start printing ("M23 filename", "M24").
class TCPConsole
{
using tcp = boost::asio::ip::tcp;
public:
TCPConsole() : m_resolver(m_io_context), m_socket(m_io_context) { set_defaults(); }
TCPConsole(const std::string& host_name, const std::string& port_name) : m_resolver(m_io_context), m_socket(m_io_context)
{ set_defaults(); set_remote(host_name, port_name); }
~TCPConsole() = default;
void set_defaults()
{
m_newline = "\n";
m_done_string = "ok";
m_connect_timeout = std::chrono::milliseconds(5000);
m_write_timeout = std::chrono::milliseconds(10000);
m_read_timeout = std::chrono::milliseconds(10000);
m_tcp_queue_delay = std::chrono::milliseconds(0);
}
void set_write_timeout(std::chrono::steady_clock::duration timeout) {
m_write_timeout = timeout;
}
void set_read_timeout(std::chrono::steady_clock::duration timeout) {
m_read_timeout = timeout;
}
void set_tcp_queue_delay(std::chrono::steady_clock::duration delay) {
m_tcp_queue_delay = delay;
}
void set_line_delimiter(const std::string& newline) {
m_newline = newline;
}
void set_command_done_string(const std::string& done_string) {
m_done_string = done_string;
}
void set_remote(const std::string& host_name, const std::string& port_name)
{
m_host_name = host_name;
m_port_name = port_name;
}
bool enqueue_cmd(const SerialMessage& cmd) {
// TODO: Add multithread protection to queue
m_cmd_queue.push_back(cmd);
return true;
}
bool run_queue();
std::string error_message() const { return m_error_code.message(); }
private:
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;
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;
};
} // Utils
} // Slic3r
#endif