-
Notifications
You must be signed in to change notification settings - Fork 169
/
snd_raw_buffer.cpp
187 lines (157 loc) · 5.61 KB
/
snd_raw_buffer.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
/* ========================= 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 raw message buffer handling
**/
#include <thread>
#include "ecal_process.h"
#include "snd_raw_buffer.h"
#include "io/msg_type.h"
namespace
{
// random number generator
unsigned long xorshf96(unsigned long& x, unsigned long& y, unsigned long& z) // period 2^96-1
{
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
}
namespace eCAL
{
size_t CreateSampleBuffer(const std::string& sample_name_, const eCAL::pb::Sample& ecal_sample_, std::vector<char>& payload_)
{
const unsigned short sample_name_size = (unsigned short)sample_name_.size() + 1;
#if GOOGLE_PROTOBUF_VERSION >= 3001000
const size_t sample_size = ecal_sample_.ByteSizeLong();
#else
size_t sample_size = ecal_sample_.ByteSize();
#endif
const size_t data_size = sizeof(sample_name_size) + sample_name_size + sample_size;
// create payload buffer with reserved space for first message head
payload_.resize(data_size + sizeof(struct SUDPMessageHead));
char* payload_data = payload_.data() + sizeof(struct SUDPMessageHead);
// write topic name size
((unsigned short*)payload_data)[0] = sample_name_size;
// write topic name
memcpy(payload_data + sizeof(sample_name_size), sample_name_.c_str(), sample_name_size);
// write payload
if (ecal_sample_.SerializeWithCachedSizesToArray((google::protobuf::uint8*)payload_data + sizeof(sample_name_size) + sample_name_size))
{
return data_size;
}
return (0);
}
size_t SendSampleBuffer(char* buf_, size_t buf_len_, long bandwidth_, TransmitCallbackT transmit_cb_)
{
if (!buf_) return(0);
size_t sent(0);
size_t sent_sum(0);
int32_t total_packet_num = int32_t(buf_len_ / MSG_PAYLOAD_SIZE);
if (buf_len_%MSG_PAYLOAD_SIZE) total_packet_num++;
// create message header
struct SUDPMessageHead msg_header;
switch (total_packet_num)
{
case 1:
{
// create start packet
msg_header.type = msg_type_header_with_content;
msg_header.id = -1; // not needed for combined header / data message
msg_header.num = 1;
msg_header.len = int32_t(buf_len_);
// copy msg_header in send buffer
memcpy(buf_, &msg_header, sizeof(struct SUDPMessageHead));
// send single header + data package
sent = transmit_cb_(buf_, sizeof(struct SUDPMessageHead) + buf_len_);
if (sent == 0) return(sent);
sent_sum += sent;
#ifndef NDEBUG
// log it
//std::cout << "SendRawBuffer Packet Sent - HEADER_WITH_CONTENT (" + std::to_string(sent) + " Bytes)" << std::endl;
#endif
}
break;
default:
{
// calculate bandwidth timing parameter
long long send_sleep_us(0);
if (bandwidth_ > 0)
{
send_sleep_us = MSG_BUFFER_SIZE;
send_sleep_us *= 1000 * 1000;
send_sleep_us /= bandwidth_;
}
// create start package
msg_header.type = msg_type_header;
{
// create random number for message id
static unsigned long x = 123456789, y = 362436069, z = 521288629;
msg_header.id = xorshf96(x, y, z);
}
msg_header.num = total_packet_num;
msg_header.len = int32_t(buf_len_);
// send start package
sent = transmit_cb_(&msg_header, sizeof(struct SUDPMessageHead));
if (sent == 0) return(sent);
sent_sum += sent;
#ifndef NDEBUG
// log it
//std::cout << "SendRawBuffer Packet Sent - HEADER (" + std::to_string(sent) + " Bytes)" << std::endl;
#endif
// send data packages
msg_header.type = msg_type_content;
for (int32_t current_packet_num = 0; current_packet_num < total_packet_num; current_packet_num++)
{
// calculate current payload
size_t current_snd_len = buf_len_;
if (current_snd_len > MSG_PAYLOAD_SIZE) current_snd_len = MSG_PAYLOAD_SIZE;
if (current_snd_len > 0)
{
// reduce total send len
buf_len_ -= current_snd_len;
// create data packet numbering
msg_header.num = current_packet_num;
msg_header.len = int32_t(current_snd_len);
// copy msg_header in send buffer
memcpy(buf_ + static_cast<size_t>(current_packet_num)*MSG_PAYLOAD_SIZE, &msg_header, sizeof(struct SUDPMessageHead));
// send data package
sent = transmit_cb_(buf_ + static_cast<size_t>(current_packet_num)*MSG_PAYLOAD_SIZE, sizeof(struct SUDPMessageHead) + current_snd_len);
if (sent == 0) return(sent);
if (send_sleep_us)
eCAL::Process::SleepFor(std::chrono::microseconds(send_sleep_us));
#ifndef NDEBUG
// log it
//std::cout << "SendRawBuffer Packet Sent - CONTENT (" + std::to_string(sent) + " Bytes)" << std::endl;
#endif
sent_sum += sent;
}
}
}
break;
}
return(sent_sum);
}
}