aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java
diff options
context:
space:
mode:
authorritchie <ritchie@gmx.at>2015-05-23 15:37:13 +0200
committerritchie <ritchie@gmx.at>2015-05-23 15:37:13 +0200
commit284d64856c4ab6629228cfdb5437e54d0bd0bf7a (patch)
tree80f92a15ed386866dd7d9ec6d58b6e860edf7bc3 /core/src/main/java
parentb9edc335cd641876e0468fa68a302716e2d51c86 (diff)
downloadnanohttpd-284d64856c4ab6629228cfdb5437e54d0bd0bf7a.tar.gz
try for #159 to allow the start method be called in the android ui
thread.
Diffstat (limited to 'core/src/main/java')
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java27
1 files changed, 25 insertions, 2 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index a00f49e..90fa479 100644
--- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
+++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
@@ -1340,12 +1340,23 @@ public abstract class NanoHTTPD {
private final int timeout;
+ private IOException bindException;
+
+ private boolean hasBinded = false;
+
private ServerRunnable(int timeout) {
this.timeout = timeout;
}
@Override
public void run() {
+ try {
+ myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort));
+ hasBinded = true;
+ } catch (IOException e) {
+ this.bindException = e;
+ return;
+ }
do {
try {
final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept();
@@ -1802,12 +1813,24 @@ public abstract class NanoHTTPD {
this.myServerSocket = new ServerSocket();
}
this.myServerSocket.setReuseAddress(true);
- this.myServerSocket.bind(this.hostname != null ? new InetSocketAddress(this.hostname, this.myPort) : new InetSocketAddress(this.myPort));
- this.myThread = new Thread(createServerRunnable(timeout));
+ ServerRunnable serverRunnable = createServerRunnable(timeout);
+ this.myThread = new Thread(serverRunnable);
this.myThread.setDaemon(true);
this.myThread.setName("NanoHttpd Main Listener");
this.myThread.start();
+ while (!serverRunnable.hasBinded && serverRunnable.bindException == null) {
+ try {
+ Thread.sleep(10L);
+ } catch (Throwable e) {
+ // on android this may not be allowed, that's why we
+ // catch throwable the wait should be very short because we are
+ // just waiting for the bind of the socket
+ }
+ }
+ if (serverRunnable.bindException != null) {
+ throw serverRunnable.bindException;
+ }
}
/**