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));
        }
        private void butParseResponse_Click(object sender, RoutedEventArgs e)
        {
            var logList = new TelnetLogList();

            var text  = TextBox1.Text;
            var lines = text.Split(
                new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var ba = ParseHexLines(lines);

            Process5250.ParseResponseStream(logList, ba);

            foreach (var item in logList)
            {
                if (item.NewGroup == true)
                {
                    this.Model.RunLog.Add("");
                }
                this.Model.RunLog.Add(item.Text);
            }

            // print the bytes of the response stream as lines of hex text.
            {
                var rep = ba.ToHexReport(16);
                logList.AddItem(Direction.Read, "Length:" + ba.Length + " Byte stream bytes:");
                logList.AddItems(Direction.Read, rep);
            }
        }
        ParseAndProcessWorkstationCommand(
            InputByteArray inputArray, DataStreamHeader StreamHeader)
        {
            var logList = new TelnetLogList();
            WorkstationCommandBase wrkstnCommand           = null;
            DataStreamHeader       currentDataStreamHeader = StreamHeader;
            ShowItemList           showItemList            = null;

            wrkstnCommand = WorkstationCommandBase.ParseFactory(inputArray);
            if (wrkstnCommand == null)
            {
                currentDataStreamHeader = null;
            }
            else
            {
                logList.AddItems(Direction.Read, wrkstnCommand.ToReportLines(), true);

                // process WriteToDisplay command. This command contains a list TextData and
                // start field orders. Create ShowItemList items from those orders.
                if (wrkstnCommand is WriteToDisplayCommand)
                {
                    var wtdCommand = wrkstnCommand as WriteToDisplayCommand;
                    showItemList = wtdCommand.BuildShowItemList(logList);
                }
            }
            return(new Tuple <WorkstationCommandBase, ShowItemList, DataStreamHeader, TelnetLogList>(
                       wrkstnCommand, showItemList, currentDataStreamHeader, 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));
        }
Exemple #5
0
        public static Tuple <bool, OneRowCol> PaintCanvas(
            this WriteToDisplayCommand WTD_command, ItemCanvas ItemCanvas,
            bool EraseScreen = true, TelnetLogList LogList = null
            )
        {
            OneRowCol caret    = null;
            bool      drawDone = false;

            caret    = ItemCanvas.PaintScreen(WTD_command, EraseScreen, LogList);
            drawDone = true;

            return(new Tuple <bool, OneRowCol>(drawDone, caret));
        }
        /// <summary>
        /// parse the 5250 data stream that is sent from the client to the server.
        /// </summary>
        /// <param name="LogFile"></param>
        /// <param name="ToServerStream"></param>
        /// <returns></returns>
        public static Tuple <object[], string> ParseResponseStream(
            TelnetLogList LogList, byte[] ResponseStream)
        {
            List <object> listItems = new List <object>();
            string        errmsg    = null;

            var inputArray =
                new NetworkStreamBackedInputByteArray(ResponseStream, ResponseStream.Length);
            var writeStream                 = new ByteArrayBuilder();
            DataStreamHeader dsHeader       = null;
            ResponseHeader   responseHeader = null;

            // stream starts with data stream header.
            dsHeader = new DataStreamHeader(inputArray);
            listItems.Add(dsHeader);
            errmsg = dsHeader.Errmsg;
            LogList.AddItems(Direction.Write, dsHeader.ToReportLines(), true);

            // next is the response header.
            if (errmsg == null)
            {
                responseHeader = new ResponseHeader(inputArray);
                listItems.Add(responseHeader);
                errmsg = responseHeader.Errmsg;
                LogList.AddItems(Direction.Write, responseHeader.ToReportLines(), true);
            }

            // repeating instances of sbaOrder, textDataOrder pairs.
            while (true)
            {
                // check that an SBA order is starting. Leave loop when it is not.
                if (SetBufferAddressOrder.CheckOrder(inputArray) != null)
                {
                    break;
                }

                var orderPair = new LocatedTextDataOrderPair(inputArray);
                if (orderPair.Errmsg != null)
                {
                    break;
                }
                listItems.Add(orderPair);
                LogList.AddItems(Direction.Write, orderPair.ToReportLines(), true);
            }

            return(new Tuple <object[], string>(listItems.ToArray(), errmsg));
        }
        void NewParse()
        {
            var logList = new TelnetLogList();

            var text  = TextBox1.Text;
            var lines = text.Split(
                new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var ba              = ParseHexLines(lines);
            var inputArray      = new NetworkStreamBackedInputByteArray(ba, ba.Length);
            var sessionSettings = new SessionSettings();

            Process5250.ParseResponseStream(logList, ba);

            foreach (var item in logList)
            {
                if (item.NewGroup == true)
                {
                    this.Model.RunLog.Add("");
                }
                this.Model.RunLog.Add(item.Text);
            }


            // peek at the input stream from server. Classify the data that is next
            // to receive.
            var typeData = ServerDataStream.PeekServerCommand(inputArray);

            // input data not recogizied. Not a 5250 data strem header.
            if (typeData == null)
            {
                logList.AddItem(Direction.Read, "Unknown data stream data");
                logList.AddItems(
                    Direction.Read, inputArray.PeekBytes().ToHexReport(16));
            }

            else if (typeData.Value == TypeServerData.workstationHeader)
            {
                {
                    var rv = Process5250.GetAndParseWorkstationCommandList(
                        inputArray, sessionSettings);

                    var workstationCmdList = rv.Item1;
                    logList.AddItems(rv.Item2);
                }
            }
        }
        public OneRowCol PaintScreen(
            WriteToDisplayCommand WTD_command, bool Erase = true, TelnetLogList LogList = null)
        {
            if (Erase == true)
            {
                this.EraseScreen();
                if (LogList != null)
                {
                    LogList.AddItem(Direction.Read, "PaintScreen. EraseScreen.");
                }
            }

            this.ShowItemList = null;

            var caret = PaintScreen_Actual(WTD_command, LogList);

            return(caret);
        }
        private void BuildAndSendResponseDataStream(
            AidKey AidKey, ScreenContent ScreenContent, TelnetLogList LogList = null)
        {
            // send response data stream up to the server.
            {
                var ra = BuildResponseByteStream(
                    ScreenContent,
                    AidKey, ScreenContent.CaretRowCol, ScreenContent.HowRead);
                ra = ra.Append(EOR_Command.Build());

                // send response stream back to server.
                TelnetConnection.WriteToHost(LogList, ra, this.Client.GetStream());

                // blank line in the log file.
                var logItem = new LogListItem(Direction.Read, "", true);
                this.LogList.AddItem(logItem);
            }
        }
        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);
        }
        private static void WriteToHost(
            TelnetLogList LogList, byte[] Buffer, NetworkStream NetStream)
        {
            if (NetStream.CanWrite == true)
            {
                LogList.Write(Direction.Write, Buffer);

                {
                    string hexText = Buffer.ToHex(' ');
                    LogList.AddItem(Direction.Write, "Hex: " + Buffer.Length.ToString() +
                                    "  " + hexText);
                }

                NetStream.Write(Buffer, 0, Buffer.Length);
            }
            else
            {
                LogList.AddItem(Direction.Write, "Error. Could not write to network stream.");
            }
        }
        public static Tuple <bool, TelnetLogList> PaintCanvas(
            this WorkstationCommandList CmdList, ItemCanvas ItemCanvas, TelnetLogList LogList = null)
        {
            TelnetLogList logList  = new TelnetLogList();
            bool          drawDone = false;
            OneRowCol     caret    = null;

            foreach (var cmdBase in CmdList)
            {
                if ((cmdBase is ClearUnitCommand) || (cmdBase is ClearUnitAlternateCommand))
                {
                    ItemCanvas.EraseScreen();
                }

                else if (cmdBase is WriteToDisplayCommand)
                {
                    // store the painted from wtdCommand in the ItemCanvas itself. Used when
                    // ScreenDefn imports a screen layout into the defn of a screen.
                    ItemCanvas.WriteToDisplayCommand = cmdBase as WriteToDisplayCommand;

                    bool eraseScreen = false;
                    var  rv          = ItemCanvas.WriteToDisplayCommand.PaintCanvas(
                        ItemCanvas, eraseScreen, LogList);
                    var localDrawDone = rv.Item1;
                    if (rv.Item2 != null)
                    {
                        caret = rv.Item2;
                    }

                    if (localDrawDone == true)
                    {
                        drawDone = true;
                    }
                }
            }

            ItemCanvas.PositionCaret(caret);

            return(new Tuple <bool, TelnetLogList>(drawDone, logList));
        }
        /// <summary>
        /// run the BuildShowItemList method against the first WriteToDisplay command in
        /// the list of workstation commands.
        /// </summary>
        /// <param name="LogList"></param>
        /// <returns></returns>
        public static ShowItemList BuildShowItemList(
            this WorkstationCommandList CommandList, ScreenDim ScreenDim, TelnetLogList LogList)
        {
            ShowItemList showItemList = null;
            OneRowCol    caret        = null;

            foreach (var workstationCommand in CommandList)
            {
                if (workstationCommand is WriteToDisplayCommand)
                {
                    var           wtdCmd  = workstationCommand as WriteToDisplayCommand;
                    TelnetLogList logList = new TelnetLogList();
                    var           rv      = wtdCmd.BuildShowItemList(ScreenDim, logList);
                    showItemList = rv.Item1;
                    if (rv.Item2 != null)
                    {
                        caret = rv.Item2;
                    }
                    break;
                }
            }
            return(showItemList);
        }
