// Called via reflection:
 Proxy(FastChannel channel, int objectID, byte domainAddress, Type actualInstanceType)
 {
     Channel           = channel;
     ObjectID          = objectID;
     DomainAddress     = domainAddress;
     _actualObjectType = actualInstanceType;
 }
        /// <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));
        }
        public async Task DemoClient()
        {
            using (var channel = new FastChannel("test", false, GetType().Module))
            {
                Proxy <Foo> fooProxy = await channel.Activate <Foo>();

                int remoteProcessID = await fooProxy.Eval(foo => foo.ProcessID);

                Console.WriteLine("Remote Process ID: " + remoteProcessID);

                int sum = await fooProxy.Eval(foo => foo.Add(2, 2));

                Console.WriteLine("Sum " + sum);

                int sum2 = await fooProxy.Eval(foo => foo.AddAsync(3, 3));

                Console.WriteLine("Sum " + sum2);

                #region Marshaling

                Proxy <Bar> barProxy = await channel.Activate <Bar>();

                Proxy <Foo> fooProxy2 = await barProxy.Eval(b => b.GetFoo());

                var dx = (await fooProxy2.Eval(foo => foo.Add(2, 2))).ToString();
                Console.WriteLine(dx);

                await barProxy.Run(b => b.PrintFoo(fooProxy2));

                await barProxy.Run(b => b.PrintFoo(new Foo()));

                #endregion Marshaling
            }
        }
 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;
     }
 }
        /// <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));
        }