-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
195 lines (165 loc) · 5.22 KB
/
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
//
// Copyright 2018 - 2025 (C). Alex Robenko. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "Client.h"
#include <iostream>
#include <sstream>
#include "cc_demo3/MsgId.h"
#include "comms/units.h"
#include "comms/process.h"
namespace cc_demo3
{
namespace client
{
namespace
{
template <typename TField>
void printVersionDependentField(const TField& f)
{
std::cout << "\t\t" << f.field().name() << " = ";
if (f.isMissing()) {
std::cout << "(missing)";
}
else {
std::cout << static_cast<unsigned>(f.field().value());
}
std::cout << '\n';
}
} // namespace
Client::Client(
common::boost_wrap::io& io,
const std::string& server,
std::uint16_t port)
: m_io(io),
m_socket(io),
m_server(server),
m_port(port)
{
if (m_server.empty()) {
m_server = "localhost";
}
}
bool Client::start()
{
boost::asio::ip::tcp::resolver resolver(m_io);
boost::system::error_code ec;
auto resolveResult = resolver.resolve(m_server, std::to_string(m_port), ec);
if (ec) {
std::cerr << "ERROR: Failed to resolve \"" << m_server << ':' << m_port << "\" " <<
"with error: " << ec.message() << std::endl;
return false;
}
if (resolveResult.empty()) {
std::cerr << "ERROR: No resolution result" << std::endl;
return false;
}
auto endpoint = resolveResult.begin()->endpoint();
m_socket.connect(endpoint, ec);
if (ec) {
std::cerr << "ERROR: Failed to connect to \"" << endpoint << "\" " <<
"with error: " << ec.message() << std::endl;
return false;
}
std::cout << "INFO: Connected to " << m_socket.remote_endpoint() << std::endl;
readDataFromServer();
readDataFromStdin();
return true;
}
void Client::handle(InputMsg&)
{
std::cerr << "WARNING: Unexpected message received" << std::endl;
}
void Client::readDataFromServer()
{
m_socket.async_read_some(
boost::asio::buffer(m_readBuf),
[this](const boost::system::error_code& ec, std::size_t bytesCount)
{
if (ec == boost::asio::error::operation_aborted) {
return;
}
if (ec) {
std::cerr << "ERROR: Failed to read with error: " << ec.message() << std::endl;
m_io.stop();
return;
}
std::cout << "<-- " << std::hex;
std::copy_n(m_readBuf.begin(), bytesCount, std::ostream_iterator<unsigned>(std::cout, " "));
std::cout << std::dec << std::endl;
m_inputBuf.insert(m_inputBuf.end(), m_readBuf.begin(), m_readBuf.begin() + bytesCount);
processInput();
readDataFromServer();
});
}
void Client::readDataFromStdin()
{
std::cout << "\nEnter (new) version to send: " << std::endl;
m_sentVersion = std::numeric_limits<decltype(m_sentVersion)>::max();
do {
// Unfortunatelly Windows doesn't provide an easy way to
// asynchronously read from stdin with boost::asio,
// read synchronously. DON'T COPY-PASTE TO PRODUCTION CODE!!!
std::cin >> m_sentVersion;
if (!std::cin.good()) {
std::cerr << "ERROR: Unexpected input, use numeric value" << std::endl;
std::cin.clear();
std::cin.ignore();
break;
}
sendConnect();
sendMsg1();
} while (false);
common::boost_wrap::post(
m_io,
[this]()
{
readDataFromStdin();
});
}
void Client::sendConnect()
{
cc_demo3::message::Connect<OutputMsg> msg;
comms::cast_assign(msg.field_version().value()) = m_sentVersion;
sendMessage(msg);
}
void Client::sendMsg1()
{
cc_demo3::message::Msg1<OutputMsg> msg;
comms::cast_assign(msg.transportField_version().value()) = m_sentVersion;
msg.doRefresh();
sendMessage(msg);
}
void Client::sendMessage(const OutputMsg& msg)
{
std::vector<std::uint8_t> outputBuf;
outputBuf.reserve(m_frame.length(msg));
auto iter = std::back_inserter(outputBuf);
auto es = m_frame.write(msg, iter, outputBuf.max_size());
if (es == comms::ErrorStatus::UpdateRequired) {
auto updateIter = &outputBuf[0];
es = m_frame.update(updateIter, outputBuf.size());
}
if (es != comms::ErrorStatus::Success) {
static constexpr bool Unexpected_error = false;
static_cast<void>(Unexpected_error);
assert(Unexpected_error);
return;
}
std::cout << "INFO: Sending message: " << msg.name() << '\n';
std::cout << "--> " << std::hex;
std::copy(outputBuf.begin(), outputBuf.end(), std::ostream_iterator<unsigned>(std::cout, " "));
std::cout << std::dec << std::endl;
m_socket.send(boost::asio::buffer(outputBuf));
}
void Client::processInput()
{
if (!m_inputBuf.empty()) {
auto consumed = comms::processAllWithDispatch(&m_inputBuf[0], m_inputBuf.size(), m_frame, *this);
m_inputBuf.erase(m_inputBuf.begin(), m_inputBuf.begin() + consumed);
}
}
} // namespace client
} // namespace cc_demo3