Exemple #14
0
        /// <summary>
        /// apply the commands of the workstation command list to the screen content
        /// block.
        /// </summary>
        /// <param name="applyMaster"></param>
        /// <param name="CmdList"></param>
        /// <param name="ToThread"></param>
        /// <param name="LogList"></param>
        /// <returns></returns>
        public static Tuple <bool, ScreenContent> Apply(
            this ScreenContent InBaseMaster,
            WorkstationCommandList CmdList,
            ToThread ToThread, PaintThread PaintThread, TelnetLogList LogList)
        {
            bool wtdApplied = false;
            var  baseMaster = InBaseMaster;
            var  master     = baseMaster.GetWorkingContentBlock();

            foreach (var cmdBase in CmdList)
            {
                if (cmdBase is ClearUnitCommand)
                {
                    var rv = ProcessClearUnit(baseMaster, baseMaster.ScreenDim, PaintThread);
                    baseMaster = rv.Item1;
                    master     = rv.Item2;
                }

                // same as ClearUnit. Only signals that a wide screen to be used.
                else if (cmdBase is ClearUnitAlternateCommand)
                {
                    var cua = cmdBase as ClearUnitAlternateCommand;

                    // screen size.
                    ScreenDim screenDim;
                    if (cua.RequestByte == 0x00)
                    {
                        screenDim = new ScreenDim(27, 132);
                    }
                    else
                    {
                        screenDim = new ScreenDim(24, 80);
                    }

                    var rv = ProcessClearUnit(baseMaster, screenDim, PaintThread);
                    baseMaster = rv.Item1;
                    master     = rv.Item2;
                }

                // apply the orders of the WriteToDisplay command.
                else if (cmdBase is WriteToDisplayCommand)
                {
                    var curMaster = master.Apply(cmdBase as WriteToDisplayCommand);
                    master     = curMaster;
                    wtdApplied = true;
                }

                else if (cmdBase is ReadMdtFieldsCommand)
                {
                    master.HowRead = HowReadScreen.ReadMdt;
                }

                // save screen command. Build response, send back to server.
                else if (cmdBase is SaveScreenCommand)
                {
                    var msg = new SaveScreenMessage(master.Copy());
                    ToThread.PostInputMessage(msg);
                }

                // read screen command. Build response, send back to server.
                else if (cmdBase is ReadScreenCommand)
                {
                    var msg = new ReadScreenMessage(master.Copy());
                    ToThread.PostInputMessage(msg);
                }

                else if (cmdBase is WriteStructuredFieldCommand)
                {
                    var wsfCmd = cmdBase as WriteStructuredFieldCommand;
                    if (wsfCmd.RequestCode == WSF_RequestCode.Query5250)
                    {
                        var msg = new Query5250ResponseMessage();
                        ToThread.PostInputMessage(msg);
                    }
                }
                else if (cmdBase is WriteSingleStructuredFieldCommand)
                {
                }
            }
            return(new Tuple <bool, ScreenContent>(wtdApplied, baseMaster));
        }
