|
25 | 25 | #include "mock_server.hpp" |
26 | 26 |
|
27 | 27 | #include <string.h> |
| 28 | +#include <string> |
28 | 29 |
|
29 | 30 | #if defined(PLATFORM_UNIX) |
30 | 31 | # include <fcntl.h> |
@@ -190,43 +191,81 @@ bool mock_server::wait_for_data(std::optional<double> wait_timeout_sec) |
190 | 191 | return !!count; |
191 | 192 | } |
192 | 193 |
|
| 194 | +int32_t bytes_to_int32_le(const std::byte* bytes) |
| 195 | +{ |
| 196 | + return static_cast<int32_t>( |
| 197 | + (bytes[0] << 0) | (bytes[1] << 8) | (bytes[2] << 16) | |
| 198 | + (bytes[3] << 24)); |
| 199 | +} |
| 200 | + |
193 | 201 | size_t mock_server::recv(double wait_timeout_sec) |
194 | 202 | { |
195 | 203 | if (!wait_for_data(wait_timeout_sec)) |
196 | 204 | return 0; |
197 | 205 |
|
198 | | - char chunk[1024]; |
| 206 | + std::byte chunk[1024]; |
199 | 207 | size_t chunk_len{sizeof(chunk)}; |
200 | | - std::vector<char> accum; |
| 208 | + std::vector<std::byte> accum; |
201 | 209 | for (;;) |
202 | 210 | { |
203 | 211 | wait_for_data(); |
204 | | - sock_ssize_t count = |
205 | | - ::recv(_conn_fd, &chunk[0], static_cast<sock_len_t>(chunk_len), 0); |
| 212 | + sock_ssize_t count = ::recv( |
| 213 | + _conn_fd, |
| 214 | + reinterpret_cast<char*>(&chunk[0]), |
| 215 | + static_cast<sock_len_t>(chunk_len), |
| 216 | + 0); |
206 | 217 | if (count == -1) |
207 | 218 | throw std::runtime_error{"Bad `recv()`."}; |
208 | 219 | const size_t u_count = static_cast<size_t>(count); |
209 | 220 | accum.insert(accum.end(), chunk, chunk + u_count); |
210 | 221 | if (accum.size() < 2) |
211 | 222 | continue; |
212 | | - if ((accum[accum.size() - 1] == '\n') && |
213 | | - (accum[accum.size() - 2] != '\\')) |
| 223 | + if ((accum[accum.size() - 1] == std::byte('\n')) && |
| 224 | + (accum[accum.size() - 2] != std::byte('\\'))) |
214 | 225 | break; |
215 | 226 | } |
216 | 227 |
|
217 | 228 | size_t received_count{0}; |
218 | | - const char* head{&accum[0]}; |
219 | | - for (size_t index = 1; index < accum.size(); ++index) |
| 229 | + const std::byte* head{&accum[0]}; |
| 230 | + size_t index{1}; |
| 231 | + while (index < accum.size()) |
220 | 232 | { |
221 | | - const char& last = accum[index]; |
222 | | - const char& prev = accum[index - 1]; |
223 | | - if ((last == '\n') && (prev != '\\')) |
| 233 | + const std::byte& last = accum[index]; |
| 234 | + const std::byte& prev = accum[index - 1]; |
| 235 | + if (last == std::byte('=') && prev == std::byte('=')) |
| 236 | + { |
| 237 | + index++; |
| 238 | + std::byte& binary_type = accum[index]; |
| 239 | + if (binary_type == std::byte(16)) // DOUBLE_BINARY_FORMAT_TYPE |
| 240 | + index += sizeof(double) + 1; |
| 241 | + else if (binary_type == std::byte(14)) // ARRAY_BINARY_FORMAT_TYPE |
| 242 | + { |
| 243 | + index++; |
| 244 | + const std::byte& array_elem_type = accum[index]; |
| 245 | + if (array_elem_type == std::byte(10)) |
| 246 | + { |
| 247 | + index++; |
| 248 | + const size_t dims = size_t(accum[index]); |
| 249 | + index++; |
| 250 | + size_t data_size{sizeof(double)}; |
| 251 | + for (size_t i = 0; i < dims; i++) |
| 252 | + { |
| 253 | + data_size *= bytes_to_int32_le(&accum[index]); |
| 254 | + index += sizeof(int32_t); |
| 255 | + } |
| 256 | + index += data_size; |
| 257 | + } |
| 258 | + } |
| 259 | + continue; |
| 260 | + } |
| 261 | + else if ((last == std::byte('\n')) && (prev != std::byte('\\'))) |
224 | 262 | { |
225 | | - const char* tail{&last + 1}; |
226 | | - _msgs.emplace_back(head, tail - head); |
| 263 | + const std::byte* tail{&last + 1}; |
| 264 | + _msgs.emplace_back(head, tail); |
227 | 265 | head = tail; |
228 | 266 | ++received_count; |
229 | 267 | } |
| 268 | + index++; |
230 | 269 | } |
231 | 270 | return received_count; |
232 | 271 | } |
|
0 commit comments