Make the default packet size of transmission changeable

PiperOrigin-RevId: 702411852
This commit is contained in:
Guogang Li
2024-12-03 11:37:20 -08:00
committed by Copybara-Service
parent 2a43f74e1e
commit 13e2f1a23b
4 changed files with 50 additions and 8 deletions
+1
View File
@@ -150,6 +150,7 @@ cc_library(
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@com_google_ukey2//:ukey2",
@@ -15,18 +15,29 @@
#include "connections/implementation/base_endpoint_channel.h"
#include <cassert>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/endpoint_channel_manager.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/offline_frames.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/system_clock.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/logging.h"
#include "internal/platform/mutex.h"
#include "internal/platform/mutex_lock.h"
#include "internal/platform/output_stream.h"
namespace nearby {
namespace connections {
@@ -98,6 +109,8 @@ BaseEndpointChannel::BaseEndpointChannel(
int try_count)
: service_id_(service_id),
channel_name_(channel_name),
max_allowed_read_bytes_(GetMaxAllowedReadBytes()),
default_max_transmit_packet_size_(GetDefaultMaxTransmitPacketSize()),
reader_(reader),
writer_(writer),
technology_(technology),
@@ -122,7 +135,7 @@ ExceptionOr<ByteArray> BaseEndpointChannel::Read(
return ExceptionOr<ByteArray>(read_int.exception());
}
if (read_int.result() < 0 || read_int.result() > kMaxAllowedReadBytes) {
if (read_int.result() < 0 || read_int.result() > max_allowed_read_bytes_) {
NEARBY_LOGS(WARNING) << __func__ << ": Read an invalid number of bytes: "
<< read_int.result();
return ExceptionOr<ByteArray>(Exception::kIo);
@@ -230,7 +243,7 @@ Exception BaseEndpointChannel::Write(const ByteArray& data,
}
size_t data_size = data_to_write->size();
if (data_size < 0 || data_size > kMaxAllowedReadBytes) {
if (data_size < 0 || data_size > max_allowed_read_bytes_) {
NEARBY_LOGS(WARNING) << __func__ << ": Write an invalid number of bytes: "
<< data_size;
return {Exception::kIo};
@@ -357,7 +370,7 @@ std::string BaseEndpointChannel::GetName() const { return channel_name_; }
int BaseEndpointChannel::GetMaxTransmitPacketSize() const {
// Return default value if the medium never define it's chunk size.
return kDefaultMaxTransmitPacketSize;
return default_max_transmit_packet_size_;
}
void BaseEndpointChannel::EnableEncryption(
@@ -432,6 +445,23 @@ int BaseEndpointChannel::GetFrequency() const { return frequency_; }
// Returns the try count of this EndpointChannel.
int BaseEndpointChannel::GetTryCount() const { return try_count_; }
int BaseEndpointChannel::GetMaxAllowedReadBytes() const {
int64_t max_allowed_read_bytes = NearbyFlags::GetInstance().GetInt64Flag(
config_package_nearby::nearby_connections_feature::
kMediumMaxAllowedReadBytes);
return max_allowed_read_bytes >= INT_MAX ? INT_MAX : max_allowed_read_bytes;
}
int BaseEndpointChannel::GetDefaultMaxTransmitPacketSize() const {
int32_t default_max_transmit_packet_size =
NearbyFlags::GetInstance().GetInt64Flag(
config_package_nearby::nearby_connections_feature::
kMediumDefaultMaxTransmitPacketSize);
return default_max_transmit_packet_size >= INT_MAX
? INT_MAX
: default_max_transmit_packet_size;
}
bool BaseEndpointChannel::IsEncryptionEnabledLocked() const {
return crypto_context_ != nullptr;
}
@@ -15,11 +15,12 @@
#ifndef CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_
#define CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_
#include <cstdint>
#include <memory>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/analytics/packet_meta_data.h"
#include "connections/implementation/endpoint_channel.h"
@@ -92,11 +93,11 @@ class BaseEndpointChannel : public EndpointChannel {
std::unique_ptr<std::string> EncodeMessageForTests(absl::string_view data);
private:
// Used to sanity check that our frame sizes are reasonable.
static constexpr std::int32_t kMaxAllowedReadBytes = 1048576; // 1MB
// Gets the maximum number of bytes that can be read from the channel.
int GetMaxAllowedReadBytes() const;
// The default maximum transmit unit/packet size.
static constexpr int kDefaultMaxTransmitPacketSize = 65536; // 64 KB
// Gets the default maximum transmit unit/packet size.
int GetDefaultMaxTransmitPacketSize() const;
bool IsEncryptionEnabledLocked() const
ABSL_EXCLUSIVE_LOCKS_REQUIRED(crypto_mutex_);
@@ -119,6 +120,9 @@ class BaseEndpointChannel : public EndpointChannel {
const std::string service_id_;
const std::string channel_name_;
const int max_allowed_read_bytes_;
const int default_max_transmit_packet_size_;
// The reader and writer are synchronized independently since we can't have
// writes waiting on reads that might potentially block forever.
Mutex reader_mutex_;
@@ -62,6 +62,13 @@ constexpr auto kEnableSafeToDisconnect =
// by default, enable Wi-Fi Hotspot client.
constexpr auto kEnableWifiHotspotClient =
flags::Flag<bool>(kConfigPackage, "45648734", true);
// Default max transmit packet size for medium.
constexpr auto kMediumDefaultMaxTransmitPacketSize =
flags::Flag<int64_t>(kConfigPackage, "45669529", 65536);
// Default max allowed read bytes for medium.
constexpr auto kMediumMaxAllowedReadBytes =
flags::Flag<int64_t>(kConfigPackage, "45669530", 1048576);
// Enable/Disable payload-received-ack feature.
// Set the safe-to-disconnect version.
// Enable 1. safe-to-disconnect check 2. reserved 3. auto-reconnect 4.
// auto-resume 5. non-distance-constraint-recovery 6. payload_ack