|
|
@@ -74,7 +74,7 @@
|
|
|
sockstate :: emqx_types:sockstate(),
|
|
|
sockname :: {inet:ip_address(), inet:port()},
|
|
|
peername :: {inet:ip_address(), inet:port()},
|
|
|
- channel :: emqx_channel:channel(),
|
|
|
+ channel :: maybe(emqx_channel:channel()),
|
|
|
registry :: emqx_sn_registry:registry(),
|
|
|
clientid :: maybe(binary()),
|
|
|
username :: maybe(binary()),
|
|
|
@@ -105,6 +105,8 @@
|
|
|
|
|
|
-define(NO_PEERCERT, undefined).
|
|
|
|
|
|
+%% TODO: fix when https://github.com/emqx/emqx-sn/pull/170 is merged
|
|
|
+-dialyzer([{nowarn_function, [idle/3]}]).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% Exported APIs
|
|
|
@@ -136,7 +138,8 @@ init([{_, SockPid, Sock}, Peername, Options]) ->
|
|
|
EnableStats = proplists:get_value(enable_stats, Options, false),
|
|
|
case inet:sockname(Sock) of
|
|
|
{ok, Sockname} ->
|
|
|
- Channel = emqx_channel:init(#{sockname => Sockname,
|
|
|
+ Channel = emqx_channel:init(#{socktype => udp,
|
|
|
+ sockname => Sockname,
|
|
|
peername => Peername,
|
|
|
protocol => 'mqtt-sn',
|
|
|
peercert => ?NO_PEERCERT,
|
|
|
@@ -195,14 +198,18 @@ idle(cast, {incoming, ?SN_PUBLISH_MSG(_Flag, _TopicId, _MsgId, _Data)}, State =
|
|
|
idle(cast, {incoming, ?SN_PUBLISH_MSG(#mqtt_sn_flags{qos = ?QOS_NEG1,
|
|
|
topic_id_type = TopicIdType
|
|
|
}, TopicId, _MsgId, Data)},
|
|
|
- State = #state{clientid = ClientId, registry = Registry}) ->
|
|
|
+ State = #state{clientid = ClientId, registry = Registry}) ->
|
|
|
TopicName = case (TopicIdType =:= ?SN_SHORT_TOPIC) of
|
|
|
- false ->
|
|
|
- emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId);
|
|
|
+ false -> emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId);
|
|
|
true -> <<TopicId:16>>
|
|
|
end,
|
|
|
- Msg = emqx_message:make(?NEG_QOS_CLIENT_ID, ?QOS_0, TopicName, Data),
|
|
|
- (TopicName =/= undefined) andalso emqx_broker:publish(Msg),
|
|
|
+ case TopicName =/= undefined of
|
|
|
+ true ->
|
|
|
+ Msg = emqx_message:make(?NEG_QOS_CLIENT_ID, ?QOS_0, TopicName, Data),
|
|
|
+ emqx_broker:publish(Msg);
|
|
|
+ false ->
|
|
|
+ ok
|
|
|
+ end,
|
|
|
?LOG(debug, "Client id=~p receives a publish with QoS=-1 in idle mode!", [ClientId], State),
|
|
|
{keep_state_and_data, State#state.idle_timeout};
|
|
|
|
|
|
@@ -473,10 +480,10 @@ handle_event(info, {datagram, SockPid, Data}, StateName,
|
|
|
State = #state{sockpid = SockPid, channel = _Channel}) ->
|
|
|
?LOG(debug, "RECV ~p", [Data], State),
|
|
|
Oct = iolist_size(Data),
|
|
|
- emqx_pd:inc_counter(recv_oct, Oct),
|
|
|
+ inc_counter(recv_oct, Oct),
|
|
|
try emqx_sn_frame:parse(Data) of
|
|
|
{ok, Msg} ->
|
|
|
- emqx_pd:inc_counter(recv_cnt, 1),
|
|
|
+ inc_counter(recv_cnt, 1),
|
|
|
?LOG(info, "RECV ~s at state ~s",
|
|
|
[emqx_sn_frame:format(Msg), StateName], State),
|
|
|
{keep_state, State, next_event({incoming, Msg})}
|
|
|
@@ -562,10 +569,9 @@ terminate(Reason, _StateName, #state{clientid = ClientId,
|
|
|
channel = Channel,
|
|
|
registry = Registry}) ->
|
|
|
emqx_sn_registry:unregister_topic(Registry, ClientId),
|
|
|
- case {Channel, Reason} of
|
|
|
- {undefined, _} -> ok;
|
|
|
- {_, _} ->
|
|
|
- emqx_channel:terminate(Reason, Channel)
|
|
|
+ case Channel =:= undefined of
|
|
|
+ true -> ok;
|
|
|
+ false -> emqx_channel:terminate(Reason, Channel)
|
|
|
end.
|
|
|
|
|
|
code_change(_Vsn, StateName, State, _Extra) ->
|
|
|
@@ -597,8 +603,8 @@ handle_info(Info, State = #state{channel = Channel}) ->
|
|
|
handle_return(emqx_channel:handle_info(Info, Channel), State).
|
|
|
|
|
|
handle_ping(_PingReq, State) ->
|
|
|
- emqx_pd:inc_counter(recv_oct, 2),
|
|
|
- emqx_pd:inc_counter(recv_msg, 1),
|
|
|
+ inc_counter(recv_oct, 2),
|
|
|
+ inc_counter(recv_msg, 1),
|
|
|
ok = send_message(?SN_PINGRESP_MSG(), State),
|
|
|
{keep_state, State}.
|
|
|
|
|
|
@@ -947,7 +953,7 @@ do_publish_will(#state{will_msg = WillMsg, clientid = ClientId}) ->
|
|
|
qos = QoS, retain = Retain},
|
|
|
variable = #mqtt_packet_publish{topic_name = Topic, packet_id = 1000},
|
|
|
payload = Payload},
|
|
|
- emqx_broker:publish(emqx_packet:to_message(Publish, ClientId)),
|
|
|
+ _ = emqx_broker:publish(emqx_packet:to_message(Publish, ClientId)),
|
|
|
ok.
|
|
|
|
|
|
do_puback(TopicId, MsgId, ReturnCode, _StateName,
|
|
|
@@ -1075,18 +1081,24 @@ transform_fun() ->
|
|
|
fun(Packet, State) -> transform(Packet, FunMsgIdToTopicId, State) end.
|
|
|
|
|
|
inc_incoming_stats(Type) ->
|
|
|
- emqx_pd:inc_counter(recv_pkt, 1),
|
|
|
+ inc_counter(recv_pkt, 1),
|
|
|
case Type == ?PUBLISH of
|
|
|
true ->
|
|
|
- emqx_pd:inc_counter(recv_msg, 1),
|
|
|
- emqx_pd:inc_counter(incoming_pubs, 1);
|
|
|
+ inc_counter(recv_msg, 1),
|
|
|
+ inc_counter(incoming_pubs, 1);
|
|
|
false -> ok
|
|
|
end.
|
|
|
|
|
|
inc_outgoing_stats(Type) ->
|
|
|
- emqx_pd:inc_counter(send_pkt, 1),
|
|
|
- (Type == ?SN_PUBLISH)
|
|
|
- andalso emqx_pd:inc_counter(send_msg, 1).
|
|
|
+ inc_counter(send_pkt, 1),
|
|
|
+ case Type =:= ?SN_PUBLISH of
|
|
|
+ true -> inc_counter(send_msg, 1);
|
|
|
+ false -> ok
|
|
|
+ end.
|
|
|
|
|
|
next_event(Content) ->
|
|
|
{next_event, cast, Content}.
|
|
|
+
|
|
|
+inc_counter(Key, Inc) ->
|
|
|
+ _ = emqx_pd:inc_counter(Key, Inc),
|
|
|
+ ok.
|