Skip to content

Commit

Permalink
fix bug: fail to register NsdService (#22914)
Browse files Browse the repository at this point in the history
* fix bug: fail to register NsdService

* import package

* Restyled by google-java-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Aug 24, 2023
1 parent d2be1c1 commit 5317617
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class NsdManagerServiceResolver implements ServiceResolver {
private static final String TAG = NsdManagerServiceResolver.class.getSimpleName();
Expand All @@ -35,6 +36,7 @@ public class NsdManagerServiceResolver implements ServiceResolver {
private MulticastLock multicastLock;
private Handler mainThreadHandler;
private List<NsdManager.RegistrationListener> registrationListeners = new ArrayList<>();
private final CopyOnWriteArrayList<String> mMFServiceName = new CopyOnWriteArrayList<>();

public NsdManagerServiceResolver(Context context) {
this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
Expand Down Expand Up @@ -134,6 +136,15 @@ public void publish(
String[] textEntriesKeys,
byte[][] textEntriesDatas,
String[] subTypes) {
/**
* Note, MF's NSDService will be repeatedly registered until it exceeds the OS's
* maximum(http://androidxref.com/9.0.0_r3/xref/frameworks/base/services/core/java/com/android/server/NsdService.java#MAX_LIMIT)
* limit at which time the registration will fail.
*/
if (serviceName.contains("-") && mMFServiceName.contains(serviceName)) {
Log.w(TAG, "publish: duplicate MF nsdService");
return;
}
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(serviceName);

Expand Down Expand Up @@ -193,6 +204,7 @@ public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
multicastLock.acquire();
}
registrationListeners.add(registrationListener);
mMFServiceName.add(serviceName);

nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener);
Log.d(TAG, "publish " + registrationListener + " count = " + registrationListeners.size());
Expand All @@ -209,5 +221,6 @@ public void removeServices() {
nsdManager.unregisterService(l);
}
registrationListeners.clear();
mMFServiceName.clear();
}
}

0 comments on commit 5317617

Please sign in to comment.