Exemple #15
0
        /// <summary>
        /// create list of ShowLiteralItem and ShowFieldItem from the list of orders of
        /// a WriteToDisplay command.
        /// </summary>
        /// <param name="WtdCommand"></param>
        /// <param name="LogList"></param>
        /// <returns></returns>
        public static Tuple <ShowItemList, OneRowCol> BuildShowItemList(
            this WriteToDisplayCommand WtdCommand, ScreenDim ScreenDim,
            TelnetLogList LogList)
        {
            var       itemList  = new ShowItemList();
            IRowCol   curRowCol = new ZeroRowCol(0, 0);
            OneRowCol caret     = null;

            foreach (var order in WtdCommand.OrderList)
            {
                bool newGroup = false;
                if ((order.OrderCode == WtdOrder.SetBufferAddress) ||
                    (order.OrderCode == WtdOrder.StartHeader))
                {
                    newGroup = true;
                }
                LogList.AddItem(Direction.Read, order.ToString(), newGroup);
                LogList.AddItem(Direction.Read, order.ToHexString());

                if (order is TextDataOrder)
                {
                    var tdo = order as TextDataOrder;
                    var s1  = tdo.ShowText;
                    if ((tdo.AttrByte != null) || (s1.Length > 0) ||
                        (tdo.TailAttrByte != null))
                    {
                        var litItem = new ShowLiteralItem(
                            (ZeroRowCol)curRowCol, tdo.AttrByte, s1, tdo.TailAttrByte);
                        litItem.tdo_Length = s1.Length;

                        itemList.Add(litItem);
                        curRowCol = curRowCol.Advance(litItem.ItemLength());
                    }
                }

                else if (order.OrderCode == WtdOrder.StartField)
                {
                    var sfo      = order as StartFieldOrder;
                    var lx       = sfo.LL_Length;
                    var attrByte = sfo.AttrByte;

                    var fldItem = new ShowFieldItem((ZeroRowCol)curRowCol, sfo.AttrByte,
                                                    ShowUsage.Both, ShowDtyp.Char, lx);
                    fldItem.IsMonocase   = sfo.IsMonocase;
                    fldItem.sfo_FCW      = sfo.FCW_Bytes;
                    fldItem.sfo_FFW      = sfo.FFW_Bytes;
                    fldItem.sfo_Length   = sfo.LL_Length;
                    fldItem.IsNonDisplay = sfo.IsNonDisplay;

                    // field is non display.
                    //        if ((attrByte & 0x07) == 0x07)
                    //        {
                    //          fldItem.IsNonDisplay = true;
                    //        }

                    itemList.Add(fldItem);
                    curRowCol = curRowCol.Advance(1); // advance because of attrbyte.
                }

                else if (order.OrderCode == WtdOrder.SetBufferAddress)
                {
                    var sba = order as SetBufferAddressOrder;
                    curRowCol = sba.GetRowCol(ScreenDim).ToZeroRowCol();
                }

                else if (order.OrderCode == WtdOrder.InsertCursor)
                {
                    var ico = order as InsertCursorOrder;
                    caret = ico.RowCol;
                }

                else if (order.OrderCode == WtdOrder.RepeatToAddress)
                {
                    var rao = order as RepeatToAddressOrder;
                    var s1  = rao.RepeatShowText((ZeroRowCol)curRowCol);

                    var litItem = new ShowLiteralItem((ZeroRowCol)curRowCol, s1);
                    litItem.rao_RepeatTextByte = rao.RepeatTextByte;
                    litItem.rao_ToRowCol       = rao.RowCol;

                    itemList.Add(litItem);
                    curRowCol = curRowCol.Advance(s1.Length);
                }

                else if (order.OrderCode == WtdOrder.EraseToAddress)
                {
                    var eao     = order as EraseToAddressOrder;
                    var lx      = eao.EraseLength((ZeroRowCol)curRowCol);
                    var s1      = (" ").Repeat(lx);
                    var litItem = new ShowLiteralItem((ZeroRowCol)curRowCol, s1);
                    litItem.rao_RepeatTextByte = 0x00;
                    litItem.rao_ToRowCol       = eao.RowCol;

                    itemList.Add(litItem);
                    curRowCol = curRowCol.Advance(s1.Length);
                }
            }
            return(new Tuple <ShowItemList, OneRowCol>(itemList, caret));
        }
        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));
        }
        public static void ProcessAndPaint(
            this WorkstationCommandList CmdList, TcpClient TcpClient,
            ItemCanvas ItemCanvas, Window Window,
            TelnetLogList LogList = null)
        {
            OneRowCol caret = null;

            foreach (var cmdBase in CmdList)
            {
                if ((cmdBase is ClearUnitCommand) || (cmdBase is ClearUnitAlternateCommand))
                {
                    ItemCanvas.EraseScreen();
                    caret = null;
                }

                else if (cmdBase is WriteToDisplayCommand)
                {
                    bool doEraseScreen = false;
                    var  WTD_Command   = cmdBase as WriteToDisplayCommand;
                    ItemCanvas.WriteToDisplayCommand = WTD_Command;
                    var localCaret = ItemCanvas.PaintScreen(WTD_Command, doEraseScreen, LogList);
                    if (localCaret != null)
                    {
                        caret = localCaret;
                    }
                }

                else if (cmdBase is ReadMdtFieldsCommand)
                {
                    ItemCanvas.HowRead = HowReadScreen.ReadMdt;
                }

                // save screen command. Build response, send back to server.
                else if (cmdBase is SaveScreenCommand)
                {
                    var ra = SaveScreenCommandExt.BuildSaveScreenResponse(
                        ItemCanvas.VisualItems, ItemCanvas.CaretCursor);

                    // send response stream back to server.
                    if (TcpClient != null)
                    {
                        TelnetConnection.WriteToHost(
                            null, ra, TcpClient.GetStream());
                    }
                }

                else if (cmdBase is WriteStructuredFieldCommand)
                {
                    var wsfCmd = cmdBase as WriteStructuredFieldCommand;
                    if (wsfCmd.RequestCode == WSF_RequestCode.Query5250)
                    {
                        var ra = Query5250Response.BuildQuery5250Response();

                        // send response stream back to server.
                        if (TcpClient != null)
                        {
                            TelnetConnection.WriteToHost(LogList, ra, TcpClient.GetStream());
                        }
                    }
                }
                else if (cmdBase is WriteSingleStructuredFieldCommand)
                {
                }
            }

            ItemCanvas.PositionCaret(caret);
        }
        ProcessWorkstationDataStream(
            this ServerConnectPack ConnectPack, ScreenVisualItems VisualItems,
            CanvasPositionCursor Caret)
        {
            WorkstationCommandList       workstationCmdList = null;
            List <WriteToDisplayCommand> wtdCmdList         = null;
            DataStreamHeader             dsh = null;
            var           returnCmdList      = new WorkstationCommandList();
            HowReadScreen?howRead            = null;
            var           logList            = new TelnetLogList();
            bool          gotEOR             = false;

            while (gotEOR == false)
            {
                // input array is eof. Exit loop if have read a READ workstn command.
                // Otherwise, read from server.
                if (ConnectPack.ReadBlocks.IsEmpty == true)
                {
                    if (howRead != null)
                    {
                        break;
                    }
                }

                var item = ConnectPack.ReadBlocks.WaitAndDequeue();

                if (item is DataStreamHeader)
                {
                    dsh = item as DataStreamHeader;
                    continue;
                }

                gotEOR = true;
                if (item is WorkstationCommandList)
                {
                    workstationCmdList = item as WorkstationCommandList;

                    foreach (var workstationCmd in workstationCmdList)
                    {
                        if (workstationCmd is ClearUnitCommand)
                        {
                            returnCmdList.Add(workstationCmd);
                        }

                        // WTD command. Add to list of WTD commands. This list is returned to
                        // the caller of this method.
                        else if (workstationCmd is WriteToDisplayCommand)
                        {
                            returnCmdList.Add(workstationCmd);
                            var wtdCommand = workstationCmd as WriteToDisplayCommand;
                            if (wtdCmdList == null)
                            {
                                wtdCmdList = new List <WriteToDisplayCommand>();
                            }
                            wtdCmdList.Add(wtdCommand);
                        }

                        else if (workstationCmd is ReadMdtFieldsCommand)
                        {
                            howRead = HowReadScreen.ReadMdt;
                        }

                        // save screen command. Build response, send back to server.
                        else if (workstationCmd is SaveScreenCommand)
                        {
                            var ra =
                                SaveScreenCommandExt.BuildSaveScreenResponse(VisualItems, Caret);

                            // send response stream back to server.
                            {
                                TelnetConnection.WriteToHost(
                                    logList, ra, ConnectPack.TcpClient.GetStream());
                                gotEOR = false;
                            }
                        }

                        else if (workstationCmd is WriteStructuredFieldCommand)
                        {
                            var wsfCmd = workstationCmd as WriteStructuredFieldCommand;
                            if (wsfCmd.RequestCode == WSF_RequestCode.Query5250)
                            {
                                var ra = Query5250Response.BuildQuery5250Response();

                                // send response stream back to server.
                                {
                                    TelnetConnection.WriteToHost(
                                        logList, ra, ConnectPack.TcpClient.GetStream());
                                    gotEOR = false;
                                }
                            }
                        }
                    }
                }
            }

            return(new Tuple <HowReadScreen?, List <WriteToDisplayCommand>,
                              TelnetLogList, WorkstationCommandList, DataStreamHeader, bool>(
                       howRead, wtdCmdList, logList, returnCmdList, dsh, gotEOR));
        }
        ProcessWorkstationDataStream(
            this ConnectedSocketPack SocketPack, ScreenVisualItems VisualItems,
            CanvasPositionCursor Caret)
        {
            WorkstationCommandList       workstationCmdList = null;
            List <WriteToDisplayCommand> wtdCmdList         = null;
            DataStreamHeader             dsh = null;
            var           returnCmdList      = new WorkstationCommandList();
            HowReadScreen?howRead            = null;
            var           logList            = new TelnetLogList();
            bool          gotEOR             = false;

            while (gotEOR == false)
            {
                // input array is eof. Exit loop if have read a READ workstn command.
                // Otherwise, read from server.
                if (SocketPack.InputArray.IsEof() == true)
                {
                    if (howRead != null)
                    {
                        break;
                    }
                    {
                        var log = SocketPack.InputArray.ReadFromNetworkStream(10, 60);
                        logList.AddItems(log);
                        if (SocketPack.InputArray.IsEof() == true)
                        {
                            break;
                        }
                    }
                }

                // peek at the input stream from server. Classify the data that is next
                // to receive.
                var typeData = ServerDataStream.PeekServerCommand(SocketPack.InputArray);

                // input data not recogizied. Not a 5250 data strem header.
                if (typeData == null)
                {
                    logList.AddItem(Direction.Read, "Unknown data stream data");
                    logList.AddItems(
                        Direction.Read, SocketPack.InputArray.PeekBytes().ToHexReport(16));
                    break;
                }

                if (typeData.Value == TypeServerData.workstationHeader)
                {
                    {
                        var rv = Process5250.GetAndParseWorkstationCommandList(
                            SocketPack.InputArray, SocketPack.Settings);
                        dsh = rv.Item1;
                        workstationCmdList = rv.Item2;
                        logList.AddItems(rv.Item3);
                        gotEOR = rv.Item4;
                    }

                    foreach (var workstationCmd in workstationCmdList)
                    {
                        if (workstationCmd is ClearUnitCommand)
                        {
                            returnCmdList.Add(workstationCmd);
                        }

                        // WTD command. Add to list of WTD commands. This list is returned to the
                        // caller of this method.
                        else if (workstationCmd is WriteToDisplayCommand)
                        {
                            returnCmdList.Add(workstationCmd);
                            var wtdCommand = workstationCmd as WriteToDisplayCommand;
                            if (wtdCmdList == null)
                            {
                                wtdCmdList = new List <WriteToDisplayCommand>();
                            }
                            wtdCmdList.Add(wtdCommand);
                        }

                        else if (workstationCmd is ReadMdtFieldsCommand)
                        {
                            howRead = HowReadScreen.ReadMdt;
                        }

                        // save screen command. Build response, send back to server.
                        else if (workstationCmd is SaveScreenCommand)
                        {
                            var ra = SaveScreenCommandExt.BuildSaveScreenResponse(VisualItems, Caret);

                            // send response stream back to server.
                            {
                                TelnetConnection.WriteToHost(logList, ra, SocketPack.TcpStream);
                                gotEOR = false;
                            }
                        }

                        else if (workstationCmd is WriteStructuredFieldCommand)
                        {
                            var wsfCmd = workstationCmd as WriteStructuredFieldCommand;
                            if (wsfCmd.RequestCode == WSF_RequestCode.Query5250)
                            {
                                var ra = Query5250Response.BuildQuery5250Response();

                                // send response stream back to server.
                                {
                                    TelnetConnection.WriteToHost(logList, ra, SocketPack.TcpStream);
                                    gotEOR = false;
                                }
                            }
                        }
                        else if (workstationCmd is WriteSingleStructuredFieldCommand)
                        {
                        }
                    }
                }
            }
            return(new Tuple <HowReadScreen?, List <WriteToDisplayCommand>,
                              TelnetLogList, WorkstationCommandList, DataStreamHeader, bool>(
                       howRead, wtdCmdList, logList, returnCmdList, dsh, gotEOR));
        }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string itemText = null;

            if (sender is MenuItem)
            {
                itemText = (sender as MenuItem).Header as string;
            }

            if (itemText == "Test")
            {
                var logList     = new TelnetLogList();
                var negSettings = NegotiateSettings.Build5250Settings("SRICHTER", "Steve25");

                var lines = System.IO.File.ReadAllLines("c:\\downloads\\hextext.txt");
                var ba    = ParseHexLines(lines);

                // print the bytes of the response stream as lines of hex text.
                {
                    var rep = ba.ToHexReport(16);
                    logList.AddItem(Direction.Read, "Length:" + ba.Length + " Byte stream bytes:");
                    logList.AddItems(Direction.Read, rep);
                }

                var inputArray      = new NetworkStreamBackedInputByteArray(ba, ba.Length);
                var sessionSettings = new SessionSettings();

                var rv = Process5250.GetAndParseWorkstationCommandList(
                    inputArray, sessionSettings);

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

                // draw the fields and literals on the canvas.
                if (wrkstnCmdList != null)
                {
                    foreach (var workstationCmd in wrkstnCmdList)
                    {
                    }
                }

                foreach (var item in logList)
                {
                    this.Model.RunLog.Add(item.Text);
                }

                return;
            }

            else if (itemText == "Parse server stream")
            {
                var text  = TextBox1.Text;
                var lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                var ba = ParseHexLines(lines);

                // print the bytes of the response stream as lines of hex text.
                {
                    TelnetLogList logList = new TelnetLogList();
                    var           rep     = ba.ToHexReport(16);
                    logList.AddItem(Direction.Read, "Length:" + ba.Length + " Byte stream bytes:");
                    logList.AddItems(Direction.Read, rep);
                }
            }

            else if (itemText == "Parse response stream")
            {
                var logList     = new TelnetLogList();
                var negSettings = NegotiateSettings.Build5250Settings("SRICHTER", "Steve25");

                var lines = System.IO.File.ReadAllLines("c:\\downloads\\hextext.txt");
                var ba    = ParseHexLines(lines);

                // print the bytes of the response stream as lines of hex text.
                {
                    var rep = ba.ToHexReport(16);
                    logList.AddItem(Direction.Read, "Length:" + ba.Length + " Byte stream bytes:");
                    logList.AddItems(Direction.Read, rep);
                }

                Process5250.ParseResponseStream(logList, ba);

                foreach (var item in logList)
                {
                    if (item.NewGroup == true)
                    {
                        this.Model.RunLog.Add("");
                    }
                    this.Model.RunLog.Add(item.Text);
                }

                return;
            }

            else if (itemText == "Exit")
            {
                this.Close();
            }

            else if (itemText == "Settings")
            {
            }

            else if (itemText == "Print")
            {
                LinePrinter.PrintLines(this.Model.RunLog);
            }
            else if (itemText == "Clear log")
            {
                Model.RunLog.Clear();
            }
        }
        /// <summary>
        /// create visual items from the show items and place those visual items on the
        /// canvase.
        /// </summary>
        /// <param name="ShowItemList"></param>
        private OneRowCol PaintScreen_Actual(
            WriteToDisplayCommand WTD_command, TelnetLogList LogList = null)
        {
            IRowCol   curRowCol = new ZeroRowCol(0, 0);
            OneRowCol caret     = null;

            foreach (var order in WTD_command.OrderList)
            {
                if (LogList != null)
                {
                    LogList.AddItem(Direction.Read, "PaintScreen. " + order.ToString());
                }

                if (order is TextDataOrder)
                {
                    var tdo = order as TextDataOrder;
                    if (tdo.IsEmpty() == false)
                    {
                        PaintScreen_ApplyTextDataOrder(tdo, curRowCol);
                    }
                    curRowCol = tdo.Advance(curRowCol);
                }

                else if (order is SetBufferAddressOrder)
                {
                    var sba = order as SetBufferAddressOrder;
                    curRowCol = sba.GetRowCol(this.ScreenDim).ToZeroRowCol();
                }

                else if (order.OrderCode == WtdOrder.InsertCursor)
                {
                    var ico = order as InsertCursorOrder;
                    caret = ico.RowCol;
                }

                else if (order is StartFieldOrder)
                {
                    var sfo = order as StartFieldOrder;
                    {
                        var showText = new String(' ', sfo.LL_Length);
                        var vtb      = new VisualTextBlock(
                            showText, (ZeroRowCol)curRowCol, sfo.AttrByte,
                            this.CanvasDefn.CharBoxDim, this.CanvasDefn.KernDim,
                            this.CanvasDefn.FontDefn);
                        var iMore = vtb as IVisualItemMore;
                        var node  = iMore.InsertIntoVisualItemsList(this.VisualItems);
                        iMore.AddToCanvas(this);

                        vtb.SetupFieldItem(
                            sfo, this.CanvasDefn.CharBoxDim, this.CanvasDefn.KernDim);
                    }
                    curRowCol = curRowCol.Advance(1);
                }

                else if (order is RepeatToAddressOrder)
                {
                    var rao = order as RepeatToAddressOrder;
                    this.VisualItems.ApplyRepeatToAddressOrder(curRowCol, rao, this);
                    var lx = rao.RepeatLength((ZeroRowCol)curRowCol);
                    curRowCol = curRowCol.Advance(lx);
                }

                else if (order is EraseToAddressOrder)
                {
                    var eao = order as EraseToAddressOrder;
                    this.VisualItems.ApplyEraseToAddressOrder(curRowCol, eao, this);
                    var lx = eao.EraseLength((ZeroRowCol)curRowCol);
                    curRowCol = curRowCol.Advance(lx);
                }
            }
            return(caret);
        }
 private static void WriteToHost(
     TelnetLogList LogList, TelnetCommand Stmt, NetworkStream NetStream)
 {
     WriteToHost(LogList, Stmt.ToBytes(), NetStream);
 }