Example #1
0
        /// <summary>Initializes a new instance of the <see cref="Connection"/> class.</summary>
        /// <param name="socket">The socket upon which this connection is to be based.</param>
        /// <param name="connectionHost">The system hosting this connection.</param>
        public Connection(Socket socket, ISubSystem connectionHost)
        {
            Buffer          = new StringBuilder();
            OutputBuffer    = new OutputBuffer();
            Data            = new byte[1];
            TerminalOptions = new TerminalOptions();
            this.socket     = socket;
            var remoteEndPoint = (IPEndPoint)this.socket.RemoteEndPoint;

            CurrentIPAddress = remoteEndPoint.Address;
            ID = Guid.NewGuid().ToString();
            TelnetCodeHandler   = new TelnetCodeHandler(this);
            this.connectionHost = connectionHost;
        }
Example #2
0
        // TODO: Optimize more
        public static string Parse(char[] buffer, int bufferPos, TerminalOptions terminalOptions)
        {
            var result          = "";
            var token           = "";
            var charFromNewline = 0;
            var isToken         = false;

            for (var i = 0; i < bufferPos; i++)
            {
                // OutputBuffer / OutputParser should be the only place writing ANSI NewLine character sequences, to help guarantee correct Telnet protocol handling.
                // (Telnet protocol says new lines sent should be CR LF regardless of what OS the server is running on and what OS the client is running on.)
                Debug.Assert(buffer[i] != '\r' && buffer[i] != '\n', "Output should not include explicit newline characters. Use <%nl%> or OutputBuffer WriteLine functions instead.");
                if (isToken && buffer[i] == '>')
                {
                    if (buffer[i - 1] == '%')
                    {
                        isToken = false;
                        token   = token.Trim('%');

                        if (token == "nl")
                        {
                            charFromNewline = 0;
                            token           = "";
                            result         += AnsiSequences.NewLine;
                            continue;
                        }

                        if (terminalOptions.UseANSI)
                        {
                            result += AnsiHandler.ConvertCode(token);
                        }

                        token = "";
                        continue;
                    }
                }

                if (!isToken && buffer[i] == '<')
                {
                    if (buffer[i + 1] == '%')
                    {
                        if (i == 0 || buffer[i - 1] != '\\')
                        {
                            isToken = true;
                            continue;
                        }

                        result = result.Remove(result.Length - 1);
                    }
                }

                if (!isToken)
                {
                    result += buffer[i];
                    charFromNewline++;

                    if (charFromNewline <= terminalOptions.Width || terminalOptions.UseWordWrap == false)
                    {
                        continue;
                    }

                    var lastSpaceIndex = result.LastIndexOf(' ');

                    if (lastSpaceIndex > 0)
                    {
                        result          = result.Remove(lastSpaceIndex, 1);
                        charFromNewline = result.Length - lastSpaceIndex;
                        result          = result.Insert(lastSpaceIndex, AnsiSequences.NewLine);
                    }
                    else
                    {
                        result         += AnsiSequences.NewLine;
                        charFromNewline = 0;
                    }
                }
                else
                {
                    token += buffer[i];
                }
            }

            return(result);
        }
Example #3
0
 public string Parse(TerminalOptions terminalOptions)
 {
     return(OutputParser.Parse(buffer, bufferPos, terminalOptions));
 }
Example #4
0
        //TODO: Optimize more
        public static string Parse(char[] buffer, int bufferPos, TerminalOptions terminalOptions)
        {
            var result          = "";
            var token           = "";
            var charFromNewline = 0;

            var isToken = false;

            for (var i = 0; i < bufferPos; i++)
            {
                if (isToken && buffer[i] == '>')
                {
                    if (buffer[i - 1] == '%')
                    {
                        isToken = false;
                        token   = token.Trim('%');

                        if (token == "nl")
                        {
                            charFromNewline = 0;
                            token           = "";
                            result         += AnsiSequences.NewLine;
                            continue;
                        }

                        if (terminalOptions.UseANSI)
                        {
                            result += AnsiHandler.ConvertCode(token);
                        }

                        token = "";
                        continue;
                    }
                }

                if (!isToken && buffer[i] == '<')
                {
                    if (buffer[i + 1] == '%')
                    {
                        if (i == 0 || buffer[i - 1] != '\\')
                        {
                            isToken = true;
                            continue;
                        }

                        result = result.Remove(result.Length - 1);
                    }
                }

                if (!isToken)
                {
                    result += buffer[i];
                    charFromNewline++;

                    if (charFromNewline <= terminalOptions.Width || terminalOptions.UseWordWrap == false)
                    {
                        continue;
                    }

                    var lastSpaceIndex = result.LastIndexOf(' ');

                    if (lastSpaceIndex > 0)
                    {
                        result          = result.Remove(lastSpaceIndex, 1);
                        charFromNewline = result.Length - lastSpaceIndex;
                        result          = result.Insert(lastSpaceIndex, AnsiSequences.NewLine);
                    }
                    else
                    {
                        result         += AnsiSequences.NewLine;
                        charFromNewline = 0;
                    }
                }
                else
                {
                    token += buffer[i];
                }
            }

            return(result);
        }