diff --git a/android/src/main/java/com/tradle/react/UdpSenderTask.java b/android/src/main/java/com/tradle/react/UdpSenderTask.java index 925f94ac..b852f2fa 100644 --- a/android/src/main/java/com/tradle/react/UdpSenderTask.java +++ b/android/src/main/java/com/tradle/react/UdpSenderTask.java @@ -7,8 +7,6 @@ package com.tradle.react; -import android.os.AsyncTask; - import java.io.IOException; import java.lang.ref.WeakReference; import java.net.DatagramPacket; @@ -18,24 +16,32 @@ /** * Specialized AsyncTask that transmits data in the background, and notifies listeners of the result. */ -public class UdpSenderTask extends AsyncTask { +public class UdpSenderTask implements Runnable { private static final String TAG = "UdpSenderTask"; - private DatagramSocket mSocket; - private WeakReference mListener; + private final DatagramSocket mSocket; + private final WeakReference mListener; + + private SocketAddress mSocketAddress; + private byte[] mData; - public UdpSenderTask(DatagramSocket socket, OnDataSentListener listener) { + public UdpSenderTask(DatagramSocket socket, OnDataSentListener listener, SocketAddress socketAddress, byte[] data) { this.mSocket = socket; - this.mListener = new WeakReference(listener); + this.mListener = new WeakReference<>(listener); + this.mSocketAddress = socketAddress; + this.mData = data; } @Override - protected Void doInBackground(SenderPacket... params) { + public void run() { OnDataSentListener listener = mListener.get(); try { - SenderPacket packet = params[0]; - mSocket.send(new DatagramPacket(packet.data, packet.data.length, packet.socketAddress)); + if (mSocket == null) { + return; + } + + mSocket.send(new DatagramPacket(mData, mData.length, mSocketAddress)); if (listener != null) { listener.onDataSent(this); @@ -49,16 +55,6 @@ protected Void doInBackground(SenderPacket... params) { listener.onDataSentRuntimeException(this, rte); } } - - return null; - } - - /** - * Simple class to marshall outgoing data across to this AsyncTask - */ - public static class SenderPacket { - SocketAddress socketAddress; - byte[] data; } /** diff --git a/android/src/main/java/com/tradle/react/UdpSocketClient.java b/android/src/main/java/com/tradle/react/UdpSocketClient.java index 6c7f36ea..79946577 100644 --- a/android/src/main/java/com/tradle/react/UdpSocketClient.java +++ b/android/src/main/java/com/tradle/react/UdpSocketClient.java @@ -15,6 +15,9 @@ import java.net.UnknownHostException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import static com.tradle.react.UdpSenderTask.OnDataSentListener; @@ -25,13 +28,15 @@ public final class UdpSocketClient implements UdpReceiverTask.OnDataReceivedList private final OnDataReceivedListener mReceiverListener; private final OnRuntimeExceptionListener mExceptionListener; + private ExecutorService executor = Executors.newSingleThreadExecutor(); + private UdpReceiverTask mReceiverTask; private final Map mPendingSends; private DatagramSocket mSocket; private boolean mIsMulticastSocket = false; - public UdpSocketClient(OnDataReceivedListener receiverListener, OnRuntimeExceptionListener exceptionListener) { + public UdpSocketClient(OnDataReceivedListener receiverListener, OnRuntimeExceptionListener exceptionListener) { this.mReceiverListener = receiverListener; this.mExceptionListener = exceptionListener; this.mPendingSends = new ConcurrentHashMap<>(); @@ -125,10 +130,7 @@ public void send(String base64String, Integer port, String address, @Nullable Ca byte[] data = Base64.decode(base64String, Base64.NO_WRAP); - UdpSenderTask task = new UdpSenderTask(mSocket, this); - UdpSenderTask.SenderPacket packet = new UdpSenderTask.SenderPacket(); - packet.data = data; - packet.socketAddress = new InetSocketAddress(InetAddress.getByName(address), port); + UdpSenderTask task = new UdpSenderTask(mSocket, this, new InetSocketAddress(InetAddress.getByName(address), port), data); if (callback != null) { synchronized (mPendingSends) { @@ -136,7 +138,7 @@ public void send(String base64String, Integer port, String address, @Nullable Ca } } - task.execute(packet); + executor.submit(task); } /** @@ -157,6 +159,9 @@ public void close() { mReceiverTask.terminate(); } + // stop pending send tasks + executor.shutdownNow(); + // close the socket if (mSocket != null && !mSocket.isClosed()) { mSocket.close(); diff --git a/android/src/main/java/com/tradle/react/UdpSockets.java b/android/src/main/java/com/tradle/react/UdpSockets.java index 4bceb123..bdf1e441 100644 --- a/android/src/main/java/com/tradle/react/UdpSockets.java +++ b/android/src/main/java/com/tradle/react/UdpSockets.java @@ -66,7 +66,7 @@ public void run() { } /** - * Private method to retrieve clients. Must be called from a GuardedAsyncTask for thread-safety. + * Private method to retrieve clients. */ private UdpSocketClient findClient(final Integer cId, final Callback callback) { final UdpSocketClient client = mClients.get(cId);