Skip to content

Commit

Permalink
add handle_info to optional callback functions
Browse files Browse the repository at this point in the history
* Module:handle_info(Info, CBState) ->
            {noreply, NewCbState}             |
            {noreply, NewCbState, Timeout}    |
            {close, Reason, NewCbState}
  • Loading branch information
pvieytes authored and Christopher Faulet committed Dec 11, 2012
1 parent e755418 commit 167fdb8
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/yaws_websockets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
terminate_fun,
open_fun,
msg_fun_1,
msg_fun_2}).
msg_fun_2,
info_fun}).

-export([start/3, send/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
Expand Down Expand Up @@ -277,9 +278,26 @@ handle_info(timeout, #state{wsstate=WSState}=State) ->
{stop, normal, State#state{reason={error,Reason}}}
end;

%% Skip all other messages
handle_info(_Info, State) ->
{noreply, State, State#state.timeout}.
%% handle info messages
handle_info(Info, State) ->
case (CbInfo=State#state.cbinfo)#cbinfo.info_fun of
undefined ->
{noreply, State, State#state.timeout};
InfoFun ->
CbMod = CbInfo#cbinfo.module,
{FrameState, CbState} = State#state.cbstate,
Res = CbMod:InfoFun(Info, CbState),
case Res of
{noreply, NewCbState} ->
{noreply, State#state{cbstate={FrameState,NewCbState}}};
{noreply, NewCbState, Timeout} ->
{noreply, State#state{cbstate={FrameState,NewCbState}}, Timeout};
{close, Reason, NewCbState} ->
{stop, normal, State#state{reason=Reason,
cbstate={FrameState,NewCbState}}}
end
end.



%% ----
Expand Down Expand Up @@ -334,12 +352,17 @@ get_callback_info(Mod) ->
true -> handle_message;
false -> undefined
end,
InfoFun = case erlang:function_exported(Mod, handle_info, 2) of
true -> handle_info;
false -> undefined
end,
{ok, #cbinfo{module = Mod,
init_fun = InitFun,
terminate_fun = TerminateFun,
open_fun = OpenFun,
msg_fun_1 = MsgFun1,
msg_fun_2 = MsgFun2}};
msg_fun_2 = MsgFun2,
info_fun = InfoFun}};
{error, Reason} ->
error_logger:error_msg("Cannot load callback module '~p': ~p",
[Mod, Reason]),
Expand Down

0 comments on commit 167fdb8

Please sign in to comment.