-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrrcp_async_tcp_client.cpp
More file actions
110 lines (91 loc) · 2.62 KB
/
Copy pathrrcp_async_tcp_client.cpp
File metadata and controls
110 lines (91 loc) · 2.62 KB
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
/***
* rrcp_async_tcp_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 <fmt/format.h>
#include <boost/algorithm/string/trim.hpp>
#include <chrono>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
// optional #define USE_SIMPLE_RRCP_CLIENT
#ifdef USE_SIMPLE_RRCP_CLIENT
#include "async_rrcp_client.hpp"
#else
#include "async_rrcp_client_threadsafe.hpp"
#endif
namespace
{
void print(std::string msg) { fmt::print("{}\n", msg); }
} // namespace
// NOLINTNEXTLINE(bugprone-exception-escape)
auto main(int argc, char* argv[]) -> int
{
if (argc < 3)
{
fmt::print(stderr, "Usage: {} <host> <port> [input_file]\n", argv[0]); // NOLINT
return EXIT_FAILURE;
}
std::ifstream file; // persistent file object (if used)
std::istream* input_str = &std::cin; // pointer to chosen input stream
if (argc == 4)
{
file.open(argv[3]); // NOLINT
if (!file)
{
fmt::print(stderr, "cannot open input file: {}\n", argv[3]); // NOLINT
return 2;
}
input_str = &file;
}
try
{
using namespace rrcp;
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
auto client = std::make_shared< async_rrcp_client >(io_context);
client->register_trap_handler(&print);
client->start(resolver.resolve(argv[1], argv[2]));
std::thread io_thread([&io_context]() -> void { io_context.run(); });
std::this_thread::sleep_for(TIMEOUT_DURATION); // NOTE: only for gcov results! CK
for (std::string line; client->connected() && std::getline(*input_str, line); fmt::print(stderr, "Enter command: "))
{
const std::string::size_type sz = line.find("//");
if ((sz != std::string::npos))
{
line.resize(sz); // NOTE: w/o c++ comments
}
boost::trim(line);
if (line.empty())
{
continue;
}
if (boost::algorithm::starts_with(line, "E:"))
{
continue;
}
const auto response = client->write(line);
fmt::print("{}\n", response);
}
std::this_thread::sleep_for(HEARTBEAT_INTERVAL); // NOTE: only for gcov results! CK
client->stop();
io_thread.join();
}
catch (const std::exception& e)
{
fmt::print(stderr, "Exception: {}\n", e.what()); // NOLINT
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}