fix: Ignore repeatedly receiving connection packet in the wait_will_msg/wait_will_topic/connected state
This commit is contained in:
parent
8da6d5cf16
commit
8c0c6974d9
|
@ -250,8 +250,9 @@ wait_for_will_topic(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, _State)
|
||||||
% ignore
|
% ignore
|
||||||
keep_state_and_data;
|
keep_state_and_data;
|
||||||
|
|
||||||
wait_for_will_topic(cast, {incoming, ?SN_CONNECT_MSG(Flags, _ProtoId, Duration, ClientId)}, State) ->
|
wait_for_will_topic(cast, {incoming, ?SN_CONNECT_MSG(_Flags, _ProtoId, _Duration, _ClientId)}, _State) ->
|
||||||
do_2nd_connect(Flags, Duration, ClientId, State);
|
?LOG(warning, "Receive connect packet in wait_for_will_topic state", []),
|
||||||
|
keep_state_and_data;
|
||||||
|
|
||||||
wait_for_will_topic(cast, {outgoing, Packet}, State) ->
|
wait_for_will_topic(cast, {outgoing, Packet}, State) ->
|
||||||
{keep_state, handle_outgoing(Packet, State)};
|
{keep_state, handle_outgoing(Packet, State)};
|
||||||
|
@ -275,9 +276,9 @@ wait_for_will_msg(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, _State) -
|
||||||
% ignore
|
% ignore
|
||||||
keep_state_and_data;
|
keep_state_and_data;
|
||||||
|
|
||||||
%% XXX: ?? Why we will handling the 2nd CONNECT packet ??
|
wait_for_will_msg(cast, {incoming, ?SN_CONNECT_MSG(_Flags, _ProtoId, _Duration, _ClientId)}, _State) ->
|
||||||
wait_for_will_msg(cast, {incoming, ?SN_CONNECT_MSG(Flags, _ProtoId, Duration, ClientId)}, State) ->
|
?LOG(warning, "Receive connect packet in wait_for_will_msg state", []),
|
||||||
do_2nd_connect(Flags, Duration, ClientId, State);
|
keep_state_and_data;
|
||||||
|
|
||||||
wait_for_will_msg(cast, {outgoing, Packet}, State) ->
|
wait_for_will_msg(cast, {outgoing, Packet}, State) ->
|
||||||
{keep_state, handle_outgoing(Packet, State)};
|
{keep_state, handle_outgoing(Packet, State)};
|
||||||
|
@ -365,8 +366,9 @@ connected(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, State) ->
|
||||||
% ignore
|
% ignore
|
||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
|
|
||||||
connected(cast, {incoming, ?SN_CONNECT_MSG(Flags, _ProtoId, Duration, ClientId)}, State) ->
|
connected(cast, {incoming, ?SN_CONNECT_MSG(_Flags, _ProtoId, _Duration, _ClientId)}, _State) ->
|
||||||
do_2nd_connect(Flags, Duration, ClientId, State);
|
?LOG(warning, "Receive connect packet in wait_for_will_topic state", []),
|
||||||
|
keep_state_and_data;
|
||||||
|
|
||||||
connected(cast, {outgoing, Packet}, State) ->
|
connected(cast, {outgoing, Packet}, State) ->
|
||||||
{keep_state, handle_outgoing(Packet, State)};
|
{keep_state, handle_outgoing(Packet, State)};
|
||||||
|
@ -845,26 +847,6 @@ do_connect(ClientId, CleanStart, WillFlag, Duration, State) ->
|
||||||
handle_incoming(?CONNECT_PACKET(ConnPkt), NState)
|
handle_incoming(?CONNECT_PACKET(ConnPkt), NState)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_2nd_connect(Flags, Duration, ClientId, State = #state{sockname = Sockname,
|
|
||||||
peername = Peername,
|
|
||||||
channel = Channel}) ->
|
|
||||||
emqx_logger:set_metadata_clientid(ClientId),
|
|
||||||
#mqtt_sn_flags{will = Will, clean_start = CleanStart} = Flags,
|
|
||||||
NChannel = case CleanStart of
|
|
||||||
true ->
|
|
||||||
emqx_channel:terminate(normal, Channel),
|
|
||||||
emqx_sn_registry:unregister_topic(ClientId),
|
|
||||||
emqx_channel:init(#{socktype => udp,
|
|
||||||
sockname => Sockname,
|
|
||||||
peername => Peername,
|
|
||||||
peercert => ?NO_PEERCERT,
|
|
||||||
conn_mod => ?MODULE
|
|
||||||
}, ?DEFAULT_CHAN_OPTIONS);
|
|
||||||
false -> Channel
|
|
||||||
end,
|
|
||||||
NState = State#state{channel = NChannel},
|
|
||||||
do_connect(ClientId, CleanStart, Will, Duration, NState).
|
|
||||||
|
|
||||||
handle_subscribe(?SN_NORMAL_TOPIC, TopicName, QoS, MsgId,
|
handle_subscribe(?SN_NORMAL_TOPIC, TopicName, QoS, MsgId,
|
||||||
State=#state{channel = Channel}) ->
|
State=#state{channel = Channel}) ->
|
||||||
ClientId = emqx_channel:info(clientid, Channel),
|
ClientId = emqx_channel:info(clientid, Channel),
|
||||||
|
|
|
@ -98,19 +98,6 @@ t_connect(_) ->
|
||||||
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
|
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
|
||||||
gen_udp:close(Socket).
|
gen_udp:close(Socket).
|
||||||
|
|
||||||
t_do_2nd_connect(_) ->
|
|
||||||
{ok, Socket} = gen_udp:open(0, [binary]),
|
|
||||||
ClientId = ?CLIENTID,
|
|
||||||
send_connect_msg(Socket, ClientId),
|
|
||||||
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
|
|
||||||
timer:sleep(100),
|
|
||||||
send_connect_msg(Socket, <<"client_id_other">>),
|
|
||||||
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
|
|
||||||
|
|
||||||
send_disconnect_msg(Socket, undefined),
|
|
||||||
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
|
|
||||||
gen_udp:close(Socket).
|
|
||||||
|
|
||||||
t_subscribe(_) ->
|
t_subscribe(_) ->
|
||||||
Dup = 0,
|
Dup = 0,
|
||||||
QoS = 0,
|
QoS = 0,
|
||||||
|
|
Loading…
Reference in New Issue