/
FastChannel.cs
1116 lines (961 loc) · 41.4 KB
/
FastChannel.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Threading.Tasks;
namespace Super_Fast_Inter_Process_Communication
{
internal class Foo
{
public int ProcessID => Process.GetCurrentProcess().Id;
public int Add(int x, int y) => x + y;
public async Task<int> AddAsync(int x, int y)
{
await Task.Delay(1000);
return x + y;
}
}
#region Marshaling
internal class Bar
{
public Proxy<Foo> GetFoo() => new Foo();
public async Task PrintFoo(Proxy<Foo> foo)
{
int fooID = await foo.Eval(f => f.ProcessID);
Console.WriteLine("Foo process ID is " + fooID);
}
}
#endregion Marshaling
#region FastChannel
internal class FastChannel : SafeDisposable
{
static int _lastMessageID, _lastObjectID;
public readonly byte DomainAddress;
readonly OutPipe _outPipe;
readonly InPipe _inPipe;
readonly Module _module; // The module for which serialization is optimized.
readonly object _proxiesLock = new object();
readonly Dictionary<int, IProxy> _proxiesByID = new Dictionary<int, IProxy>();
readonly Dictionary<object, IProxy> _proxiesByObject = new Dictionary<object, IProxy>();
readonly Dictionary<int, Action<MessageType, object>> _pendingReplies = new Dictionary<int, Action<MessageType, object>>();
int _messagesReceived;
public int MessagesReceived { get { return _messagesReceived; } }
private enum MessageType : byte
{ Activation, Deactivation, MethodCall, ReturnValue, ReturnException }
public FastChannel(string name, bool isOwner, Module module)
{
_module = module; // Types belonging to this module will serialize faster
DomainAddress = (byte)(isOwner ? 1 : 2);
_outPipe = new OutPipe(name + (isOwner ? ".A" : ".B"), isOwner);
_inPipe = new InPipe(name + (isOwner ? ".B" : ".A"), isOwner, OnMessageReceived);
}
protected override void DisposeCore()
{
base.DisposeCore();
lock (_proxiesLock)
{
_proxiesByID.Clear();
_proxiesByObject.Clear();
}
_inPipe.Dispose();
_outPipe.Dispose();
}
IProxy FindProxy(int objectID)
{
IProxy proxy;
lock (_proxiesLock) _proxiesByID.TryGetValue(objectID, out proxy);
return proxy;
}
IProxy RegisterLocalProxy(IProxy proxy)
{
lock (_proxiesLock)
{
// Avoid multiple identities for the same object:
IProxy existingProxy;
bool alreadyThere = _proxiesByObject.TryGetValue(proxy.LocalInstanceUntyped, out existingProxy);
if (alreadyThere) return existingProxy;
int objectID = Interlocked.Increment(ref _lastObjectID);
proxy.RegisterLocal(this, objectID, () => UnregisterLocalProxy(proxy, objectID));
_proxiesByID[objectID] = proxy;
_proxiesByObject[proxy] = proxy;
return proxy;
}
}
void UnregisterLocalProxy(IProxy proxy, int objectID)
{
lock (_proxiesLock)
{
_proxiesByID.Remove(objectID);
_proxiesByObject.Remove(proxy.LocalInstanceUntyped);
}
}
/// <summary>Instantiates an object remotely. To release it, you can either call Disconnect on the proxy returned
/// or wait for its finalizer to do the same.s</summary>
public Task<Proxy<TRemote>> Activate<TRemote>() where TRemote : class
{
lock (DisposeLock)
{
AssertSafe();
int messageNumber = Interlocked.Increment(ref _lastMessageID);
var ms = new MemoryStream();
var writer = new BinaryWriter(ms);
writer.Write((byte)MessageType.Activation);
writer.Write(messageNumber);
SerializeType(writer, typeof(TRemote));
writer.Flush();
var task = GetResponseFuture<Proxy<TRemote>>(messageNumber);
_outPipe.Write(ms.ToArray());
return task;
}
}
internal void InternalDeactivate(int objectID)
{
var ms = new MemoryStream();
var writer = new BinaryWriter(ms);
writer.Write((byte)MessageType.Deactivation);
writer.Write(objectID);
writer.Flush();
lock (DisposeLock)
{
if (IsDisposed) return;
_outPipe.Write(ms.ToArray());
}
}
internal Task<TResult> SendMethodCall<TResult>(Expression expressionBody, int objectID, bool awaitRemoteTask)
{
lock (DisposeLock)
{
AssertSafe();
int messageNumber = Interlocked.Increment(ref _lastMessageID);
byte[] payload = SerializeMethodCall(expressionBody, messageNumber, objectID, awaitRemoteTask);
var task = GetResponseFuture<TResult>(messageNumber);
_outPipe.Write(payload);
return task;
}
}
Task<T> GetResponseFuture<T>(int messageNumber)
{
var tcs = new TaskCompletionSource<T>();
lock (_pendingReplies)
_pendingReplies.Add(messageNumber, (msgType, value) =>
{
if (msgType == MessageType.ReturnValue)
tcs.SetResult((T)value);
else
{
var ex = (Exception)value;
MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic);
if (preserveStackTrace != null) preserveStackTrace.Invoke(ex, null);
tcs.SetException(ex);
}
});
return tcs.Task;
}
void OnMessageReceived(byte[] data)
{
Interlocked.Increment(ref _messagesReceived);
lock (DisposeLock)
{
if (IsDisposed) return;
var ms = new MemoryStream(data);
var reader = new BinaryReader(ms);
var messageType = (MessageType)reader.ReadByte();
if (messageType == MessageType.ReturnValue || messageType == MessageType.ReturnException)
ReceiveReply(messageType, reader);
else if (messageType == MessageType.MethodCall)
ReceiveMethodCall(reader);
else if (messageType == MessageType.Activation)
ReceiveActivation(reader);
else if (messageType == MessageType.Deactivation)
ReceiveDeactivation(reader);
}
}
void ReceiveReply(MessageType messageType, BinaryReader reader)
{
int msgNumber = reader.ReadInt32();
object value = DeserializeValue(reader);
Action<MessageType, object> reply;
lock (_pendingReplies)
{
if (!_pendingReplies.TryGetValue(msgNumber, out reply)) return; // Orphan reply
_pendingReplies.Remove(msgNumber);
}
reply(messageType, value);
}
void ReceiveMethodCall(BinaryReader reader)
{
int messageNumber = reader.ReadInt32();
int objectID = reader.ReadInt32();
bool awaitRemoteTask = reader.ReadBoolean();
var method = DeserializeMethod(reader);
Exception error = null;
object returnValue = null;
object[] args = null;
IProxy proxy = null;
try
{
args = DeserializeArguments(reader, method.GetParameters()).ToArray();
proxy = FindProxy(objectID);
if (proxy == null) throw new ObjectDisposedException("Proxy " + objectID + " has been disposed.");
}
catch (Exception ex)
{
error = ex;
}
Task.Factory.StartNew(() =>
{
if (error == null)
try
{
var instance = proxy.LocalInstanceUntyped;
if (instance == null)
{
string typeInfo = proxy.ObjectType == null ? "?" : proxy.ObjectType.FullName;
error = new ObjectDisposedException("Proxy " + objectID + " is disposed. Type: " + typeInfo);
}
else returnValue = method.Invoke(instance, args);
}
catch (Exception ex)
{
if (ex is TargetInvocationException) error = ex.InnerException;
else error = ex;
}
SendReply(messageNumber, returnValue, error, awaitRemoteTask);
}, TaskCreationOptions.PreferFairness);
}
void ReceiveActivation(BinaryReader reader)
{
int messageNumber = reader.ReadInt32();
object instance = null;
Exception error = null;
try
{
var type = DeserializeType(reader);
instance = Activator.CreateInstance(type, true);
var proxy = (IProxy)Activator.CreateInstance(typeof(Proxy<>).MakeGenericType(type), BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { instance, DomainAddress }, null);
instance = RegisterLocalProxy(proxy);
}
catch (Exception ex)
{
error = ex;
}
SendReply(messageNumber, instance, error, false);
}
void SendReply(int messageNumber, object returnValue, Exception error, bool awaitRemoteTask)
{
lock (DisposeLock)
{
if (IsDisposed) return;
if (awaitRemoteTask)
{
var returnTask = (Task)returnValue;
// The method we're calling is itself asynchronous. Delay sending a reply until the method itself completes.
returnTask.ContinueWith(ant => SendReply(messageNumber, ant.IsFaulted ? null : ant.GetUntypedResult(), ant.Exception, false));
return;
}
var ms = new MemoryStream();
var writer = new BinaryWriter(ms);
writer.Write((byte)(error == null ? MessageType.ReturnValue : MessageType.ReturnException));
writer.Write(messageNumber);
SerializeValue(writer, error ?? returnValue);
writer.Flush();
_outPipe.Write(ms.ToArray());
}
}
void ReceiveDeactivation(BinaryReader reader)
{
int objectID = reader.ReadInt32();
lock (_proxiesLock)
{
var proxy = FindProxy(objectID);
if (proxy == null) return;
proxy.Disconnect();
}
}
byte[] SerializeMethodCall(Expression expr, int messageNumber, int objectID, bool awaitRemoteTask)
{
if (expr == null) throw new ArgumentNullException("expr");
MethodInfo method;
IEnumerable<Expression> args = new Expression[0];
var mc = expr as MethodCallExpression;
if (mc != null)
{
method = mc.Method;
args = mc.Arguments;
}
else
{
var me = expr as MemberExpression;
if (me != null && me.Member is PropertyInfo)
method = ((PropertyInfo)me.Member).GetGetMethod();
else
throw new InvalidOperationException("Only method calls and property reads can be serialized");
}
var ms = new MemoryStream();
var writer = new BinaryWriter(ms);
writer.Write((byte)MessageType.MethodCall);
writer.Write(messageNumber);
writer.Write(objectID);
writer.Write(awaitRemoteTask);
SerializeMethod(writer, method);
SerializeArguments(writer, args.Select(a => GetExprValue(a, true)).ToArray());
writer.Flush();
return ms.ToArray();
}
void SerializeArguments(BinaryWriter writer, object[] args)
{
writer.Write((byte)args.Length);
foreach (var o in args) SerializeValue(writer, o);
}
IEnumerable<object> DeserializeArguments(BinaryReader reader, ParameterInfo[] args)
{
byte objectCount = reader.ReadByte();
for (int i = 0; i < objectCount; i++) yield return DeserializeValue(reader, args[i].ParameterType);
}
private enum FastTypeCode : byte
{ Null, False, True, Byte, Char, String, Int32, Proxy, Other }
void SerializeValue(BinaryWriter writer, object o)
{
if (o == null)
{
writer.Write((byte)FastTypeCode.Null);
}
else if (o is bool)
{
writer.Write((byte)((bool)o ? FastTypeCode.True : FastTypeCode.False));
}
else if (o is byte)
{
writer.Write((byte)FastTypeCode.Byte);
writer.Write((byte)o);
}
else if (o is char)
{
writer.Write((byte)FastTypeCode.Char);
writer.Write((char)o);
}
else if (o is string)
{
writer.Write((byte)FastTypeCode.String);
writer.Write((string)o);
}
else if (o is int)
{
writer.Write((byte)FastTypeCode.Int32);
writer.Write((int)o);
}
else if (o is IProxy)
{
writer.Write((byte)FastTypeCode.Proxy);
var proxy = (IProxy)o;
if (proxy.LocalInstanceUntyped != null) proxy = RegisterLocalProxy(proxy);
var typeArgType = o.GetType().GetGenericArguments()[0];
SerializeType(writer, typeArgType);
SerializeType(writer, proxy.LocalInstanceUntyped == null ? typeArgType : proxy.LocalInstanceUntyped.GetType());
writer.Write(proxy.ObjectID.Value);
// The domain address will be zero if created via implicit conversion.
writer.Write(proxy.DomainAddress == 0 ? DomainAddress : proxy.DomainAddress);
}
else
{
writer.Write((byte)FastTypeCode.Other);
writer.Flush();
new BinaryFormatter().Serialize(writer.BaseStream, o);
}
}
object DeserializeValue(BinaryReader reader, Type expectedType = null)
{
var typeCode = (FastTypeCode)reader.ReadByte();
if (typeCode == FastTypeCode.Null) return null;
if (typeCode == FastTypeCode.False) return false;
if (typeCode == FastTypeCode.True) return true;
if (typeCode == FastTypeCode.Byte) return reader.ReadByte();
if (typeCode == FastTypeCode.Char) return reader.ReadChar();
if (typeCode == FastTypeCode.String) return reader.ReadString();
if (typeCode == FastTypeCode.Int32) return reader.ReadInt32();
if (typeCode == FastTypeCode.Proxy)
{
Type genericType = DeserializeType(reader);
Type actualType = DeserializeType(reader);
int objectID = reader.ReadInt32();
byte domainAddress = reader.ReadByte();
if (domainAddress == DomainAddress) // We own the real object
{
var proxy = FindProxy(objectID);
if (proxy == null)
throw new ObjectDisposedException("Cannot deserialize type '" + genericType.Name + "' - object has been disposed");
// Automatically unmarshal if necessary:
if (expectedType != null && expectedType.IsInstanceOfType(proxy.LocalInstanceUntyped)) return proxy.LocalInstanceUntyped;
return proxy;
}
// The other domain owns the object.
var proxyType = typeof(Proxy<>).MakeGenericType(genericType);
return Activator.CreateInstance(proxyType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[]
{
this,
objectID,
domainAddress,
actualType
}, null);
}
return new BinaryFormatter().Deserialize(reader.BaseStream);
}
void SerializeType(BinaryWriter writer, Type t)
{
if (t.Module == _module)
{
writer.Write((byte)1);
writer.Write(t.MetadataToken);
}
else
{
writer.Write((byte)2);
writer.Write(t.AssemblyQualifiedName);
}
}
Type DeserializeType(BinaryReader reader)
{
int b = reader.ReadByte();
if (b == 1)
return _module.ResolveType(reader.ReadInt32());
else
return Type.GetType(reader.ReadString());
}
void SerializeMethod(BinaryWriter writer, MethodInfo mi)
{
if (mi.Module == _module)
{
writer.Write((byte)1);
writer.Write(mi.MetadataToken);
}
else
{
writer.Write((byte)2);
writer.Write(mi.DeclaringType.AssemblyQualifiedName);
writer.Write(mi.MetadataToken);
}
}
MethodBase DeserializeMethod(BinaryReader reader)
{
int b = reader.ReadByte();
if (b == 1)
return _module.ResolveMethod(reader.ReadInt32());
else
return Type.GetType(reader.ReadString(), true).Module.ResolveMethod(reader.ReadInt32());
}
/// <summary>Evalulates an expression tree quickly on the local side without the cost of calling Compile().
/// This works only with simple method calls and property reads. In other cases, it returns null.</summary>
public static Func<T, U> FastEvalExpr<T, U>(Expression body)
{
// Optimize common cases:
MethodInfo method;
IEnumerable<Expression> args = new Expression[0];
var mc = body as MethodCallExpression;
if (mc != null)
{
method = mc.Method;
args = mc.Arguments;
}
else
{
var me = body as MemberExpression;
if (me != null && me.Member is PropertyInfo)
method = ((PropertyInfo)me.Member).GetGetMethod();
else
return null;
}
return x =>
{
try
{
return (U)method.Invoke(x, args.Select(a => GetExprValue(a, false)).ToArray());
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
};
}
static object GetExprValue(Expression expr, bool deferLocalInstanceProperty)
{
// Optimize the common simple cases, the first being a simple constant:
var constant = expr as ConstantExpression;
if (constant != null) return constant.Value;
// The second common simple case is accessing a field in a closure:
var me = expr as MemberExpression;
if (me != null && me.Member is FieldInfo && me.Expression is ConstantExpression)
return ((FieldInfo)me.Member).GetValue(((ConstantExpression)me.Expression).Value);
// If we're referring to the LocalInstance property of the proxy, we need to defer its evaluation
// until it's deserialized at the other end, as it will likely be null:
if (deferLocalInstanceProperty && me != null && me.Member is PropertyInfo)
{
if (me.Member.Name == "LocalInstance" &&
me.Member.ReflectedType.IsGenericType &&
me.Member.ReflectedType.GetGenericTypeDefinition() == typeof(Proxy<>))
{
return GetExprValue(me.Expression, true);
}
}
// This will take longer:
var objectMember = Expression.Convert(expr, typeof(object));
var getterLambda = Expression.Lambda<Func<object>>(objectMember);
var getter = getterLambda.Compile();
return getter();
}
}
#endregion FastChannel
#region Proxy
internal interface IProxy
{
/// <summary>Nongeneric version of the LocalInstance </summary>
object LocalInstanceUntyped { get; }
FastChannel Channel { get; }
int? ObjectID { get; }
byte DomainAddress { get; }
void Disconnect();
Type ObjectType { get; }
bool IsDisconnected { get; }
/// <summary>Connects the proxy for channel implementors. Used by FastChannel.</summary>
void RegisterLocal(FastChannel fastChannel, int? objectID, Action onDisconnect);
}
/// <summary>
/// Wraps a reference to an object that potentially lives in another domain. This ensures that cross-domain calls are explicit in the
/// source code, and allows for a transport other than .NET Remoting (e.g., FastChannel).
/// </summary>
internal class Proxy<TRemote> : IProxy where TRemote : class
{
/// <summary>Any reference-type object can be implicitly converted to a proxy. The proxy will become connected
/// automatically when it's serialized during a remote method call.</summary>
public static implicit operator Proxy<TRemote>(TRemote instance)
{
if (instance == null) return null;
return new Proxy<TRemote>(instance);
}
readonly object _locker = new object();
/// <summary>The object being Remoted. This is populated only on the local side.</summary>
public TRemote LocalInstance { get; private set; }
/// <summary>This is populated if the proxy was obtained via FastChannel instead of Remoting.</summary>
public FastChannel Channel { get; private set; }
/// <summary>This is populated if the proxy was obtained via FastChannel instead of Remoting. It uniquely identifies the
/// object within the channel if the object is connected (has a presence in the other domain).</summary>
public int? ObjectID { get; private set; }
/// <summary>Identifies the domain that owns the local instance, when FastChannel is in use.</summary>
public byte DomainAddress { get; private set; }
public bool IsDisconnected { get { return LocalInstance == null && Channel == null; } }
Action _onDisconnect;
readonly Type _actualObjectType;
object IProxy.LocalInstanceUntyped { get { return LocalInstance; } }
/// <summary>The real type of the object being proxied. This may be a subclass or derived implementation of TRemote.</summary>
public Type ObjectType { get { return _actualObjectType ?? (LocalInstance != null ? LocalInstance.GetType() : typeof(TRemote)); } }
Proxy(IProxy conversionSource, Action onDisconnect, Type actualObjectType)
{
LocalInstance = (TRemote)conversionSource.LocalInstanceUntyped;
Channel = conversionSource.Channel;
ObjectID = conversionSource.ObjectID;
DomainAddress = conversionSource.DomainAddress;
_onDisconnect = onDisconnect;
_actualObjectType = actualObjectType;
}
public Proxy(TRemote instance)
{
LocalInstance = instance;
}
// Called via reflection:
Proxy(TRemote instance, byte domainAddress) { LocalInstance = instance; DomainAddress = domainAddress; }
// Called via reflection:
Proxy(FastChannel channel, int objectID, byte domainAddress, Type actualInstanceType)
{
Channel = channel;
ObjectID = objectID;
DomainAddress = domainAddress;
_actualObjectType = actualInstanceType;
}
public void AssertRemote()
{
if (LocalInstance != null)
throw new InvalidOperationException("Object " + LocalInstance.GetType().Name + " is not remote");
}
/// <summary>Runs a (void) method on the object being proxied. This works on both the local and remote side.</summary>
public Task Run(Expression<Action<TRemote>> remoteMethod)
{
var li = LocalInstance;
if (li != null)
try
{
var fastEval = FastChannel.FastEvalExpr<TRemote, object>(remoteMethod.Body);
if (fastEval != null) fastEval(li);
else remoteMethod.Compile()(li);
return Task.FromResult(false);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
return SendMethodCall<object>(remoteMethod.Body, false);
}
/// <summary>Runs a (void) method on the object being proxied. This works on both the local and remote side.
/// Use this overload for methods on the other domain that are themselves asynchronous.</summary>
public Task Run(Expression<Func<TRemote, Task>> remoteMethod)
{
var li = LocalInstance;
if (li != null)
try
{
var fastEval = FastChannel.FastEvalExpr<TRemote, Task>(remoteMethod.Body);
if (fastEval != null) return fastEval(li);
return remoteMethod.Compile()(li);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
return SendMethodCall<object>(remoteMethod.Body, true);
}
/// <summary>Runs a non-void method on the object being proxied. This works on both the local and remote side.</summary>
public Task<TResult> Eval<TResult>(Expression<Func<TRemote, TResult>> remoteMethod)
{
var li = LocalInstance;
if (li != null)
try
{
var fastEval = FastChannel.FastEvalExpr<TRemote, TResult>(remoteMethod.Body);
if (fastEval != null) return Task.FromResult(fastEval(li));
return Task.FromResult(remoteMethod.Compile()(li));
}
catch (Exception ex)
{
return Task.FromException<TResult>(ex);
}
return SendMethodCall<TResult>(remoteMethod.Body, false);
}
/// <summary>Runs a non-void method on the object being proxied. This works on both the local and remote side.
/// Use this overload for methods on the other domain that are themselves asynchronous.</summary>
public Task<TResult> Eval<TResult>(Expression<Func<TRemote, Task<TResult>>> remoteMethod)
{
var li = LocalInstance;
if (li != null)
try
{
var fastEval = FastChannel.FastEvalExpr<TRemote, Task<TResult>>(remoteMethod.Body);
if (fastEval != null) return fastEval(li);
return remoteMethod.Compile()(li);
}
catch (Exception ex)
{
return Task.FromException<TResult>(ex);
}
return SendMethodCall<TResult>(remoteMethod.Body, true);
}
Task<TResult> SendMethodCall<TResult>(Expression expressionBody, bool awaitRemoteTask)
{
FastChannel channel;
int? objectID;
lock (_locker)
{
if (Channel == null)
return Task.FromException<TResult>(new InvalidOperationException("Channel has been disposed on Proxy<" + typeof(TRemote).Name + "> " + expressionBody));
channel = Channel;
objectID = ObjectID;
}
return channel.SendMethodCall<TResult>(expressionBody, objectID.Value, awaitRemoteTask);
}
/// <summary>This is useful when this.ObjectType is a subclass or derivation of TRemote.</summary>
public Proxy<TNew> CastTo<TNew>() where TNew : class, TRemote
{
if (!typeof(TNew).IsAssignableFrom(ObjectType))
throw new InvalidCastException("Type '" + ObjectType.FullName + "' cannot be cast to '" + typeof(TNew).FullName + "'.");
return new Proxy<TNew>(this, _onDisconnect, _actualObjectType);
}
void IProxy.RegisterLocal(FastChannel fastChannel, int? objectID, Action onDisconnect)
{
// This is called by FastChannel to connect/register the proxy.
lock (_locker)
{
Channel = fastChannel;
ObjectID = objectID;
DomainAddress = fastChannel.DomainAddress;
_onDisconnect = onDisconnect;
}
}
public void Disconnect()
{
Action onDisconnect;
lock (_locker)
{
onDisconnect = _onDisconnect;
_onDisconnect = null;
}
if (onDisconnect != null) onDisconnect();
// If the remote reference drops away, we should ensure that it gets release on the other domain as well:
lock (_locker)
{
if (Channel == null || LocalInstance != null || ObjectID == null)
LocalInstance = null;
else
Channel.InternalDeactivate(ObjectID.Value);
Channel = null;
ObjectID = null;
}
}
~Proxy()
{
if (_locker != null)
try
{
Disconnect();
}
catch (ObjectDisposedException) { }
}
}
#endregion Proxy
#region Pipe
internal class SafeMemoryMappedFile : SafeDisposable
{
readonly MemoryMappedFile _mmFile;
readonly MemoryMappedViewAccessor _accessor;
unsafe byte* _pointer;
public int Length { get; private set; }
public MemoryMappedViewAccessor Accessor {
get { AssertSafe(); return _accessor; }
}
public unsafe byte* Pointer {
get { AssertSafe(); return _pointer; }
}
public unsafe SafeMemoryMappedFile(MemoryMappedFile mmFile)
{
_mmFile = mmFile;
_accessor = _mmFile.CreateViewAccessor();
_pointer = (byte*)_accessor.SafeMemoryMappedViewHandle.DangerousGetHandle().ToPointer();
Length = (int)_accessor.Capacity;
}
unsafe protected override void DisposeCore()
{
base.DisposeCore();
_accessor.Dispose();
_mmFile.Dispose();
_pointer = null;
}
}
internal abstract class MutexFreePipe : SafeDisposable
{
protected const int MinimumBufferSize = 0x10000;
protected readonly int MessageHeaderLength = sizeof(int);
protected readonly int StartingOffset = sizeof(int) + sizeof(bool);
public readonly string Name;
protected readonly EventWaitHandle NewMessageSignal;
protected SafeMemoryMappedFile Buffer;
protected int Offset, Length;
protected MutexFreePipe(string name, bool createBuffer)
{
Name = name;
var mmFile = createBuffer
? MemoryMappedFile.CreateNew(name + ".0", MinimumBufferSize, MemoryMappedFileAccess.ReadWrite)
: MemoryMappedFile.OpenExisting(name + ".0");
Buffer = new SafeMemoryMappedFile(mmFile);
NewMessageSignal = new EventWaitHandle(false, EventResetMode.AutoReset, name + ".signal");
Length = Buffer.Length;
Offset = StartingOffset;
}
protected override void DisposeCore()
{
base.DisposeCore();
Buffer.Dispose();
NewMessageSignal.Dispose();
}
}
internal class OutPipe : MutexFreePipe
{
int _messageNumber;
int _bufferCount;
readonly List<SafeMemoryMappedFile> _oldBuffers = new List<SafeMemoryMappedFile>();
public int PendingBuffers => _oldBuffers.Count;
public OutPipe(string name, bool createBuffer) : base(name, createBuffer)
{
}
public unsafe void Write(byte[] data)
{
lock (DisposeLock) // If there are multiple threads, write just one message at a time
{
AssertSafe();
if (data.Length > Length - Offset - 8)
{
// Not enough space left in the shared memory buffer to write the message.
WriteContinuation(data.Length);
}
WriteMessage(data);
NewMessageSignal.Set(); // Signal reader that a message is available
}
}
unsafe void WriteMessage(byte[] block)
{
byte* ptr = Buffer.Pointer;
byte* offsetPointer = ptr + Offset;
var msgPointer = (int*)offsetPointer;
*msgPointer = block.Length;
Offset += MessageHeaderLength;
offsetPointer += MessageHeaderLength;
if (block != null && block.Length > 0)
{
//MMF.Accessor.WriteArray (Offset, block, 0, block.Length); // Horribly slow. No. No. No.
Marshal.Copy(block, 0, new IntPtr(offsetPointer), block.Length);
Offset += block.Length;
}
// Write the latest message number to the start of the buffer:
int* iptr = (int*)ptr;
*iptr = ++_messageNumber;
}
void WriteContinuation(int messageSize)
{
// First, allocate a new buffer:
string newName = Name + "." + ++_bufferCount;
int newLength = Math.Max(messageSize * 10, MinimumBufferSize);
var newFile = new SafeMemoryMappedFile(MemoryMappedFile.CreateNew(newName, newLength, MemoryMappedFileAccess.ReadWrite));
Console.WriteLine("Allocated new buffer of " + newLength + " bytes");
// Write a message to the old buffer indicating the address of the new buffer:
WriteMessage(new byte[0]);
// Keep the old buffer alive until the reader has indicated that it's seen it:
_oldBuffers.Add(Buffer);
// Make the new buffer current:
Buffer = newFile;
Length = newFile.Length;
Offset = StartingOffset;
// Release old buffers that have been read:
foreach (var buffer in _oldBuffers.Take(_oldBuffers.Count - 1).ToArray())
lock (buffer.DisposeLock)
if (!buffer.IsDisposed && buffer.Accessor.ReadBoolean(4))
{
_oldBuffers.Remove(buffer);
buffer.Dispose();
Console.WriteLine("Cleaned file");
}
}
protected override void DisposeCore()
{
base.DisposeCore();
foreach (var buffer in _oldBuffers) buffer.Dispose();
}
}
internal class InPipe : MutexFreePipe
{
int _lastMessageProcessed;
int _bufferCount;
readonly Action<byte[]> _onMessage;
public InPipe(string name, bool createBuffer, Action<byte[]> onMessage) : base(name, createBuffer)
{
_onMessage = onMessage;
new Thread(Go).Start();
}
void Go()
{
int spinCycles = 0;
while (true)
{
int? latestMessageID = GetLatestMessageID();
if (latestMessageID == null) return; // We've been disposed.
if (latestMessageID > _lastMessageProcessed)
{
Thread.MemoryBarrier(); // We need this because of lock-free implementation
byte[] msg = GetNextMessage();
if (msg == null) return;