Skip to content

Commit b80f131

Browse files
committed
Network.erl use handle_continue
Cleaner, DRY code - and avoid any potential races. Signed-off-by: Peter M <petermm@gmail.com>
1 parent d6f17ad commit b80f131

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

libs/eavmlib/src/network.erl

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
init/1,
3333
handle_call/3,
3434
handle_cast/2,
35+
handle_continue/2,
3536
handle_info/2,
3637
terminate/2
3738
]).
@@ -270,31 +271,11 @@ wait_for_ap(ApConfig, Timeout) ->
270271
%%-----------------------------------------------------------------------------
271272
-spec start(Config :: network_config()) -> {ok, pid()} | {error, Reason :: term()}.
272273
start(Config) ->
273-
case gen_server:start({local, ?MODULE}, ?MODULE, Config, []) of
274-
{ok, Pid} = R ->
275-
case gen_server:call(Pid, start) of
276-
ok ->
277-
R;
278-
Error ->
279-
Error
280-
end;
281-
Error ->
282-
Error
283-
end.
274+
gen_server:start({local, ?MODULE}, ?MODULE, Config, []).
284275

285276
-spec start_link(Config :: network_config()) -> {ok, pid()} | {error, Reason :: term()}.
286277
start_link(Config) ->
287-
case gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []) of
288-
{ok, Pid} = R ->
289-
case gen_server:call(Pid, start) of
290-
ok ->
291-
R;
292-
Error ->
293-
Error
294-
end;
295-
Error ->
296-
Error
297-
end.
278+
gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []).
298279

299280
%%-----------------------------------------------------------------------------
300281
%% @returns ok, if the network interface was stopped, or {error, Reason} if
@@ -329,28 +310,23 @@ sta_rssi() ->
329310

330311
%% @hidden
331312
init(Config) ->
332-
{ok, #state{config = Config}}.
333-
334-
%% @hidden
335-
handle_call(start, From, #state{config = Config} = State) ->
336313
Port = get_port(),
337314
Ref = make_ref(),
338-
Port ! {self(), Ref, {start, Config}},
339-
wait_start_reply(Ref, From, Port, State);
340-
handle_call(_Msg, _From, State) ->
341-
{reply, {error, unknown_message}, State}.
315+
{ok, #state{config = Config, port = Port, ref = Ref}, {continue, start_port}}.
342316

343-
%% @private
344-
wait_start_reply(Ref, From, Port, State) ->
317+
handle_continue(start_port, #state{config = Config, port = Port, ref = Ref} = State) ->
318+
Port ! {self(), Ref, {start, Config}},
345319
receive
346320
{Ref, ok} ->
347-
gen_server:reply(From, ok),
348-
{noreply, State#state{port = Port, ref = Ref}};
349-
{Ref, {error, Reason} = ER} ->
350-
gen_server:reply(From, {error, Reason}),
351-
{stop, {start_failed, Reason}, ER, State}
321+
{noreply, State};
322+
{Ref, {error, Reason}} ->
323+
{stop, {start_port_failed, Reason}, State}
352324
end.
353325

326+
%% @hidden
327+
handle_call(_Msg, _From, State) ->
328+
{reply, {error, unknown_message}, State}.
329+
354330
%% @hidden
355331
handle_cast(_Msg, State) ->
356332
{noreply, State}.

0 commit comments

Comments
 (0)