Remove the ServiceTypeClient after socket destroyed

The ServiceTypeClient should be removed after socket destroyed
because it's no longer used by any request. The requests will be
matched to the newly created ServiceTypeClient.

Bug: 278635632
Test: atest FrameworksNetTests CtsNetTestCases
(cherry picked from https://android-review.googlesource.com/q/commit:f2a51ac5f8cc6b1cfaaa0f8fd5c28af522806ca0)
Merged-In: Ia917b14d3666f3bfe8e874606a34800a4ce65c5a
Change-Id: Ia917b14d3666f3bfe8e874606a34800a4ce65c5a
This commit is contained in:
Paul Hu
2023-04-26 10:17:48 +08:00
committed by Cherrypicker Worker
parent 20e768a2f3
commit 45c1eaadb4
7 changed files with 181 additions and 12 deletions

View File

@@ -429,6 +429,10 @@ public final class NsdManager {
private final DiscoveryListener mWrapped;
private final Executor mWrappedExecutor;
private final ArraySet<TrackedNsdInfo> mFoundInfo = new ArraySet<>();
// When this flag is set to true, no further service found or lost callbacks should be
// handled. This flag indicates that the network for this DelegatingDiscoveryListener is
// lost, and any further callbacks would be redundant.
private boolean mAllServicesLost = false;
private DelegatingDiscoveryListener(Network network, DiscoveryListener listener,
Executor executor) {
@@ -445,6 +449,7 @@ public final class NsdManager {
serviceInfo.setNetwork(mNetwork);
mWrappedExecutor.execute(() -> mWrapped.onServiceLost(serviceInfo));
}
mAllServicesLost = true;
}
@Override
@@ -486,12 +491,22 @@ public final class NsdManager {
@Override
public void onServiceFound(NsdServiceInfo serviceInfo) {
if (mAllServicesLost) {
// This DelegatingDiscoveryListener no longer has a network connection. Ignore
// the callback.
return;
}
mFoundInfo.add(new TrackedNsdInfo(serviceInfo));
mWrappedExecutor.execute(() -> mWrapped.onServiceFound(serviceInfo));
}
@Override
public void onServiceLost(NsdServiceInfo serviceInfo) {
if (mAllServicesLost) {
// This DelegatingDiscoveryListener no longer has a network connection. Ignore
// the callback.
return;
}
mFoundInfo.remove(new TrackedNsdInfo(serviceInfo));
mWrappedExecutor.execute(() -> mWrapped.onServiceLost(serviceInfo));
}