-record(state, {sockmod, socket, receiver}).
+-define(HIBERNATE_TIMEOUT, 90000).
+
%%====================================================================
%% API
%%====================================================================
{ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts),
ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
Reply = ok,
- {reply, Reply, State#state{socket = TLSSocket, sockmod = tls}};
+ {reply, Reply, State#state{socket = TLSSocket, sockmod = tls},
+ ?HIBERNATE_TIMEOUT};
handle_call({starttls, TLSOpts, Data}, _From, State) ->
{ok, TLSSocket} = tls:tcp_to_tls(State#state.socket, TLSOpts),
catch (State#state.sockmod):send(
State#state.socket, Data),
Reply = ok,
- {reply, Reply, State#state{socket = TLSSocket, sockmod = tls}};
+ {reply, Reply, State#state{socket = TLSSocket, sockmod = tls},
+ ?HIBERNATE_TIMEOUT};
handle_call(compress, _From, State) ->
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
State#state.socket),
ejabberd_receiver:compress(State#state.receiver, ZlibSocket),
Reply = ok,
- {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib}};
+ {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib},
+ ?HIBERNATE_TIMEOUT};
handle_call({compress, Data}, _From, State) ->
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
catch (State#state.sockmod):send(
State#state.socket, Data),
Reply = ok,
- {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib}};
+ {reply, Reply, State#state{socket = ZlibSocket, sockmod = ejabberd_zlib},
+ ?HIBERNATE_TIMEOUT};
handle_call(reset_stream, _From, State) ->
ejabberd_receiver:reset_stream(State#state.receiver),
Reply = ok,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({send, Data}, _From, State) ->
catch (State#state.sockmod):send(
State#state.socket, Data),
Reply = ok,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({change_shaper, Shaper}, _From, State) ->
ejabberd_receiver:change_shaper(State#state.receiver, Shaper),
Reply = ok,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_sockmod, _From, State) ->
Reply = State#state.sockmod,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_peer_certificate, _From, State) ->
Reply = tls:get_peer_certificate(State#state.socket),
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_verify_result, _From, State) ->
Reply = tls:get_verify_result(State#state.socket),
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(close, _From, State) ->
ejabberd_receiver:close(State#state.receiver),
_ ->
SockMod:sockname(Socket)
end,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(peername, _From, State) ->
#state{sockmod = SockMod, socket = Socket} = State,
_ ->
SockMod:peername(Socket)
end,
- {reply, Reply, State};
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(_Request, _From, State) ->
Reply = ok,
- {reply, Reply, State}.
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
- {noreply, State}.
+ {noreply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
+handle_info(timeout, State) ->
+ proc_lib:hibernate(gen_server, enter_loop, [?MODULE, [], State]),
+ {noreply, State, ?HIBERNATE_TIMEOUT};
handle_info(_Info, State) ->
- {noreply, State}.
+ {noreply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
xml_stream_state,
timeout}).
+-define(HIBERNATE_TIMEOUT, 90000).
+
%%====================================================================
%% API
%%====================================================================
xml_stream_state = NewXMLStreamState},
case tls:recv_data(TLSSocket, "") of
{ok, TLSData} ->
- {reply, ok, process_data(TLSData, NewState)};
+ {reply, ok, process_data(TLSData, NewState), ?HIBERNATE_TIMEOUT};
{error, _Reason} ->
{stop, normal, ok, NewState}
end;
xml_stream_state = NewXMLStreamState},
case ejabberd_zlib:recv_data(ZlibSocket, "") of
{ok, ZlibData} ->
- {reply, ok, process_data(ZlibData, NewState)};
+ {reply, ok, process_data(ZlibData, NewState), ?HIBERNATE_TIMEOUT};
{error, _Reason} ->
{stop, normal, ok, NewState}
end;
xml_stream:close(XMLStreamState),
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
Reply = ok,
- {reply, Reply, State#state{xml_stream_state = NewXMLStreamState}};
+ {reply, Reply, State#state{xml_stream_state = NewXMLStreamState},
+ ?HIBERNATE_TIMEOUT};
handle_call({become_controller, C2SPid}, _From, State) ->
XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
NewState = State#state{c2s_pid = C2SPid,
xml_stream_state = XMLStreamState},
activate_socket(NewState),
Reply = ok,
- {reply, Reply, NewState};
+ {reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
handle_call(_Request, _From, State) ->
Reply = ok,
- {reply, Reply, State}.
+ {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%%--------------------------------------------------------------------
handle_cast({change_shaper, Shaper}, State) ->
NewShaperState = shaper:new(Shaper),
- {noreply, State#state{shaper_state = NewShaperState}};
+ {noreply, State#state{shaper_state = NewShaperState}, ?HIBERNATE_TIMEOUT};
handle_cast(close, State) ->
{stop, normal, State};
handle_cast(_Msg, State) ->
- {noreply, State}.
+ {noreply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
tls ->
case tls:recv_data(Socket, Data) of
{ok, TLSData} ->
- {noreply, process_data(TLSData, State)};
+ {noreply, process_data(TLSData, State),
+ ?HIBERNATE_TIMEOUT};
{error, _Reason} ->
{stop, normal, State}
end;
ejabberd_zlib ->
case ejabberd_zlib:recv_data(Socket, Data) of
{ok, ZlibData} ->
- {noreply, process_data(ZlibData, State)};
+ {noreply, process_data(ZlibData, State),
+ ?HIBERNATE_TIMEOUT};
{error, _Reason} ->
{stop, normal, State}
end;
_ ->
- {noreply, process_data(Data, State)}
+ {noreply, process_data(Data, State), ?HIBERNATE_TIMEOUT}
end;
handle_info({Tag, _TCPSocket}, State)
when (Tag == tcp_closed) or (Tag == ssl_closed) ->
when (Tag == tcp_error) or (Tag == ssl_error) ->
case Reason of
timeout ->
- {noreply, State};
+ {noreply, State, ?HIBERNATE_TIMEOUT};
_ ->
{stop, normal, State}
end;
handle_info({timeout, _Ref, activate}, State) ->
activate_socket(State),
- {noreply, State};
+ {noreply, State, ?HIBERNATE_TIMEOUT};
+handle_info(timeout, State) ->
+ proc_lib:hibernate(gen_server, enter_loop, [?MODULE, [], State]),
+ {noreply, State, ?HIBERNATE_TIMEOUT};
handle_info(_Info, State) ->
- {noreply, State}.
+ {noreply, State, ?HIBERNATE_TIMEOUT}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()