Example #1
0
            public void Run()
            {
                AnsiEscapeDecoder ansiDecoder = null;

                if (client.enableColorDecoding)
                {
                    ansiDecoder = new AnsiEscapeDecoder(AnsiEscapeHandler, AnsiDataHandler);
                }

                Exception potentialException = null;

                try
                {
                    Int32  offset     = 0;
                    Byte[] byteBuffer = new Byte[2048];

                    while (true)
                    {
                        Int32 bytesRead = stream.Receive(byteBuffer, offset, byteBuffer.Length - offset);
                        if (bytesRead <= 0)
                        {
                            if (offset > 0)
                            {
                                Console.Write(Encoding.ASCII.GetString(byteBuffer, 0, offset));
                                offset = 0;
                            }
                            break;
                        }

                        if (client.enableColorDecoding)
                        {
                            UInt32 processed = ansiDecoder.Decode(byteBuffer, 0, (UInt32)bytesRead);

                            UInt32 bytesLeft = (UInt32)bytesRead - processed;
                            if (bytesLeft > 0)
                            {
                                Array.Copy(byteBuffer, offset, byteBuffer, 0, bytesLeft);
                                offset = (Int32)bytesLeft;
                            }
                        }
                        else
                        {
                            Int32 charCount = Encoding.ASCII.GetChars(byteBuffer, 0, bytesRead, charBuffer, 0);
                            Console.Write(charBuffer, 0, charCount);
                        }
                    }
                }
                catch (Exception e)
                {
                    potentialException = e;
                }
                finally
                {
                    client.ReceiveLoopDone(potentialException);
                }
            }