diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 174a1a40..30159c03 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -66,6 +66,7 @@ namespace nearby { namespace connections { + namespace { using ::location::nearby::connections::OsInfo; @@ -79,13 +80,8 @@ bool IsFeatureUseStableEndpointIdEnabled() { connections::config_package_nearby::nearby_connections_feature:: kUseStableEndpointId); } - } // namespace -// The definition is necessary before C++17. -constexpr absl::Duration - ClientProxy::kHighPowerAdvertisementEndpointIdCacheTimeout; - ClientProxy::ClientProxy(::nearby::analytics::EventLogger* event_logger) : client_id_(Prng().NextInt64()) { NEARBY_LOGS(INFO) << "ClientProxy ctor event_logger=" << event_logger; @@ -558,8 +554,9 @@ void ClientProxy::OnBandwidthChanged(const std::string& endpoint_id, NEARBY_LOGS(INFO) << "ClientProxy [BandwidthChanged]: id=" << endpoint_id; MutexLock lock(&mutex_); - const ConnectionPair* item = LookupConnection(endpoint_id); + ConnectionPair* item = LookupConnection(endpoint_id); if (item != nullptr) { + item->first.connected_medium = new_medium; item->first.connection_listener.bandwidth_changed_cb(endpoint_id, new_medium); NEARBY_LOGS(INFO) << "ClientProxy [reporting onBandwidthChanged]: client=" @@ -600,6 +597,17 @@ bool ClientProxy::ConnectionStatusMatches(const std::string& endpoint_id, return false; } +Medium ClientProxy::GetConnectedMedium(const std::string& endpoint_id) const { + MutexLock lock(&mutex_); + + const ConnectionPair* item = LookupConnection(endpoint_id); + if (item != nullptr) { + return item->first.connected_medium; + } + + return Medium::UNKNOWN_MEDIUM; +} + BooleanMediumSelector ClientProxy::GetUpgradeMediums( const std::string& endpoint_id) const { MutexLock lock(&mutex_); diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index 881c1605..0cf143fd 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -179,6 +179,9 @@ class ClientProxy final { // ConnectionListener.disconnected_cb() callback. void OnDisconnected(const std::string& endpoint_id, bool notify); + // Returns the medium we're currently connected to the endpoint over, or + // UNKNOWN if we don't know or don't have a connection. + Medium GetConnectedMedium(const std::string& endpoint_id) const; // Returns all mediums eligible for upgrade. BooleanMediumSelector GetUpgradeMediums(const std::string& endpoint_id) const; // Returns if this endpoint support 5G for WIFI. @@ -363,6 +366,7 @@ class ClientProxy final { kConnected = 1 << 4, }; bool is_incoming{false}; + Medium connected_medium{Medium::UNKNOWN_MEDIUM}; Status status{kPending}; ConnectionListener connection_listener; ConnectionOptions connection_options; diff --git a/connections/implementation/endpoint_manager.cc b/connections/implementation/endpoint_manager.cc index 2411ad86..26236aaf 100644 --- a/connections/implementation/endpoint_manager.cc +++ b/connections/implementation/endpoint_manager.cc @@ -30,7 +30,6 @@ #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/offline_frames.h" -#include "connections/implementation/payload_manager.h" #include "connections/implementation/proto/offline_wire_formats.pb.h" #include "connections/implementation/service_id_constants.h" #include "connections/listeners.h" @@ -57,9 +56,8 @@ using ::location::nearby::analytics::proto::ConnectionsLog; using ::location::nearby::connections::OfflineFrame; using ::location::nearby::connections::PayloadTransferFrame; using ::location::nearby::connections::V1Frame; +using ::location::nearby::proto::connections::DisconnectionReason; using ::nearby::analytics::PacketMetaData; -using DisconnectionReason = - ::location::nearby::proto::connections::DisconnectionReason; // We set this to 11s to provide sufficient time for an in-progress WebRTC // bandwidth upgrade to resolve. This is chosen to be slightly longer than the diff --git a/connections/implementation/payload_manager.cc b/connections/implementation/payload_manager.cc index e0a7cf9c..5a07c40b 100644 --- a/connections/implementation/payload_manager.cc +++ b/connections/implementation/payload_manager.cc @@ -48,6 +48,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" +#include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" @@ -631,8 +632,10 @@ void PayloadManager::OnEndpointDisconnect(ClientProxy* client, case DisconnectionReason::IO_ERROR: default: payload_status = PayloadStatus::ENDPOINT_IO_ERROR; - // TODO(edwinwu): Add for return result code - operation_result_code = OperationResultCode::DETAIL_UNKNOWN; + operation_result_code = + client->GetAnalyticsRecorder() + .GetChannelIoErrorResultCodeFromMedium( + client->GetConnectedMedium(endpoint_id)); break; } @@ -783,14 +786,14 @@ PayloadTransferFrame::PayloadChunk PayloadManager::CreatePayloadChunk( return payload_chunk; } -std::pair +ErrorOr PayloadManager::CreateIncomingPayload(const PayloadTransferFrame& frame, const std::string& endpoint_id) { - // TODO(edwinwu): Add for return result code - auto internal_payload = + // TODO(edwinwu): Add for return result code in CreateIncomingInternalPayload. + std::unique_ptr internal_payload = CreateIncomingInternalPayload(frame, custom_save_path_); if (!internal_payload) { - return {PendingPayloadHandle(), OperationResultCode::DETAIL_UNKNOWN}; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } Payload::Id payload_id = internal_payload->GetId(); @@ -800,8 +803,7 @@ PayloadManager::CreateIncomingPayload(const PayloadTransferFrame& frame, std::make_unique( std::move(internal_payload), EndpointIds{endpoint_id}, true, absl::bind_front(&PayloadManager::OnPendingPayloadDestroy, this))); - return {pending_payloads_.GetPayload(payload_id), - OperationResultCode::DETAIL_UNKNOWN}; + return {pending_payloads_.GetPayload(payload_id)}; } void PayloadManager::OnPendingPayloadDestroy(const PendingPayload* payload) { @@ -847,13 +849,14 @@ void PayloadManager::SendClientCallbacksForFinishedOutgoingPayload( // Notify the client. client->OnPayloadProgress(endpoint_id, update); - // TODO(edwinwu): Add for return result code // Mark this payload as done for analytics. client->GetAnalyticsRecorder().OnOutgoingPayloadDone( endpoint_id, payload_header.id(), status, (operation_result_code == OperationResultCode::DETAIL_UNKNOWN && status == PayloadStatus::ENDPOINT_IO_ERROR) - ? OperationResultCode::DETAIL_UNKNOWN + ? client->GetAnalyticsRecorder() + .GetChannelIoErrorResultCodeFromMedium( + client->GetConnectedMedium(endpoint_id)) : operation_result_code); } @@ -1334,17 +1337,17 @@ void PayloadManager::ProcessDataPacket( payload_header.total_size()); }); - std::pair result = + ErrorOr result = CreateIncomingPayload(payload_transfer_frame, from_endpoint_id); - pending_payload = std::move(result.first); - OperationResultCode operation_result_code = result.second; - if (!pending_payload) { + if (result.has_error()) { LOG(WARNING) << "PayloadManager failed to create InternalPayload from " "PayloadTransferFrame with payload_id=" << payload_header.id() << " and type " << payload_header.type() << ", aborting receipt."; // Analyticize. + OperationResultCode operation_result_code = + result.error().operation_result_code().value(); RunOnStatusUpdateThread( "process-data-packet", [to_client, from_endpoint_id, payload_header, operation_result_code]() @@ -1359,6 +1362,8 @@ void PayloadManager::ProcessDataPacket( payload_chunk.offset(), PayloadTransferFrame::ControlMessage::PAYLOAD_ERROR); return; + } else { + pending_payload = std::move(result.value()); } // Also, let the client know of this new incoming payload. RunOnStatusUpdateThread( diff --git a/connections/implementation/payload_manager.h b/connections/implementation/payload_manager.h index 544d9409..3f489178 100644 --- a/connections/implementation/payload_manager.h +++ b/connections/implementation/payload_manager.h @@ -39,6 +39,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/condition_variable.h" #include "internal/platform/count_down_latch.h" +#include "internal/platform/expected.h" #include "internal/platform/mutex.h" #include "internal/platform/single_thread_executor.h" @@ -324,9 +325,7 @@ class PayloadManager : public EndpointManager::FrameProcessor { LAST_CHUNK) != 0); } - std::pair - CreateIncomingPayload( + ErrorOr CreateIncomingPayload( const location::nearby::connections::PayloadTransferFrame& frame, const std::string& endpoint_id) ABSL_LOCKS_EXCLUDED(mutex_);