TelnetConnectAndNegotiate( string Host, NegotiateSettings NegotiateSettings, ConcurrentMessageQueue TelnetQueue, ToThread ToThread) { var sessionSettings = new SessionSettings(); TelnetLogList logList = null; bool breakLoop = false; // loop reading from NetworkStream and processing the telnet command. // loop until break flag is set. while (breakLoop == false) { var item = TelnetQueue.WaitAndPeek(); if ((item is TelnetCommand) == false) { break; } var telCmd = TelnetQueue.WaitAndDequeue() as TelnetCommand; byte[] responseBytes = null; { var rv = ProcessTelnetCommand(telCmd, NegotiateSettings); var cx = rv.Item1; responseBytes = rv.Item2; } if ((responseBytes != null) && (responseBytes.Length > 0)) { var dataMessage = new SendDataMessage(responseBytes); ToThread.PostInputMessage(dataMessage); } } return(new Tuple <SessionSettings, TelnetLogList>( sessionSettings, logList)); }
private void MenuItem_Click(object sender, RoutedEventArgs e) { string itemText = null; string tagText = null; var senderItem = sender as MenuItem; if (sender is MenuItem) { itemText = (sender as MenuItem).Header as string; } tagText = senderItem.Tag as string; if (itemText == "Telnet") { var devName = "STEVE26"; var termType = "IBM-3477-FC"; // IBM-3477-FC, IBM-3179-2 TelnetInitialConnect( this.Model.SystemName, this.Model.AutoConnect, this.Model.DeviceName, this.Model.TerminalType); } else if (itemText == "Printer") { TelnetPrinterConnect(this.Model.SystemName); } else if (itemText == "Exit") { this.canAutoClose = true; this.Close(); } else if (itemText == "Test") { var s2 = this.Model.ScreenDefnPath; Debug.Print("ScreenDefnPath:" + s2); } else if (itemText == "Read xml") { string xmlPath = "c:\\skydrive\\c#\\TextCanvasLib\\xmlfile1.xml"; var items = ScreenDocReader.ReadDoc(xmlPath); OneRowCol caret = null; this.Model.TelnetCanvas.PaintScreen(items, caret); } else if (itemText == "Print log") { LinePrinter.PrintLines(this.Model.RunLog); } else if (itemText == "Clear log") { MainWindow.LogFile.ClearFile(); this.Model.RunLog.Clear(); this.PaintThread.PostInputMessage(ThreadMessageCode.ClearLog); this.MasterThread.PostInputMessage(ThreadMessageCode.ClearLog); this.ToThread.PostInputMessage(ThreadMessageCode.ClearLog); this.FromThread.PostInputMessage(ThreadMessageCode.ClearLog); } else if (itemText == "view special") { var specialPath = "c:\\downloads\\specialLog.txt"; string exePath = Environment.ExpandEnvironmentVariables(@"%windir%\system32\notepad.exe"); Process.Start(exePath, specialPath); } else if (itemText == "Report canvas items") { MasterThread.PostInputMessage(ThreadMessageCode.ReportVisualItems); var visualItems = this.Model.TelnetCanvas.VisualItems; var report = wtdReportExt.PrintVisualItems(visualItems); this.Model.RunLog.AddRange(report); } else if (itemText == "Send data") { var dataBytes = new byte[] { 0x00, 0x0a, 0x12, 0xa0, 0x01, 0x02, 0x04, 0x00, 0x00, 0x01, 0xff, 0xef }; dataBytes = new byte[] { 0x00, 0x0A, 0x12, 0xA0, 0x01, 0x02, 0x04, 0x00, 0x00, 0x01, 0xFF, 0xEF }; var dataMessage = new SendDataMessage(dataBytes); this.ToThread.PostInputMessage(dataMessage); } // capture the currently matched screen. else if (tagText == "Capture") { if (this.Model.MatchScreenDefn == null) { MessageBox.Show("no matched screen to capture"); } else { // send message to master thread to get the current screen content. var msg = new ExchangeMessage(ThreadMessageCode.GetScreenContent); this.MasterThread.PostInputMessage(msg); msg.WaitReplyEvent(); var content = msg.ReplyMessage as ScreenContent; // send message to capture thread telling it to capture the current // screen. var captureMessage = new CaptureContentMessage( this.Model.CaptureFolderPath, this.Model.CaptureAuto, this.Model.MatchScreenDefn, content); this.CaptureThread.PostInputMessage(captureMessage); } } else if (tagText == "CaptureViewer") { var window = new CaptureViewerWindow(); window.CaptureFolderPath = this.Model.CaptureFolderPath; window.Show(); } }
public void EntryPoint() { this.ThreadEndedEvent.Reset(); try { // loop receiving from the server until: // - the foreground thread wants to shutdown the connection. It has set // the ShutdownFlag event. while ((ShutdownFlag.State == false) && (this.ConnectionFailedEvent.State == false)) { var message = InputQueue.WaitAndDequeue( this.ShutdownFlag.EventObject, this.ConnectionFailedEvent.EventObject); if (message != null) { if (message is DataStreamHeaderMessage) { var dshMessage = message as DataStreamHeaderMessage; var dataBytes = new byte[] { 0x00, 0x0A, 0x12, 0xA0, 0x01, 0x02, 0x04, 0x00, 0x00, 0x01, 0xFF, 0xEF }; var dataMessage = new SendDataMessage(dataBytes); this.ToThread.PostInputMessage(dataMessage); } else if (message is DataStreamHeader) { var dsh = message as DataStreamHeader; var dataBytes = new byte[] { 0x00, 0x0A, 0x12, 0xA0, 0x01, 0x02, 0x04, 0x00, 0x00, 0x01, 0xFF, 0xEF }; var dataMessage = new SendDataMessage(dataBytes); this.ToThread.PostInputMessage(dataMessage); } else if (message is PrinterDataBytesMessage) { var dataMessage = message as PrinterDataBytesMessage; var dataBytes = dataMessage.DataBytes; var dataBytesLength = dataBytes.Length; // remove the IAC EOR from the end of the stream. // ( the data stream may end with a partial control function which // is continued in the next data stream. Do not want to // confuse the FF EF EOR bytes as data bytes of the possible // incomplete control function. ) { var endBytes = dataBytes.Tail(2); var cmdCode = endBytes.ParseTelnetCommandCode(); if ((cmdCode != null) && (cmdCode.Value == CommandCode.EOR)) { dataBytesLength -= 2; // dataBytes = dataBytes.SubArray(0, dataBytesLength); } } // there are remaining bytes from the previous dataBytes message. // insert these remaining bytes after the dataStreamHeader. if (this.RemainingBytes != null) { dataBytes = dataBytes.SubArray(0, dataBytesLength); var headerLength = dataBytes.GetDataStreamHeaderLength(); if (headerLength != null) { dataBytes = dataBytes.Insert(headerLength.Value, this.RemainingBytes); dataBytesLength += this.RemainingBytes.Length; } } // parse the bytes. var rv = ServerDataStream.ParseByteArray(dataBytes, dataBytesLength); var wrkstnCmdList = rv.Item1; var responseList = rv.Item2; var dsh = rv.Item3; var telList = rv.Item4; var funcList = rv.Item5; // bytes at the end of the data stream that were not recognized as // complete SCS function codes. Save now and add to the front of the // next data stream that arrives. ( add to front after the data // stream header. ) this.RemainingBytes = rv.Item6; if (1 == 1) { this.OpenDoc = PrintToPdf(dsh, funcList, this.OpenDoc); } var respBytes = new byte[] { 0x00, 0x0A, 0x12, 0xA0, 0x01, 0x02, 0x04, 0x00, 0x00, 0x01, 0xFF, 0xEF }; var respMessage = new SendDataMessage(respBytes); this.ToThread.PostInputMessage(respMessage); } else if (message is GeneralThreadMessage) { var generalMessage = message as GeneralThreadMessage; switch (generalMessage.MessageCode) { case ThreadMessageCode.ClearLog: { break; } } } else if (message is ExchangeMessage) { var exchangeMessage = message as ExchangeMessage; if (exchangeMessage.MessageCode == ThreadMessageCode.GetScreenContent) { } } } } } finally { // in case anyone waiting for this thread to end. Signal the ended event. ThreadEndedEvent.Set(); } }