fix(mqtt_sn): reformat some code

This commit is contained in:
Shawn 2021-04-08 20:38:28 +08:00
parent 6a07c8ef6d
commit a925395814
2 changed files with 20 additions and 16 deletions

View File

@ -269,10 +269,10 @@ message_type(Type) ->
io_lib:format("Unknown Type ~p", [Type]).
format(?SN_PUBLISH_MSG(Flags, TopicId, MsgId, Data)) ->
io_lib:format("mqtt_sn_message SN_PUBLISH, ~p, TopicId=~w, MsgId=~w, Payload=~w",
io_lib:format("mqtt_sn_message SN_PUBLISH, ~s, TopicId=~w, MsgId=~w, Payload=~w",
[format_flag(Flags), TopicId, MsgId, Data]);
format(?SN_PUBACK_MSG(Flags, MsgId, ReturnCode)) ->
io_lib:format("mqtt_sn_message SN_PUBACK, ~p, MsgId=~w, ReturnCode=~w",
io_lib:format("mqtt_sn_message SN_PUBACK, ~s, MsgId=~w, ReturnCode=~w",
[format_flag(Flags), MsgId, ReturnCode]);
format(?SN_PUBREC_MSG(?SN_PUBCOMP, MsgId)) ->
io_lib:format("mqtt_sn_message SN_PUBCOMP, MsgId=~w", [MsgId]);
@ -281,13 +281,13 @@ format(?SN_PUBREC_MSG(?SN_PUBREC, MsgId)) ->
format(?SN_PUBREC_MSG(?SN_PUBREL, MsgId)) ->
io_lib:format("mqtt_sn_message SN_PUBREL, MsgId=~w", [MsgId]);
format(?SN_SUBSCRIBE_MSG(Flags, Msgid, Topic)) ->
io_lib:format("mqtt_sn_message SN_SUBSCRIBE, ~p, MsgId=~w, TopicId=~w",
io_lib:format("mqtt_sn_message SN_SUBSCRIBE, ~s, MsgId=~w, TopicId=~w",
[format_flag(Flags), Msgid, Topic]);
format(?SN_SUBACK_MSG(Flags, TopicId, MsgId, ReturnCode)) ->
io_lib:format("mqtt_sn_message SN_SUBACK, ~p, MsgId=~w, TopicId=~w, ReturnCode=~w",
io_lib:format("mqtt_sn_message SN_SUBACK, ~s, MsgId=~w, TopicId=~w, ReturnCode=~w",
[format_flag(Flags), MsgId, TopicId, ReturnCode]);
format(?SN_UNSUBSCRIBE_MSG(Flags, Msgid, Topic)) ->
io_lib:format("mqtt_sn_message SN_UNSUBSCRIBE, ~p, MsgId=~w, TopicId=~w",
io_lib:format("mqtt_sn_message SN_UNSUBSCRIBE, ~s, MsgId=~w, TopicId=~w",
[format_flag(Flags), Msgid, Topic]);
format(?SN_UNSUBACK_MSG(MsgId)) ->
io_lib:format("mqtt_sn_message SN_UNSUBACK, MsgId=~w", [MsgId]);

View File

@ -219,7 +219,7 @@ idle(cast, {connack, ConnAck}, State) ->
{next_state, connected, State};
idle(timeout, _Timeout, State) ->
stop({shutdown, idle_timeout}, State);
stop(idle_timeout, State);
idle(EventType, EventContent, State) ->
handle_event(EventType, EventContent, idle, State).
@ -374,10 +374,10 @@ connected(cast, {connack, ConnAck}, State) ->
connected(cast, {shutdown, Reason, Packet}, State) ->
ok = handle_outgoing(Packet, State),
{stop, {shutdown, Reason}, State};
stop(Reason, State);
connected(cast, {shutdown, Reason}, State) ->
{stop, {shutdown, Reason}, State};
stop(Reason, State);
connected(cast, {close, Reason}, State) ->
?LOG(debug, "Force to close the socket due to ~p", [Reason], State),
@ -614,11 +614,11 @@ handle_return({ok, NChannel}, State, AddEvents) ->
handle_return({ok, Replies, NChannel}, State, AddEvents) ->
{keep_state, State#state{channel = NChannel}, outgoing_events(append(Replies, AddEvents))};
handle_return({shutdown, Reason, NChannel}, State, _AddEvents) ->
stop({shutdown, Reason}, State#state{channel = NChannel});
stop(Reason, State#state{channel = NChannel});
handle_return({shutdown, Reason, OutPacket, NChannel}, State, _AddEvents) ->
NState = State#state{channel = NChannel},
ok = handle_outgoing(OutPacket, NState),
stop({shutdown, Reason}, NState).
stop(Reason, NState).
outgoing_events(Actions) ->
lists:map(fun outgoing_event/1, Actions).
@ -772,22 +772,26 @@ goto_asleep_state(Duration, State=#state{asleep_timer = AsleepTimer}) ->
-compile({inline, [shutdown/2, shutdown/3]}).
shutdown(Reason, State) ->
?LOG(error, "shutdown due to ~p", [Reason], State),
stop({shutdown, Reason}, State).
stop(Reason, State).
shutdown(Reason, Reply, State) ->
?LOG(error, "shutdown due to ~p", [Reason], State),
stop({shutdown, Reason}, Reply, State).
stop(Reason, Reply, State).
-compile({inline, [stop/2, stop/3]}).
stop({shutdown, Reason}, State) ->
stop(Reason, State);
stop(Reason, State) ->
case Reason of
%% FIXME: The Will-Msg should publish when a Session terminated!
asleep_timeout -> do_publish_will(State);
{shutdown, keepalive_timeout} -> do_publish_will(State);
_ -> ok
asleep_timeout -> do_publish_will(State);
keepalive_timeout -> do_publish_will(State);
_ -> ok
end,
{stop, Reason, State}.
{stop, {shutdown, Reason}, State}.
stop({shutdown, Reason}, Reply, State) ->
stop(Reason, Reply, State);
stop(Reason, Reply, State) ->
{stop, Reason, Reply, State}.