From a6bd1c90d5d199b0a9f69947240051638c214fdb Mon Sep 17 00:00:00 2001 From: Turtle Date: Wed, 23 Jun 2021 11:08:14 +0800 Subject: [PATCH] fix: Ignore repeatedly receiving connection packet in the wait_will_msg/wait_will_topic/connected state --- apps/emqx_sn/src/emqx_sn_gateway.erl | 36 +++++--------------- apps/emqx_sn/test/emqx_sn_protocol_SUITE.erl | 13 ------- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/apps/emqx_sn/src/emqx_sn_gateway.erl b/apps/emqx_sn/src/emqx_sn_gateway.erl index 9a8c68583..1bccf0c1a 100644 --- a/apps/emqx_sn/src/emqx_sn_gateway.erl +++ b/apps/emqx_sn/src/emqx_sn_gateway.erl @@ -250,8 +250,9 @@ wait_for_will_topic(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, _State) % ignore keep_state_and_data; -wait_for_will_topic(cast, {incoming, ?SN_CONNECT_MSG(Flags, _ProtoId, Duration, ClientId)}, State) -> - do_2nd_connect(Flags, Duration, ClientId, State); +wait_for_will_topic(cast, {incoming, ?SN_CONNECT_MSG(_Flags, _ProtoId, _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) -> {keep_state, handle_outgoing(Packet, State)}; @@ -275,9 +276,9 @@ wait_for_will_msg(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, _State) - % ignore 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) -> - do_2nd_connect(Flags, 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", []), + keep_state_and_data; wait_for_will_msg(cast, {outgoing, Packet}, State) -> {keep_state, handle_outgoing(Packet, State)}; @@ -365,8 +366,9 @@ connected(cast, {incoming, ?SN_ADVERTISE_MSG(_GwId, _Radius)}, State) -> % ignore {keep_state, State}; -connected(cast, {incoming, ?SN_CONNECT_MSG(Flags, _ProtoId, Duration, ClientId)}, State) -> - do_2nd_connect(Flags, Duration, ClientId, State); +connected(cast, {incoming, ?SN_CONNECT_MSG(_Flags, _ProtoId, _Duration, _ClientId)}, _State) -> + ?LOG(warning, "Receive connect packet in wait_for_will_topic state", []), + keep_state_and_data; connected(cast, {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) 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, State=#state{channel = Channel}) -> ClientId = emqx_channel:info(clientid, Channel), diff --git a/apps/emqx_sn/test/emqx_sn_protocol_SUITE.erl b/apps/emqx_sn/test/emqx_sn_protocol_SUITE.erl index a953a45e7..0947bdaca 100644 --- a/apps/emqx_sn/test/emqx_sn_protocol_SUITE.erl +++ b/apps/emqx_sn/test/emqx_sn_protocol_SUITE.erl @@ -98,19 +98,6 @@ t_connect(_) -> ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(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(_) -> Dup = 0, QoS = 0,