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

fix(emqx_gateway): fix the function_clause error when client disconnect after connection created

lafirest 4 лет назад
Родитель
Сommit
cfe2954a88

+ 7 - 5
apps/emqx_gateway/src/coap/emqx_coap_channel.erl

@@ -98,10 +98,10 @@ info(ctx, #channel{ctx = Ctx}) ->
 stats(_) ->
     [].
 
-init(ConnInfo = #{peername := {PeerHost, _},
-                  sockname := {_, SockPort}},
+init(ConnInfoT = #{peername := {PeerHost, _},
+                   sockname := {_, SockPort}},
      #{ctx := Ctx} = Config) ->
-    Peercert = maps:get(peercert, ConnInfo, undefined),
+    Peercert = maps:get(peercert, ConnInfoT, undefined),
     Mountpoint = maps:get(mountpoint, Config, <<>>),
     ListenerId = case maps:get(listener, Config, undefined) of
                      undefined -> undefined;
@@ -123,6 +123,10 @@ init(ConnInfo = #{peername := {PeerHost, _},
                     }
                   ),
 
+    %% because it is possible to disconnect after init, and then trigger the $event.disconnected hook
+    %% and these two fields are required in the hook
+    ConnInfo = ConnInfoT#{proto_name => <<"CoAP">>, proto_ver => <<"1">>},
+
     Heartbeat = ?GET_IDLE_TIME(Config),
     #channel{ ctx = Ctx
             , conninfo = ConnInfo
@@ -349,8 +353,6 @@ ensure_connected(Channel = #channel{ctx = Ctx,
                                     conninfo = ConnInfo,
                                     clientinfo = ClientInfo}) ->
     NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
-                         , proto_name => <<"COAP">>
-                         , proto_ver => <<"1">>
                          },
     ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
     _ = run_hooks(Ctx, 'client.connack', [NConnInfo, connection_accepted, []]),

+ 7 - 6
apps/emqx_gateway/src/lwm2m/emqx_lwm2m_channel.erl

@@ -93,10 +93,10 @@ info(ctx, #channel{ctx = Ctx}) ->
 stats(_) ->
     [].
 
-init(ConnInfo = #{peername := {PeerHost, _},
-                  sockname := {_, SockPort}},
+init(ConnInfoT = #{peername := {PeerHost, _},
+                   sockname := {_, SockPort}},
      #{ctx := Ctx} = Config) ->
-    Peercert = maps:get(peercert, ConnInfo, undefined),
+    Peercert = maps:get(peercert, ConnInfoT, undefined),
     Mountpoint = maps:get(mountpoint, Config, undefined),
     ListenerId = case maps:get(listener, Config, undefined) of
                      undefined -> undefined;
@@ -118,18 +118,20 @@ init(ConnInfo = #{peername := {PeerHost, _},
                     }
                   ),
 
+    ConnInfo = ConnInfoT#{proto_name => <<"LwM2M">>, proto_ver => <<"0.0">>},
+
     #channel{ ctx = Ctx
             , conninfo = ConnInfo
             , clientinfo = ClientInfo
             , timers = #{}
             , session = emqx_lwm2m_session:new()
-            %% FIXME: don't store anonymouse func
+              %% FIXME: don't store anonymouse func
             , with_context = with_context(Ctx, ClientInfo)
             }.
 
 with_context(Ctx, ClientInfo) ->
     fun(Type, Topic) ->
-        with_context(Type, Topic, Ctx, ClientInfo)
+            with_context(Type, Topic, Ctx, ClientInfo)
     end.
 
 lookup_cmd(Channel, Path, Action) ->
@@ -293,7 +295,6 @@ check_lwm2m_version(#coap_message{options = Opts},
               end,
     if IsValid ->
             NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
-                                 , proto_name => <<"LwM2M">>
                                  , proto_ver => Ver
                                  },
             {ok, Channel#channel{conninfo = NConnInfo}};