Skip to content

Commit

Permalink
Merge pull request #27 from aprock/master
Browse files Browse the repository at this point in the history
android switch to MulticastSocket for sockets
  • Loading branch information
aprock authored Sep 30, 2016
2 parents e77d9f6 + 6c9c242 commit 2f43357
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
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

0 comments on commit 2f43357

Please sign in to comment.