Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
detecting closed connections immediately?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven G  
View profile  
 More options Jul 16 2008, 1:57 pm
From: Steven G <gr...@speakeasy.net>
Date: Wed, 16 Jul 2008 10:57:03 -0700 (PDT)
Local: Wed, Jul 16 2008 1:57 pm
Subject: detecting closed connections immediately?
I'm working on an app that uses comet-style long-polls.  So far
Mochiweb has worked out well (after a week with it), but I'd really
like to be able to detect when the client closes the GET request.
(The reason is that I can use it to tell if someone is "logged in",
and I'd like to get the "log out" due to them closing a window ASAP,
i.e. without waiting for a time-out).

From my perusal of the Mochiweb code (and I'm an Erlang newbie here,
forgive me), it appears that there is no mechanism for this.

If I understand gen_tcp right, a socket connection can be either
active or passive.  If active, then a closed socket will result in a
message being sent to the controlling process.  If passive, then the
error will be returned on a recv.

My feature appears to require the socket connection be in active mode,
and to get the closed message passed up to the application.  It looks
like by default the listen places the socket in passive mode.  And my
Erlang/Mochiweb mojo is not sufficient yet to tell if it is possible
to adapt it without hacking on the mochiweb src.

Has anyone done this?  Any hints on how to proceed?

Thanks,
  Steven

PS I will be at BayFP tomorrow if anyone wants to chat about Mochiweb/
Erlang experiences.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Grady  
View profile  
 More options Aug 5 2008, 6:57 pm
From: Steven Grady <gr...@speakeasy.net>
Date: Tue, 5 Aug 2008 15:57:12 -0700
Local: Tues, Aug 5 2008 6:57 pm
Subject: Re: [mochiweb] detecting closed connections immediately?
I figured out how to do this.  It's pretty simple; the only trick is  
that active mode http is undocumented, but there are only 5 messages,  
equivalent to the passive mode ones:

Passive
{ok, {http_response, Version, Status, Phrase}}
{ok, {http_request, Mth, Uri, Version}}
{ok, {http_header, Bit, Name, IValue, Value}}
{ok, http_eoh}
{error, {http_error, Line}}

Active
{http_response, S, Version, Status, Phrase}
{http_request, S, Meth, Uri, Version}
{http_header, S, Bit, Name, Code, Value}
{http_eoh, S}
{http_error, S, Line}

Change the "gen_tcp:recv()" to "receive" and it's mostly done.

I don't have a patch, because I just modified it to use active mode,  
whereas a real patch would give the developer the option of which mode  
to use.  But let me know if you have any questions.

        Steven

PS Oh and BTW, apparently the Erlang developers are planning on  
changing the form of the active messages, so anyone who uses active  
mode should be aware of that when the next version comes out.

On Jul 16, 2008, at 10:57 AM, Steven G wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jacky zhao  
View profile  
 More options Aug 7 2008, 4:39 am
From: "jacky zhao" <jackyz.z...@gmail.com>
Date: Thu, 7 Aug 2008 16:39:14 +0800
Local: Thurs, Aug 7 2008 4:39 am
Subject: Re: [mochiweb] Re: detecting closed connections immediately?

Hi Steven,

I had tried your method, seems it doesn't fit my senario. Since there are a
lot of calls to gen_tcp:recv() in mochiweb_request.erl. But I don't want to
change them all. Your words inspire me anyway. I found a new way to do this,
need not to chang any code in mochiweb itself.

in inet module's documents, I found these words:

...To make programming easier, a socket where the peer closed and this was
detected while in {active, false} mode, will still generate the message
{tcp_closed,Socket} when set to {active, once} or {active, true} mode...

So, I tried these lines:

%% mochiweb's callback loop
loop(Req) ->
    %% parse request or what ever
    %% assume we send out something, waiting for response here
    case wait_data(Req) of
        {error, socket_closed} ->
             %% socket has closed, need not response anymore
             ok;
        {error, timeout} ->
             %% perform timeout response, let them call again
             response(Req, timeout);
        {ok, Data} ->
             %% perform data response, and, let them call again
             response(Req, Data)
    end.

wait_data(Req) ->
    Socket = Req:get(socket),
    inet:setopts(Socket, [{active, once}]),
    receive
         {tcp_closed, Socket} ->
             {error, socket_closed};
         Data ->
             {ok, Data}
    after
         ?TimeOut ->
             {error, timeout}
    end.

It's just works fine for me. Hope these helps.

Jackyz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google