forked from boston-dynamics/spot-cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_chunking.h
More file actions
94 lines (81 loc) · 3.21 KB
/
Copy pathdata_chunking.h
File metadata and controls
94 lines (81 loc) · 3.21 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
/**
* Copyright (c) 2023 Boston Dynamics, Inc. All rights reserved.
*
* Downloading, reproducing, distributing or otherwise using the SDK Software
* is subject to the terms and conditions of the Boston Dynamics Software
* Development Kit License (20191101-BDSDK-SL).
*/
#pragma once
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "bosdyn/client/error_codes/sdk_error_code.h"
#include "bosdyn/client/service_client/result.h"
#include <bosdyn/api/data_chunk.pb.h>
namespace bosdyn {
namespace client {
/**
* Create a std::string from a vector of data chunks.
*
* @param data_chunks Vector of data chunks to concatenate together.
*
* @return Result with a std::shared_ptr with a std::string.
*/
Result<std::string> StringFromDataChunks(
const std::vector<const ::bosdyn::api::DataChunk*>& data_chunks);
/**
* Create a message from deserializing concatenated data chunks.
*
* @param data_chunks Vector of chunks to concatenate and deserialize into a message.
*
* @return Result struct with std::shared_ptr with the deserialized message.
*/
template <class T>
Result<T> MessageFromDataChunks(const std::vector<const ::bosdyn::api::DataChunk*>& data_chunks) {
const Result<std::string>& result = StringFromDataChunks(data_chunks);
if (!result) {
return {result.status, {}};
}
google::protobuf::io::ArrayInputStream array_input(result.response.data(),
result.response.size());
google::protobuf::io::CodedInputStream coded_input(&array_input);
coded_input.SetRecursionLimit(500);
T output;
if (!output.ParseFromCodedStream(&coded_input)) {
return {::bosdyn::common::Status(SDKErrorCode::GenericSDKError,
"Could not deserialize concatenated chunks into message"),
{}};
}
return {::bosdyn::common::Status(SDKErrorCode::Success), std::move(output)};
}
/**
* Create a vector of data chunks from a std::string.
*
* @param data Input buffer to chunk into data chunks.
* @param chunks Output argument with the vector of data chunks.
*
* @return None, output from method is returned in chunks argument.
*/
void StringToDataChunks(const std::string& data, std::vector<::bosdyn::api::DataChunk>* chunks);
/**
* Create a vector of data chunks from a templatized message.
*
* NOTE: this is not using a Result return type for consistency with the StringToDataChunk method.
*
* @param message Message to be serialized and chunked into 4MB chunks.
* @param chunks Vector of chunks created from the serialized message.
*
* @return ::bosdyn::common::Status with any errors found.
*/
template <class T>
::bosdyn::common::Status MessageToDataChunks(const T& message,
std::vector<::bosdyn::api::DataChunk>* chunks) {
std::string buffer;
if (!message.SerializeToString(&buffer)) {
return ::bosdyn::common::Status(SDKErrorCode::GenericSDKError,
"Could not serialize message");
}
StringToDataChunks(buffer, chunks);
return ::bosdyn::common::Status(SDKErrorCode::Success);
}
} // namespace client
} // namespace bosdyn