diff options
author | j.uhlig <[email protected]> | 2018-05-03 13:22:30 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-05-07 13:11:16 +0200 |
commit | 301f582b97f82e7f7dc2d41bb575671bcc30215e (patch) | |
tree | d9cff0b381bb7189eb4ebee27438927d82e2b2ff /examples/tcp_reverse/src | |
parent | 7006c50c3ed6c3cbcb24e9e88a76ebd1aaf3a5f8 (diff) | |
download | ranch-301f582b97f82e7f7dc2d41bb575671bcc30215e.tar.gz ranch-301f582b97f82e7f7dc2d41bb575671bcc30215e.tar.bz2 ranch-301f582b97f82e7f7dc2d41bb575671bcc30215e.zip |
Replace gen_server with gen_statem in examples
Diffstat (limited to 'examples/tcp_reverse/src')
-rw-r--r-- | examples/tcp_reverse/src/reverse_protocol.erl | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/examples/tcp_reverse/src/reverse_protocol.erl b/examples/tcp_reverse/src/reverse_protocol.erl index 80ed62c..a37ca46 100644 --- a/examples/tcp_reverse/src/reverse_protocol.erl +++ b/examples/tcp_reverse/src/reverse_protocol.erl @@ -1,19 +1,18 @@ %% Feel free to use, reuse and abuse the code in this file. -module(reverse_protocol). --behaviour(gen_server). +-behaviour(gen_statem). -behaviour(ranch_protocol). %% API. -export([start_link/4]). -%% gen_server. +%% gen_statem. +-export([callback_mode/0]). -export([init/1]). --export([handle_call/3]). --export([handle_cast/2]). --export([handle_info/2]). --export([terminate/2]). --export([code_change/3]). +-export([connected/3]). +-export([terminate/3]). +-export([code_change/4]). -define(TIMEOUT, 5000). @@ -24,45 +23,49 @@ start_link(Ref, Socket, Transport, Opts) -> {ok, proc_lib:spawn_link(?MODULE, init, [{Ref, Socket, Transport, Opts}])}. -%% gen_server. +%% gen_statem. -%% This function is never called. We only define it so that -%% we can use the -behaviour(gen_server) attribute. -%init([]) -> {ok, undefined}. +callback_mode() -> + state_functions. init({Ref, Socket, Transport, _Opts = []}) -> ok = ranch:accept_ack(Ref), - ok = Transport:setopts(Socket, [{active, once}]), - gen_server:enter_loop(?MODULE, [], + ok = Transport:setopts(Socket, [{active, once}, {packet, line}]), + gen_statem:enter_loop(?MODULE, [], connected, #state{socket=Socket, transport=Transport}, - ?TIMEOUT). + [?TIMEOUT]). -handle_info({tcp, Socket, Data}, State=#state{ +connected(info, {tcp, Socket, Data}, _StateData=#state{ socket=Socket, transport=Transport}) when byte_size(Data) > 1 -> Transport:setopts(Socket, [{active, once}]), Transport:send(Socket, reverse_binary(Data)), - {noreply, State, ?TIMEOUT}; -handle_info({tcp_closed, _Socket}, State) -> - {stop, normal, State}; -handle_info({tcp_error, _, Reason}, State) -> - {stop, Reason, State}; -handle_info(timeout, State) -> - {stop, normal, State}; -handle_info(_Info, State) -> - {stop, normal, State}. - -handle_call(_Request, _From, State) -> - {reply, ok, State}. - -handle_cast(_Msg, State) -> - {noreply, State}. - -terminate(_Reason, _State) -> + {keep_state_and_data, ?TIMEOUT}; +connected(info, {tcp_closed, _Socket}, _StateData) -> + {stop, normal}; +connected(info, {tcp_error, _, Reason}, _StateData) -> + {stop, Reason}; +connected({call, From}, _Request, _StateData) -> + gen_statem:reply(From, ok), + keep_state_and_data; +connected(cast, _Msg, _StateData) -> + keep_state_and_data; +connected(timeout, _Msg, _StateData) -> + {stop, normal}; +connected(_EventType, _Msg, _StateData) -> + {stop, normal}. + +terminate(Reason, StateName, StateData=#state{ + socket=Socket, transport=Transport}) + when Socket=/=undefined andalso Transport=/=undefined -> + catch Transport:close(Socket), + terminate(Reason, StateName, + StateData#state{socket=undefined, transport=undefined}); +terminate(_Reason, _StateName, _StateData) -> ok. -code_change(_OldVsn, State, _Extra) -> - {ok, State}. +code_change(_OldVsn, StateName, StateData, _Extra) -> + {ok, StateName, StateData}. %% Internal. |