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(Android): Remove use of AsyncTask.THREAD_POOL_EXECUTOR #46

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix threads
  • Loading branch information
Rapsssito committed May 7, 2020
commit c6ab7a9edb18fe237c5a7a7116f6a79a6d6c2d5a
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* notifies it's listener when data is received. This is not threadsafe, the listener
* should handle synchronicity.
*/
public class TcpReceiverTask extends AsyncTask<Pair<TcpSocketClient, TcpReceiverTask.OnDataReceivedListener>, Void, Void> {
class TcpReceiverTask extends AsyncTask<Pair<TcpSocketClient, TcpReceiverTask.OnDataReceivedListener>, Void, Void> {
/**
* An infinite loop to block and read data from the socket.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.net.Network;
import android.os.AsyncTask;
import android.util.Pair;

import com.facebook.react.bridge.ReadableMap;
Expand All @@ -13,6 +12,7 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.util.concurrent.ExecutorService;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
Expand All @@ -23,16 +23,18 @@

class TcpSocketClient {
private final int id;
private final ExecutorService executorService;
private TcpReceiverTask receiverTask;
private Socket socket;
private TcpReceiverTask.OnDataReceivedListener mReceiverListener;

TcpSocketClient(final int id) {
TcpSocketClient(final int id, final ExecutorService executorService) {
this.id = id;
this.executorService = executorService;
}

TcpSocketClient(@NonNull final TcpReceiverTask.OnDataReceivedListener receiverListener, @NonNull final Integer id, @Nullable final Socket socket) {
this(id);
TcpSocketClient(@NonNull final TcpReceiverTask.OnDataReceivedListener receiverListener, @NonNull final Integer id, @NonNull final ExecutorService executorService, @Nullable final Socket socket) {
this(id, executorService);
this.socket = socket;
receiverTask = new TcpReceiverTask();
mReceiverListener = receiverListener;
Expand All @@ -52,7 +54,7 @@ public void connect(@NonNull final Context context, @NonNull final String addres
final boolean isTls = options.hasKey("tls") && options.getBoolean("tls");
if (isTls) {
SocketFactory sf;
if (options.hasKey("tlsCheckValidity") && !options.getBoolean("tlsCheckValidity")){
if (options.hasKey("tlsCheckValidity") && !options.getBoolean("tlsCheckValidity")) {
sf = SSLCertificateHelper.createBlindSocketFactory();
} else {
final String customTlsCert = options.hasKey("tlsCert") ? options.getString("tlsCert") : null;
Expand Down Expand Up @@ -86,10 +88,14 @@ public void connect(@NonNull final Context context, @NonNull final String addres
startListening();
}

ExecutorService getExecutorService() {
return this.executorService;
}

@SuppressWarnings("WeakerAccess")
public void startListening() {
//noinspection unchecked
receiverTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Pair<>(this, mReceiverListener));
receiverTask.executeOnExecutor(getExecutorService(), new Pair<>(this, mReceiverListener));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.AsyncTask;
import android.util.Base64;
import android.net.Network;

Expand All @@ -26,6 +25,8 @@
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand All @@ -34,10 +35,12 @@

public class TcpSocketModule extends ReactContextBaseJavaModule implements TcpReceiverTask.OnDataReceivedListener {
private static final String TAG = "TcpSockets";
private static final int N_THREADS = 2;
private final ReactApplicationContext mReactContext;
private final ConcurrentHashMap<Integer, TcpSocketClient> socketClients = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Network> mNetworkMap = new ConcurrentHashMap<>();
private final CurrentNetwork currentNetwork = new CurrentNetwork();
private final ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS);

public TcpSocketModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -81,15 +84,15 @@ protected void doInBackgroundGuarded(Void... params) {
final String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : null;
final String iface = options.hasKey("interface") ? options.getString("interface") : null;
selectNetwork(iface, localAddress);
client = new TcpSocketClient(TcpSocketModule.this, cId, null);
client = new TcpSocketClient(TcpSocketModule.this, cId, executorService, null);
socketClients.put(cId, client);
client.connect(mReactContext, host, port, options, currentNetwork.getNetwork());
onConnect(cId, host, port);
} catch (Exception e) {
onError(cId, e.getMessage());
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}.executeOnExecutor(executorService);
}

@SuppressLint("StaticFieldLeak")
Expand All @@ -115,7 +118,7 @@ protected void doInBackgroundGuarded(Void... params) {
onError(cId, e.toString());
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}.executeOnExecutor(executorService);
}

@SuppressLint("StaticFieldLeak")
Expand All @@ -132,7 +135,7 @@ protected void doInBackgroundGuarded(Void... params) {
socketClient.close();
socketClients.remove(cId);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}.executeOnExecutor(executorService);
}

@SuppressWarnings("unused")
Expand All @@ -149,7 +152,7 @@ public void listen(final Integer cId, final ReadableMap options) {
@Override
protected void doInBackgroundGuarded(Void... params) {
try {
TcpSocketServer server = new TcpSocketServer(socketClients, TcpSocketModule.this, cId, options);
TcpSocketServer server = new TcpSocketServer(socketClients, TcpSocketModule.this, cId, executorService, options);
socketClients.put(cId, server);
int port = options.getInt("port");
String host = options.getString("host");
Expand All @@ -158,7 +161,7 @@ protected void doInBackgroundGuarded(Void... params) {
onError(cId, uhe.getMessage());
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}.executeOnExecutor(executorService);
}

private void requestNetwork(final int transportType) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;

public final class TcpSocketServer extends TcpSocketClient {
private ServerSocket serverSocket;
Expand All @@ -26,7 +27,7 @@ protected Void doInBackground(Object[] objects) {
while (!isCancelled() && !serverSocket.isClosed()) {
Socket socket = serverSocket.accept();
int clientId = getClientId();
TcpSocketClient socketClient = new TcpSocketClient(mReceiverListener, clientId, socket);
TcpSocketClient socketClient = new TcpSocketClient(mReceiverListener, clientId, getExecutorService(), socket);
socketClients.put(clientId, socketClient);
socketClient.startListening();
mReceiverListener.onConnection(getId(), clientId, new InetSocketAddress(socket.getInetAddress(), socket.getPort()));
Expand All @@ -41,9 +42,9 @@ protected Void doInBackground(Object[] objects) {
};


public TcpSocketServer(final ConcurrentHashMap<Integer, TcpSocketClient> socketClients, final TcpReceiverTask.OnDataReceivedListener receiverListener, final Integer id,
public TcpSocketServer(final ConcurrentHashMap<Integer, TcpSocketClient> socketClients, final TcpReceiverTask.OnDataReceivedListener receiverListener, final Integer id, final ExecutorService executorService,
final ReadableMap options) throws IOException {
super(id);
super(id, executorService);
// Get data from options
int port = options.getInt("port");
String address = options.getString("host");
Expand Down Expand Up @@ -76,7 +77,7 @@ private int getClientId() {

private void listen() {
//noinspection unchecked
listening.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
listening.executeOnExecutor(getExecutorService());
}

@Override
Expand Down