[erlang-questions] TCP receive in gen_server

David N Murray dmurray@REDACTED
Wed May 12 14:18:08 CEST 2010


On May 8, Steve Vinoski scribed:

>
> Are you setting {active, once} on the socket? Or {active, true}?
> Either will work but the {active, once} setting is preferred since it
> will keep the writer from overrunning the reader.
>
> Pass {active, once} in your options list when you connect the socket.
> Then, within your handle_info clause that receives the tcp messages,
> call inet:setopts(Sock, [{active, once}]) before returning in order to
> set up the socket for the next message.
>

I modified my code to connect as:

    case gen_tcp:connect(Host, Port, [list, inet, {active, once},
        {exit_on_close, true}, {nodelay, true}, {packet, raw},
        {recbuf, 524288}]) of

and receive as:

handle_info({tcp, Socket, Data}, State) ->
    % process data via read_pkt()
    inet:setopts(Socket, [{active, once}]),
    {noreply, NewState};

I'm still sending with another process.

I'm still seeing the same problem.  No data from the socket is sent to
handle_info unless I first send something to the socket, and then, only
one packet/message from the server is delivered at a time, even though
there are at least 3 messages pending:

05/12:07:58:48: ib:connect: Connected
05/12:07:58:48: ibr:process_data.neg_time: ServerVersion=45
ServerTime=20100512 07:58:48 EST
05/12:07:58:48: ibr:read_pkt: Dispatch:
[{msg_id,next_valid_id},{version,1},{orderId,3230}]
05/12:07:58:49: ibr:read_pkt: Dispatch:
[{msg_id,open_order_end},{version,1}]

(the above messages arrive as a single packet in response to me sending a
client id as part of the connect/negotiate process.)

05/12:07:59:02: ibs:bld_send: [{pkt_id,49},{version,1}] %% I requested
current time
05/12:07:59:02: ibr:read_pkt: Dispatch: [{msg_id,err_msg},
                         {version,2},
                         {id,-1},
                         {errorCode,2104},
                         {errorMsg,"Market data farm connection is
OK:usfuture"}]

that message was sitting in buffer

05/12:07:59:14: ibs:bld_send: [{pkt_id,49},{version,1}] %% again
05/12:07:59:14: ibr:read_pkt: Dispatch: [{msg_id,err_msg},
                         {version,2},
                         {id,-1},
                         {errorCode,2104},
                         {errorMsg,"Market data farm connection is
OK:usfarm"}]

so was this one

05/12:07:59:22: ibs:bld_send: [{pkt_id,49},{version,1}] %% and again
05/12:07:59:22: ibr:read_pkt: Dispatch: [{msg_id,err_msg},
                         {version,2},
                         {id,-1},
                         {errorCode,2107},
                         {errorMsg,"HMDS data farm connection is inactive
but should be available upon demand.ushmds2a"}]

and this one

05/12:07:59:33: ibs:bld_send: [{pkt_id,49},{version,1}]
05/12:07:59:33: ibr:read_pkt: Dispatch:
[{msg_id,current_time},{version,1},{time,1273665541}]

now I finally get a reply to the first request made at 59:02.

Any thoughts would be appreciated.

TIA,
Dave



More information about the erlang-questions mailing list