1
- #include < iostream>
2
- #include < array>
3
- #include < fcntl.h>
4
- #include < unistd.h>
5
- #include < termios.h>
6
-
7
- #include < boost/asio.hpp>
8
- #include < boost/program_options.hpp>
9
- #include < boost/log/core.hpp>
10
- #include < boost/log/trivial.hpp>
11
- #include < boost/log/expressions.hpp>
12
- #include < boost/log/utility/setup/file.hpp>
13
- #include < boost/log/utility/setup/console.hpp>
14
- #include < boost/log/utility/setup/common_attributes.hpp>
15
- #include < boost/log/sources/severity_logger.hpp>
16
- #include < boost/log/sources/record_ostream.hpp>
17
- #include < boost/log/support/date_time.hpp>
18
-
19
- #include " logging.h"
20
- #include " VirtualSerialPort.h"
1
+ #include " client.h"
21
2
22
3
using namespace boost ::asio;
23
4
using namespace boost ::program_options;
@@ -43,49 +24,39 @@ void init_logging() {
43
24
boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char >(" Severity" );
44
25
}
45
26
46
- class SerialClient {
47
- private:
48
- io_service io_service_;
49
- tcp::socket socket_;
50
- std::array<char , 256 > buffer_;
51
- VirtualSerialPort vsp_;
52
-
53
- public:
54
- SerialClient (const string& server_ip, unsigned short server_port, const string& vsp_name)
55
- : socket_(io_service_), vsp_(vsp_name) {
56
- BOOST_LOG_TRIVIAL (info) << " Initializing client..." ;
57
- tcp::resolver resolver (io_service_);
58
- auto endpoint_iterator = resolver.resolve ({server_ip, std::to_string (server_port)});
59
- BOOST_LOG_TRIVIAL (info) << " Connecting to server at " << server_ip << " :" << server_port;
60
- connect (socket_, endpoint_iterator);
61
- BOOST_LOG_TRIVIAL (info) << " Connected to server." ;
62
- BOOST_LOG_TRIVIAL (info) << " Opening virtual serial port: " << vsp_name;
63
- }
27
+ SerialClient::SerialClient (const string& server_ip, unsigned short server_port, const string& vsp_name)
28
+ : socket_(io_service_), vsp_(vsp_name) {
29
+ BOOST_LOG_TRIVIAL (info) << " Initializing client..." ;
30
+ tcp::resolver resolver (io_service_);
31
+ auto endpoint_iterator = resolver.resolve ({server_ip, std::to_string (server_port)});
32
+ BOOST_LOG_TRIVIAL (info) << " Connecting to server at " << server_ip << " :" << server_port;
33
+ connect (socket_, endpoint_iterator);
34
+ BOOST_LOG_TRIVIAL (info) << " Connected to server." ;
35
+ BOOST_LOG_TRIVIAL (info) << " Opening virtual serial port: " << vsp_name;
36
+ }
64
37
65
- void run () {
66
- BOOST_LOG_TRIVIAL (info) << " Starting client I/O operations." ;
67
- do_read_write ();
68
- io_service_.run ();
69
- }
38
+ void SerialClient:: run () {
39
+ BOOST_LOG_TRIVIAL (info) << " Starting client I/O operations." ;
40
+ do_read_write ();
41
+ io_service_.run ();
42
+ }
70
43
71
- private:
72
- void do_read_write () {
73
- socket_.async_read_some (boost::asio::buffer (buffer_), [this ](boost::system::error_code ec, std::size_t length) {
74
- if (!ec) {
75
- string data (buffer_.data (), length);
76
- BOOST_LOG_TRIVIAL (info) << " Received data: " << data;
77
- if (vsp_.write (data)) {
78
- BOOST_LOG_TRIVIAL (info) << " Data written to virtual serial port." ;
79
- } else {
80
- BOOST_LOG_TRIVIAL (error) << " Failed to write to virtual serial port." ;
81
- }
82
- do_read_write ();
44
+ void SerialClient::do_read_write () {
45
+ socket_.async_read_some (boost::asio::buffer (buffer_), [this ](boost::system::error_code ec, std::size_t length) {
46
+ if (!ec) {
47
+ string data (buffer_.data (), length);
48
+ BOOST_LOG_TRIVIAL (info) << " Received data: " << data;
49
+ if (vsp_.write (data)) {
50
+ BOOST_LOG_TRIVIAL (info) << " Data written to virtual serial port." ;
83
51
} else {
84
- BOOST_LOG_TRIVIAL (error) << " Read error: " << ec. message () ;
52
+ BOOST_LOG_TRIVIAL (error) << " Failed to write to virtual serial port. " ;
85
53
}
86
- });
87
- }
88
- };
54
+ do_read_write ();
55
+ } else {
56
+ BOOST_LOG_TRIVIAL (error) << " Read error: " << ec.message ();
57
+ }
58
+ });
59
+ }
89
60
90
61
int main (int argc, char * argv[]) {
91
62
init_logging ();
0 commit comments