25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Use xml_stream:reset() when possible

This commit is contained in:
Paweł Chmielowski 2015-11-09 16:05:02 +01:00
parent de7422b372
commit 58c1bc4276

View File

@ -127,20 +127,10 @@ init([Socket, SockMod, Shaper, MaxStanzaSize]) ->
shaper_state = ShaperState, shaper_state = ShaperState,
max_stanza_size = MaxStanzaSize, timeout = Timeout}}. max_stanza_size = MaxStanzaSize, timeout = Timeout}}.
handle_call({starttls, TLSSocket}, _From, handle_call({starttls, TLSSocket}, _From, State) ->
#state{xml_stream_state = XMLStreamState, State1 = reset_parser(State),
c2s_pid = C2SPid, NewState = State1#state{socket = TLSSocket,
max_stanza_size = MaxStanzaSize} = State) -> sock_mod = p1_tls},
close_stream(XMLStreamState),
NewXMLStreamState = case C2SPid of
undefined ->
XMLStreamState;
_ ->
xml_stream:new(C2SPid, MaxStanzaSize)
end,
NewState = State#state{socket = TLSSocket,
sock_mod = p1_tls,
xml_stream_state = NewXMLStreamState},
case p1_tls:recv_data(TLSSocket, <<"">>) of case p1_tls:recv_data(TLSSocket, <<"">>) of
{ok, TLSData} -> {ok, TLSData} ->
{reply, ok, {reply, ok,
@ -149,20 +139,16 @@ handle_call({starttls, TLSSocket}, _From,
{stop, normal, ok, NewState} {stop, normal, ok, NewState}
end; end;
handle_call({compress, Data}, _From, handle_call({compress, Data}, _From,
#state{xml_stream_state = XMLStreamState, #state{socket = Socket, sock_mod = SockMod} =
c2s_pid = C2SPid, socket = Socket, sock_mod = SockMod,
max_stanza_size = MaxStanzaSize} =
State) -> State) ->
{ok, ZlibSocket} = ezlib:enable_zlib(SockMod, {ok, ZlibSocket} = ezlib:enable_zlib(SockMod,
Socket), Socket),
if Data /= undefined -> do_send(State, Data); if Data /= undefined -> do_send(State, Data);
true -> ok true -> ok
end, end,
close_stream(XMLStreamState), State1 = reset_parser(State),
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize), NewState = State1#state{socket = ZlibSocket,
NewState = State#state{socket = ZlibSocket, sock_mod = ezlib},
sock_mod = ezlib,
xml_stream_state = NewXMLStreamState},
case ezlib:recv_data(ZlibSocket, <<"">>) of case ezlib:recv_data(ZlibSocket, <<"">>) of
{ok, ZlibData} -> {ok, ZlibData} ->
{reply, {ok, ZlibSocket}, {reply, {ok, ZlibSocket},
@ -170,16 +156,10 @@ handle_call({compress, Data}, _From,
{error, _Reason} -> {error, _Reason} ->
{stop, normal, ok, NewState} {stop, normal, ok, NewState}
end; end;
handle_call(reset_stream, _From, handle_call(reset_stream, _From, State) ->
#state{xml_stream_state = XMLStreamState, NewState = reset_parser(State),
c2s_pid = C2SPid, max_stanza_size = MaxStanzaSize} =
State) ->
close_stream(XMLStreamState),
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
Reply = ok, Reply = ok,
{reply, Reply, {reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
State#state{xml_stream_state = NewXMLStreamState},
?HIBERNATE_TIMEOUT};
handle_call({become_controller, C2SPid}, _From, State) -> handle_call({become_controller, C2SPid}, _From, State) ->
XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size), XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
NewState = State#state{c2s_pid = C2SPid, NewState = State#state{c2s_pid = C2SPid,
@ -332,6 +312,24 @@ close_stream(undefined) -> ok;
close_stream(XMLStreamState) -> close_stream(XMLStreamState) ->
xml_stream:close(XMLStreamState). xml_stream:close(XMLStreamState).
reset_parser(#state{xml_stream_state = undefined} = State) ->
State;
reset_parser(#state{c2s_pid = C2SPid,
max_stanza_size = MaxStanzaSize,
xml_stream_state = XMLStreamState}
= State) ->
NewStreamState = try xml_stream:reset(XMLStreamState)
catch error:_ ->
close_stream(XMLStreamState),
case C2SPid of
undefined ->
undefined;
_ ->
xml_stream:new(C2SPid, MaxStanzaSize)
end
end,
State#state{xml_stream_state = NewStreamState}.
do_send(State, Data) -> do_send(State, Data) ->
(State#state.sock_mod):send(State#state.socket, Data). (State#state.sock_mod):send(State#state.socket, Data).