From 563d57b57d5335e726750a063a24d26867ba2236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Thu, 14 Jul 2005 17:50:43 +0000 Subject: [PATCH] Added MyProxy, preparing NDebugger for MTA git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@178 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Debugger.Core.csproj | 2 + .../Project/Src/Breakpoints/Breakpoint.cs | 2 +- .../Project/Src/Debugger/Internal/MTA2STA.cs | 10 +++- .../Debugger/Internal/ManagedCallbackProxy.cs | 12 +++- .../Debugger/Internal/RemotingObjectBase.cs | 11 ++++ .../Project/Src/Debugger/NDebugger.cs | 15 ++++- .../Project/Src/Modules/Module.cs | 2 +- .../PrivateEventHandlersSink/MyProxy.cs | 55 +++++++++++++++++++ .../Project/Src/Threads/Exception.cs | 2 +- .../Project/Src/Threads/Function.cs | 2 +- .../Project/Src/Threads/Process.cs | 10 +++- .../Project/Src/Threads/SourcecodeSegment.cs | 2 +- .../Project/Src/Threads/Thread.cs | 2 +- .../Project/Src/Variables/Variable.cs | 2 +- 14 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/RemotingObjectBase.cs create mode 100644 src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/RemotingSinks/PrivateEventHandlersSink/MyProxy.cs diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Debugger.Core.csproj b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Debugger.Core.csproj index 21f1343b39..cfcd7e229f 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Debugger.Core.csproj +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Debugger.Core.csproj @@ -62,6 +62,8 @@ + + diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Breakpoints/Breakpoint.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Breakpoints/Breakpoint.cs index 2bd3dc3ddf..79ea74e428 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Breakpoints/Breakpoint.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Breakpoints/Breakpoint.cs @@ -11,7 +11,7 @@ using DebuggerInterop.Core; namespace DebuggerLibrary { - public class Breakpoint: MarshalByRefObject + public class Breakpoint: RemotingObjectBase { NDebugger debugger; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/MTA2STA.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/MTA2STA.cs index c508467a5c..803a1cbe1d 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/MTA2STA.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/MTA2STA.cs @@ -66,8 +66,13 @@ namespace DebuggerInterop.Core } return returnValue; } - + void PerformCall(object sender, EventArgs e) + { + returnValue = Call(targetObject, functionName, functionParameters); + } + + public object Call (object targetObject, string functionName, object[] functionParameters) { MethodInfo method; object[] outputParams; @@ -107,7 +112,7 @@ namespace DebuggerInterop.Core } } TraceMsg ("Invoke " + functionName + "{"); - returnValue = null; + object returnValue = null; try { if (targetObject is Type) { returnValue = method.Invoke(null, outputParams); @@ -118,6 +123,7 @@ namespace DebuggerInterop.Core System.Diagnostics.Debug.Fail("Invoke of " + functionName + " failed.", exception.ToString()); } TraceMsg ("} \\\\ Invoke"); + return returnValue; } } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/ManagedCallbackProxy.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/ManagedCallbackProxy.cs index 25fc79712a..bc52b87513 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/ManagedCallbackProxy.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/ManagedCallbackProxy.cs @@ -21,18 +21,24 @@ namespace DebuggerLibrary { class ManagedCallbackProxy :ICorDebugManagedCallback, ICorDebugManagedCallback2 { - MTA2STA mta2sta; + NDebugger debugger; ManagedCallback realCallback; + MTA2STA mta2sta; - public ManagedCallbackProxy(ManagedCallback realCallback) + public ManagedCallbackProxy(NDebugger debugger, ManagedCallback realCallback) { + this.debugger = debugger; this.realCallback = realCallback; mta2sta = new MTA2STA(); } private void CallbackReceived (string function, object[] parameters) { - mta2sta.CallInSTA(realCallback, function, parameters); + if (debugger.RequiredApartmentState == ApartmentState.STA) { + mta2sta.CallInSTA(realCallback, function, parameters); + } else { + mta2sta.Call(realCallback, function, parameters); + } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/RemotingObjectBase.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/RemotingObjectBase.cs new file mode 100644 index 0000000000..4c32d2c26a --- /dev/null +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/Internal/RemotingObjectBase.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; +using CustomSinks; + +namespace DebuggerLibrary +{ + public class RemotingObjectBase: MarshalByRefObject + { + } +} diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs index 66d06a878f..c785c3c099 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Debugger/NDebugger.cs @@ -7,13 +7,14 @@ using System.Diagnostics; using System.Collections; using System.Runtime.InteropServices; using System.Text; +using System.Threading; using DebuggerInterop.Core; using DebuggerInterop.MetaData; namespace DebuggerLibrary { - public partial class NDebugger: MarshalByRefObject + public partial class NDebugger: RemotingObjectBase { ICorDebug corDebug; ManagedCallback managedCallback; @@ -23,6 +24,14 @@ namespace DebuggerLibrary public bool CatchHandledExceptions = false; + ApartmentState requiredApartmentState; + + public ApartmentState RequiredApartmentState { + get { + return requiredApartmentState; + } + } + internal ICorDebug CorDebug { get { return corDebug; @@ -60,6 +69,8 @@ namespace DebuggerLibrary public NDebugger() { + requiredApartmentState = System.Threading.Thread.CurrentThread.GetApartmentState(); + InitDebugger(); ResetEnvironment(); @@ -82,7 +93,7 @@ namespace DebuggerLibrary //corDebug = new CorDebugClass(); managedCallback = new ManagedCallback(this); - managedCallbackProxy = new ManagedCallbackProxy(managedCallback); + managedCallbackProxy = new ManagedCallbackProxy(this, managedCallback); corDebug.Initialize(); corDebug.SetManagedHandler(managedCallbackProxy); diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs index 71651764e5..dd01d0a936 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Modules/Module.cs @@ -11,7 +11,7 @@ using DebuggerInterop.MetaData; namespace DebuggerLibrary { - public class Module: MarshalByRefObject + public class Module: RemotingObjectBase { string fullPath; ulong baseAdress; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/RemotingSinks/PrivateEventHandlersSink/MyProxy.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/RemotingSinks/PrivateEventHandlersSink/MyProxy.cs new file mode 100644 index 0000000000..1280bcffbf --- /dev/null +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/RemotingSinks/PrivateEventHandlersSink/MyProxy.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.Remoting.Proxies; +using System.Runtime.Remoting.Messaging; +using System.Runtime.Remoting.Activation; +using System.Runtime.Remoting; +using System.Runtime.Remoting.Services; + +namespace CustomSinks +{ + [AttributeUsage(AttributeTargets.Class)] + public class MyProxyAttribute: ProxyAttribute + { + public override MarshalByRefObject CreateInstance(Type type) + { + Console.WriteLine("Creating proxy of type " + type.ToString()); + MarshalByRefObject instance = base.CreateInstance(type); + MyProxy proxy = new MyProxy(type, instance); + return (MarshalByRefObject)proxy.GetTransparentProxy(); + } + } + + public class MyProxy: RealProxy + { + private MarshalByRefObject realObject; + + public MyProxy(Type type, MarshalByRefObject realObject):base(type) + { + this.realObject = realObject; + } + + public override IMessage Invoke(IMessage msg) + { + Console.WriteLine("Proxy called: " + msg.Properties["__MethodName"]); + if (msg is IConstructionCallMessage) { + IConstructionCallMessage ctorMsg = (IConstructionCallMessage)msg; + + try { + RemotingServices.GetRealProxy(realObject).InitializeServerObject(ctorMsg); + } catch (Exception e) { + } + + ObjRef objRef = RemotingServices.Marshal(realObject); + RemotingServices.Unmarshal(objRef); + + MarshalByRefObject transpProxy = (MarshalByRefObject)this.GetTransparentProxy(); + + return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctorMsg, transpProxy); + } else { + return RemotingServices.ExecuteMessage(realObject, (IMethodCallMessage)msg); + } + } + } +} diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Exception.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Exception.cs index 8799ed8517..16999fb665 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Exception.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Exception.cs @@ -11,7 +11,7 @@ using DebuggerInterop.MetaData; namespace DebuggerLibrary { - public class Exception: MarshalByRefObject + public class Exception: RemotingObjectBase { NDebugger debugger; Thread thread; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs index 3e97a0c166..614a5066d6 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Function.cs @@ -14,7 +14,7 @@ using System.Collections.Generic; namespace DebuggerLibrary { - public class Function: MarshalByRefObject + public class Function: RemotingObjectBase { NDebugger debugger; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Process.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Process.cs index b1093d3e3c..fea0b2334b 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Process.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Process.cs @@ -12,7 +12,7 @@ using DebuggerInterop.MetaData; namespace DebuggerLibrary { - public class Process: MarshalByRefObject + public class Process: RemotingObjectBase { NDebugger debugger; @@ -88,9 +88,13 @@ namespace DebuggerLibrary static public Process CreateProcess(NDebugger debugger, string filename, string workingDirectory, string arguments) { - MTA2STA m2s = new MTA2STA(); Process createdProcess = null; - createdProcess = (Process)m2s.CallInSTA(typeof(Process), "StartInternal", new Object[] {debugger, filename, workingDirectory, arguments}); + if (debugger.RequiredApartmentState == ApartmentState.STA) { + MTA2STA m2s = new MTA2STA(); + createdProcess = (Process)m2s.CallInSTA(typeof(Process), "StartInternal", new Object[] {debugger, filename, workingDirectory, arguments}); + } else { + createdProcess = StartInternal(debugger, filename, workingDirectory, arguments); + } return createdProcess; } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/SourcecodeSegment.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/SourcecodeSegment.cs index b8f8ae761d..8515ffde40 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/SourcecodeSegment.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/SourcecodeSegment.cs @@ -7,7 +7,7 @@ using System.Diagnostics.SymbolStore; namespace DebuggerLibrary { - public class SourcecodeSegment: MarshalByRefObject + public class SourcecodeSegment: RemotingObjectBase { string moduleFilename; string sourceFullFilename; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Thread.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Thread.cs index f590cd36d2..ece23523b4 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Thread.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Threads/Thread.cs @@ -12,7 +12,7 @@ using DebuggerInterop.MetaData; namespace DebuggerLibrary { - public partial class Thread: MarshalByRefObject + public partial class Thread: RemotingObjectBase { NDebugger debugger; diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Variable.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Variable.cs index bb5e75c6a6..30a009511a 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Variable.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Variable.cs @@ -10,7 +10,7 @@ using DebuggerInterop.Core; namespace DebuggerLibrary { - public abstract class Variable: MarshalByRefObject + public abstract class Variable: RemotingObjectBase { protected NDebugger debugger;