Пример #1
0
        // Called when an asynchronous read completes
        private void Stream_ReadComplete(IAsyncResult Result)
        {
            try
            {
                // Retrieve the notification byte
                In.EndReadByte(Result);
                Result.AsyncWaitHandle.Dispose();

                // Read a unknown type of message
                Message New = Message.ReadMessage(In);

                // If it's a disconnection, handle it internally
                if (New is DisconnectMessage)
                {
                    // Signal the disconnection
                    Disconnect(this, (DisconnectMessage)New);
                    return;
                }
                else // Not a disconnection, allow controlling code to handle it
                {
                    MessageReceived(this, New);
                }

                StartRead(); // Go for another message read
                return;
            }
            catch (ObjectDisposedException)
            { } // Stream was closed
            catch (IOException)
            { } // Stream was closed

            // If something went wrong, disconnect unexpectedly
            Disconnect(this, new DisconnectMessage(DisconnectType.Unexpected));
        }
Пример #2
0
        protected void Stream_ReadComplete(IAsyncResult Result)
        {
            try
            {
                // Read and ignore the notification byte
                In.EndReadByte(Result);
                Result.AsyncWaitHandle.Dispose();

                // Read in the message preceded by the notification byte
                Message New = Message.ReadMessage(In);

                if (New is DisconnectMessage) // D/C if neccessary
                {
                    Disconnect(this, (DisconnectMessage)New);
                    return;
                }
                else // Otherwise fire the message received event
                {
                    MessageReceived(this, New);
                }

                StartRead(); // Go for another read
            }
            catch
            {
                Disconnect(this, new DisconnectMessage(DisconnectType.Unexpected));
            }
        }