-
Notifications
You must be signed in to change notification settings - Fork 0
/
PInvokableContext.cs
145 lines (115 loc) · 4.23 KB
/
PInvokableContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
namespace DynamicPInvoke
{
/// <summary>
/// This attribute is set to indicate that the method signature should be used to dynamically
/// generate a PInvoke call that is implicitly invoked before the body of the method whenever
/// a call is made.
/// </summary>
[System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public sealed class PInvokableAttribute : Attribute { }
/// <summary>
/// The context attribute that causes the
/// </summary>
[System.AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
internal sealed class PInvokerContextAttribute : Attribute, IContextAttribute
{
private bool _initialized = false;
public void GetPropertiesForNewContext(IConstructionCallMessage msg)
{
msg.ContextProperties.Add(new PInvokerContextProperty());
_initialized = true;
}
public bool IsContextOK(Context ctx, IConstructionCallMessage msg)
{
return _initialized;
}
};
internal sealed class PInvokerContextProperty :
IContextProperty,
IContributeObjectSink,
IContextPropertyActivator
{
private PInvokerSink _sink = null;
#region IContextProperty
public string Name => "PInvoker";
public void Freeze(Context newContext) { }
public bool IsNewContextOK(Context newCtx)
{
return true;
}
#endregion // IContextProperty
#region IContributeObjectSink
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
_sink = new PInvokerSink(nextSink);
return _sink;
}
#endregion // IContributeObjectSink
#region IContextPropertyActivator
public bool IsOKToActivate(IConstructionCallMessage msg)
{
return true;
}
public void CollectFromClientContext(IConstructionCallMessage msg) { }
public bool DeliverClientContextToServerContext(IConstructionCallMessage msg)
{
return true;
}
public void CollectFromServerContext(IConstructionReturnMessage msg)
{
var retProxy = msg.ReturnValue as ObjRef;
try {
_sink.SetCallable((IExternalCallable)RemotingServices.Unmarshal(retProxy, true));
}
catch(InvalidCastException ex) {
throw new InvalidOperationException("PInvokableContextAttribute may only be applied to to a class that implements IExternalCallable", ex);
}
}
public bool DeliverServerContextToClientContext(IConstructionReturnMessage msg)
{
return true;
}
#endregion // IContextPropertyActivator
}
internal sealed class PInvokerSink : IMessageSink
{
private IExternalCallable _pinvoker = null;
public PInvokerSink(IMessageSink next)
{
NextSink = next;
}
#region IMessageSink
public IMessageSink NextSink { get; }
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
throw new InvalidOperationException("PInvoker doesn't suppport asynchronous calls");
}
public IMessage SyncProcessMessage(IMessage msg)
{
// if this message is a method call tagged [PInvokable]
if (msg is IMethodMessage call) {
var attribs = call.MethodBase.GetCustomAttributes(typeof(PInvokableAttribute), false);
if (0 < attribs.Length) {
DoPInvoke(call);
}
}
return NextSink.SyncProcessMessage(msg);
}
#endregion // IMessageSink
public void SetCallable(IExternalCallable callable)
{
_pinvoker = callable;
}
private void DoPInvoke(IMethodMessage msg)
{
if (null != _pinvoker) {
_pinvoker.CallMethod(msg.MethodName, msg.Args);
}
}
}
}