From 6a56aedebf5abf2f5281cf1c972573468b99f990 Mon Sep 17 00:00:00 2001 From: Andy Prock Date: Thu, 31 Dec 2015 11:02:57 -0800 Subject: [PATCH] refactor android, remove useless callbacks --- TcpSocket.js | 6 +- android/build.gradle | 40 ++------- android/core/build.gradle | 30 +++++++ android/core/proguard-rules.pro | 17 ++++ .../{ => core}/src/main/AndroidManifest.xml | 2 - .../com/peel/react/TcpSocketListener.java | 0 .../java/com/peel/react/TcpSocketManager.java | 81 +++++++++---------- .../main/java/com/peel/react/TcpSockets.java | 6 +- .../java/com/peel/react/TcpSocketsModule.java | 0 android/gradlew | 0 android/settings.gradle | 1 + examples/rctsockets/android/settings.gradle | 2 +- ios/TcpSockets.m | 35 +++----- 13 files changed, 113 insertions(+), 107 deletions(-) create mode 100644 android/core/build.gradle create mode 100644 android/core/proguard-rules.pro rename android/{ => core}/src/main/AndroidManifest.xml (58%) rename android/{ => core}/src/main/java/com/peel/react/TcpSocketListener.java (100%) rename android/{ => core}/src/main/java/com/peel/react/TcpSocketManager.java (70%) rename android/{ => core}/src/main/java/com/peel/react/TcpSockets.java (97%) rename android/{ => core}/src/main/java/com/peel/react/TcpSocketsModule.java (100%) mode change 100644 => 100755 android/gradlew create mode 100644 android/settings.gradle diff --git a/TcpSocket.js b/TcpSocket.js index bf2ad2b..9f80f42 100644 --- a/TcpSocket.js +++ b/TcpSocket.js @@ -162,9 +162,9 @@ TcpSocket.prototype.end = function(data, encoding) { } this._destroyed = true; - this._debug('closing'); + this._debug('ending'); - Sockets.end(this._id, this._debug.bind(this, 'end')); + Sockets.end(this._id); }; TcpSocket.prototype.destroy = function() { @@ -172,7 +172,7 @@ TcpSocket.prototype.destroy = function() { this._destroyed = true; this._debug('destroying'); - Sockets.destroy(this._id, this._debug.bind(this, 'destroy')); + Sockets.destroy(this._id); } }; diff --git a/android/build.gradle b/android/build.gradle index e59e7e6..94137aa 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,3 +1,5 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + buildscript { repositories { jcenter() @@ -5,40 +7,14 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:1.3.1' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files } } -apply plugin: 'com.android.library' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" - - defaultConfig { - minSdkVersion 16 - targetSdkVersion 22 - versionCode 1 - versionName "0.2.0" - } - - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } - - // needed for https://github.com/square/okio/issues/58 - lintOptions { - warning 'InvalidPackage' +allprojects { + repositories { + jcenter() } } - -repositories { - mavenCentral() -} - -dependencies { - compile 'com.facebook.react:react-native:0.17.+' - compile 'com.koushikdutta.async:androidasync:2.1.6' -} diff --git a/android/core/build.gradle b/android/core/build.gradle new file mode 100644 index 0000000..fc5d8b1 --- /dev/null +++ b/android/core/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 22 + versionCode 1 + versionName "0.2.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + // needed for https://github.com/square/okio/issues/58 + lintOptions { + warning 'InvalidPackage' + } +} + +dependencies { + compile 'com.facebook.react:react-native:0.17.+' + compile 'com.koushikdutta.async:androidasync:2.1.6' +} diff --git a/android/core/proguard-rules.pro b/android/core/proguard-rules.pro new file mode 100644 index 0000000..a92fa17 --- /dev/null +++ b/android/core/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/android/src/main/AndroidManifest.xml b/android/core/src/main/AndroidManifest.xml similarity index 58% rename from android/src/main/AndroidManifest.xml rename to android/core/src/main/AndroidManifest.xml index 0892cb6..8ab4b59 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/core/src/main/AndroidManifest.xml @@ -2,6 +2,4 @@ - - diff --git a/android/src/main/java/com/peel/react/TcpSocketListener.java b/android/core/src/main/java/com/peel/react/TcpSocketListener.java similarity index 100% rename from android/src/main/java/com/peel/react/TcpSocketListener.java rename to android/core/src/main/java/com/peel/react/TcpSocketListener.java diff --git a/android/src/main/java/com/peel/react/TcpSocketManager.java b/android/core/src/main/java/com/peel/react/TcpSocketManager.java similarity index 70% rename from android/src/main/java/com/peel/react/TcpSocketManager.java rename to android/core/src/main/java/com/peel/react/TcpSocketManager.java index 378485b..0ce5201 100644 --- a/android/src/main/java/com/peel/react/TcpSocketManager.java +++ b/android/core/src/main/java/com/peel/react/TcpSocketManager.java @@ -22,33 +22,25 @@ import java.net.UnknownHostException; /** * Created by aprock on 12/29/15. */ -public class TcpSocketManager { +public final class TcpSocketManager { private SparseArray mClients = new SparseArray(); - private WeakReference listener; - private AsyncServer server = AsyncServer.getDefault(); + private WeakReference mListener; + private AsyncServer mServer = AsyncServer.getDefault(); - private int instances = 5000; + private int mInstances = 5000; public TcpSocketManager(TcpSocketListener listener) throws IOException { - this.listener = new WeakReference(listener); - } - - private TcpSocketListener getListener() { - if (listener != null) { - return listener.get(); - } - - return null; + mListener = new WeakReference(listener); } private void setSocketCallbacks(final Integer cId, final AsyncSocket socket) { socket.setClosedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { - TcpSocketListener listener = getListener(); + TcpSocketListener listener = mListener.get(); if (listener != null) { - listener.onClose(cId, null); + listener.onClose(cId, ex==null?null:ex.getMessage()); } } }); @@ -56,7 +48,7 @@ public class TcpSocketManager { socket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { - TcpSocketListener listener = getListener(); + TcpSocketListener listener = mListener.get(); if (listener != null) { listener.onData(cId, bb.getAllByteArray()); } @@ -67,7 +59,7 @@ public class TcpSocketManager { @Override public void onCompleted(Exception ex) { if (ex != null) { - TcpSocketListener listener = getListener(); + TcpSocketListener listener = mListener.get(); if (listener != null) { listener.onError(cId, ex.getMessage()); } @@ -86,12 +78,12 @@ public class TcpSocketManager { socketAddress = new InetSocketAddress(port); } - server.listen(InetAddress.getByName(host), port, new ListenCallback() { + mServer.listen(InetAddress.getByName(host), port, new ListenCallback() { @Override public void onListening(AsyncServerSocket socket) { mClients.put(cId, socket); - TcpSocketListener listener = getListener(); + TcpSocketListener listener = mListener.get(); if (listener != null) { listener.onConnect(cId, socketAddress); } @@ -99,25 +91,25 @@ public class TcpSocketManager { @Override public void onAccepted(AsyncSocket socket) { - setSocketCallbacks(instances, socket); - mClients.put(instances, socket); + setSocketCallbacks(mInstances, socket); + mClients.put(mInstances, socket); - TcpSocketListener listener = getListener(); + TcpSocketListener listener = mListener.get(); if (listener != null) { - listener.onConnection(cId, instances, socketAddress); + listener.onConnection(cId, mInstances, socketAddress); } - instances++; + mInstances++; } @Override public void onCompleted(Exception ex) { - TcpSocketListener listener = getListener(); + mClients.delete(cId); + + TcpSocketListener listener = mListener.get(); if (listener != null) { listener.onClose(cId, ex != null ? ex.getMessage() : null); } - - mClients.remove(cId); } }); } @@ -131,22 +123,19 @@ public class TcpSocketManager { socketAddress = new InetSocketAddress(port); } - server.connectSocket(socketAddress, new ConnectCallback() { + mServer.connectSocket(socketAddress, new ConnectCallback() { @Override public void onConnectCompleted(Exception ex, AsyncSocket socket) { - if (ex != null) { - TcpSocketListener listener = getListener(); - if (listener != null) { - listener.onError(cId, ex.getMessage()); - } - } else { + TcpSocketListener listener = mListener.get(); + if (ex == null) { mClients.put(cId, socket); setSocketCallbacks(cId, socket); - TcpSocketListener listener = getListener(); if (listener != null) { listener.onConnect(cId, socketAddress); } + } else if (listener != null) { + listener.onError(cId, ex.getMessage()); } } }); @@ -161,14 +150,17 @@ public class TcpSocketManager { public void close(final Integer cId) { Object socket = mClients.get(cId); - if (socket == null) { - return; - } - - if (socket instanceof AsyncSocket) { - ((AsyncSocket) socket).close(); - } else if (socket instanceof AsyncServerSocket) { - ((AsyncServerSocket) socket).stop(); + if (socket != null) { + if (socket instanceof AsyncSocket) { + ((AsyncSocket) socket).close(); + } else if (socket instanceof AsyncServerSocket) { + ((AsyncServerSocket) socket).stop(); + } + } else { + TcpSocketListener listener = mListener.get(); + if (listener != null) { + listener.onError(cId, "unable to find socket"); + } } } @@ -177,4 +169,5 @@ public class TcpSocketManager { close(mClients.keyAt(i)); } mClients.clear(); - }} + } +} diff --git a/android/src/main/java/com/peel/react/TcpSockets.java b/android/core/src/main/java/com/peel/react/TcpSockets.java similarity index 97% rename from android/src/main/java/com/peel/react/TcpSockets.java rename to android/core/src/main/java/com/peel/react/TcpSockets.java index 8e979e7..844cbe6 100644 --- a/android/src/main/java/com/peel/react/TcpSockets.java +++ b/android/core/src/main/java/com/peel/react/TcpSockets.java @@ -124,7 +124,7 @@ public final class TcpSockets extends ReactContextBaseJavaModule implements TcpS } @ReactMethod - public void end(final Integer cId, final Callback callback) { + public void end(final Integer cId) { new GuardedAsyncTask(getReactApplicationContext()) { @Override protected void doInBackgroundGuarded(Void... params) { @@ -134,8 +134,8 @@ public final class TcpSockets extends ReactContextBaseJavaModule implements TcpS } @ReactMethod - public void destroy(final Integer cId, final Callback callback) { - end(cId, callback); + public void destroy(final Integer cId) { + end(cId); } /** TcpSocketListener */ diff --git a/android/src/main/java/com/peel/react/TcpSocketsModule.java b/android/core/src/main/java/com/peel/react/TcpSocketsModule.java similarity index 100% rename from android/src/main/java/com/peel/react/TcpSocketsModule.java rename to android/core/src/main/java/com/peel/react/TcpSocketsModule.java diff --git a/android/gradlew b/android/gradlew old mode 100644 new mode 100755 diff --git a/android/settings.gradle b/android/settings.gradle new file mode 100644 index 0000000..2620d05 --- /dev/null +++ b/android/settings.gradle @@ -0,0 +1 @@ +include ':core' diff --git a/examples/rctsockets/android/settings.gradle b/examples/rctsockets/android/settings.gradle index 511d0ea..0cf9df2 100644 --- a/examples/rctsockets/android/settings.gradle +++ b/examples/rctsockets/android/settings.gradle @@ -2,4 +2,4 @@ rootProject.name = 'rctsockets' include ':app' include ':react-native-tcp' -project(':react-native-tcp').projectDir = new File(settingsDir, '../node_modules/react-native-tcp/android') +project(':react-native-tcp').projectDir = new File(settingsDir, '../node_modules/react-native-tcp/android/core') diff --git a/ios/TcpSockets.m b/ios/TcpSockets.m index 3c34d67..b658bd5 100644 --- a/ios/TcpSockets.m +++ b/ios/TcpSockets.m @@ -27,7 +27,7 @@ RCT_EXPORT_MODULE() -(void)dealloc { for (NSNumber *cId in _clients.allKeys) { - [self destroyClient:cId callback:nil]; + [self destroyClient:cId]; } } @@ -73,7 +73,7 @@ RCT_EXPORT_METHOD(connect:(nonnull NSNumber*)cId RCT_EXPORT_METHOD(write:(nonnull NSNumber*)cId string:(NSString *)base64String callback:(RCTResponseSenderBlock)callback) { - TcpSocketClient* client = [self findClient:cId callback:callback]; + TcpSocketClient* client = [self findClient:cId]; if (!client) return; // iOS7+ @@ -82,14 +82,12 @@ RCT_EXPORT_METHOD(write:(nonnull NSNumber*)cId [client writeData:data callback:callback]; } -RCT_EXPORT_METHOD(end:(nonnull NSNumber*)cId - callback:(RCTResponseSenderBlock)callback) { - [self endClient:cId callback:callback]; +RCT_EXPORT_METHOD(end:(nonnull NSNumber*)cId) { + [self endClient:cId]; } -RCT_EXPORT_METHOD(destroy:(nonnull NSNumber*)cId - callback:(RCTResponseSenderBlock)callback) { - [self destroyClient:cId callback:callback]; +RCT_EXPORT_METHOD(destroy:(nonnull NSNumber*)cId) { + [self destroyClient:cId]; } RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId @@ -143,22 +141,19 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId } - (void)onError:(TcpSocketClient*) client withError:(NSError *)err { - NSString *msg = [err userInfo][@"NSLocalizedFailureReason"] ?: [err userInfo][@"NSLocalizedDescription"]; + NSString *msg = err.localizedFailureReason ?: err.localizedDescription; [self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"tcp-%@-error", client.id] body:msg]; } --(TcpSocketClient*)findClient:(nonnull NSNumber*)cId callback:(RCTResponseSenderBlock)callback +-(TcpSocketClient*)findClient:(nonnull NSNumber*)cId { TcpSocketClient *client = _clients[cId]; if (!client) { - if (!callback) { - RCTLogError(@"%@.missing callback parameter.", [self class]); - } else { - callback(@[[NSString stringWithFormat:@"no client found with id %@", cId]]); - } - + NSString *msg = [NSString stringWithFormat:@"no client found with id %@", cId]; + [self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"tcp-%@-error", cId] + body:msg]; return nil; } @@ -166,20 +161,16 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId } -(void)endClient:(nonnull NSNumber*)cId - callback:(RCTResponseSenderBlock)callback { - TcpSocketClient* client = [self findClient:cId callback:callback]; + TcpSocketClient* client = [self findClient:cId]; if (!client) return; [client end]; - - if (callback) callback(@[]); } -(void)destroyClient:(nonnull NSNumber*)cId - callback:(RCTResponseSenderBlock)callback { - TcpSocketClient* client = [self findClient:cId callback:nil]; + TcpSocketClient* client = [self findClient:cId]; if (!client) return; [client destroy];