Improve ejabberd_c2s:close()

This commit is contained in:
Evgeniy Khramtsov 2017-04-15 08:30:41 +03:00
parent 5c23187d2c
commit 5774edfe79
8 changed files with 55 additions and 49 deletions

View File

@ -116,10 +116,10 @@ get_subscribed(Ref) ->
close(Ref) ->
xmpp_stream_in:close(Ref).
-spec close(pid(), boolean()) -> ok;
(state(), boolean()) -> state().
close(Ref, SendTrailer) ->
xmpp_stream_in:close(Ref, SendTrailer).
-spec close(pid(), atom()) -> ok;
(state(), atom()) -> state().
close(Ref, Reason) ->
xmpp_stream_in:close(Ref, Reason).
-spec stop(pid()) -> ok;
(state()) -> no_return().

View File

@ -42,7 +42,7 @@
-export([handle_unexpected_info/2, handle_unexpected_cast/2,
reject_unauthenticated_packet/2, process_closed/2]).
%% API
-export([stop/1, close/1, send/2, update_state/2, establish/1,
-export([stop/1, close/1, close/2, send/2, update_state/2, establish/1,
host_up/1, host_down/1]).
-include("ejabberd.hrl").
@ -71,6 +71,9 @@ start_link(SockData, Opts) ->
close(Ref) ->
xmpp_stream_in:close(Ref).
close(Ref, Reason) ->
xmpp_stream_in:close(Ref, Reason).
stop(Ref) ->
xmpp_stream_in:stop(Ref).

View File

@ -39,7 +39,7 @@
-export([process_auth_result/2, process_closed/2, handle_unexpected_info/2,
handle_unexpected_cast/2, process_downgraded/2]).
%% API
-export([start/3, start_link/3, connect/1, close/1, stop/1, send/2,
-export([start/3, start_link/3, connect/1, close/1, close/2, stop/1, send/2,
route/2, establish/1, update_state/2, host_up/1, host_down/1]).
-include("ejabberd.hrl").
@ -75,6 +75,11 @@ connect(Ref) ->
close(Ref) ->
xmpp_stream_out:close(Ref).
-spec close(pid(), atom()) -> ok;
(state(), atom()) -> state().
close(Ref, Reason) ->
xmpp_stream_out:close(Ref, Reason).
-spec stop(pid()) -> ok;
(state()) -> no_return().
stop(Ref) ->

View File

@ -27,7 +27,7 @@
-protocol({xep, 114, '1.6'}).
%% ejabberd_socket callbacks
-export([start/2, start_link/2, socket_type/0]).
-export([start/2, start_link/2, socket_type/0, close/1, close/2]).
%% ejabberd_config callbacks
-export([opt_type/1, transform_listen_option/2]).
%% xmpp_stream_in callbacks
@ -63,6 +63,16 @@ socket_type() ->
send(Stream, Pkt) ->
xmpp_stream_in:send(Stream, Pkt).
-spec close(pid()) -> ok;
(state()) -> state().
close(Ref) ->
xmpp_stream_in:close(Ref).
-spec close(pid(), atom()) -> ok;
(state(), atom()) -> state().
close(Ref, Reason) ->
xmpp_stream_in:close(Ref, Reason).
%%%===================================================================
%%% xmpp_stream_in callbacks
%%%===================================================================

View File

@ -146,7 +146,7 @@ handle_cast({iq_pong, JID, timeout}, State) ->
JID,
case ejabberd_sm:get_session_pid(User, Server, Resource)
of
Pid when is_pid(Pid) -> ejabberd_c2s:close(Pid, _SendTrailer = false);
Pid when is_pid(Pid) -> ejabberd_c2s:close(Pid, ping_timeout);
_ -> ok
end;
_ -> ok

View File

@ -233,9 +233,7 @@ c2s_handle_info(#{mgmt_ack_timer := TRef, jid := JID, mod := Mod} = State,
{timeout, TRef, ack_timeout}) ->
?DEBUG("Timed out waiting for stream management acknowledgement of ~s",
[jid:encode(JID)]),
State1 = State#{stop_reason => {socket, timeout}},
State2 = Mod:close(State1, _SendTrailer = false),
{stop, transition_to_pending(State2)};
{stop, Mod:close(State, ack_timeout)};
c2s_handle_info(#{mgmt_state := pending, jid := JID, mod := Mod} = State,
{timeout, _, pending_timeout}) ->
?DEBUG("Timed out waiting for resumption of stream for ~s",

View File

@ -46,7 +46,7 @@
-type state() :: map().
-type stop_reason() :: {stream, reset | {in | out, stream_error()}} |
{tls, inet:posix() | atom() | binary()} |
{socket, inet:posix() | closed | timeout} |
{socket, inet:posix() | atom()} |
internal_failure.
-export_type([state/0, stop_reason/0]).
-callback init(list()) -> {ok, state()} | {error, term()} | ignore.
@ -152,15 +152,18 @@ send(_, _) ->
-spec close(pid()) -> ok;
(state()) -> state().
close(Ref) ->
close(Ref, true).
close(Ref, closed).
-spec close(pid(), boolean()) -> ok;
(state(), boolean()) -> state().
close(Pid, SendTrailer) when is_pid(Pid) ->
cast(Pid, {close, SendTrailer});
close(#{owner := Owner} = State, SendTrailer) when Owner == self() ->
if SendTrailer -> send_trailer(State);
true -> close_socket(State)
-spec close(pid(), atom()) -> ok;
(state(), atom()) -> state().
close(Pid, Reason) when is_pid(Pid) ->
cast(Pid, {close, Reason});
close(#{owner := Owner} = State, Reason) when Owner == self() ->
case is_disconnected(State) of
true -> State;
false ->
_IgnoreState = close_socket(State),
process_stream_end({socket, Reason}, State)
end;
close(_, _) ->
erlang:error(badarg).
@ -271,16 +274,8 @@ handle_cast({send, Pkt}, State) ->
noreply(send_pkt(State, Pkt));
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast({close, SendTrailer}, #{mod := Mod} = State) ->
noreply(
case is_disconnected(State) of
true -> State;
false ->
State1 = close(State, SendTrailer),
try Mod:handle_stream_end({socket, closed}, State1)
catch _:undef -> stop(State1)
end
end);
handle_cast({close, Reason}, State) ->
noreply(close(State, Reason));
handle_cast(Cast, #{mod := Mod} = State) ->
noreply(try Mod:handle_cast(Cast, State)
catch _:undef -> State

View File

@ -57,7 +57,7 @@
{tls, inet:posix() | atom() | binary()} |
{pkix, binary()} |
{auth, atom() | binary() | string()} |
{socket, inet:posix() | closed | timeout} |
{socket, inet:posix() | atom()} |
internal_failure.
-export_type([state/0, stop_reason/0]).
-callback init(list()) -> {ok, state()} | {error, term()} | ignore.
@ -162,15 +162,18 @@ send(_, _) ->
-spec close(pid()) -> ok;
(state()) -> state().
close(Ref) ->
close(Ref, true).
close(Ref, closed).
-spec close(pid(), boolean()) -> ok;
(state(), boolean()) -> state().
close(Pid, SendTrailer) when is_pid(Pid) ->
cast(Pid, {close, SendTrailer});
close(#{owner := Owner} = State, SendTrailer) when Owner == self() ->
if SendTrailer -> send_trailer(State);
true -> close_socket(State)
-spec close(pid(), atom()) -> ok;
(state(), atom()) -> state().
close(Pid, Reason) when is_pid(Pid) ->
cast(Pid, {close, Reason});
close(#{owner := Owner} = State, Reason) when Owner == self() ->
case is_disconnected(State) of
true -> State;
false ->
_IgnoreState = close_socket(State),
process_stream_end({socket, Reason}, State)
end;
close(_, _) ->
erlang:error(badarg).
@ -302,16 +305,8 @@ handle_cast({send, Pkt}, State) ->
noreply(send_pkt(State, Pkt));
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast({close, SendTrailer}, #{mod := Mod} = State) ->
noreply(
case is_disconnected(State) of
true -> State;
false ->
State1 = close(State, SendTrailer),
try Mod:handle_stream_end({socket, closed}, State1)
catch _:undef -> stop(State1)
end
end);
handle_cast({close, Reason}, State) ->
noreply(close(State, Reason));
handle_cast(Cast, #{mod := Mod} = State) ->
noreply(try Mod:handle_cast(Cast, State)
catch _:undef -> State