managing the tcp/ip data flow and internal messages

View: New views
2 Messages — Rating Filter:   Alert me  

managing the tcp/ip data flow and internal messages

by info-1679 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm using gen_server to manage several socket connections.
For each connection a process is running, listen a client, reading with gen_tcp:recv

loop(S)->
    case gen_tcp:recv(S,0)->
    {ok,Data} ->
        %% handle data
        loop(S);
    {error,closed} ->
        exit( normal)
    end.

Now I would like to complicate the situation ...
I would like, from another gen_server, to send data to a specific client !

My first problem is to find the process "attached" to a client. Probably I must associate an ident (the client) to each process.
My second problem is to not disturb the tcp/ip connection. I suppose I must use {active,once} and the instruction "receive".

loop(S)->
    inet:setopts(S,[{active,once}]),
    receive
    {tcp,S,Data} ->
        %%handle data
    {tcp_close,S}->
        %%handle close
    {tcp_error,S,Reason}->
        %%handle reason
    {message,Data}->
        gen_tcp:send(S,Data),
        ...
    end.

Is my approach coherent ? robust ? yes/no why ?

John

Re: managing the tcp/ip data flow and internal messages

by kamiseq :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ok,
first of all, you can have only one owner process of the socket, so you need
to have one process listening/sending both for socket messages and for
erlang messages. so gen_tcp:receive (as it is blocking) is not enough, and
yes you need to use receive and {active, once}. but now you will face
problems such that you can have many messages from inside of the system that
will result in delayed response to socket clients. and what if your server
get killed then you loose all messages in queue.

you should keep message queue as short as possible.
hope that may help
--

pozdrawiam
Paweł Kamiński

kamiseq@...
pkaminski.prv@...
______________________