Example #1
0
        public void ReadLoop()
        {
            // This is the thread function for the reader thread. This runs continuously
            // performing a blokcing read on the socket and dispatching all commands
            // received.
            //
            // Exception Handling
            // ------------------
            // If an Exception occurs during the reading/marshalling, then the connection
            // is effectively broken because position cannot be re-established to the next
            // message.  This is reported to the app via the exceptionHandler and the socket
            // is closed to prevent further communication attempts.
            //
            // An exception in the command handler may not be fatal to the transport, so
            // these are simply reported to the exceptionHandler.
            //
            while (!closed.Value)
            {
                Command command = null;
                try
                {
                    command = (Command)Wireformat.Unmarshal(socketReader);
                }
                catch (Exception ex)
                {
                    if (!closed.Value)
                    {
                        this.exceptionHandler(this, ex);
                        // Close the socket as there's little that can be done with this transport now.
                        Close();
                        break;
                    }
                }

                try
                {
                    if (command != null)
                    {
                        this.commandHandler(this, command);
                    }
                }
                catch (Exception e)
                {
                    this.exceptionHandler(this, e);
                }
            }
        }
Example #2
0
        /// <summary>
        ///     This is the thread function for the reader thread. This runs continuously performing a blocking read on the socket
        ///     and dispatching all commands received.
        /// </summary>
        /// <remarks>
        ///     Exception Handling
        ///     ------------------
        ///     If an Exception occurs during the reading/marshaling, then the connection is effectively broken because position
        ///     cannot be re-established to the next message. This is reported to the application via the exceptionHandler and the
        ///     socket is closed to prevent further communication attempts.
        ///     An exception in the command handler may not be fatal to the transport, so these are simply reported to the
        ///     exceptionHandler.
        /// </remarks>
        private void ReadLoop()
        {
            while (!_closed.Value)
            {
                ICommand command;

                try
                {
                    command = Wireformat.Unmarshal(_socketReader);
                }
                catch (Exception ex)
                {
                    if (!_closed.Value)
                    {
                        // Close the socket as there's little that can be done with this transport now.
                        Close();

                        if (!_seenShutdown)
                        {
                            Exception(this, ex);
                        }
                    }

                    break;
                }

                try
                {
                    if (command != null)
                    {
                        Command(this, command);
                    }
                }
                catch (Exception ex)
                {
                    Exception(this, ex);
                }
            }
        }