TReKiE / msnp-sharp

Automatically exported from code.google.com/p/msnp-sharp
0 stars 0 forks source link

SendSocketData exception #59

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.   occasionally, SendSocketData  method of SocketMessageProcessor class

     throws some exception. It will made whole program exit. But how can I

     catch this exception and let the program still running?

2.   in order to let the program still run after this exception, I have to 

     do sth. like this:

     protected static void SendSocketData(Socket psocket, byte[] data)
        {
            try
            {
                if (psocket != null)
                    lock (psocket)
                    {
                        psocket.Send(data);
                    }
            }
            catch (Exception e)
            {
                //throw new MSNPSharpException("Error while sending network 
message. See the inner exception for more details.", e);
            }
        } 

but I think there will be more error.

Original issue reported on code.google.com by pengs...@gmail.com on 8 Jan 2009 at 7:58

GoogleCodeExporter commented 9 years ago
It seems that you've send messages while the socket is disconnected, just check 
your 
code and figure out why this happens.

Original comment by freezing...@gmail.com on 8 Jan 2009 at 1:57

GoogleCodeExporter commented 9 years ago

Original comment by hepha...@gmail.com on 23 Jan 2009 at 5:35

GoogleCodeExporter commented 9 years ago
Just wanted to let you all know my results with this.

1. User enters message to send.
2. Calling code verifies that socket is still connected.
3. Socket Disconnects.
4. Calling code tries to send message.
Exception occurs.

And there is no way for the calling code to prevent these.
I run a testing system that runs a stress-test program to simulate a loaded-down
system, and it can take 50-500 ms to execute between steps 2 and 4.  If the 
socket
disconnects anywhere in that window, an unhandled exception is thrown.  I had 
to hack
MSNPSharp a bit to fix it:

        protected void SendSocketData(byte[] data)
        {
            if (socket == null || !socket.Connected)
            {
                // the connection is closed
                OnDisconnected();
                return;
            }

            SendSocketData(this, data);
        }

        protected static void SendSocketData(SocketMessageProcessor processor, byte[]
data)
        {
            try
            {
                if (processor.socket != null)
                    lock (processor.socket)
                    {
                        processor.socket.Send(data);
                    }
            }
            catch (Exception e)
            {
                processor.OnDisconnected();
            }
        }

I'm not sure that it is proper to do this - it may cause other problems that I 
can't
see.  But I needed a better solution than ignoring these SocketExceptions in the
application unhandled error handler, and this solution seems to work.   

Original comment by EricCoxH...@gmail.com on 29 Jun 2009 at 7:54

GoogleCodeExporter commented 9 years ago
Is psocket.Connected == true when you get that exception?

Original comment by freezing...@gmail.com on 30 Jun 2009 at 6:52

GoogleCodeExporter commented 9 years ago
ok,try r1067

Original comment by freezing...@gmail.com on 30 Jun 2009 at 8:34

GoogleCodeExporter commented 9 years ago
Thanks!   I'll let you know how testing goes.

Original comment by EricCoxH...@gmail.com on 30 Jun 2009 at 7:02