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 static IEnumerable <string> ReportParseResults( DataStreamHeader dsHeader, WorkstationCommandList wrkstnCmdList, ResponseItemList responseList, TelnetCommandList telList) { var report = new List <string>(); if (dsHeader != null) { report.AddRange(dsHeader.ToColumnReport()); } if (wrkstnCmdList != null) { report.AddRange(wrkstnCmdList.ToColumnReport()); } if (responseList != null) { report.AddRange(responseList.ReportResponseItems()); } if (telList != null) { report.AddRange(telList.ToColumnReport()); } return(report); }
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 byte[] BuildReadScreenResponse(ScreenContent ScreenContent) { var ra = new ByteArrayBuilder(); // data stream header. { var buf = DataStreamHeader.Build(50, TerminalOpcode.ReadScreen, 0, 0); ra.Append(buf); } // screen regeneration buffer. each byte on the screen. { var buf = ScreenContent.GetRegenerationBuffer(); ra.Append(buf); } // update length of response data stream. { var wk = new ByteArrayBuilder(); wk.AppendBigEndianShort((short)ra.Length); ra.CopyToBuffer(wk.ToByteArray(), 0); } // IAC EOR { ra.Append(EOR_Command.Build()); } return(ra.ToByteArray()); }
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)); }
/// <summary> /// build the entire 5250 query response stream. Starting from data stream header /// and concluding with IAC EOR. /// </summary> /// <returns></returns> public static byte[] BuildQuery5250Response() { var ra = new ByteArrayBuilder(); // data stream header. { var buf = DataStreamHeader.Build(50, DataStreamOpcode.Noop, 0, 0); ra.Append(buf); } // response header. send the special AID code - 0x88. { var buf = ResponseHeader.Build(0, 0, AidKey.Query5250Reply); ra.Append(buf); } // build the 5250 query response. { byte byteZero = 0x00; ra.AppendBigEndianShort(58); // LL. total length of structured field. ra.Append(new byte[] { 0xd9, 0x70, 0x80 }); // 5250 query response ra.Append(new byte[] { 0x06, 0x00 }); // control unit code. ra.Append(new byte[] { 0x01, 0x03, 0x00 }); // code release level. ra.Append(byteZero.Repeat(16)); // 16 bytes of null ra.Append(0x01); // 01 - display station ra.Append("3180002".ToEbcdicBytes()); // machine type and model. ra.Append(0x02); // keyboard id ra.Append(0x00); // extended keyboard id ra.Append(0x00); // reserve ra.Append(new byte[] { 0x00, 0x61, 0x50, 0x00 }); // device serial number. ra.AppendBigEndianShort(256); // max number input fields. ra.Append(0x00); // control unit customization ra.Append(new byte[] { 0x00, 0x00 }); // reserved ra.Append(new byte[] { 0x18, 0x11, 0x00, 0x00, 0x00 }); // ra.Append(byteZero.Repeat(7)); // 7 bytes of null. } // update length of response data stream. { var wk = new ByteArrayBuilder(); wk.AppendBigEndianShort((short)ra.Length); ra.CopyToBuffer(wk.ToByteArray(), 0); } // IAC EOR { ra.Append(EOR_Command.Build()); } return(ra.ToByteArray()); }
public static byte[] BuildSaveScreenResponse(ScreenContent ScreenContent) { var ra = new ByteArrayBuilder(); // data stream header. { var buf = DataStreamHeader.Build(50, TerminalOpcode.SaveScreen, 0, 0); ra.Append(buf); } // restore screen workstation command. { var cmd = new RestoreScreenCommand(); ra.Append(cmd.ToBytes()); } // clear unit command. if (ScreenContent.ScreenDim.GetIsWideScreen( ) == true) { var cmd = new ClearUnitAlternateCommand(0x00); ra.Append(cmd.ToBytes()); } else { var cmd = new ClearUnitCommand(); ra.Append(cmd.ToBytes()); } // WTD command. { var ordersByteStream = ScreenContent.BuildOrderStream( ); var buf = WriteToDisplayCommand.Build(0x00, 0x18, ordersByteStream); ra.Append(buf); } // update length of response data stream. { var wk = new ByteArrayBuilder(); wk.AppendBigEndianShort((short)ra.Length); ra.CopyToBuffer(wk.ToByteArray(), 0); } // IAC EOR { ra.Append(EOR_Command.Build()); } return(ra.ToByteArray()); }
/// <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)); }
/// <summary> /// no longer used. called WorkstationCommandListExt.ProcessAndPaint, which /// itself is not used. /// </summary> /// <param name="VisualItems"></param> /// <param name="Caret"></param> /// <returns></returns> public static byte[] BuildSaveScreenResponse( ScreenVisualItems VisualItems, CanvasPositionCursor Caret) { var ra = new ByteArrayBuilder(); // data stream header. { var buf = DataStreamHeader.Build(50, TerminalOpcode.SaveScreen, 0, 0); ra.Append(buf); } // restore screen workstation command. { var cmd = new RestoreScreenCommand(); ra.Append(cmd.ToBytes()); } // clear unit command. { var cmd = new ClearUnitCommand(); ra.Append(cmd.ToBytes()); } // WTD command. { var ordersByteStream = VisualItems.BuildOrderStream(Caret); var buf = WriteToDisplayCommand.Build(0x00, 0x18, ordersByteStream); ra.Append(buf); } // update length of response data stream. { var wk = new ByteArrayBuilder(); wk.AppendBigEndianShort((short)ra.Length); ra.CopyToBuffer(wk.ToByteArray(), 0); } // IAC EOR { ra.Append(EOR_Command.Build()); } return(ra.ToByteArray()); }
/// <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 <ResponseItemList, string> ParseResponseStream( InputByteArray InputArray, ResponseHeader ResponseHeader = null) { var responseItemList = new ResponseItemList(); string errmsg = null; var writeStream = new ByteArrayBuilder(); DataStreamHeader dsHeader = null; ResponseHeader responseHeader = ResponseHeader; // stream starts with data stream header. if (responseHeader == null) { var rv = DataStreamHeader.Factory(InputArray); dsHeader = rv.Item1; responseItemList.Add(dsHeader); errmsg = dsHeader.Errmsg; // next is the response header. if (errmsg == null) { responseHeader = new ResponseHeader(InputArray); responseItemList.Add(responseHeader); errmsg = responseHeader.Errmsg; } } // look for 5250 query reply. if ((errmsg == null) && (responseHeader.AidKey != null) && (responseHeader.AidKey.Value == AidKey.Query5250Reply)) { var queryResp = new Query5250Response(InputArray); if (queryResp.Errmsg == null) { responseItemList.Add(queryResp); } } // repeating instances of sbaOrder, textDataOrder pairs. while (true) { var telCmd = TelnetCommand.Factory(InputArray); if (telCmd != null) { continue; } // 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; } responseItemList.Add(orderPair); } return(new Tuple <ResponseItemList, string>(responseItemList, errmsg)); }
private OpenPdfDocument PrintToPdf( DataStreamHeader dsh, ControlFunctionList funcList, OpenPdfDocument OpenDoc = null) { OpenPdfDocument openDoc = OpenDoc; if (dsh is StartPrinterFileDataStreamHeader) { var docPath = "c:\\downloads\\printout.pdf"; openDoc = new OpenPdfDocument(docPath); } else if (dsh is PrinterDataStreamHeader) { if (openDoc == null) { var docPath = "c:\\downloads\\printout.pdf"; openDoc = new OpenPdfDocument(docPath); } if (funcList != null) { foreach (var func in funcList) { if (func.ControlCode == ControlFunctionCode.CarriageReturn) { openDoc.ApplyCarriageReturn(); } else if (func.ControlCode == ControlFunctionCode.NewLine) { openDoc.ApplyNewLine(); } else if (func.ControlCode == ControlFunctionCode.PresentationPosition) { var presentPos = func as PresentationPositionControlFunction; if (presentPos.Direction == PresentationPositionDirection.Horizontal) { openDoc.ApplyPos(presentPos.PositionValue - 1); } } else if (func.ControlCode == ControlFunctionCode.Text) { var textFunc = func as TextControlFunction; openDoc.ApplyText(textFunc.TextBytes.EbcdicBytesToString()); } else if ((func.ControlCode == ControlFunctionCode.Undocumented2) || (func.ControlCode == ControlFunctionCode.Undocumented3)) { openDoc.ApplyText(" "); } else if (func.ControlCode == ControlFunctionCode.FormFeed) { openDoc.ApplyNewPage(); } } if (funcList.IsSingleNullList() == true) { openDoc.CloseDoc(); openDoc = null; } } } return(openDoc); }
/// <summary> /// write buffer contents to log file. /// </summary> /// <param name="Direction"></param> /// <param name="Buffer"></param> /// <param name="Length"></param> public void Write(string Direction, byte[] Buffer, int Length) { var lines = new List <string>(); this.Counter += 1; var inputArray = new InputByteArray(Buffer, Length); while (inputArray.IsEof() == false) { // peek to see if the current byte is a telnet command. var escapeCmd = inputArray.PeekNextByte().ToTelnetCommandCode(); // peek to see if the current bytes are an IBM5250 datastream header. DataStreamHeader dsh = null; string errmsg = null; { var rv = DataStreamHeader.Factory(inputArray); dsh = rv.Item1; errmsg = rv.Item2; } // current bytes are an ibm5250 datastream header. ( page 2-4 of rfc 1205 ) // store this header as the "current datastream header". When there is a // current data stream header the code will match the current bytes against // ibm5250 data stream commands. if (errmsg == null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + dsh.ToString()); this.CurrentDataStreamHeader = dsh; continue; } // currently in a 5250 data stream header. if (this.CurrentDataStreamHeader != null) { var dataStreamCommand = WorkstationCommandBase.ParseFactory(inputArray); if (dataStreamCommand != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + dataStreamCommand.ToString()); // all the data stream command ToString methods provide enough info. // But for the WriteToDisplayCommand, list out the ToString contents of // the WtdOrders of that command. if (dataStreamCommand is WriteToDisplayCommand) { var wtdCommand = dataStreamCommand as WriteToDisplayCommand; foreach (var order in wtdCommand.OrderList) { lines.Add(order.ToString()); } } continue; } } { var stmt = TelnetCommand.Factory(inputArray); if (stmt != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + stmt.ToString()); continue; } } if ((escapeCmd != null) && (escapeCmd.Value == CommandCode.ESCAPE)) { var rv = TerminalVt100Statement.Factory(inputArray); var vtStmt = rv.Item1; var otStmt = rv.Item2; lines.Add( Direction + " " + this.Counter.ToString() + " " + vtStmt.ToString()); if (otStmt != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + otStmt.ToString()); } continue; } { var remBytes = inputArray.GetBytesToEnd(); lines.Add(Direction + " " + this.Counter.ToString() + " Raw bytes follow:"); LogFile_WriteRawBytes(remBytes, lines); } } WriteTextLines(lines); }
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)); }
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 byte[] BuildResponseByteStream( ScreenContent ScreenContent, AidKey AidKey, OneRowCol CaretRowCol, HowReadScreen?HowRead = null) { var ba = new ByteArrayBuilder(); // what to read from the screen. HowReadScreen howRead = HowReadScreen.ReadAllInput; if (HowRead != null) { howRead = HowRead.Value; } { var buf = DataStreamHeader.Build(99, TerminalOpcode.PutGet); ba.Append(buf); } // location of caret on the canvas. { var rowCol = CaretRowCol.ToParentRelative(ScreenContent); var buf = ResponseHeader.Build(rowCol as OneRowCol, AidKey); ba.Append(buf); } foreach (var dictItem in ScreenContent.FieldDict) { ContentField responseField = null; var contentItem = dictItem.Value; // process only ContentField. if (contentItem is ContentField) { var contentField = contentItem as ContentField; responseField = contentField; } // only process the first of a continued entry field. if ((responseField != null) && (responseField is ContinuedContentField)) { var contContentField = responseField as ContinuedContentField; if (contContentField.ContinuedFieldSegmentCode != ContinuedFieldSegmentCode.First) { responseField = null; } } if ((howRead == HowReadScreen.ReadMdt) && (responseField != null) && (responseField.GetModifiedFlag(ScreenContent) == false)) { responseField = null; } if (responseField != null) { IRowCol rowCol = responseField.RowCol.ToOneRowCol().AdvanceRight(); { rowCol = rowCol.ToParentRelative(ScreenContent); var buf = SetBufferAddressOrder.Build(rowCol as OneRowCol); ba.Append(buf); } { var buf = TextDataOrder.Build(responseField.GetFieldShowText(ScreenContent)); ba.Append(buf); } } } // update length of response data stream. var ra = ba.ToByteArray(); { var wk = new ByteArrayBuilder(); wk.AppendBigEndianShort((short)ra.Length); Array.Copy(wk.ToByteArray(), ra, 2); } return(ra); }
public DataStreamHeaderMessage(DataStreamHeader DataStreamHeader) { this.DataStreamHeader = DataStreamHeader; }
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)); }
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> /// write buffer contents to log file. /// </summary> /// <param name="Direction"></param> /// <param name="Buffer"></param> /// <param name="Length"></param> public void Write(string Direction, byte[] Buffer, int Length) { lock (this.LockFlag) { this.Counter += 1; if (this.Counter > 7) { int cc = 3; } InputByteArray inputArray = new InputByteArray(Buffer, Length); while (inputArray.IsEof() == false) { TelnetStatement stmt = null; var escapeCmd = inputArray.PeekNextByte().ToTelnetCommand(); var dsHeader = DataStreamHeader.Factory(inputArray); IBM5250DataStreamCommand dataStreamCommand; if (dsHeader != null) { var s1 = dsHeader.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); this.CurrentDataStreamHeader = dsHeader; continue; } // an ibm 5250 data stream command. else if ((this.CurrentDataStreamHeader != null) && ((dataStreamCommand = IBM5250DataStreamCommand.Factory(inputArray)) != null)) { var s1 = dataStreamCommand.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); continue; } else if ((stmt = TelnetStatement.Factory(inputArray)) != null) { var s1 = stmt.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); continue; } else if ((escapeCmd != null) && (escapeCmd.Value == TelnetCommand.ESCAPE)) { var rv = TerminalVt100Statement.Factory(inputArray); var vtStmt = rv.Item1; var otStmt = rv.Item2; System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " " + vtStmt.ToString() + Environment.NewLine); if (otStmt != null) { System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " " + otStmt.ToString() + Environment.NewLine); } continue; } else { var remBytes = inputArray.GetBytesToEnd(); System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " Raw bytes follow:" + Environment.NewLine); LogFile_WriteRawBytes(remBytes, this.FilePath); } } } }