From 0160b32c1d432f4608cd409a7026e675afb5eccf Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Fri, 6 Oct 2023 09:43:44 -0700 Subject: [PATCH 1/3] fix auto-resume logging issue PiperOrigin-RevId: 571361682 --- connections/implementation/endpoint_channel_manager.h | 1 + connections/implementation/endpoint_manager.cc | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/connections/implementation/endpoint_channel_manager.h b/connections/implementation/endpoint_channel_manager.h index 2f7348aa..df38a538 100644 --- a/connections/implementation/endpoint_channel_manager.h +++ b/connections/implementation/endpoint_channel_manager.h @@ -28,6 +28,7 @@ #include "internal/platform/feature_flags.h" #include "internal/platform/mutex.h" #include "internal/proto/analytics/connections_log.pb.h" +#include "proto/connections_enums.pb.h" namespace nearby { namespace connections { diff --git a/connections/implementation/endpoint_manager.cc b/connections/implementation/endpoint_manager.cc index 0e61b8c1..97c393dd 100644 --- a/connections/implementation/endpoint_manager.cc +++ b/connections/implementation/endpoint_manager.cc @@ -47,6 +47,8 @@ using ::location::nearby::analytics::proto::ConnectionsLog; using ::location::nearby::connections::OfflineFrame; using ::location::nearby::connections::V1Frame; 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 @@ -204,7 +206,7 @@ ExceptionOr EndpointManager::TryDecryptFrame( } auto elapsed = SystemClock::ElapsedRealtime() - start_time; if (elapsed > kDecryptRetryTimeout) { - NEARBY_LOGS(WARNING) << "Can't decrypt the mesage. Timeout after " + NEARBY_LOGS(WARNING) << "Can't decrypt the message. Timeout after " << elapsed; return Exception::kTimeout; } @@ -781,6 +783,7 @@ bool EndpointManager::ApplySafeToDisconnect(const std::string& endpoint_id, DisconnectionReason reason) { NEARBY_LOGS(INFO) << "[safe-to-disconnect] ApplySafeToDisconnect reason: " << reason; + // TODO(b/303544913): clean up the safe-to-disconnect logic bool is_safe_disconnection = false; bool send_disconnection_frame = true; absl::Duration timeout_millis = FeatureFlags::GetInstance() @@ -790,6 +793,7 @@ bool EndpointManager::ApplySafeToDisconnect(const std::string& endpoint_id, switch (reason) { case DisconnectionReason::UPGRADED: case DisconnectionReason::SHUTDOWN: + case DisconnectionReason::PREV_CHANNEL_DISCONNECTION_IN_RECONNECT: case DisconnectionReason::UNFINISHED: return true; // safe disconnection case DisconnectionReason::IO_ERROR: From 6a3fc35be801dfb3d71a969b4e4bced195189628 Mon Sep 17 00:00:00 2001 From: ggli-google Date: Fri, 6 Oct 2023 15:36:12 -0700 Subject: [PATCH 2/3] Compile protos --- compiled_proto/proto/connections_enums.pb.cc | 52 +++++++++++--------- compiled_proto/proto/connections_enums.pb.h | 10 ++-- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/compiled_proto/proto/connections_enums.pb.cc b/compiled_proto/proto/connections_enums.pb.cc index 20cd2d6f..76d86f05 100644 --- a/compiled_proto/proto/connections_enums.pb.cc +++ b/compiled_proto/proto/connections_enums.pb.cc @@ -656,29 +656,33 @@ bool ConnectionAttemptType_IsValid(int value) { case 0: case 1: case 2: + case 3: return true; default: return false; } } -static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed ConnectionAttemptType_strings[3] = {}; +static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed ConnectionAttemptType_strings[4] = {}; static const char ConnectionAttemptType_names[] = "INITIAL" + "RECONNECT" "UNKNOWN_CONNECTION_ATTEMPT_TYPE" "UPGRADE"; static const ::PROTOBUF_NAMESPACE_ID::internal::EnumEntry ConnectionAttemptType_entries[] = { { {ConnectionAttemptType_names + 0, 7}, 1 }, - { {ConnectionAttemptType_names + 7, 31}, 0 }, - { {ConnectionAttemptType_names + 38, 7}, 2 }, + { {ConnectionAttemptType_names + 7, 9}, 3 }, + { {ConnectionAttemptType_names + 16, 31}, 0 }, + { {ConnectionAttemptType_names + 47, 7}, 2 }, }; static const int ConnectionAttemptType_entries_by_number[] = { - 1, // 0 -> UNKNOWN_CONNECTION_ATTEMPT_TYPE + 2, // 0 -> UNKNOWN_CONNECTION_ATTEMPT_TYPE 0, // 1 -> INITIAL - 2, // 2 -> UPGRADE + 3, // 2 -> UPGRADE + 1, // 3 -> RECONNECT }; const std::string& ConnectionAttemptType_Name( @@ -687,12 +691,12 @@ const std::string& ConnectionAttemptType_Name( ::PROTOBUF_NAMESPACE_ID::internal::InitializeEnumStrings( ConnectionAttemptType_entries, ConnectionAttemptType_entries_by_number, - 3, ConnectionAttemptType_strings); + 4, ConnectionAttemptType_strings); (void) dummy; int idx = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumName( ConnectionAttemptType_entries, ConnectionAttemptType_entries_by_number, - 3, value); + 4, value); return idx == -1 ? ::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString() : ConnectionAttemptType_strings[idx].get(); } @@ -700,7 +704,7 @@ bool ConnectionAttemptType_Parse( ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, ConnectionAttemptType* value) { int int_value; bool success = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumValue( - ConnectionAttemptType_entries, 3, name, &int_value); + ConnectionAttemptType_entries, 4, name, &int_value); if (success) { *value = static_cast(int_value); } @@ -715,17 +719,19 @@ bool DisconnectionReason_IsValid(int value) { case 4: case 5: case 6: + case 7: return true; default: return false; } } -static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed DisconnectionReason_strings[7] = {}; +static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed DisconnectionReason_strings[8] = {}; static const char DisconnectionReason_names[] = "IO_ERROR" "LOCAL_DISCONNECTION" + "PREV_CHANNEL_DISCONNECTION_IN_RECONNECT" "REMOTE_DISCONNECTION" "SHUTDOWN" "UNFINISHED" @@ -735,21 +741,23 @@ static const char DisconnectionReason_names[] = static const ::PROTOBUF_NAMESPACE_ID::internal::EnumEntry DisconnectionReason_entries[] = { { {DisconnectionReason_names + 0, 8}, 3 }, { {DisconnectionReason_names + 8, 19}, 1 }, - { {DisconnectionReason_names + 27, 20}, 2 }, - { {DisconnectionReason_names + 47, 8}, 5 }, - { {DisconnectionReason_names + 55, 10}, 6 }, - { {DisconnectionReason_names + 65, 28}, 0 }, - { {DisconnectionReason_names + 93, 8}, 4 }, + { {DisconnectionReason_names + 27, 39}, 7 }, + { {DisconnectionReason_names + 66, 20}, 2 }, + { {DisconnectionReason_names + 86, 8}, 5 }, + { {DisconnectionReason_names + 94, 10}, 6 }, + { {DisconnectionReason_names + 104, 28}, 0 }, + { {DisconnectionReason_names + 132, 8}, 4 }, }; static const int DisconnectionReason_entries_by_number[] = { - 5, // 0 -> UNKNOWN_DISCONNECTION_REASON + 6, // 0 -> UNKNOWN_DISCONNECTION_REASON 1, // 1 -> LOCAL_DISCONNECTION - 2, // 2 -> REMOTE_DISCONNECTION + 3, // 2 -> REMOTE_DISCONNECTION 0, // 3 -> IO_ERROR - 6, // 4 -> UPGRADED - 3, // 5 -> SHUTDOWN - 4, // 6 -> UNFINISHED + 7, // 4 -> UPGRADED + 4, // 5 -> SHUTDOWN + 5, // 6 -> UNFINISHED + 2, // 7 -> PREV_CHANNEL_DISCONNECTION_IN_RECONNECT }; const std::string& DisconnectionReason_Name( @@ -758,12 +766,12 @@ const std::string& DisconnectionReason_Name( ::PROTOBUF_NAMESPACE_ID::internal::InitializeEnumStrings( DisconnectionReason_entries, DisconnectionReason_entries_by_number, - 7, DisconnectionReason_strings); + 8, DisconnectionReason_strings); (void) dummy; int idx = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumName( DisconnectionReason_entries, DisconnectionReason_entries_by_number, - 7, value); + 8, value); return idx == -1 ? ::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString() : DisconnectionReason_strings[idx].get(); } @@ -771,7 +779,7 @@ bool DisconnectionReason_Parse( ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, DisconnectionReason* value) { int int_value; bool success = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumValue( - DisconnectionReason_entries, 7, name, &int_value); + DisconnectionReason_entries, 8, name, &int_value); if (success) { *value = static_cast(int_value); } diff --git a/compiled_proto/proto/connections_enums.pb.h b/compiled_proto/proto/connections_enums.pb.h index 5f00902d..c7023f89 100644 --- a/compiled_proto/proto/connections_enums.pb.h +++ b/compiled_proto/proto/connections_enums.pb.h @@ -273,11 +273,12 @@ bool ConnectionAttemptDirection_Parse( enum ConnectionAttemptType : int { UNKNOWN_CONNECTION_ATTEMPT_TYPE = 0, INITIAL = 1, - UPGRADE = 2 + UPGRADE = 2, + RECONNECT = 3 }; bool ConnectionAttemptType_IsValid(int value); constexpr ConnectionAttemptType ConnectionAttemptType_MIN = UNKNOWN_CONNECTION_ATTEMPT_TYPE; -constexpr ConnectionAttemptType ConnectionAttemptType_MAX = UPGRADE; +constexpr ConnectionAttemptType ConnectionAttemptType_MAX = RECONNECT; constexpr int ConnectionAttemptType_ARRAYSIZE = ConnectionAttemptType_MAX + 1; const std::string& ConnectionAttemptType_Name(ConnectionAttemptType value); @@ -297,11 +298,12 @@ enum DisconnectionReason : int { IO_ERROR = 3, UPGRADED = 4, SHUTDOWN = 5, - UNFINISHED = 6 + UNFINISHED = 6, + PREV_CHANNEL_DISCONNECTION_IN_RECONNECT = 7 }; bool DisconnectionReason_IsValid(int value); constexpr DisconnectionReason DisconnectionReason_MIN = UNKNOWN_DISCONNECTION_REASON; -constexpr DisconnectionReason DisconnectionReason_MAX = UNFINISHED; +constexpr DisconnectionReason DisconnectionReason_MAX = PREV_CHANNEL_DISCONNECTION_IN_RECONNECT; constexpr int DisconnectionReason_ARRAYSIZE = DisconnectionReason_MAX + 1; const std::string& DisconnectionReason_Name(DisconnectionReason value); From ad6bd802679a5a1bb27a83207ff18fec5600d2ca Mon Sep 17 00:00:00 2001 From: Deling Ren Date: Tue, 10 Oct 2023 11:53:23 -0700 Subject: [PATCH 3/3] Fix the crash when an endpoint without a UTF8 name is discovered --- .../Example/iOS Example/Model/Model.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift b/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift index 6128816c..df9bd086 100644 --- a/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift +++ b/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift @@ -120,10 +120,13 @@ class Model: ObservableObject { extension Model: DiscovererDelegate { func discoverer(_ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) { + guard let endpointName = String(data: context, encoding: .utf8) else { + return + } let endpoint = DiscoveredEndpoint( id: UUID(), endpointID: endpointID, - endpointName: String(data: context, encoding: .utf8)! + endpointName: endpointName ) endpoints.insert(endpoint, at: 0) } @@ -138,10 +141,13 @@ extension Model: DiscovererDelegate { extension Model: AdvertiserDelegate { func advertiser(_ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID, with context: Data, connectionRequestHandler: @escaping (Bool) -> Void) { + guard let endpointName = String(data: context, encoding: .utf8) else { + return + } let endpoint = DiscoveredEndpoint( id: UUID(), endpointID: endpointID, - endpointName: String(data: context, encoding: .utf8)! + endpointName: endpointName ) endpoints.insert(endpoint, at: 0) connectionRequestHandler(true)