Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Bring back the active socket. Supply proper packet framing in ucp_com…

…mon.
  • Loading branch information...
commit 2bfb8bcc1b7b32abd59adf5d44ac80b3dedae655 1 parent 9d9eb6a
Adam Rutkowski authored

Showing 2 changed files with 19 additions and 13 deletions. Show diff stats Hide diff stats

  1. +18 12 src/ucp_server.erl
  2. +1 1  src/ucp_simulator_sup.erl
30 src/ucp_server.erl
@@ -26,7 +26,9 @@
26 26 lsock, % listening socket
27 27 sock, % socket
28 28 trn, % message number
29   - status}).
  29 + status,
  30 + buffer % TCP buffer
  31 + }).
30 32
31 33 %%%===================================================================
32 34 %%% API
@@ -56,7 +58,7 @@ handle_call(Msg, From, State) ->
56 58
57 59 handle_cast(accept, State = #state{lsock = S}) ->
58 60 {ok, Sock} = gen_tcp:accept(S),
59   - loop(Sock),
  61 + ?SYS_INFO("Accepting: ~p", [Sock]),
60 62 {noreply, State#state{sock = Sock}};
61 63
62 64 handle_cast({send_message, _Msg}, State) ->
@@ -66,6 +68,17 @@ handle_cast({send_message, _Msg}, State) ->
66 68 handle_cast(stop, State) ->
67 69 {stop, normal, State}.
68 70
  71 +handle_info({tcp, Socket, Data}, State = #state{buffer = B}) ->
  72 + {Messages, Incomplete} = ucp_framing:try_decode(Data, B),
  73 + [handle_data(Socket, M) || M <- Messages],
  74 + {noreply, State#state{
  75 + buffer = Incomplete
  76 + }};
  77 +
  78 +handle_info({tcp_closed, Socket}, State) ->
  79 + ?SYS_WARN("Socket ~p closed.", [Socket]),
  80 + ucp_simulator_sup:start_child(),
  81 + {noreply, State};
69 82
70 83 handle_info(Any, State) ->
71 84 ?SYS_INFO("Unhandled message: ~p", [Any]),
@@ -81,15 +94,6 @@ code_change(_OldVsn, State, _Extra) ->
81 94 %%% Internal functions
82 95 %%%===================================================================
83 96
84   -loop(Sock) ->
85   - case ucp_framing:recv_ucp_sock(Sock) of
86   - {ok, Data} ->
87   - handle_data(Sock, Data);
88   - Other ->
89   - Other
90   - end,
91   - loop(Sock).
92   -
93 97 handle_data(Socket, RawData) ->
94 98 try
95 99 case handle_message(RawData) of
@@ -155,4 +159,6 @@ process_message({#ucp_header{o_r = "R"} , _Body}) ->
155 159 noreply.
156 160
157 161 send(S, Msg) ->
158   - gen_tcp:send(S, Msg).
  162 + gen_tcp:send(S, Msg),
  163 + inet:setopts(S, [{active, once}]).
  164 +
2  src/ucp_simulator_sup.erl
@@ -7,7 +7,7 @@
7 7 -define(DEFAULT_PORT, 7777).
8 8 -define(TCP_OPTIONS, [binary,
9 9 {packet, 0},
10   - {active, false},
  10 + {active, once},
11 11 {reuseaddr, true}]).
12 12
13 13 start_link() ->

0 comments on commit 2bfb8bc

Please sign in to comment.
Something went wrong with that request. Please try again.