Skip to content

Commit

Permalink
fix(iOS): Fixed error when connecting without localAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito committed Feb 18, 2020
1 parent 862944a commit 18c430d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.InetSocketAddress;
import java.net.Socket;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class TcpSocketClient {
Expand All @@ -25,7 +26,7 @@ class TcpSocketClient {
this.id = id;
}

TcpSocketClient(final TcpReceiverTask.OnDataReceivedListener receiverListener, final Integer id, @Nullable final Socket socket) {
TcpSocketClient(@NonNull final TcpReceiverTask.OnDataReceivedListener receiverListener, @NonNull final Integer id, @Nullable final Socket socket) {
this(id);
this.socket = socket;
receiverTask = new TcpReceiverTask();
Expand All @@ -41,25 +42,25 @@ public Socket getSocket() {
return socket;
}

public void connect(final String address, final Integer port, final ReadableMap options, final Network network) throws IOException {
public void connect(@NonNull final String address, @NonNull final Integer port, @NonNull final ReadableMap options, @Nullable final Network network) throws IOException {
if (socket != null) throw new IOException("Already connected");
socket = new Socket();
// Get the addresses
String localAddress = options.getString("localAddress");
InetAddress localInetAddress = InetAddress.getByName(localAddress);
InetAddress remoteInetAddress = InetAddress.getByName(address);
final String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : "0.0.0.0";
final InetAddress localInetAddress = InetAddress.getByName(localAddress);
final InetAddress remoteInetAddress = InetAddress.getByName(address);
if (network != null)
network.bindSocket(socket);
// setReuseAddress
try {
if (options.hasKey("reuseAddress")) {
boolean reuseAddress = options.getBoolean("reuseAddress");
socket.setReuseAddress(reuseAddress);
} catch (Exception e) {
} else {
// Default to true
socket.setReuseAddress(true);
}
final int localPort = options.hasKey("localPort") ? options.getInt("localPort") : 0;
// bind
int localPort = options.getInt("localPort");
socket.bind(new InetSocketAddress(localInetAddress, localPort));
socket.connect(new InetSocketAddress(remoteInetAddress, port));
startListening();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TcpSocketModule extends ReactContextBaseJavaModule implements TcpRe
private final ReactApplicationContext mReactContext;
private final ConcurrentHashMap<Integer, TcpSocketClient> socketClients = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Network> mNetworkMap = new ConcurrentHashMap<>();
@Nullable
private Network mSelectedNetwork;

private static final String TAG = "TcpSockets";
Expand Down Expand Up @@ -114,24 +115,19 @@ public void onUnavailable() {
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("unused")
@ReactMethod
public void connect(final Integer cId, final String host, final Integer port, final ReadableMap options) {
public void connect(@NonNull final Integer cId, @NonNull final String host, @NonNull final Integer port, @NonNull final ReadableMap options) {
new GuardedAsyncTask<Void, Void>(mReactContext.getExceptionHandler()) {
@Override
protected void doInBackgroundGuarded(Void... params) {
// Check for cID
if (cId == null) {
onError(cId, TAG + "createSocket called with nil id parameter.");
return;
}
TcpSocketClient client = socketClients.get(cId);
if (client != null) {
onError(cId, TAG + "createSocket called twice with the same id.");
return;
}
try {
// Get the network interface
String localAddress = options.getString("localAddress");
String iface = options.getString("interface");
String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : null;
String iface = options.hasKey("interface") ? options.getString("interface") : null;
selectNetwork(iface, localAddress);
client = new TcpSocketClient(TcpSocketModule.this, cId, null);
socketClients.put(cId, client);
Expand Down
3 changes: 0 additions & 3 deletions src/TcpSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ export default class TcpSocket {
// Normalize args
options.host = options.host || 'localhost';
options.port = Number(options.port) || 0;
options.localPort = Number(options.localPort) || 0;
options.localAddress = options.localAddress || '0.0.0.0';
options.interface = options.interface || '';
const connectListener = this.on('connect', (ev) => {
connectListener.remove();
if (callback) callback(ev.address);
Expand Down

0 comments on commit 18c430d

Please sign in to comment.