diff options
author | Andrew Comminos <andrewcomminos@gmail.com> | 2014-07-14 10:22:30 +0400 |
---|---|---|
committer | Andrew Comminos <andrewcomminos@gmail.com> | 2014-07-14 10:22:30 +0400 |
commit | 5886f6c799601111fdef4a8da827ca01f2f5e4f6 (patch) | |
tree | aa1c872e11eb147d16498979551b12b34e8de3fe | |
parent | 198207ea22ecefc34962c801af65fd197139a35f (diff) |
Properly notify listener of UDP socket errors.
-rw-r--r-- | src/main/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | src/main/java/com/morlunk/jumble/net/JumbleUDP.java | 37 |
2 files changed, 29 insertions, 9 deletions
diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 1b4faaa..18d4700 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -20,7 +20,6 @@ android:versionCode="1" android:versionName="1.0"> - <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true"> diff --git a/src/main/java/com/morlunk/jumble/net/JumbleUDP.java b/src/main/java/com/morlunk/jumble/net/JumbleUDP.java index ea17dec..9892c92 100644 --- a/src/main/java/com/morlunk/jumble/net/JumbleUDP.java +++ b/src/main/java/com/morlunk/jumble/net/JumbleUDP.java @@ -75,11 +75,15 @@ public class JumbleUDP extends JumbleNetworkThread { try { mResolvedHost = InetAddress.getByName(mHost); mUDPSocket = new DatagramSocket(); - } catch (SocketException e) { - if(mListener != null) mListener.onUDPConnectionError(e); - return; - } catch (UnknownHostException e) { - if(mListener != null) mListener.onUDPConnectionError(e); + } catch (final IOException e) { + if(mListener != null) { + executeOnMainThread(new Runnable() { + @Override + public void run() { + mListener.onUDPConnectionError(e); + } + }); + } return; } @@ -115,12 +119,25 @@ public class JumbleUDP extends JumbleNetworkThread { } else if(mCryptState.getLastGoodElapsed() > 5000000 && mCryptState.getLastRequestElapsed() > 5000000) { mCryptState.resetLastRequestTime(); - mListener.resyncCryptState(); + executeOnMainThread(new Runnable() { + @Override + public void run() { + mListener.resyncCryptState(); + } + }); } } - } catch (IOException e) { - e.printStackTrace(); + } catch (final IOException e) { + // If a UDP exception is thrown while connected, notify the listener to fall back to TCP. + if(mConnected && mListener != null) { + executeOnMainThread(new Runnable() { + @Override + public void run() { + mListener.onUDPConnectionError(e); + } + }); + } break; } } @@ -164,6 +181,10 @@ public class JumbleUDP extends JumbleNetworkThread { }); } + /** + * Note that all connection state related calls are made on the main thread. + * onUDPDataReceived is always called on the UDP receive thread. + */ public interface UDPConnectionListener { public void onUDPDataReceived(byte[] data); public void onUDPConnectionError(Exception e); |