Sha256: 2c79f37517fddc743f7b63611e40b976242deeed54cfbd482b3203390b63c4ae

Contents?: true

Size: 1.58 KB

Versions: 49

Compression:

Stored size: 1.58 KB

Contents

-module (pm_client).
-behavior(gen_server).

-export([init/1, code_change/3, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-export([accept_loop/1]).
-export([start/3]).

-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).

-record(server_state, {
        port,
        loop,
        ip=any,
        lsocket=null}).

start(Name, Port, Loop) ->
    State = #server_state{port = Port, loop = Loop},
    gen_server:start_link({global, Name}, ?MODULE, State, []).

init(State = #server_state{port=Port}) ->
		process_flag(trap_exit, true),
    case gen_tcp:listen(Port, ?TCP_OPTIONS) of
        {ok, LSocket} ->
            NewState = State#server_state{lsocket = LSocket},
            {ok, accept(NewState)};
        {error, Reason} ->
            {stop, Reason}
    end.

handle_cast({accepted, _Pid}, State=#server_state{}) ->
    {noreply, accept(State)}.

accept_loop({Server, LSocket, {M, F}}) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    % Let the server spawn a new process and replace this loop
    % with the echo loop, to avoid blocking 
    gen_server:cast(Server, {accepted, self()}),
    M:F(Socket).
    
% To be more robust we should be using spawn_link and trapping exits
accept(State = #server_state{lsocket=LSocket, loop = Loop}) ->
    proc_lib:spawn(?MODULE, accept_loop, [{self(), LSocket, Loop}]),
    State.

% These are just here to suppress warnings.
handle_call(_Msg, _Caller, State) -> {noreply, State}.
handle_info(_Msg, Library) -> {noreply, Library}.
terminate(_Reason, _Library) -> ok.
code_change(_OldVersion, Library, _Extra) -> {ok, Library}.

Version data entries

49 entries across 49 versions & 3 rubygems

Version Path
auser-poolparty-0.2.49 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.50 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.51 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.52 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.53 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.54 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.55 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.56 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.57 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.58 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.59 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.60 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.61 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.62 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.63 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.64 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.65 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.66 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.67 lib/erlang/messenger/src/pm_client.erl
auser-poolparty-0.2.68 lib/erlang/messenger/src/pm_client.erl