Ejemplo n.º 1
0
        private void RecvCallback_obsolete(IAsyncResult ar)
        {
            try
            {
                if (RemoteState != null)
                {
                    if (RemoteState.Stream.CanRead)
                    {
                        int len = RemoteState.Stream.EndRead(ar);
                        if (len == 0)
                        {
                            Disconnect();
                        }
                        else
                        {
                            string data = "";
                            if (ProtocolStrategy != null)
                            {
                                data = ProtocolStrategy.Encoding.GetString(RemoteState.ReadBuffer, 0, len);
                                ProtocolStrategy.Parse(data, null);
                            }

                            RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, new AsyncCallback(RecvCallback_obsolete), null);
                        }
                    }
                }
                else
                {
                }
            }
            catch (System.IO.IOException ioe)
            {
                if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError))
                {
                    System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException;

                    try
                    {
                        Disconnect();
                    }
                    catch (NullReferenceException)
                    { }
                    catch (Exception e)
                    {
                        OnAsyncException(new ExceptionEventArgs(e));
                    }
                }
            }
            catch (NullReferenceException)
            { }
            catch (Exception e)
            {
                OnAsyncException(new ExceptionEventArgs(e));
            }
        }
Ejemplo n.º 2
0
        private void ReadCallback(IAsyncResult ar)
        {
            try
            {
                RemoteHostState state = ar.AsyncState as RemoteHostState;
                int             len   = 0;
                len = state.Stream.EndRead(ar);

                if (len == 0)
                {
                    CloseConnection();
                }
                else
                {
                    try
                    {
                        if (ProtocolStrategy != null)
                        {
                            if (ProtocolStrategy.Encoding != null)
                            {
                                if (state.Decoder == null)
                                {
                                    state.Decoder = ProtocolStrategy.Encoding.GetDecoder();
                                }

                                int    charCount = state.Decoder.GetCharCount(state.ReadBuffer, 0, len);
                                char[] chars     = new char[charCount];
                                state.Decoder.GetChars(state.ReadBuffer, 0, len, chars, 0);
                                string msg = new string(chars);

                                ProtocolStrategy.Parse(msg, state);
                            }
                            else
                            {
                                ProtocolStrategy.Parse(state.ReadBuffer, len, state);
                            }
                        }
                    }
                    catch { }

                    state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state);
                }
            }
            catch (System.IO.IOException ioe)
            {
                if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError))
                {
                    System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException;

                    if (DoCloseConnection())
                    {
                        OnClosedConnection((se.SocketErrorCode == SocketError.Interrupted) ? null : se);
                    }
                }
                else
                if (DoCloseConnection())
                {
                    OnClosedConnection(ioe);
                }
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }
        }