Compare commits

...

2 Commits

Author SHA1 Message Date
Gideon le Grange 4c2f69a609 Mark connection as closed after a ApiConnectionException in the reader thread 2018-08-02 11:18:31 +02:00
Gideon le Grange fcaec18675 Trying soTimeout solution for thread lock-up issue 2018-08-02 10:16:21 +02:00
2 changed files with 12 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import java.io.UnsupportedEncodingException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
@ -82,6 +83,13 @@ public final class ApiConnectionImpl extends ApiConnection {
public void setTimeout(int timeout) throws MikrotikApiException { public void setTimeout(int timeout) throws MikrotikApiException {
if (timeout > 0) { if (timeout > 0) {
this.timeout = timeout; this.timeout = timeout;
if (sock != null) {
try {
sock.setSoTimeout(timeout);
} catch (SocketException ex) {
throw new MikrotikApiException(String.format("Error setting socket timeout: %s", ex.getMessage()), ex);
}
}
} else { } else {
throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout)); throw new MikrotikApiException(String.format("Invalid timeout value '%d'; must be postive", timeout));
} }
@ -136,6 +144,7 @@ public final class ApiConnectionImpl extends ApiConnection {
InetAddress ia = InetAddress.getByName(host.trim()); InetAddress ia = InetAddress.getByName(host.trim());
sock = fact.createSocket(); sock = fact.createSocket();
sock.connect(new InetSocketAddress(ia, port), conTimeout); sock.connect(new InetSocketAddress(ia, port), conTimeout);
sock.setSoTimeout(timeout);
in = new DataInputStream(sock.getInputStream()); in = new DataInputStream(sock.getInputStream());
out = new DataOutputStream(sock.getOutputStream()); out = new DataOutputStream(sock.getOutputStream());
connected = true; connected = true;
@ -207,8 +216,9 @@ public final class ApiConnectionImpl extends ApiConnection {
put(ex); put(ex);
} catch (ApiConnectionException ex) { } catch (ApiConnectionException ex) {
if (connected || !sock.isClosed()) { if (connected || !sock.isClosed()) {
put(ex); connected = false;
} }
put(ex);
} }
} }
} }

View File

@ -192,4 +192,5 @@ final class Util {
} }
return c; return c;
} }
} }