public string WaitForKey(TNServerScreen currentScreen) { //Console.WriteLine("--wait for key"); byte[] data = null; bool screen = false; do { do { while (!mDataSemaphore.Acquire(1000)) { if (mSocket == null) { throw new TN3270ServerException("Connection dropped"); } } data = (byte[])mData.Dequeue(); }while (data == null); if (data[0] == 255 && data[1] == 253) { // assume do/will string for (int i = 0; i < data.Length; i++) { if (data[i] == 253) { data[i] = 251; // swap DO to WILL Console.WriteLine("DO " + data[i + 1]); } else if (data[i] == 251) { data[i] = 253; // swap WILL to DO Console.WriteLine("WILL " + data[i + 1]); } } mSocket.Send(data); screen = false; } else { screen = true; } }while (!screen); return(currentScreen.HandleTN3270Data(data, data.Length)); }
/// <summary> /// Refresh the current screen. If timeout > 0, it will wait for /// this number of milliseconds. /// If waitForValidScreen is true, it will wait for a valid screen, otherwise it /// will return immediately that any screen data is visible /// </summary> /// <param name="waitForValidScreen"></param> /// <param name="timeoutMS">The time to wait in ms</param> /// <returns></returns> public bool Refresh(bool waitForValidScreen, int timeoutMS) { long start = DateTime.Now.Ticks / (10 * 1000); long end = start + timeoutMS; if (currentConnection == null) { throw new TNHostException("TNEmulator is not connected", "There is no currently open TN3270 connection", null); } if (sout != null && Debug == true) { sout.WriteLine("Refresh::Refresh(" + waitForValidScreen + ", " + timeoutMS + "). FastScreenMode=" + this.mConnectionConfiguration.FastScreenMode); } do { if (waitForValidScreen) { bool run = false; int timeout = 0; do { timeout = (int)(end - (DateTime.Now.Ticks / 10000)); if (timeout > 0) { if (sout != null && this.Debug) { sout.WriteLine("Refresh::Acquire(" + timeout + " milliseconds). unsafe Count is currently " + semaphore.Count); } run = semaphore.Acquire(Math.Min(timeout, 1000)); if (!IsConnected) { throw new TNHostException("The TN3270 connection was lost", this.currentConnection.DisconnectReason, null); } if (run) { if (sout != null && this.Debug) { sout.WriteLine("Refresh::return true at line 279"); } return(true); } else { } } }while (!run && timeout > 0); if (sout != null && this.Debug) { sout.WriteLine("Refresh::Timeout or acquire failed. run= " + run + " timeout=" + timeout); } } if (this.mConnectionConfiguration.FastScreenMode || this.KeyboardLocked == 0) { // Store screen in screen database and identify it this.DisposeOfCurrentScreenXML(); // Force a refresh currentScreenXML = null; if (sout != null && this.Debug) { sout.WriteLine("Refresh::Timeout, but since keyboard is not locked or fastmode=true, return true anyway"); } return(true); } else { System.Threading.Thread.Sleep(10); } }while (DateTime.Now.Ticks / 10000 < end); if (sout != null) { sout.WriteLine("Refresh::Timed out (2) waiting for a valid screen. Timeout was " + timeoutMS); } if (Config.FastScreenMode == false && Config.ThrowExceptionOnLockedScreen && this.KeyboardLocked != 0) { throw new ApplicationException("Timeout waiting for new screen with keyboard inhibit false - screen present with keyboard inhibit. Turn off the configuration option 'ThrowExceptionOnLockedScreen' to turn off this exception. Timeout was " + timeoutMS + " and keyboard inhibit is " + this.KeyboardLocked); } if (Config.IdentificationEngineOn) { throw new TNIdentificationException(mScreenName, GetScreenAsXML()); } else { return(false); } }
/// <summary> /// Refresh the current screen. If timeout > 0, it will wait for /// this number of milliseconds. /// If waitForValidScreen is true, it will wait for a valid screen, otherwise it /// will return immediately that any screen data is visible /// </summary> /// <param name="waitForValidScreen"></param> /// <param name="timeoutMS">The time to wait in ms</param> /// <returns></returns> public bool Refresh(bool waitForValidScreen, int timeoutMS) { long start = DateTime.Now.Ticks / (10 * 1000); long end = start + timeoutMS; // timeout if (currentConnection == null) { throw new TNHostException("TNEmulator is not connected", "There is no currently open TN3270 connection", null); } if (sout != null && Debug == true) { sout.WriteLine("Refresh::Refresh(" + waitForValidScreen + ", " + timeoutMS + "). FastScreenMode=" + this.mConnectionConfiguration.FastScreenMode); } do { if (waitForValidScreen) { bool run = false; int timeout = 0; do { timeout = (int)(end - (DateTime.Now.Ticks / 10000)); if (timeout > 0) { if (sout != null && this.Debug) { sout.WriteLine("Refresh::Acquire(" + timeout + " milliseconds). unsafe Count is currently " + mre.Count); } run = mre.Acquire(Math.Min(timeout, 1000)); //Console.WriteLine("run = "+run); if (!IsConnected) { throw new TNHostException("The TN3270 connection was lost", this.currentConnection.DisconnectReason, null); } if (run) { if (sout != null && this.Debug) { sout.WriteLine("Refresh::return true at line 279"); } return(true); } } }while (!run && timeout > 0); if (sout != null && this.Debug) { sout.WriteLine("Refresh::Timeout or acquire failed. run= " + run + " timeout=" + timeout); } } if (this.mConnectionConfiguration.FastScreenMode || this.KeyboardLocked == 0) { // // Store screen in screen database and identify it // _currentScreenXML = null; // force a refresh if (sout != null && this.Debug) { sout.WriteLine("Refresh::Timeout, but since keyboard is not locked and fastmode=true, return true anyway"); } return(true); // // --Screen identification code here-- // #if FALSE if (waitForValidScreen) { if (sout != null) { sout.WriteLine("Refresh::waitForValidScreen is true - loop will loop and acquire another mre lock."); } // reset screen count to let us then wait for the next transition // wait for the next screen transistion //Console.WriteLine("-- acquire(0), release any pending transactions"); //mre.AcquireAll(0); //mre.Reset(); } else { if (sout != null && this.Debug == true) { sout.WriteLine("waitForValidScreen is false. Refresh returned false."); } return(false); } #endif } else { System.Threading.Thread.Sleep(10); } }while (DateTime.Now.Ticks / 10000 < end); // // if (sout != null) { sout.WriteLine("Refresh::Timed out (2) waiting for a valid screen. Timeout was " + timeoutMS); } if (Config.FastScreenMode == false && Config.ThrowExceptionOnLockedScreen && this.KeyboardLocked != 0) { throw new ApplicationException("Timeout waiting for new screen with keyboard inhibit false - screen present with keyboard inhibit. Turn off the configuration option 'ThrowExceptionOnLockedScreen' to turn off this exception. Timeout was " + timeoutMS + " and keyboard inhibit is " + this.KeyboardLocked); } if (Config.IdentificationEngineOn) { throw new TNIdentificationException(mScreenName, GetScreenAsXML()); } else { return(false); } }