From: Niki Roo Date: Mon, 22 Apr 2019 09:13:48 +0000 (+0200) Subject: server: allow closing conn from server action X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=a72ea8a7a30837734f7f3f27a4ec7979d2549043;p=fanfix-jexer.git server: allow closing conn from server action --- diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java index 60f8db8..10d3440 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java @@ -13,6 +13,8 @@ import be.nikiroo.utils.Version; * @author niki */ abstract class ConnectActionServer { + private boolean closing; + /** * The underlying {@link ConnectAction}. *

@@ -77,6 +79,32 @@ abstract class ConnectActionServer { }).start(); } + /** + * Stop the client/server connection on behalf of the server (usually, the + * client connects then is allowed to send as many requests as it wants; in + * some cases, though, the server may wish to forcefully close the + * connection and can do via this value, when it is set to TRUE). + *

+ * Example of usage: the client failed an authentication check, cut the + * connection here and now. + */ + public boolean isClosing() { + return closing; + } + + /** + * Can be called to stop the client/server connection on behalf of the + * server (usually, the client connects then is allowed to send as many + * requests as it wants; in some cases, though, the server may wish to + * forcefully close the connection and can do so by calling this method). + *

+ * Example of usage: the client failed an authentication check, cut the + * connection here and now. + */ + public void close() { + closing = true; + } + /** * The total amount of bytes received. * diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 53606ed..6d3d539 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -72,6 +72,9 @@ abstract public class ServerObject extends Server { Object rep = null; try { rep = onRequest(this, clientVersion, data); + if (isClosing()) { + return; + } } catch (Exception e) { onError(e); } diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index 3185c6f..89a7b28 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -71,6 +71,9 @@ abstract public class ServerString extends Server { String rep = null; try { rep = onRequest(this, clientVersion, data); + if (isClosing()) { + return; + } } catch (Exception e) { onError(e); }