Skip to content

Commit

Permalink
Bring back the active socket. Supply proper packet framing in ucp_com…
Browse files Browse the repository at this point in the history
…mon.
  • Loading branch information
aerosol committed Jan 29, 2012
1 parent 9d9eb6a commit 2bfb8bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/ucp_server.erl
Expand Up @@ -26,7 +26,9 @@
lsock, % listening socket lsock, % listening socket
sock, % socket sock, % socket
trn, % message number trn, % message number
status}). status,
buffer % TCP buffer
}).


%%%=================================================================== %%%===================================================================
%%% API %%% API
Expand Down Expand Up @@ -56,7 +58,7 @@ handle_call(Msg, From, State) ->


handle_cast(accept, State = #state{lsock = S}) -> handle_cast(accept, State = #state{lsock = S}) ->
{ok, Sock} = gen_tcp:accept(S), {ok, Sock} = gen_tcp:accept(S),
loop(Sock), ?SYS_INFO("Accepting: ~p", [Sock]),
{noreply, State#state{sock = Sock}}; {noreply, State#state{sock = Sock}};


handle_cast({send_message, _Msg}, State) -> handle_cast({send_message, _Msg}, State) ->
Expand All @@ -66,6 +68,17 @@ handle_cast({send_message, _Msg}, State) ->
handle_cast(stop, State) -> handle_cast(stop, State) ->
{stop, normal, State}. {stop, normal, State}.


handle_info({tcp, Socket, Data}, State = #state{buffer = B}) ->
{Messages, Incomplete} = ucp_framing:try_decode(Data, B),
[handle_data(Socket, M) || M <- Messages],
{noreply, State#state{
buffer = Incomplete
}};

handle_info({tcp_closed, Socket}, State) ->
?SYS_WARN("Socket ~p closed.", [Socket]),
ucp_simulator_sup:start_child(),
{noreply, State};


handle_info(Any, State) -> handle_info(Any, State) ->
?SYS_INFO("Unhandled message: ~p", [Any]), ?SYS_INFO("Unhandled message: ~p", [Any]),
Expand All @@ -81,15 +94,6 @@ code_change(_OldVsn, State, _Extra) ->
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================


loop(Sock) ->
case ucp_framing:recv_ucp_sock(Sock) of
{ok, Data} ->
handle_data(Sock, Data);
Other ->
Other
end,
loop(Sock).

handle_data(Socket, RawData) -> handle_data(Socket, RawData) ->
try try
case handle_message(RawData) of case handle_message(RawData) of
Expand Down Expand Up @@ -155,4 +159,6 @@ process_message({#ucp_header{o_r = "R"} , _Body}) ->
noreply. noreply.


send(S, Msg) -> send(S, Msg) ->
gen_tcp:send(S, Msg). gen_tcp:send(S, Msg),
inet:setopts(S, [{active, once}]).

2 changes: 1 addition & 1 deletion src/ucp_simulator_sup.erl
Expand Up @@ -7,7 +7,7 @@
-define(DEFAULT_PORT, 7777). -define(DEFAULT_PORT, 7777).
-define(TCP_OPTIONS, [binary, -define(TCP_OPTIONS, [binary,
{packet, 0}, {packet, 0},
{active, false}, {active, once},
{reuseaddr, true}]). {reuseaddr, true}]).


start_link() -> start_link() ->
Expand Down

0 comments on commit 2bfb8bc

Please sign in to comment.