forked from google/nearby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.cc
152 lines (120 loc) · 5.04 KB
/
payload.cc
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
// Copyright 2020 Google LLC
//
// 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
//
// https://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.
#include "connections/payload.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include "connections/payload_type.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/file.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/prng.h"
namespace nearby {
namespace connections {
namespace {
std::string getFileName(const std::string& s) {
std::string s_copy(s);
std::replace(s_copy.begin(), s_copy.end(), '\\',
'/'); // replace all '\\' to '/'
size_t lastForwardSepIndex = s_copy.rfind('/', s.length());
return s_copy.substr(lastForwardSepIndex + 1,
s_copy.length() - lastForwardSepIndex);
}
} // namespace
// Payload is default-constructible, and moveable, but not copyable container
// that holds at most one instance of one of:
// ByteArray, InputStream, or InputFile.
Payload::Payload(Payload&& other) noexcept = default;
Payload::~Payload() = default;
Payload& Payload::operator=(Payload&& other) noexcept = default;
// Default (invalid) payload.
Payload::Payload() : type_(PayloadType::kUnknown), content_(std::monostate()) {}
// Constructors for outgoing payloads.
Payload::Payload(ByteArray&& bytes)
: type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(const ByteArray& bytes)
: type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(InputFile input_file)
: id_(std::hash<std::string>()(input_file.GetFilePath())),
file_name_(getFileName(input_file.GetFilePath())),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(Id id, InputFile input_file)
: id_(id),
file_name_(getFileName(input_file.GetFilePath())),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(std::string parent_folder, std::string file_name,
InputFile input_file)
: id_(std::hash<std::string>()(input_file.GetFilePath())),
parent_folder_(parent_folder),
file_name_(file_name),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(std::unique_ptr<InputStream> stream)
: type_(PayloadType::kStream), content_(std::move(stream)) {}
// Constructors for incoming payloads.
Payload::Payload(Id id, ByteArray&& bytes)
: id_(id), type_(PayloadType::kBytes), content_(std::move(bytes)) {}
Payload::Payload(Id id, const ByteArray& bytes)
: id_(id), type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(Id id, std::string parent_folder, std::string file_name,
InputFile input_file)
: id_(id),
parent_folder_(parent_folder),
file_name_(file_name),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(Id id, std::unique_ptr<InputStream> stream)
: id_(id), type_(PayloadType::kStream), content_(std::move(stream)) {}
// Returns ByteArray payload, if it has been defined, or empty ByteArray.
const ByteArray& Payload::AsBytes() const& {
static const ByteArray empty; // NOLINT: function-level static is OK.
auto* result = std::get_if<ByteArray>(&content_);
return result ? *result : empty;
}
// Returns InputStream* payload, if it has been defined, or nullptr.
InputStream* Payload::AsStream() {
auto* result = std::get_if<std::unique_ptr<InputStream>>(&content_);
return result ? result->get() : nullptr;
}
// Returns InputFile* payload, if it has been defined, or nullptr.
InputFile* Payload::AsFile() { return std::get_if<InputFile>(&content_); }
// Returns Payload unique ID.
Payload::Id Payload::GetId() const { return id_; }
// Returns Payload type.
PayloadType Payload::GetType() const { return type_; }
// Sets the payload offset in bytes
void Payload::SetOffset(size_t offset) {
CHECK(type_ == PayloadType::kFile || type_ == PayloadType::kStream);
InputFile* file = AsFile();
if (file != nullptr) {
CHECK(file->GetTotalSize() > 0 && offset < (size_t)file->GetTotalSize());
}
offset_ = offset;
}
size_t Payload::GetOffset() { return offset_; }
// Generate Payload Id; to be passed to outgoing file constructor.
Payload::Id Payload::GenerateId() { return Prng().NextInt64(); }
PayloadType Payload::FindType() const {
return static_cast<PayloadType>(content_.index());
}
const std::string& Payload::GetParentFolder() const { return parent_folder_; }
const std::string& Payload::GetFileName() const { return file_name_; }
} // namespace connections
} // namespace nearby