ParseWorkstationCommandList( InputByteArray InputArray, SessionSettings SessionSettings) { var wrkstnCmdList = new WorkstationCommandList(); DataStreamHeader dsh = null; bool gotEOR = false; bool needMoreBytes = false; string errmsg = null; if (InputArray.IsDataStreamHeader()) { var rv = DataStreamHeader.Factory(InputArray); dsh = rv.Item1; errmsg = rv.Item2; } bool lastCmdWasTelnet_EOR = false; bool gotWorkstationCommand = true; gotEOR = false; while ((InputArray.IsEof( ) == false) && (gotEOR == false) && (gotWorkstationCommand == true)) { // no input data to process. lastCmdWasTelnet_EOR = false; gotWorkstationCommand = false; // check for IAC EOR var telCode = InputArray.PeekTelnetCommandCode(CommandCode.EOR); if (telCode != null) { var telCmd = InputArray.NextTelnetCommand(); lastCmdWasTelnet_EOR = true; gotEOR = true; } // process the input as workstation data stream commands. else { var rv = ParseAndProcessWorkstationCommand(InputArray); var workstationCommand = rv.Item1; var howRead = rv.Item2; if (workstationCommand != null) { wrkstnCmdList.Add(workstationCommand); gotWorkstationCommand = true; } } } // read available bytes from input stream. if (InputArray.IsEof() && (lastCmdWasTelnet_EOR == false)) { needMoreBytes = true; } return(new Tuple <DataStreamHeader, WorkstationCommandList, bool, bool>( dsh, wrkstnCmdList, gotEOR, needMoreBytes)); }
ParseDataStream( InputByteArray inputArray, SessionSettings sessionSettings, string DataStreamName, TypeTelnetDevice?TypeDevice = null) { var accumCmdList = new WorkstationCommandList(); var responseItemList = new ResponseItemList(); TelnetCommandList telList = null; ResponseHeader curRespHeader = null; ResponseHeader nextRespHeader = null; DataStreamHeader dsh = null; ControlFunctionList funcList = null; bool didParse = true; while ((didParse == true) && (inputArray.RemainingLength > 0)) { didParse = false; curRespHeader = nextRespHeader; nextRespHeader = null; if ((didParse == false) && (dsh == null)) { var telCmd = inputArray.NextTelnetCommand(); if (telCmd != null) { if (telList == null) { telList = new TelnetCommandList(); } telList.Add(telCmd); var s1 = telCmd.ToString(); didParse = true; } } // parse the data stream header. if ((didParse == false) && (dsh == null)) { var code = inputArray.PeekDataStreamCode(); if (code != null) { var rv = DataStreamHeader.Factory(inputArray); dsh = rv.Item1; didParse = true; // update the type of device depending on data stream header stream // code. if ((TypeDevice == null) && (dsh != null) && (dsh.Errmsg == null)) { TypeDevice = dsh.StreamCode.ToTypeTelnetDevice(); } } } // parse as a sequence of workstation commands. if (didParse == false) { if ((TypeDevice == null) || (TypeDevice.Value != TypeTelnetDevice.Printer)) { var rv = Process5250.ParseWorkstationCommandList( inputArray, sessionSettings); var anotherDsh = rv.Item1; var wrkstnCmdList = rv.Item2; if ((anotherDsh != null) && (anotherDsh.Errmsg == null)) { didParse = true; dsh = anotherDsh; } // draw the fields and literals on the canvas. if (wrkstnCmdList?.Count > 0) { accumCmdList.Append(wrkstnCmdList); didParse = true; } } } // parse as sequence of SCS control functions. ( printer data stream ). if (didParse == false) { if ((TypeDevice == null) || (TypeDevice.Value == TypeTelnetDevice.Printer)) { var rv = ControlFunctionList.ParseDataStream(inputArray); if (rv.Item1?.Count > 0) { didParse = true; funcList = rv.Item1; } } } // parse as response stream header. if ((didParse == false) && (ResponseHeader.IsResponseHeader(inputArray).Item1 == true)) { curRespHeader = new ResponseHeader(inputArray); didParse = true; responseItemList.Add(curRespHeader); var rv = Response5250.ParseResponseStream(inputArray, curRespHeader); responseItemList.Append(rv.Item1); } } return(new Tuple <WorkstationCommandList, ResponseItemList, DataStreamHeader, TelnetCommandList, ControlFunctionList>( accumCmdList, responseItemList, dsh, telList, funcList)); }
/// <summary> /// parse from current byte forward in the InputArray. Will either parse a /// telnet command or a workstation command. /// Route what was parsed to either the FromQueue or the MasterThread. Send to /// the FromQueue if still receiving telnet commands. Otherwise, send to the /// MasterThread. /// </summary> /// <param name="InputArray"></param> /// <param name="WipCmdList"></param> /// <returns></returns> private Tuple <WorkstationCommandList, bool> ParseAndPostInputArray( InputByteArray InputArray, WorkstationCommandList WipCmdList) { WorkstationCommandList wipCmdList = null; bool gotSomething = false; if (WipCmdList == null) { var telCmd = InputArray.NextTelnetCommand(); if (telCmd != null) { this.TelnetQueue.Enqueue(telCmd); gotSomething = true; } } if (gotSomething == false) { // peek at the input stream from server. Classify the data that is next // to receive. var typeData = ServerDataStream.PeekServerCommand(InputArray); if (typeData != null) { var rv = Process5250.ParseWorkstationCommandList( InputArray, this.SessionSettings); var dsh = rv.Item1; var workstationCmdList = rv.Item2; var gotEOR = rv.Item3; var needMoreBytes = rv.Item4; // update connectionComplete flag and typeDevice depending on the stream // code of the datastream header. if ((this.ConnectionComplete == false) && (dsh != null) && (dsh.StreamCode != null)) { this.ConnectionComplete = true; if (dsh.StreamCode.Value == DataStreamCode.Terminal) { this.TypeDevice = TypeTelnetDevice.Terminal; } else { this.TypeDevice = TypeTelnetDevice.Printer; } // post message to telnet queue so that the ConnectionThread on the // other end will know to shutdown. var message = new TelnetDeviceAttrMessage(this.TypeDevice.Value); this.TelnetQueue.Enqueue(message); } // got data stream header. if (dsh != null) { if (this.ConnectionComplete == false) { this.TelnetQueue.Enqueue(dsh); } else { var message = new DataStreamHeaderMessage(dsh); PostToProcessQueue(message); } gotSomething = true; } if (workstationCmdList != null) { gotSomething = true; } // accum the workstationCmdList if (WipCmdList == null) { wipCmdList = workstationCmdList; } else { wipCmdList = WipCmdList; wipCmdList.AddRange(workstationCmdList); } // got EOR. store the now completed workstationCmdList. if ((gotEOR == true) && (wipCmdList != null)) { var msg = new WorkstationCommandListMessage(wipCmdList); PostToProcessQueue(msg); gotSomething = true; wipCmdList = null; } } } return(new Tuple <WorkstationCommandList, bool>(wipCmdList, gotSomething)); }