PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Dynamic/Hosting/Shell/Remote/RemoteCommandDispatcher.cs

#
C# | 96 lines | 49 code | 14 blank | 33 comment | 6 complexity | 4e029e2b02becdb5e7be6186c1f59d54 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !SILVERLIGHT // Remoting
  16. using System;
  17. using System.Diagnostics;
  18. using System.Threading;
  19. using System.Security.Permissions;
  20. namespace Microsoft.Scripting.Hosting.Shell.Remote {
  21. /// <summary>
  22. /// This allows the RemoteConsoleHost to abort a long-running operation. The RemoteConsoleHost itself
  23. /// does not know which ThreadPool thread might be processing the remote call, and so it needs
  24. /// cooperation from the remote runtime server.
  25. /// </summary>
  26. public class RemoteCommandDispatcher : MarshalByRefObject, ICommandDispatcher {
  27. /// <summary>
  28. /// Since OnOutputDataReceived is sent async, it can arrive late. The remote console
  29. /// cannot know if all output from the current command has been received. So
  30. /// RemoteCommandDispatcher writes out a marker to indicate the end of the output
  31. /// </summary>
  32. internal const string OutputCompleteMarker = "{7FF032BB-DB03-4255-89DE-641CA195E5FA}";
  33. private ScriptScope _scriptScope;
  34. private Thread _executingThread;
  35. public RemoteCommandDispatcher(ScriptScope scope) {
  36. _scriptScope = scope;
  37. }
  38. public ScriptScope ScriptScope { get { return _scriptScope; } }
  39. public object Execute(CompiledCode compiledCode, ScriptScope scope) {
  40. Debug.Assert(_executingThread == null);
  41. _executingThread = Thread.CurrentThread;
  42. try {
  43. object result = compiledCode.Execute(scope);
  44. Console.WriteLine(RemoteCommandDispatcher.OutputCompleteMarker);
  45. return result;
  46. } catch (ThreadAbortException tae) {
  47. KeyboardInterruptException pki = tae.ExceptionState as KeyboardInterruptException;
  48. if (pki != null) {
  49. // Most exceptions get propagated back to the client. However, ThreadAbortException is handled
  50. // differently by the remoting infrastructure, and gets wrapped in a RemotingException
  51. // ("An error occurred while processing the request on the server"). So we filter it out
  52. // and raise the KeyboardInterruptException
  53. Thread.ResetAbort();
  54. throw pki;
  55. } else {
  56. throw;
  57. }
  58. } finally {
  59. _executingThread = null;
  60. }
  61. }
  62. /// <summary>
  63. /// Aborts the current active call to Execute by doing Thread.Abort
  64. /// </summary>
  65. /// <returns>true if a Thread.Abort was actually called. false if there is no active call to Execute</returns>
  66. public bool AbortCommand() {
  67. Thread executingThread = _executingThread;
  68. if (executingThread == null) {
  69. return false;
  70. }
  71. executingThread.Abort(new KeyboardInterruptException(""));
  72. return true;
  73. }
  74. #if !SILVERLIGHT
  75. // TODO: Figure out what is the right lifetime
  76. public override object InitializeLifetimeService() {
  77. return null;
  78. }
  79. #endif
  80. }
  81. }
  82. #endif