Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: fail to register NsdService #22914

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}