public static Tuple <List <ShowItemBase>, TelnetLogList> NextServerRequest(
            NetworkStreamBackedInputByteArray InputArray,
            NegotiateSettings NegotiateSettings,
            bool ForceRead = false)
        {
            TelnetLogList logList      = new TelnetLogList();
            var           showItemList = new ShowItemList();

            while (true)
            {
                // read available bytes from input stream.
                if (InputArray.IsEof())
                {
                    var log = InputArray.ReadFromNetworkStream(5, 5, ForceRead);
                    logList.AddItems(log);
                }

                // no input data to process.
                if (InputArray.IsEof())
                {
                    break;
                }

                var buf = InputArray.PeekDataStreamHeader();
                if (buf != null)
                {
                    var dsh = new DataStreamHeader(InputArray);
                    logList.AddItems(Direction.Read, dsh.ToReportLines( ), true);

                    var rv = ParseAndProcessWorkstationCommand(InputArray, dsh);
                    var workstationCommand = rv.Item1;
                    showItemList = rv.Item2;
                    logList.AddItems(rv.Item4);
                }

                // check for IAC EOR
                {
                    var telCode = InputArray.PeekTelnetCommandCode(CommandCode.EOR);
                    if (telCode != null)
                    {
                        var telCmd = InputArray.NextTelnetCommand();
                        logList.AddItems(Direction.Read, telCmd.ToReportLines(), true);
                    }
                }

                // peek and process input bytes as a telnet stmt. ( starts with IAC )
                {
                    var telCmd = InputArray.NextTelnetCommand();
                    if (telCmd != null)
                    {
                        var rv         = TelnetConnection.ProcessTelnetCommand(telCmd, NegotiateSettings);
                        var cx         = rv.Item1;
                        var writeBytes = rv.Item2;
                        logList.AddItems(rv.Item3);
                        WriteToHost(logList, writeBytes, InputArray.NetStream);
                    }
                }
            }
            return(new Tuple <List <ShowItemBase>, TelnetLogList>(showItemList, logList));
        }
        public static Tuple <WorkstationCommandList, TelnetLogList> GetAndParseWorkstationCommandList(
            NetworkStreamBackedInputByteArray InputArray,
            SessionSettings SessionSettings)
        {
            TelnetLogList logList       = new TelnetLogList();
            var           wrkstnCmdList = new WorkstationCommandList();

            var dsh = new DataStreamHeader(InputArray);

            if (dsh != null)
            {
                logList.AddItems(Direction.Read, dsh.ToReportLines( ), true);
            }

            if ((dsh != null) && (dsh.Errmsg == null))
            {
                bool lastCmdWasTelnet_EOR = false;
                while (dsh != null)
                {
                    // read available bytes from input stream.
                    if (InputArray.IsEof() && (lastCmdWasTelnet_EOR == false))
                    {
                        var log = InputArray.ReadFromNetworkStream(5, 5);
                        logList.AddItems(log);
                    }

                    // no input data to process.
                    if (InputArray.IsEof())
                    {
                        break;
                    }
                    lastCmdWasTelnet_EOR = false;

                    // check for IAC EOR
                    var telCode = InputArray.PeekTelnetCommandCode(CommandCode.EOR);
                    if (telCode != null)
                    {
                        var telCmd = InputArray.NextTelnetCommand();
                        logList.AddItems(Direction.Read, telCmd.ToReportLines(), true);
                        lastCmdWasTelnet_EOR = true;
                    }

                    // process the input as workstation data stream commands.
                    else
                    {
                        var rv = ParseAndProcessWorkstationCommand(InputArray, dsh);
                        var workstationCommand = rv.Item1;
                        dsh = rv.Item3;
                        logList.AddItems(rv.Item4);

                        if (workstationCommand != null)
                        {
                            wrkstnCmdList.Add(workstationCommand);
                        }
                    }
                }
            }
            return(new Tuple <WorkstationCommandList, TelnetLogList>(wrkstnCmdList, logList));
        }
        public TelnetLogList ParseDataStream(
            NetworkStreamBackedInputByteArray inputArray, SessionSettings sessionSettings)
        {
            var logList = new TelnetLogList();

            bool didParse = true;

            while ((didParse == true) && (inputArray.RemainingLength > 0))
            {
                didParse = false;

                if (didParse == false)
                {
                    var telCmd = inputArray.NextTelnetCommand();
                    if (telCmd != null)
                    {
                        var s1 = telCmd.ToString();
                        logList.AddItem(Direction.Read, s1);
                        didParse = true;
                    }
                }

                if (didParse == false)
                {
                    var rv = Process5250.GetAndParseWorkstationCommandList(
                        inputArray, sessionSettings);

                    logList.AddItems(rv.Item2);
                    var wrkstnCmdList = rv.Item1;

                    // draw the fields and literals on the canvas.
                    if (wrkstnCmdList?.Count > 0)
                    {
                        didParse = true;
                        foreach (var workstationCmd in wrkstnCmdList)
                        {
                        }
                    }
                }
            }
            return(logList);
        }
        public static Tuple <int, int, List <ShowItemBase>, TelnetLogList> GetAndProcessStream(
            TcpClient Client, NetworkStream NetStream,
            NetworkStreamBackedInputByteArray InputArray,
            NegotiateSettings NegotiateSettings,
            bool ForceRead = false)
        {
            int  sendStmtCx   = 0;
            int  getStmtCx    = 0;
            var  logList      = new TelnetLogList();
            var  showItemList = new ShowItemList();
            bool exitLoop     = false;

            while (exitLoop == false)
            {
                // read available bytes from input stream.
                if (InputArray.IsEof())
                {
                    var log = InputArray.ReadFromNetworkStream(5, 5, ForceRead);
                    logList.AddItems(log);
                }

                // no input data to process.
                if (InputArray.IsEof())
                {
                    break;
                }

                DataStreamHeader currentDataStreamHeader = null;
                while (true)
                {
                    if (InputArray.IsEof() == true)
                    {
                        break;
                    }

                    // process the input as 5250 data stream commands.
                    if (currentDataStreamHeader != null)
                    {
                        var rv = ParseAndProcessWorkstationCommand(InputArray, currentDataStreamHeader);
                        var workstationCommand = rv.Item1;
                        showItemList            = rv.Item2;
                        currentDataStreamHeader = rv.Item3;
                        logList.AddItems(rv.Item4);
                        continue;
                    }

                    // peek and process input bytes as a telnet stmt. ( starts with IAC )
                    var telCmd = InputArray.NextTelnetCommand();
                    if (telCmd != null)
                    {
                        getStmtCx += 1;

                        var rv         = TelnetConnection.ProcessTelnetCommand(telCmd, NegotiateSettings);
                        var cx         = rv.Item1;
                        var writeBytes = rv.Item2;
                        logList.AddItems(rv.Item3);
                        sendStmtCx += cx;
                        WriteToHost(logList, writeBytes, NetStream);

                        continue;
                    }

                    var dsh = new DataStreamHeader(InputArray);
                    if ((dsh != null) && (dsh.Errmsg == null))
                    {
                        logList.AddItem(Direction.Read, dsh.ToString());
                        currentDataStreamHeader = dsh;
                        continue;
                    }
                    exitLoop = true;
                    break;
                }
            }
            return(new Tuple <int, int, List <ShowItemBase>, TelnetLogList>(
                       getStmtCx, sendStmtCx, showItemList, logList));
        }