Просмотр исходного кода

fix(mqttsn): ack the DISCONNECT packet even if it is not connected

MQTT-SN v1.2 spec:
"As with MQTT, the DISCONNECT message is sent by a client to indicate that it
 wants to close the connection. The gateway will acknowledge the receipt of that
 message by returning a DISCONNECT to the client."
JianBo He 2 лет назад
Родитель
Сommit
8339bccc69

+ 6 - 0
apps/emqx_gateway_mqttsn/src/emqx_mqttsn_channel.erl

@@ -443,6 +443,12 @@ handle_in(
 handle_in(?SN_ADVERTISE_MSG(_GwId, _Radius), Channel) ->
     % ignore
     shutdown(normal, Channel);
+%% Ack DISCONNECT even if it is not connected
+handle_in(
+    ?SN_DISCONNECT_MSG(_Duration),
+    Channel = #channel{conn_state = idle}
+) ->
+    handle_out(disconnect, normal, Channel);
 handle_in(
     Publish =
         ?SN_PUBLISH_MSG(

+ 12 - 0
apps/emqx_gateway_mqttsn/test/emqx_sn_protocol_SUITE.erl

@@ -176,6 +176,18 @@ t_connect(_) ->
     ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
 
     send_disconnect_msg(Socket, undefined),
+    %% assert: mqttsn gateway will ack disconnect msg with DISCONNECT packet
+    ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
+    gen_udp:close(Socket).
+
+t_first_disconnect(_) ->
+    SockName = {'mqttsn:udp:default', 1884},
+    ?assertEqual(true, lists:keymember(SockName, 1, esockd:listeners())),
+
+    {ok, Socket} = gen_udp:open(0, [binary]),
+    send_disconnect_msg(Socket, undefined),
+
+    %% assert: mqttsn gateway will ack disconnect msg with DISCONNECT packet
     ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
     gen_udp:close(Socket).