Skip to content

Commit

Permalink
fix: Remove AsyncTask in the send function (it's deprecated) (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaslecomte authored Feb 22, 2022
1 parent 76c4586 commit 179a948
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
36 changes: 16 additions & 20 deletions android/src/main/java/com/tradle/react/UdpSenderTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,24 +16,32 @@
/**
* Specialized AsyncTask that transmits data in the background, and notifies listeners of the result.
*/
public class UdpSenderTask extends AsyncTask<UdpSenderTask.SenderPacket, Void, Void> {
public class UdpSenderTask implements Runnable {
private static final String TAG = "UdpSenderTask";

private DatagramSocket mSocket;
private WeakReference<OnDataSentListener> mListener;
private final DatagramSocket mSocket;
private final WeakReference<OnDataSentListener> 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<OnDataSentListener>(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);
Expand All @@ -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;
}

/**
Expand Down
17 changes: 11 additions & 6 deletions android/src/main/java/com/tradle/react/UdpSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<UdpSenderTask, Callback> 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<>();
Expand Down Expand Up @@ -125,18 +130,15 @@ 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) {
mPendingSends.put(task, callback);
}
}

task.execute(packet);
executor.submit(task);
}

/**
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/tradle/react/UdpSockets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 179a948

Please sign in to comment.