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)); }
/// <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)); }
public void EntryPoint() { this.ThreadEndedEvent.Reset(); var master = BaseMaster_InitialSetup(); 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 WorkstationCommandListMessage) { var cmdList = (message as WorkstationCommandListMessage).WorkstationCommandList; var rv = this.BaseMaster.Apply( cmdList, this.ToThread, this.PaintThread, this.LogList); bool wtdApplied = rv.Item1; this.BaseMaster = rv.Item2; // signal the paint thread to paint the canvas with the screen // content block. if (wtdApplied == true) { this.BaseMaster.PostAidKey = this.PostAidKey; this.PostAidKey = null; var masterCopy = this.BaseMaster.Copy(); var content = masterCopy.GetWorkingContentBlock(); var paintMessage = new PaintCanvasMessage(content); this.PaintThread.PostInputMessage(paintMessage); } // send another copy of the screenContent to the match thread. // Match thread will match the screen to the screen definitions. // Looking for screen id, hover code, help text, etc. if (wtdApplied == true) { var masterCopy = this.BaseMaster.Copy(); var content = masterCopy.GetWorkingContentBlock(); this.MatchThread.PostInputMessage( ThreadMessageCode.MatchScreenContentToScreenDefn, content); } } else if (message is KeyboardInputMessage) { var kbInput = message as KeyboardInputMessage; if (kbInput.Text != null) { master = this.BaseMaster.GetWorkingContentBlock(); master.ApplyInput(kbInput); } } // message sent from UI thread when the caret is moved. See the // ItemCanvas class. else if (message is CaretMoveMessage) { var caretMove = message as CaretMoveMessage; master = this.BaseMaster.GetWorkingContentBlock(); master.ApplyCaretMove(caretMove); } // enter key has been pressed. UI thread sent the message to this // Master thread. Master thread relays the message to the // ToThread along with a copy of the master ScreenContent. ToThread // creates and sends the response data stream based on the master // screen content. else if (message is AidKeyResponseMessage) { var msg = message as AidKeyResponseMessage; this.PostAidKey = msg.AidKey; msg.ScreenContent = this.BaseMaster.Copy(); ToThread.PostInputMessage(msg); } else if (message is GeneralThreadMessage) { var generalMessage = message as GeneralThreadMessage; switch (generalMessage.MessageCode) { case ThreadMessageCode.ClearLog: { this.LogList.Clear(); break; } // report visual items. Add screenContent to the message and // send it to PaintThread. case ThreadMessageCode.ReportVisualItems: { generalMessage.ScreenContent = this.BaseMaster.Copy(); PaintThread.PostInputMessage(generalMessage); break; } } } else if (message is ExchangeMessage) { var exchangeMessage = message as ExchangeMessage; if (exchangeMessage.MessageCode == ThreadMessageCode.GetScreenContent) { master = this.BaseMaster.GetWorkingContentBlock(); var masterCopy = master.Copy(); exchangeMessage.PostReplyMessage(masterCopy); } } } } } finally { // in case anyone waiting for this thread to end. Signal the ended event. ThreadEndedEvent.Set(); } }