|
1 | 1 | %% @author Marc Worrell <[email protected]> |
2 | | -%% @copyright 2018-2024 Marc Worrell |
| 2 | +%% @copyright 2018-2025 Marc Worrell |
3 | 3 | %% @doc Process handling one single MQTT session. |
4 | 4 | %% MQTT connections attach and detach from this session. Buffers outgoing |
5 | 5 | %% messages if there is not connection attached. |
6 | 6 | %% @end |
7 | 7 |
|
8 | | -%% Copyright 2018-2024 Marc Worrell |
| 8 | +%% Copyright 2018-2025 Marc Worrell |
9 | 9 | %% |
10 | 10 | %% Licensed under the Apache License, Version 2.0 (the "License"); |
11 | 11 | %% you may not use this file except in compliance with the License. |
|
42 | 42 | update_user_context/2, |
43 | 43 |
|
44 | 44 | get_transport/1, |
| 45 | + is_connected/1, |
45 | 46 | kill/1, |
46 | 47 | incoming_connect/3, |
47 | 48 | incoming_data/2, |
@@ -168,6 +169,15 @@ get_transport(Pid) -> |
168 | 169 | {error, noproc} |
169 | 170 | end. |
170 | 171 |
|
| 172 | +-spec is_connected( pid() ) -> boolean(). |
| 173 | +is_connected(Pid) -> |
| 174 | + try |
| 175 | + gen_server:call(Pid, is_connected, infinity) |
| 176 | + catch |
| 177 | + exit:{noproc, _} -> |
| 178 | + false |
| 179 | + end. |
| 180 | + |
171 | 181 | -spec kill( pid() ) -> ok. |
172 | 182 | kill(Pid) when is_pid(Pid) -> |
173 | 183 | MRef = monitor(process, Pid), |
@@ -237,6 +247,9 @@ handle_call(get_transport, _From, #state{ transport = undefined } = State) -> |
237 | 247 | handle_call(get_transport, _From, #state{ transport = Transport } = State) -> |
238 | 248 | {reply, {ok, Transport}, State}; |
239 | 249 |
|
| 250 | +handle_call(is_connected, _From, #state{ is_connected = IsConnected } = State) -> |
| 251 | + {reply, IsConnected, State}; |
| 252 | + |
240 | 253 | handle_call({incoming_data, NewData, ConnectionPid}, _From, #state{ incoming_data = Data, connection_pid = ConnectionPid } = State) -> |
241 | 254 | Data1 = << Data/binary, NewData/binary >>, |
242 | 255 | case handle_incoming_data(Data1, State) of |
@@ -299,7 +312,7 @@ handle_info({publish_job, JobPid}, #state{ publish_jobs = Jobs } = State) when i |
299 | 312 | {noreply, State1}; |
300 | 313 |
|
301 | 314 | handle_info({'DOWN', _Mref, process, Pid, _Reason}, #state{ connection_pid = Pid } = State) -> |
302 | | - State1 = do_disconnected(State), |
| 315 | + State1 = cleanup_state_disconnected(State), |
303 | 316 | {noreply, State1}; |
304 | 317 | handle_info({'DOWN', _Mref, process, Pid, _Reason}, #state{ will_pid = Pid } = State) -> |
305 | 318 | send_transport(#{ |
@@ -1002,12 +1015,9 @@ mark_packet_sent(PacketId, #state{ awaiting_ack = AwaitAck } = State) -> |
1002 | 1015 |
|
1003 | 1016 |
|
1004 | 1017 | %% @doc Called when the connection disconnects or crashes/stops |
1005 | | -do_disconnected(#state{ will_pid = WillPid } = State) -> |
1006 | | - mqtt_sessions_will:disconnected(WillPid), |
1007 | | - cleanup_state_disconnected(State). |
1008 | | - |
1009 | 1018 | %% @todo Cleanup pending messages and awaiting states. |
1010 | | -cleanup_state_disconnected(State) -> |
| 1019 | +cleanup_state_disconnected(#state{ will_pid = WillPid } = State) -> |
| 1020 | + mqtt_sessions_will:disconnected(WillPid), |
1011 | 1021 | delete_buffered_qos0(State#state{ |
1012 | 1022 | connection_pid = undefined, |
1013 | 1023 | transport = undefined, |
@@ -1056,7 +1066,7 @@ extract_will(#{ type := connect, will_flag := true, properties := Props } = Msg) |
1056 | 1066 |
|
1057 | 1067 | force_disconnect(#state{ connection_pid = undefined, transport = undefined } = State) -> |
1058 | 1068 | State; |
1059 | | -force_disconnect(State) -> |
| 1069 | +force_disconnect(#state{ will_pid = WillPid } = State) -> |
1060 | 1070 | State1 = disconnect_transport(State), |
1061 | 1071 | if |
1062 | 1072 | is_pid(State#state.connection_pid) -> |
|
0 commit comments