Support payload offsets

Support sending a file or stream payload from a non-zero offset.
This allows the clients to resume a transfer. Upper layers should use
this feature only when both sides support it.

PiperOrigin-RevId: 385137570
This commit is contained in:
Janusz Sobczak
2021-07-16 06:52:45 -07:00
committed by Copybara-Service
parent a8fe61d846
commit ed511f962d
14 changed files with 293 additions and 15 deletions
+1
View File
@@ -19,6 +19,7 @@ cc_library(
srcs = [
"base64_utils.cc",
"bluetooth_utils.cc",
"input_stream.cc",
"prng.cc",
],
hdrs = [
+6
View File
@@ -32,6 +32,12 @@ class BaseInputStream : public InputStream {
ExceptionOr<ByteArray> Read(std::int64_t size) override;
ExceptionOr<size_t> Skip(size_t offset) override {
size_t real_offset = std::min(offset, buffer_.size() - position_);
position_ += real_offset;
return ExceptionOr<size_t>(real_offset);
}
Exception Close() override {
// Do nothing.
return {Exception::kSuccess};
+2
View File
@@ -40,6 +40,8 @@ class FeatureFlags {
// with the exp backoff, retry intervals in seconds: 3, 6, 12, 24...
absl::Duration bwu_retry_exp_backoff_initial_delay = absl::Seconds(3);
absl::Duration bwu_retry_exp_backoff_maximum_delay = absl::Seconds(300);
// Support sending file and stream payloads starting from a non-zero offset.
bool enable_send_payload_offset = true;
};
static const FeatureFlags& GetInstance() {
+42
View File
@@ -0,0 +1,42 @@
// 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 "platform/base/input_stream.h"
#include <cstddef>
#include <cstdint>
#include "platform/base/byte_array.h"
#include "platform/base/exception.h"
namespace location {
namespace nearby {
constexpr size_t kSkipBufferSize = 64 * 1024;
ExceptionOr<size_t> InputStream::Skip(size_t offset) {
size_t bytes_left = offset;
while (bytes_left > 0) {
size_t chunk_size = std::min(bytes_left, kSkipBufferSize);
ExceptionOr<ByteArray> result = Read(chunk_size);
if (!result.ok()) {
return result.GetException();
}
bytes_left -= chunk_size;
}
return ExceptionOr<size_t>(offset);
}
} // namespace nearby
} // namespace location
+4
View File
@@ -32,6 +32,10 @@ class InputStream {
// throws Exception::kIo
virtual ExceptionOr<ByteArray> Read(std::int64_t size) = 0;
// throws Exception::kIo
virtual ExceptionOr<size_t> Skip(size_t offset);
// throws Exception::kIo
virtual Exception Close() = 0;
};
+2
View File
@@ -50,6 +50,8 @@ class InputFile final {
// Returns total size of this file in bytes.
std::int64_t GetTotalSize() const { return impl_->GetTotalSize(); }
ExceptionOr<size_t> Skip(size_t offset) { return impl_->Skip(offset); }
// Disallows further reads from the file and frees system resources,
// associated with it.
Exception Close() { return impl_->Close(); }