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

android switch to MulticastSocket for sockets #27

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
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
29 changes: 6 additions & 23 deletions android/src/main/java/com/tradle/react/UdpSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class UdpSocketClient implements UdpReceiverTask.OnDataReceivedList

private final Map<UdpSenderTask, Callback> mPendingSends;
private DatagramSocket mSocket;
private boolean mIsMulticastSocket = false;

private UdpSocketClient(Builder builder) {
this.mReceiverListener = builder.receiverListener;
Expand All @@ -52,7 +53,7 @@ private UdpSocketClient(Builder builder) {
* @return boolean true IF the socket is part of a multi-cast group.
*/
public boolean isMulticast() {
return (mSocket != null && mSocket instanceof MulticastSocket);
return mIsMulticastSocket;
}

/**
Expand All @@ -68,7 +69,7 @@ public boolean isMulticast() {
* binding.
*/
public void bind(Integer port, @Nullable String address) throws IOException {
mSocket = new DatagramSocket(null);
mSocket = new MulticastSocket(null);

mReceiverTask = new UdpReceiverTask();

Expand Down Expand Up @@ -101,25 +102,8 @@ public void addMembership(String address) throws UnknownHostException, IOExcepti
throw new IllegalStateException("Socket is not bound.");
}

if (!(mSocket instanceof MulticastSocket)) {
// cancel the current receiver task
if (mReceiverTask != null) {
mReceiverTask.cancel(true);
}

mReceiverTask = new UdpReceiverTask();

// tear down the DatagramSocket, and rebuild as a MulticastSocket
final int port = mSocket.getLocalPort();
mSocket.close();
mSocket = new MulticastSocket(port);

// begin listening for data in the background
mReceiverTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
new Pair<DatagramSocket, UdpReceiverTask.OnDataReceivedListener>(mSocket, this));
}

((MulticastSocket) mSocket).joinGroup(InetAddress.getByName(address));
mIsMulticastSocket = true;
}

/**
Expand All @@ -130,9 +114,8 @@ public void addMembership(String address) throws UnknownHostException, IOExcepti
* @throws IOException
*/
public void dropMembership(String address) throws UnknownHostException, IOException {
if (mSocket instanceof MulticastSocket) {
((MulticastSocket) mSocket).leaveGroup(InetAddress.getByName(address));
}
((MulticastSocket) mSocket).leaveGroup(InetAddress.getByName(address));
mIsMulticastSocket = false;
}

/**
Expand Down
22 changes: 15 additions & 7 deletions android/src/main/java/com/tradle/react/UdpSockets.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,26 @@ protected void doInBackgroundGuarded(Void... params) {
mMulticastLock.setReferenceCounted(true);
}

if (!client.isMulticast()) {
// acquire the multi-cast lock, IF this is the
// first addMembership call for this client.
try {
mMulticastLock.acquire();
}

try {
client.addMembership(multicastAddress);
} catch (IllegalStateException ise) {
// an exception occurred
if (mMulticastLock != null && mMulticastLock.isHeld()) {
mMulticastLock.release();
}
FLog.e(TAG, "addMembership", ise);
} catch (UnknownHostException uhe) {
// an exception occurred
if (mMulticastLock != null && mMulticastLock.isHeld()) {
mMulticastLock.release();
}
FLog.e(TAG, "addMembership", uhe);
} catch (IOException ioe) {
// an exception occurred
if (mMulticastLock != null && mMulticastLock.isHeld()) {
mMulticastLock.release();
}
FLog.e(TAG, "addMembership", ioe);
}
}
Expand All @@ -217,6 +221,10 @@ protected void doInBackgroundGuarded(Void... params) {
} catch (IOException ioe) {
// an exception occurred
FLog.e(TAG, "dropMembership", ioe);
} finally {
if (mMulticastLock != null && mMulticastLock.isHeld()) {
mMulticastLock.release();
}
}
}
}.execute();
Expand Down Expand Up @@ -263,7 +271,7 @@ protected void doInBackgroundGuarded(Void... params) {
return;
}

if (client.isMulticast() && mMulticastLock.isHeld()) {
if (mMulticastLock != null && mMulticastLock.isHeld() && client.isMulticast()) {
// drop the multi-cast lock if this is a multi-cast client
mMulticastLock.release();
}
Expand Down