Przeglądaj źródła

chore(session): add obtain_next_pkt_id/1

In the current implement, mqtt-sn gateway is using the emqx_session as
its session structure. We need a method to get a pkt_id for sending like
REGISTER related messages
JianBo He 3 lat temu
rodzic
commit
5b1ba335cb

+ 5 - 0
apps/emqx/src/emqx_session.erl

@@ -60,6 +60,7 @@
         , info/2
         , is_session/1
         , stats/1
+        , obtain_next_pkt_id/1
         ]).
 
 -export([ subscribe/4
@@ -746,6 +747,10 @@ inc_pd(Key, Inc) ->
 %%--------------------------------------------------------------------
 %% Next Packet Id
 %%--------------------------------------------------------------------
+
+obtain_next_pkt_id(Session) ->
+    {Session#session.next_pkt_id, next_pkt_id(Session)}.
+
 next_pkt_id(Session = #session{next_pkt_id = ?MAX_PACKET_ID}) ->
     Session#session{next_pkt_id = 1};
 

+ 7 - 1
apps/emqx/test/emqx_session_SUITE.erl

@@ -378,7 +378,13 @@ t_next_pakt_id(_) ->
     Session2 = emqx_session:next_pkt_id(Session1),
     ?assertEqual(2, emqx_session:info(next_pkt_id, Session2)).
 
-%%--------------------------------------------------------------------
+t_obtain_next_pkt_id(_) ->
+    Session = session(#{next_pkt_id => 16#FFFF}),
+    {16#FFFF, Session1} = emqx_session:obtain_next_pkt_id(Session),
+    ?assertEqual(1, emqx_session:info(next_pkt_id, Session1)),
+    {1, Session2} = emqx_session:obtain_next_pkt_id(Session1),
+    ?assertEqual(2, emqx_session:info(next_pkt_id, Session2)).
+
 %% Helper functions
 %%--------------------------------------------------------------------