|
|
@@ -21,8 +21,7 @@
|
|
|
init/2,
|
|
|
info/1,
|
|
|
info/2,
|
|
|
- check/2,
|
|
|
- set/3
|
|
|
+ check/2
|
|
|
]).
|
|
|
|
|
|
-elvis([{elvis_style, no_if_expression, disable}]).
|
|
|
@@ -31,78 +30,57 @@
|
|
|
|
|
|
-record(keepalive, {
|
|
|
interval :: pos_integer(),
|
|
|
- statval :: non_neg_integer(),
|
|
|
- repeat :: non_neg_integer()
|
|
|
+ statval :: non_neg_integer()
|
|
|
}).
|
|
|
|
|
|
-opaque keepalive() :: #keepalive{}.
|
|
|
+-define(MAX_INTERVAL, 65535000).
|
|
|
|
|
|
%% @doc Init keepalive.
|
|
|
-spec init(Interval :: non_neg_integer()) -> keepalive().
|
|
|
init(Interval) -> init(0, Interval).
|
|
|
|
|
|
+%% from mqtt-v3.1.1 specific
|
|
|
+%% A Keep Alive value of zero (0) has the effect of turning off the keep alive mechanism.
|
|
|
+%% This means that, in this case, the Server is not required
|
|
|
+%% to disconnect the Client on the grounds of inactivity.
|
|
|
+%% Note that a Server is permitted to disconnect a Client that it determines
|
|
|
+%% to be inactive or non-responsive at any time,
|
|
|
+%% regardless of the Keep Alive value provided by that Client.
|
|
|
+%% Non normative comment
|
|
|
+%%The actual value of the Keep Alive is application specific;
|
|
|
+%% typically this is a few minutes.
|
|
|
+%% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds.
|
|
|
%% @doc Init keepalive.
|
|
|
-spec init(StatVal :: non_neg_integer(), Interval :: non_neg_integer()) -> keepalive().
|
|
|
-init(StatVal, Interval) when Interval > 0 ->
|
|
|
- #keepalive{
|
|
|
- interval = Interval,
|
|
|
- statval = StatVal,
|
|
|
- repeat = 0
|
|
|
- }.
|
|
|
+init(StatVal, Interval) when Interval > 0 andalso Interval =< ?MAX_INTERVAL ->
|
|
|
+ #keepalive{interval = Interval, statval = StatVal};
|
|
|
+init(_, 0) ->
|
|
|
+ undefined;
|
|
|
+init(StatVal, Interval) when Interval > ?MAX_INTERVAL -> init(StatVal, ?MAX_INTERVAL).
|
|
|
|
|
|
%% @doc Get Info of the keepalive.
|
|
|
-spec info(keepalive()) -> emqx_types:infos().
|
|
|
info(#keepalive{
|
|
|
interval = Interval,
|
|
|
- statval = StatVal,
|
|
|
- repeat = Repeat
|
|
|
+ statval = StatVal
|
|
|
}) ->
|
|
|
#{
|
|
|
interval => Interval,
|
|
|
- statval => StatVal,
|
|
|
- repeat => Repeat
|
|
|
+ statval => StatVal
|
|
|
}.
|
|
|
|
|
|
--spec info(interval | statval | repeat, keepalive()) ->
|
|
|
+-spec info(interval | statval, keepalive()) ->
|
|
|
non_neg_integer().
|
|
|
info(interval, #keepalive{interval = Interval}) ->
|
|
|
Interval;
|
|
|
info(statval, #keepalive{statval = StatVal}) ->
|
|
|
StatVal;
|
|
|
-info(repeat, #keepalive{repeat = Repeat}) ->
|
|
|
- Repeat.
|
|
|
+info(interval, undefined) ->
|
|
|
+ 0.
|
|
|
|
|
|
%% @doc Check keepalive.
|
|
|
-spec check(non_neg_integer(), keepalive()) ->
|
|
|
{ok, keepalive()} | {error, timeout}.
|
|
|
-check(
|
|
|
- NewVal,
|
|
|
- KeepAlive = #keepalive{
|
|
|
- statval = OldVal,
|
|
|
- repeat = Repeat
|
|
|
- }
|
|
|
-) ->
|
|
|
- if
|
|
|
- NewVal =/= OldVal ->
|
|
|
- {ok, KeepAlive#keepalive{statval = NewVal, repeat = 0}};
|
|
|
- Repeat < 1 ->
|
|
|
- {ok, KeepAlive#keepalive{repeat = Repeat + 1}};
|
|
|
- true ->
|
|
|
- {error, timeout}
|
|
|
- end.
|
|
|
-
|
|
|
-%% from mqtt-v3.1.1 specific
|
|
|
-%% A Keep Alive value of zero (0) has the effect of turning off the keep alive mechanism.
|
|
|
-%% This means that, in this case, the Server is not required
|
|
|
-%% to disconnect the Client on the grounds of inactivity.
|
|
|
-%% Note that a Server is permitted to disconnect a Client that it determines
|
|
|
-%% to be inactive or non-responsive at any time,
|
|
|
-%% regardless of the Keep Alive value provided by that Client.
|
|
|
-%% Non normative comment
|
|
|
-%%The actual value of the Keep Alive is application specific;
|
|
|
-%% typically this is a few minutes.
|
|
|
-%% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds.
|
|
|
-%% @doc Update keepalive's interval
|
|
|
--spec set(interval, non_neg_integer(), keepalive()) -> keepalive().
|
|
|
-set(interval, Interval, KeepAlive) when Interval >= 0 andalso Interval =< 65535000 ->
|
|
|
- KeepAlive#keepalive{interval = Interval}.
|
|
|
+check(Val, #keepalive{statval = Val}) -> {error, timeout};
|
|
|
+check(Val, KeepAlive) -> {ok, KeepAlive#keepalive{statval = Val}}.
|