-
Notifications
You must be signed in to change notification settings - Fork 167
/
ecal_udp_sample_receiver.cpp
362 lines (329 loc) 路 13.4 KB
/
ecal_udp_sample_receiver.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/
/**
* @brief UDP sample receiver to receive messages of type eCAL::pb::Sample
**/
#include "ecal_udp_sample_receiver.h"
#include "io/udp/fragmentation/msg_type.h"
#include <ecal/ecal_log.h>
namespace eCAL
{
namespace UDP
{
CSampleReceiver::CSampleDefragmentation::CSampleDefragmentation(CSampleReceiver* sample_receiver_)
: m_sample_receiver(sample_receiver_)
{
}
CSampleReceiver::CSampleDefragmentation::~CSampleDefragmentation() = default;
int CSampleReceiver::CSampleDefragmentation::OnMessageCompleted(std::vector<char>&& msg_buffer_)
{
if (m_sample_receiver == nullptr) return(0);
// read sample_name size
const unsigned short sample_name_size = ((unsigned short*)(msg_buffer_.data()))[0];
// read sample_name
const std::string sample_name(msg_buffer_.data() + sizeof(sample_name_size));
if (m_sample_receiver->m_has_sample_callback(sample_name))
{
// read sample
if (!m_ecal_sample.ParseFromArray(msg_buffer_.data() + sizeof(sample_name_size) + sample_name_size, static_cast<int>(msg_buffer_.size() - (sizeof(sample_name_size) + sample_name_size)))) return(0);
#ifndef NDEBUG
// log it
eCAL::Logging::Log(log_level_debug3, sample_name + "::UDP Sample Completed");
// log it
switch (m_ecal_sample.cmd_type())
{
case eCAL::pb::bct_none:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - NONE");
break;
case eCAL::pb::bct_set_sample:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - SAMPLE");
break;
case eCAL::pb::bct_reg_publisher:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER PUBLISHER");
break;
case eCAL::pb::bct_reg_subscriber:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER SUBSCRIBER");
break;
case eCAL::pb::bct_reg_process:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER PROCESS");
break;
case eCAL::pb::bct_reg_service:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER SERVER");
break;
case eCAL::pb::bct_reg_client:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER CLIENT");
break;
default:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - UNKNOWN");
break;
}
#endif
// get layer if this is a payload sample
eCAL::pb::eTLayerType layer = eCAL::pb::eTLayerType::tl_none;
if (m_ecal_sample.cmd_type() == eCAL::pb::eCmdType::bct_set_sample)
{
if (m_ecal_sample.topic().tlayer_size() > 0)
{
layer = m_ecal_sample.topic().tlayer(0).type();
}
}
// apply sample
m_sample_receiver->m_apply_sample_callback(m_ecal_sample, layer);
}
return(0);
}
CSampleReceiver::CSampleReceiver(const IO::UDP::SReceiverAttr& attr_, HasSampleCallbackT has_sample_callback_, ApplySampleCallbackT apply_sample_callback_) :
m_has_sample_callback(has_sample_callback_), m_apply_sample_callback(apply_sample_callback_)
{
// create udp receiver
m_udp_receiver.Create(attr_);
// allocate receive buffer
m_msg_buffer.resize(MSG_BUFFER_SIZE);
// start receiver thread
m_udp_receiver_thread = std::make_shared<eCAL::CCallbackThread>(std::bind(&CSampleReceiver::ReceiveThread, this));
m_udp_receiver_thread->start(std::chrono::milliseconds(0));
m_cleanup_start = std::chrono::steady_clock::now();
}
CSampleReceiver::~CSampleReceiver()
{
// stop receiver thread
m_udp_receiver_thread->stop();
// destroy udp receiver
m_udp_receiver.Destroy();
}
bool CSampleReceiver::AddMultiCastGroup(const char* ipaddr_)
{
return m_udp_receiver.AddMultiCastGroup(ipaddr_);
}
bool CSampleReceiver::RemMultiCastGroup(const char* ipaddr_)
{
return m_udp_receiver.RemMultiCastGroup(ipaddr_);
}
void CSampleReceiver::ReceiveThread()
{
// wait for any incoming message
const size_t recv_len = m_udp_receiver.Receive(m_msg_buffer.data(), m_msg_buffer.size(), CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS);
if (recv_len > 0)
{
Process(m_msg_buffer.data(), recv_len);
}
}
void CSampleReceiver::Process(const char* sample_buffer_, size_t sample_buffer_len_)
{
// we need at least the header information to start
if (sample_buffer_len_ < sizeof(IO::UDP::SUDPMessageHead)) return;
// cast buffer to udp message struct
struct IO::UDP::SUDPMessage* ecal_message = (struct IO::UDP::SUDPMessage*)sample_buffer_;
// check for eCAL 4.x header
if (
(ecal_message->header.head[0] == 'e')
&& (ecal_message->header.head[1] == 'C')
&& (ecal_message->header.head[2] == 'A')
&& (ecal_message->header.head[3] == 'L')
)
{
eCAL::Logging::Log(log_level_warning, "Received eCAL 4 traffic");
return;
}
// check for valid header
else if (
(ecal_message->header.head[0] != 'E')
|| (ecal_message->header.head[1] != 'C')
|| (ecal_message->header.head[2] != 'A')
|| (ecal_message->header.head[3] != 'L')
)
{
eCAL::Logging::Log(log_level_warning, "Received invalid traffic (eCAL Header missing)");
return;
}
// check integrity
switch (ecal_message->header.type)
{
case IO::UDP::msg_type_header:
break;
case IO::UDP::msg_type_content:
case IO::UDP::msg_type_header_with_content:
if (sample_buffer_len_ < sizeof(IO::UDP::SUDPMessageHead) + static_cast<size_t>(ecal_message->header.len))
return;
break;
default:
return;
}
#ifndef NDEBUG
// log it
switch (ecal_message->header.type)
{
case IO::UDP::msg_type_header_with_content:
eCAL::Logging::Log(log_level_debug4, "UDP Sample Received - HEADER_WITH_CONTENT");
break;
case IO::UDP::msg_type_header:
eCAL::Logging::Log(log_level_debug4, "UDP Sample Received - HEADER");
break;
case IO::UDP::msg_type_content:
eCAL::Logging::Log(log_level_debug4, "UDP Sample Received - CONTENT");
break;
}
#endif
switch (ecal_message->header.type)
{
case IO::UDP::msg_type_header_with_content:
{
// read sample_name size
unsigned short sample_name_size = 0;
memcpy(&sample_name_size, ecal_message->payload, 2);
// read sample_name
const std::string sample_name = ecal_message->payload + sizeof(sample_name_size);
if (m_has_sample_callback(sample_name))
{
// read sample
if (!m_ecal_sample.ParseFromArray(ecal_message->payload + static_cast<size_t>(sizeof(sample_name_size) + sample_name_size), static_cast<int>(static_cast<size_t>(ecal_message->header.len) - (sizeof(sample_name_size) + sample_name_size)))) return;
#ifndef NDEBUG
// log it
eCAL::Logging::Log(log_level_debug3, sample_name + "::UDP Sample Completed");
// log it
switch (m_ecal_sample.cmd_type())
{
case eCAL::pb::bct_none:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - NONE");
break;
case eCAL::pb::bct_set_sample:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - SAMPLE");
break;
case eCAL::pb::bct_reg_publisher:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER PUBLISHER");
break;
case eCAL::pb::bct_reg_subscriber:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER SUBSCRIBER");
break;
case eCAL::pb::bct_reg_process:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER PROCESS");
break;
case eCAL::pb::bct_reg_service:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER SERVICE");
break;
case eCAL::pb::bct_reg_client:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - REGISTER CLIENT");
break;
default:
eCAL::Logging::Log(log_level_debug4, sample_name + "::UDP Sample Command Type - UNKNOWN");
break;
}
#endif
// get layer if this is a payload sample
eCAL::pb::eTLayerType layer = eCAL::pb::eTLayerType::tl_none;
if (m_ecal_sample.cmd_type() == eCAL::pb::eCmdType::bct_set_sample)
{
if (m_ecal_sample.topic().tlayer_size() > 0)
{
layer = m_ecal_sample.topic().tlayer(0).type();
}
}
// apply sample
m_apply_sample_callback(m_ecal_sample, layer);
}
}
break;
// if we have a header only package
// we create a receive defragmentation buffer and apply it to the receive defragmentation map
// to process the following data packages
// we do not know the name here unfortunately
// so we have to wait for the first payload package :-(
case IO::UDP::msg_type_header:
{
// create new receive defragmentation buffer
std::shared_ptr<CSampleDefragmentation> receive_defragmentation_buf(nullptr);
receive_defragmentation_buf = std::make_shared<CSampleDefragmentation>(CSampleDefragmentation(this));
m_defrag_sample_map[ecal_message->header.id] = receive_defragmentation_buf;
// apply message
receive_defragmentation_buf->ApplyMessage(*ecal_message);
}
break;
// if we have a payload package
// we check for an existing receive defragmentation buffer and apply the data to it
case IO::UDP::msg_type_content:
{
// first data package ?
if (ecal_message->header.num == 0)
{
// read sample_name size
unsigned short sample_name_size = 0;
memcpy(&sample_name_size, ecal_message->payload, 2);
// read sample_name
const std::string sample_name = ecal_message->payload + sizeof(sample_name_size);
// remove the matching defragmentation buffer if we are not interested in this sample
if (!m_has_sample_callback(sample_name))
{
auto riter = m_defrag_sample_map.find(ecal_message->header.id);
if (riter != m_defrag_sample_map.end())
{
#ifndef NDEBUG
// log timeouted defragmentation buffers
eCAL::Logging::Log(log_level_debug3, "CUDPSampleReceiver::Receive - DISCARD PACKAGE FOR TOPIC: " + sample_name);
#endif
m_defrag_sample_map.erase(riter);
break;
}
}
}
// process data package
auto iter = m_defrag_sample_map.find(ecal_message->header.id);
if (iter != m_defrag_sample_map.end())
{
// apply message
iter->second->ApplyMessage(*ecal_message);
}
}
break;
default:
break;
}
// cleanup finished or zombie received defragmentation buffers
auto diff_time = std::chrono::steady_clock::now() - m_cleanup_start;
const std::chrono::duration<double> step_time = std::chrono::milliseconds(NET_UDP_RECBUFFER_CLEANUP);
if (diff_time > step_time)
{
m_cleanup_start = std::chrono::steady_clock::now();
for (SampleDefragmentationMapT::iterator riter = m_defrag_sample_map.begin(); riter != m_defrag_sample_map.end();)
{
const bool finished = riter->second->HasFinished();
const bool timeouted = riter->second->HasTimedOut(step_time);
if (finished || timeouted)
{
#ifndef NDEBUG
const int32_t total_len = riter->second->GetMessageTotalLength();
const int32_t current_len = riter->second->GetMessageCurrentLength();
#endif
riter = m_defrag_sample_map.erase(riter);
#ifndef NDEBUG
// log timeouted defragmentation buffer
if (timeouted)
{
eCAL::Logging::Log(log_level_debug3, "CUDPSampleReceiver::Receive - TIMEOUT (TotalLength / CurrentLength): " + std::to_string(total_len) + " / " + std::to_string(current_len));
}
#endif
}
else
{
++riter;
}
}
}
}
}
}