-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathsecondary.cpp
More file actions
285 lines (243 loc) · 8.43 KB
/
secondary.cpp
File metadata and controls
285 lines (243 loc) · 8.43 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "carla/multigpu/incomingMessage.h"
#include "carla/multigpu/secondary.h"
#include "carla/BufferPool.h"
#include "carla/Debug.h"
#include "carla/Exception.h"
#include "carla/Logging.h"
#include "carla/Time.h"
#include <boost/asio/connect.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/bind_executor.hpp>
#include <exception>
namespace carla {
namespace multigpu {
Secondary::Secondary(
boost::asio::ip::tcp::endpoint ep,
SecondaryCommands::callback_type callback) :
_pool(),
_socket(_pool.io_context()),
_endpoint(ep),
_strand(_pool.io_context()),
_connection_timer(_pool.io_context()),
_buffer_pool(std::make_shared<BufferPool>()) {
_commander.set_callback(callback);
}
Secondary::Secondary(
std::string ip,
uint16_t port,
SecondaryCommands::callback_type callback) :
_pool(),
_socket(_pool.io_context()),
_strand(_pool.io_context()),
_connection_timer(_pool.io_context()),
_buffer_pool(std::make_shared<BufferPool>()) {
boost::asio::ip::address ip_address = boost::asio::ip::address::from_string(ip);
_endpoint = boost::asio::ip::tcp::endpoint(ip_address, port);
_commander.set_callback(callback);
}
Secondary::~Secondary() {
_pool.Stop();
}
void Secondary::Connect() {
AsyncRun(2u);
_commander.set_secondary(shared_from_this());
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [weak]() {
auto self = weak.lock();
if (!self) return;
if (self->_done) {
return;
}
if (self->_socket.is_open()) {
self->_socket.close();
}
auto handle_connect = [weak](boost::system::error_code ec) {
auto self = weak.lock();
if (!self) return;
if (ec) {
log_error("secondary server: connection failed:", ec.message());
if (!self->_done)
self->Reconnect();
return;
}
if (self->_done) {
return;
}
// This forces not using Nagle's algorithm.
// Improves the sync mode velocity on Linux by a factor of ~3.
self->_socket.set_option(boost::asio::ip::tcp::no_delay(true));
log_info("secondary server: connected to ", self->_endpoint);
self->ReadData();
};
self->_socket.async_connect(self->_endpoint, boost::asio::bind_executor(self->_strand, handle_connect));
});
}
void Secondary::Stop() {
_connection_timer.cancel();
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [weak]() {
auto self = weak.lock();
if (!self) return;
self->_done = true;
if (self->_socket.is_open()) {
self->_socket.close();
}
});
}
void Secondary::Reconnect() {
std::weak_ptr<Secondary> weak = shared_from_this();
_connection_timer.expires_from_now(time_duration::seconds(1u));
_connection_timer.async_wait([weak](boost::system::error_code ec) {
auto self = weak.lock();
if (!self) return;
if (!ec) {
self->Connect();
}
});
}
void Secondary::AsyncRun(size_t worker_threads) {
_pool.AsyncRun(worker_threads);
}
void Secondary::Write(std::shared_ptr<const carla::streaming::detail::tcp::Message> message) {
DEBUG_ASSERT(message != nullptr);
DEBUG_ASSERT(!message->empty());
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [=]() {
auto self = weak.lock();
if (!self) return;
if (!self->_socket.is_open()) {
return;
}
auto handle_sent = [weak, message](const boost::system::error_code &ec, size_t) {
auto self = weak.lock();
if (!self) return;
if (ec) {
log_error("error sending data: ", ec.message());
}
};
// _deadline.expires_from_now(_timeout);
boost::asio::async_write(
self->_socket,
message->GetBufferSequence(),
boost::asio::bind_executor(self->_strand, handle_sent));
});
}
void Secondary::Write(Buffer buffer) {
auto view_data = carla::BufferView::CreateFrom(std::move(buffer));
auto message = Secondary::MakeMessage(view_data);
DEBUG_ASSERT(message != nullptr);
DEBUG_ASSERT(!message->empty());
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [=]() {
auto self = weak.lock();
if (!self) return;
if (!self->_socket.is_open()) {
return;
}
auto handle_sent = [weak, message](const boost::system::error_code &ec, size_t) {
auto self = weak.lock();
if (!self) return;
if (ec) {
log_error("error sending data: ", ec.message());
}
};
// _deadline.expires_from_now(_timeout);
boost::asio::async_write(
self->_socket,
message->GetBufferSequence(),
boost::asio::bind_executor(self->_strand, handle_sent));
});
}
void Secondary::Write(std::string text) {
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [=]() {
auto self = weak.lock();
if (!self) return;
if (!self->_socket.is_open()) {
return;
}
auto handle_sent = [weak](const boost::system::error_code &ec, size_t) {
auto self = weak.lock();
if (!self) return;
if (ec) {
log_error("error sending data: ", ec.message());
}
};
// _deadline.expires_from_now(_timeout);
// sent first size buffer
int this_size = static_cast<int>(text.size());
boost::asio::async_write(
self->_socket,
boost::asio::buffer(&this_size, sizeof(this_size)),
boost::asio::bind_executor(self->_strand, handle_sent));
// send characters
boost::asio::async_write(
self->_socket,
boost::asio::buffer(text.c_str(), text.size()),
boost::asio::bind_executor(self->_strand, handle_sent));
});
}
void Secondary::ReadData() {
std::weak_ptr<Secondary> weak = shared_from_this();
boost::asio::post(_strand, [weak]() {
auto self = weak.lock();
if (!self) return;
if (self->_done) {
return;
}
auto message = std::make_shared<IncomingMessage>(self->_buffer_pool->Pop());
auto handle_read_data = [weak, message](boost::system::error_code ec, size_t DEBUG_ONLY(bytes)) {
auto self = weak.lock();
if (!self) return;
if (!ec) {
DEBUG_ASSERT_EQ(bytes, message->size());
DEBUG_ASSERT_NE(bytes, 0u);
// Move the buffer to the callback function and start reading the next
// piece of data.
self->GetCommander().process_command(message->pop());
self->ReadData();
} else {
// As usual, if anything fails start over from the very top.
log_error("secondary server: failed to read data: ", ec.message());
// Connect();
}
};
auto handle_read_header = [weak, message, handle_read_data](
boost::system::error_code ec,
size_t DEBUG_ONLY(bytes)) {
auto self = weak.lock();
if (!self) return;
if (!ec && (message->size() > 0u)) {
DEBUG_ASSERT_EQ(bytes, sizeof(carla::streaming::detail::message_size_type));
if (self->_done) {
return;
}
// Now that we know the size of the coming buffer, we can allocate our
// buffer and start putting data into it.
boost::asio::async_read(
self->_socket,
message->buffer(),
boost::asio::bind_executor(self->_strand, handle_read_data));
} else if (!self->_done) {
log_error("secondary server: failed to read header: ", ec.message());
// DEBUG_ONLY(printf("size = ", message->size()));
// DEBUG_ONLY(printf("bytes = ", bytes));
// Connect();
}
};
// Read the size of the buffer that is coming.
boost::asio::async_read(
self->_socket,
message->size_as_buffer(),
boost::asio::bind_executor(self->_strand, handle_read_header));
});
}
} // namespace multigpu
} // namespace carla