-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrcp_client.cpp
211 lines (187 loc) · 5.34 KB
/
rrcp_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// rrcp_client.cpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Moderniced from Claus Klein and ChatGPT
#include <boost/algorithm/string/trim.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/system/error_code.hpp>
#include <cassert>
#include <chrono> // NOLINT(misc-include-cleaner)
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <iomanip>
#include <iostream>
#include <string>
#include <thread>
#include "rrcp_helper.hpp"
#include "rrcp_message.hpp"
using boost::asio::ip::tcp;
using rrcp_message_queue = std::deque< rrcp_message >;
class rrcp_client
{
public:
rrcp_client(boost::asio::io_context& io_context, const tcp::resolver::results_type& endpoints)
: io_context_(io_context), socket_(io_context)
{
do_connect(endpoints);
}
void write(const rrcp_message& msg)
{
boost::asio::post(io_context_,
[this, msg]()
{
bool const write_in_progress{!write_msgs_.empty()};
write_msgs_.push_back(msg);
if (!write_in_progress)
{
do_write();
}
});
}
void close()
{
boost::asio::post(io_context_, [this]() { write_msgs_.clear(); });
boost::asio::post(io_context_, [this]() { socket_.close(); });
}
private:
void do_connect(const tcp::resolver::results_type& endpoints)
{
boost::asio::async_connect(socket_, endpoints,
[this](boost::system::error_code ec, const tcp::endpoint&)
{
if (!ec)
{
do_read_header();
}
});
}
void do_read_header()
{
boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), rrcp_message::HEADER_LENGTH),
[this](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec && read_msg_.decode_header())
{
do_read_body();
}
else
{
socket_.close();
}
});
}
void do_read_body()
{
boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
[this](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec && read_msg_.decode_body())
{
// NOLINTNEXTLINE(bugprone-narrowing-conversions)
std::cout.write(read_msg_.body(), read_msg_.body_length());
std::cout << "\n";
do_read_header();
}
else
{
socket_.close();
}
});
}
void do_write()
{
boost::asio::async_write(socket_, boost::asio::buffer(write_msgs_.front().data(), write_msgs_.front().length()),
[this](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
do_write();
}
}
else
{
socket_.close();
}
});
}
boost::asio::io_context& io_context_;
tcp::socket socket_;
rrcp_message read_msg_;
rrcp_message_queue write_msgs_;
};
auto main(int argc, char* argv[]) -> int
{
using namespace std::chrono_literals;
using namespace std::string_literals;
try
{
if (argc != 3)
{
std::cerr << "Usage: rrcp_client <host> <port>\n";
return EXIT_FAILURE;
}
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve(argv[1], argv[2]);
rrcp_client c(io_context, endpoints);
std::thread t([&io_context]() { io_context.run(); });
//================================================================
std::string binary{"\nAB_(\0\001\002\003\004\005\006\a\b\n\r\t\v\x1b\20\'\"\?)-CD\r"s};
std::cerr << binary.length() << ' ' << std::quoted(binary) << '\n';
auto quoted = char2esc(binary);
std::cerr << quoted.length() << ' ' << std::quoted(quoted) << '\n';
assert(binary == esc2char(quoted));
assert(binary.length() < quoted.length());
assert(binary.length() == 28);
assert(quoted.length() == 33);
//================================================================
int i{};
std::this_thread::sleep_for(100ms); // NOLINT(misc-include-cleaner)
for (std::string line; std::getline(std::cin, line);)
{
const std::string::size_type sz = line.find("//");
if ((sz != std::string::npos))
{
line.resize(sz);
}
boost::trim_right(line);
if (line.empty())
{
continue;
}
std::cerr << ++i << '\t' << line << '\n';
rrcp_message msg;
msg.body_length(line.length());
std::memcpy(msg.body(), line.c_str(), msg.body_length());
msg.encode_body();
msg.encode_header();
c.write(msg);
}
std::this_thread::sleep_for(100ms); // NOLINT(misc-include-cleaner)
c.close();
t.join();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}