[erlang-questions] gen_tcp:recv and hairloss
Edwin Fine
erlang-questions_efine@REDACTED
Sun Nov 9 19:44:13 CET 2008
On Sat, Nov 8, 2008 at 11:23 AM, Kevin <q2h46uw02@REDACTED> wrote:
>
> using controlling_process sounds like a great solution, but I've tried
> that. In the cleaned up code below, I try to assign
> the socket to the Fsm, but I get the error {error,not_owner}.
I use controlling_process all the time with no problem. May I suggest an
alternative?
1. In the gen_server, open a socket and spawn_link a listener process
that waits in an gen_tcp:accept on the socket.
2. When it gets an {ok, Sock}, start the fsm, passing it the socket. If
necessary, put it in a waiting state until it is handed the ownership.
3. Give ownership of the socket to the fsm and (if necessary) tell it
that it's got the socket so it can start working.
4. Loop back to the accept.
Let the fsm do all the socket work, and the gen_server just kick off fsms
when listening.
Example (the ServerRef, unused here, is for calls the listener might want to
make to the gen_server, like storing the Pid of each fsm in its state, for
example):
listener(ServerRef, LSocket) ->
case gen_tcp:accept(LSocket) of
{ok, Socket} ->
case my_fsm:start(Socket) of
{ok, Pid} ->
case gen_tcp:controlling_process(Socket, Pid) of
ok ->
my_fsm:socket_ready(Pid),
listener(ServerRef, LSocket);
_Error ->
gen_tcp:close(Socket),
my_fsm:stop(Pid),
listener(ServerRef, LSocket)
end;
_Error ->
gen_tcp:close(Socket),
listener(ServerRef, LSocket)
end;
_Error ->
io:format("listener shutdown, accept error: ~p\n", [_Error])
end.
Hope this helps.
Regards,
Edwin Fine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081109/ca25a142/attachment.htm>
More information about the erlang-questions
mailing list