bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting dead connection with TCPSession?


From: Jon Frydensbjerg
Subject: Re: Detecting dead connection with TCPSession?
Date: Thu, 4 Apr 2002 12:47:41 +0200 (MEST)

On Thu, 4 Apr 2002, Jon Frydensbjerg wrote:

Hi.

I'm sorry for replying once again, but I totally misread your posting... 

> > I wrote a similar application and found the same problem. 
> > Does anybody know how to handle this?

Yep, the trick is to test isPending(SOCKET_PENDING_ERROR,0), and remember
to catch exceptions, when you are receiving/sending data. 

I implemented this by setting an internal bool variable in my networking
class (which inherits TCPSession), when a socket error (e.g.
disconnection) was detected. I declared two simple functions for accessing
this bool:

  bool okToDelete;

  bool isOkToDelete(void) { return  okToDelete; }
  bool isOkToSend(void)   { return !okToDelete; }

The detection was handled by a thread for each connection (overrides
'virtual void Run(void)'):

  while(1)
  {
    Yield();
    
    if ( isPending(SOCKET_PENDING_ERROR) && !okToDelete )
    {
      okToDelete = true;
    }
  }

For each connection I then do the following when broadcasting data to all
connected "port"s:

      if ( port != NULL ) 
      {
        if ( !port->isPending(SOCKET_PENDING_ERROR, 0) && 
              port->isOkToSend() )
        {        
          try
          {
            ... [insert your own code here!]
          }
          catch ( ... )
          {
            cout << "Socket error!" << endl;
          }
       }
       else if ( port->isOkToDelete() )
       {
         delete port;    

         ... [various marshalling]
       }
    }
 
I hope this helps. 

Best,
Jon




reply via email to

[Prev in Thread] Current Thread [Next in Thread]