From 27534cd8edb58295a2438ab41b417544a089ffeb Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 20:17:28 +0200 Subject: [PATCH 01/12] Add JS --- src/TcpSocket.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/TcpSocket.js b/src/TcpSocket.js index fb70202..e69f8e5 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -162,12 +162,31 @@ export default class TcpSocket extends EventEmitter { * * Passing `true` for `noDelay` or not passing an argument will disable Nagle's algorithm for the socket. Passing false for noDelay will enable Nagle's algorithm. * - * @param {boolean} noDelay + * @param {boolean} noDelay Default: `true` */ setNoDelay(noDelay = true) { Sockets.setNoDelay(this._id, noDelay); } + /** + * Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket. + * + * Set `initialDelay` (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. + * Setting `0` for initialDelay will leave the value unchanged from the default (or previous) setting. + * + * @param {boolean} enable Default: `false` + * @param {number} initialDelay Default: `0` + */ + setKeepAlive(enable = false, initialDelay = 0) { + if (this._state != STATE.CONNECTED) { + this.once('connect', () => this.setKeepAlive(enable, initialDelay)); + return this; + } + + Sockets.setKeepAlive(enable, initialDelay); + return this; + } + address() { return this._address; } From b159dceb11a2c6da56a76578f36f3dfc55a05988 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 20:26:26 +0200 Subject: [PATCH 02/12] Add Android part --- .../react/tcpsocket/TcpSocketClient.java | 10 ++++++++++ .../react/tcpsocket/TcpSocketModule.java | 16 ++++++++++++++++ src/TcpSocket.js | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java index 371e974..013ac4b 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java @@ -141,4 +141,14 @@ public void setNoDelay(final boolean noDelay) throws IOException { } socket.setTcpNoDelay(noDelay); } + + /** + * @param enable `true` to enable keep-alive functionality + */ + public void setKeepAlive(final boolean enable) throws IOException { + if (socket == null) { + throw new IOException("Socket is not connected."); + } + socket.setKeepAlive(enable); + } } diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java index a8c437b..234a365 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java @@ -164,6 +164,7 @@ protected void doInBackgroundGuarded(Void... params) { }.executeOnExecutor(executorService); } + @SuppressWarnings("unused") @ReactMethod public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) { final TcpSocketClient client = socketClients.get(cId); @@ -178,6 +179,21 @@ public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) { } } + @SuppressWarnings("unused") + @ReactMethod + public void setKeepAlive(@NonNull final Integer cId, final boolean enable) { + final TcpSocketClient client = socketClients.get(cId); + if (client == null) { + onError(cId, TAG + "socket not found."); + return; + } + try { + client.setKeepAlive(enable); + } catch (IOException e) { + onError(cId, e.getMessage()); + } + } + private void requestNetwork(final int transportType) throws InterruptedException { final NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder(); requestBuilder.addTransportType(transportType); diff --git a/src/TcpSocket.js b/src/TcpSocket.js index e69f8e5..a1aac3c 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -183,7 +183,7 @@ export default class TcpSocket extends EventEmitter { return this; } - Sockets.setKeepAlive(enable, initialDelay); + Sockets.setKeepAlive(this._id, enable); return this; } From 650f7c680c700e3e47152d597c9f515e545f07b1 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 20:34:10 +0200 Subject: [PATCH 03/12] Add warning for initialDelay param --- src/TcpSocket.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/TcpSocket.js b/src/TcpSocket.js index a1aac3c..1ad86f1 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -165,17 +165,19 @@ export default class TcpSocket extends EventEmitter { * @param {boolean} noDelay Default: `true` */ setNoDelay(noDelay = true) { + if (this._state != STATE.CONNECTED) { + this.once('connect', () => this.setNoDelay(noDelay)); + return this; + } Sockets.setNoDelay(this._id, noDelay); + return this; } /** - * Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket. - * - * Set `initialDelay` (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. - * Setting `0` for initialDelay will leave the value unchanged from the default (or previous) setting. + * Enable/disable keep-alive functionality. **`initialDelay` is ignored!** * * @param {boolean} enable Default: `false` - * @param {number} initialDelay Default: `0` + * @param {number} initialDelay ***IGNORED** */ setKeepAlive(enable = false, initialDelay = 0) { if (this._state != STATE.CONNECTED) { @@ -183,6 +185,12 @@ export default class TcpSocket extends EventEmitter { return this; } + if (initialDelay !== 0) { + console.warn( + 'WARNING: socket.setKeepAlive(enable, initialDelay) `initialDelay` param is ignored' + ); + } + Sockets.setKeepAlive(this._id, enable); return this; } From 1315223a862e9413171a974aa803a862f05db66f Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 20:59:13 +0200 Subject: [PATCH 04/12] Add iOS part --- examples/tcpsockets/ios/Podfile.lock | 4 ++-- examples/tcpsockets/package.json | 2 +- ios/TcpSocketClient.h | 2 ++ ios/TcpSocketClient.m | 18 ++++++++++++++++++ ios/TcpSockets.m | 7 +++++++ src/TcpSocket.js | 15 ++++++--------- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/examples/tcpsockets/ios/Podfile.lock b/examples/tcpsockets/ios/Podfile.lock index cab7a23..f3eac35 100644 --- a/examples/tcpsockets/ios/Podfile.lock +++ b/examples/tcpsockets/ios/Podfile.lock @@ -233,7 +233,7 @@ PODS: - React-cxxreact (= 0.62.1) - React-jsi (= 0.62.1) - React-jsinspector (0.62.1) - - react-native-tcp-socket (3.4.2): + - react-native-tcp-socket (4.1.0): - CocoaAsyncSocket - React - React-RCTActionSheet (0.62.1): @@ -430,7 +430,7 @@ SPEC CHECKSUMS: React-jsi: 600d8e42510c3254fd2abd702f4b9d3f598d8f52 React-jsiexecutor: e9698dee4fd43ceb44832baf15d5745f455b0157 React-jsinspector: f74a62727e5604119abd4a1eda52c0a12144bcd5 - react-native-tcp-socket: a100fffbe7c96f92f457fa7fad6a29ec24bb8187 + react-native-tcp-socket: 7ae44fe823a4c8e73f66e4562fceef392f94ce13 React-RCTActionSheet: af8f28dd82fec89b8fe29637b8c779829e016a88 React-RCTAnimation: 0d21fff7c20fb8ee41de5f2ebb63221127febd96 React-RCTBlob: 9496bd93130b22069bfbc5d35e98653dae7c35c6 diff --git a/examples/tcpsockets/package.json b/examples/tcpsockets/package.json index 69683bc..ccee04a 100644 --- a/examples/tcpsockets/package.json +++ b/examples/tcpsockets/package.json @@ -14,7 +14,7 @@ "dependencies": { "react": "16.11.0", "react-native": "0.62.1", - "react-native-tcp-socket": "^3.5.0" + "react-native-tcp-socket": "^4.1.0" }, "devDependencies": { "@babel/core": "^7.7.2", diff --git a/ios/TcpSocketClient.h b/ios/TcpSocketClient.h index 672ee89..165ebc8 100644 --- a/ios/TcpSocketClient.h +++ b/ios/TcpSocketClient.h @@ -94,4 +94,6 @@ typedef enum RCTTCPError RCTTCPError; - (void)setNoDelay:(BOOL)noDelay; +- (void)setKeepAlive:(BOOL)enable delay:(int)delay; + @end diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index a6105f2..4d82c4f 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -137,6 +137,24 @@ - (void)setNoDelay:(BOOL)noDelay }]; } +- (void)setKeepAlive:(BOOL)enable delay:(int)delay +{ + [_tcpSocket performBlock:^{ + int fd = [self->_tcpSocket socketFD]; + int on = enable ? 1 : 0; + int enableKA = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); + int delayKA = 0; + if (delay != 0) { + // Only change the delay when delay != 0 + delayKA = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)); + } + if (enableKA == -1 || delayKA == -1) { + /* TODO: handle error */ + RCTLogWarn(@"setNoDelay caused an unexpected error"); + } + }]; +} + - (BOOL)listen:(NSDictionary *)options error:(NSError **)error { if (_tcpSocket) { diff --git a/ios/TcpSockets.m b/ios/TcpSockets.m index 166ea54..513d267 100644 --- a/ios/TcpSockets.m +++ b/ios/TcpSockets.m @@ -123,6 +123,13 @@ - (TcpSocketClient *)createSocket:(nonnull NSNumber*)cId [client setNoDelay:noDelay]; } +RCT_EXPORT_METHOD(setKeepAlive:(nonnull NSNumber*)cId enable:(BOOL)enable delay:(int)delay) { + TcpSocketClient* client = [self findClient:cId]; + if (!client) return; + + [client setKeepAlive:enable delay:delay]; +} + - (void)onConnect:(TcpSocketClient*) client { [self sendEventWithName:@"connect" diff --git a/src/TcpSocket.js b/src/TcpSocket.js index 1ad86f1..3e5a36d 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -174,10 +174,13 @@ export default class TcpSocket extends EventEmitter { } /** - * Enable/disable keep-alive functionality. **`initialDelay` is ignored!** + * Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket. + * + * Set `initialDelay` (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. + * Setting `0` for initialDelay will leave the value unchanged from the default (or previous) setting. * * @param {boolean} enable Default: `false` - * @param {number} initialDelay ***IGNORED** + * @param {number} initialDelay Default: `0` */ setKeepAlive(enable = false, initialDelay = 0) { if (this._state != STATE.CONNECTED) { @@ -185,13 +188,7 @@ export default class TcpSocket extends EventEmitter { return this; } - if (initialDelay !== 0) { - console.warn( - 'WARNING: socket.setKeepAlive(enable, initialDelay) `initialDelay` param is ignored' - ); - } - - Sockets.setKeepAlive(this._id, enable); + Sockets.setKeepAlive(this._id, enable, Math.floor(initialDelay)); return this; } From a3af88f925b77073dbc47b296d9af3b7c7c17172 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 21:08:19 +0200 Subject: [PATCH 05/12] Fix log error name --- ios/TcpSocketClient.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index 4d82c4f..9eac007 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -150,7 +150,7 @@ - (void)setKeepAlive:(BOOL)enable delay:(int)delay } if (enableKA == -1 || delayKA == -1) { /* TODO: handle error */ - RCTLogWarn(@"setNoDelay caused an unexpected error"); + RCTLogWarn(@"setKeepAlive caused an unexpected error"); } }]; } From 107754dd4e3d4e6eb222a21271ba47c643684a2b Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 21:20:28 +0200 Subject: [PATCH 06/12] Add delay to Android --- .../java/com/asterinet/react/tcpsocket/TcpSocketClient.java | 6 +++++- .../java/com/asterinet/react/tcpsocket/TcpSocketModule.java | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java index 013ac4b..514f3e9 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java @@ -2,6 +2,7 @@ import android.content.Context; import android.net.Network; +import android.util.Log; import android.util.Pair; import com.facebook.react.bridge.ReadableMap; @@ -145,10 +146,13 @@ public void setNoDelay(final boolean noDelay) throws IOException { /** * @param enable `true` to enable keep-alive functionality */ - public void setKeepAlive(final boolean enable) throws IOException { + public void setKeepAlive(final boolean enable, final int delay) throws IOException { if (socket == null) { throw new IOException("Socket is not connected."); } + if (delay != 0) { + Log.w("ReactNativeJS", "setKeepAlive() delay is ignored on Android"); + } socket.setKeepAlive(enable); } } diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java index 234a365..56a692b 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java @@ -181,14 +181,14 @@ public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) { @SuppressWarnings("unused") @ReactMethod - public void setKeepAlive(@NonNull final Integer cId, final boolean enable) { + public void setKeepAlive(@NonNull final Integer cId, final boolean enable, final int delay) { final TcpSocketClient client = socketClients.get(cId); if (client == null) { onError(cId, TAG + "socket not found."); return; } try { - client.setKeepAlive(enable); + client.setKeepAlive(enable, delay); } catch (IOException e) { onError(cId, e.getMessage()); } From 9f5a9d01eeebe85a109436329a5277c8ef4f6177 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 1 Jul 2020 21:34:21 +0200 Subject: [PATCH 07/12] Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e4c3c7..1d2dd9e 100644 --- a/README.md +++ b/README.md @@ -184,10 +184,13 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func ### TcpSocket * **Methods:** * **[`TcpSocket.createConnection(options[, callback])`](#createconnection)** - * [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback) + * [`address()`](https://nodejs.org/api/net.html#net_socket_address) * [`destroy([error])`](https://nodejs.org/api/net.html#net_socket_destroy_error) + * [`end([data][, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback) + * [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) * [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) * [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) + * [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback) #### `createConnection()` `createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options). From 3921be61d89f1df777cb2850b9a06c2614f643b0 Mon Sep 17 00:00:00 2001 From: Rodrigo Martin Date: Mon, 6 Jul 2020 13:27:44 +0200 Subject: [PATCH 08/12] Add 4.X.X to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d2dd9e..553ed70 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ To use this library you need to ensure you are using the correct version of Reac | `react-native-tcp-socket` version | Required React Native Version | | ----------------------------------------- | --------------------------------------------------------------------------------- | -| `3.x.x` | `>= 0.61` | +| ``4.X.X`, `3.X.X` | `>= 0.61` | | `1.2.2` | `>= 0.??` | ## Usage From aeec0217d1c0e32eecadcf6b4c53c8d19f28e185 Mon Sep 17 00:00:00 2001 From: Rodrigo Martin Date: Mon, 6 Jul 2020 13:30:13 +0200 Subject: [PATCH 09/12] Disclaimer for `initialDelay` --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 553ed70..87abb0c 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ To use this library you need to ensure you are using the correct version of Reac | `react-native-tcp-socket` version | Required React Native Version | | ----------------------------------------- | --------------------------------------------------------------------------------- | -| ``4.X.X`, `3.X.X` | `>= 0.61` | +| `4.X.X`, `3.X.X` | `>= 0.61` | | `1.2.2` | `>= 0.??` | ## Usage @@ -187,7 +187,7 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func * [`address()`](https://nodejs.org/api/net.html#net_socket_address) * [`destroy([error])`](https://nodejs.org/api/net.html#net_socket_destroy_error) * [`end([data][, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback) - * [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) + * [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) - _`initialDelay` is ignored_ * [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) * [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) * [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback) From ffcbd371a79551a4e3d5ee77235b97712db538fa Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Mon, 6 Jul 2020 13:35:50 +0200 Subject: [PATCH 10/12] Ignore initialDelay --- .../asterinet/react/tcpsocket/TcpSocketClient.java | 6 ++---- .../asterinet/react/tcpsocket/TcpSocketModule.java | 4 ++-- src/TcpSocket.js | 11 ++++++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java index 514f3e9..cd40eea 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java @@ -146,13 +146,11 @@ public void setNoDelay(final boolean noDelay) throws IOException { /** * @param enable `true` to enable keep-alive functionality */ - public void setKeepAlive(final boolean enable, final int delay) throws IOException { + public void setKeepAlive(final boolean enable, final int initialDelay) throws IOException { if (socket == null) { throw new IOException("Socket is not connected."); } - if (delay != 0) { - Log.w("ReactNativeJS", "setKeepAlive() delay is ignored on Android"); - } + // `initialDelay` is ignored socket.setKeepAlive(enable); } } diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java index 56a692b..6da6153 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java @@ -181,14 +181,14 @@ public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) { @SuppressWarnings("unused") @ReactMethod - public void setKeepAlive(@NonNull final Integer cId, final boolean enable, final int delay) { + public void setKeepAlive(@NonNull final Integer cId, final boolean enable, final int initialDelay) { final TcpSocketClient client = socketClients.get(cId); if (client == null) { onError(cId, TAG + "socket not found."); return; } try { - client.setKeepAlive(enable, delay); + client.setKeepAlive(enable, initialDelay); } catch (IOException e) { onError(cId, e.getMessage()); } diff --git a/src/TcpSocket.js b/src/TcpSocket.js index 3e5a36d..aa0a206 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -176,11 +176,10 @@ export default class TcpSocket extends EventEmitter { /** * Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket. * - * Set `initialDelay` (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. - * Setting `0` for initialDelay will leave the value unchanged from the default (or previous) setting. + * `initialDelay` is ignored. * * @param {boolean} enable Default: `false` - * @param {number} initialDelay Default: `0` + * @param {number} initialDelay ***IGNORED**. Default: `0` */ setKeepAlive(enable = false, initialDelay = 0) { if (this._state != STATE.CONNECTED) { @@ -188,6 +187,12 @@ export default class TcpSocket extends EventEmitter { return this; } + if (initialDelay !== 0) { + console.warn( + 'react-native-tcp-socket: initialDelay param in socket.setKeepAlive() is ignored' + ); + } + Sockets.setKeepAlive(this._id, enable, Math.floor(initialDelay)); return this; } From ab43815287d8e9501d5d8b1e06b6f35d41b3bf8f Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Mon, 6 Jul 2020 13:44:15 +0200 Subject: [PATCH 11/12] iOS: initialDelay is ignored --- ios/TcpSocketClient.h | 2 +- ios/TcpSocketClient.m | 14 +++++--------- ios/TcpSockets.m | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/ios/TcpSocketClient.h b/ios/TcpSocketClient.h index 165ebc8..2c1b3b8 100644 --- a/ios/TcpSocketClient.h +++ b/ios/TcpSocketClient.h @@ -94,6 +94,6 @@ typedef enum RCTTCPError RCTTCPError; - (void)setNoDelay:(BOOL)noDelay; -- (void)setKeepAlive:(BOOL)enable delay:(int)delay; +- (void)setKeepAlive:(BOOL)enable initialDelay:(int)initialDelay; @end diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index 9eac007..bbcbb0d 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -132,25 +132,21 @@ - (void)setNoDelay:(BOOL)noDelay int on = noDelay ? 1 : 0; if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)) == -1) { /* TODO: handle error */ - RCTLogWarn(@"setNoDelay caused an unexpected error"); + RCTLogWarn(@"react-native-tcp-socket: setNoDelay() caused an unexpected error"); } }]; } -- (void)setKeepAlive:(BOOL)enable delay:(int)delay +- (void)setKeepAlive:(BOOL)enable initialDelay:(int)initialDelay { [_tcpSocket performBlock:^{ int fd = [self->_tcpSocket socketFD]; int on = enable ? 1 : 0; int enableKA = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); - int delayKA = 0; - if (delay != 0) { - // Only change the delay when delay != 0 - delayKA = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)); - } - if (enableKA == -1 || delayKA == -1) { + // `initialDelay` is ignored + if (enableKA == -1) { /* TODO: handle error */ - RCTLogWarn(@"setKeepAlive caused an unexpected error"); + RCTLogWarn(@"react-native-tcp-socket: setKeepAlive() caused an unexpected error"); } }]; } diff --git a/ios/TcpSockets.m b/ios/TcpSockets.m index 513d267..5666e30 100644 --- a/ios/TcpSockets.m +++ b/ios/TcpSockets.m @@ -123,11 +123,11 @@ - (TcpSocketClient *)createSocket:(nonnull NSNumber*)cId [client setNoDelay:noDelay]; } -RCT_EXPORT_METHOD(setKeepAlive:(nonnull NSNumber*)cId enable:(BOOL)enable delay:(int)delay) { +RCT_EXPORT_METHOD(setKeepAlive:(nonnull NSNumber*)cId enable:(BOOL)enable initialDelay:(int)initialDelay) { TcpSocketClient* client = [self findClient:cId]; if (!client) return; - [client setKeepAlive:enable delay:delay]; + [client setKeepAlive:enable initialDelay:initialDelay]; } - (void)onConnect:(TcpSocketClient*) client From 924be526e5c814731aa308e6ff0f40dce14f13e8 Mon Sep 17 00:00:00 2001 From: Rodrigo Martin Date: Mon, 6 Jul 2020 13:45:37 +0200 Subject: [PATCH 12/12] Remove unused imports --- .../main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java index cd40eea..30926d8 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java @@ -2,7 +2,6 @@ import android.content.Context; import android.net.Network; -import android.util.Log; import android.util.Pair; import com.facebook.react.bridge.ReadableMap;