Ejemplo n.º 1
0
        public object Request(string messageName, object request)
        {
            transceiver.VerifyConnection();

            var rpcRequest = new RpcRequest(messageName, request, new RpcContext());

            CallFuture <Object> future =
                GetMessage(rpcRequest).Oneway.GetValueOrDefault() ? null : new CallFuture <Object>();

            Request(rpcRequest, future);

            return(future == null ? null : future.WaitForResult());
        }
Ejemplo n.º 2
0
        private void Request <T>(RpcRequest request, ICallback <T> callback)
        {
            Transceiver t = transceiver;

            if (!t.IsConnected)
            {
                Monitor.Enter(handshakeLock);
                handshakeThread = Thread.CurrentThread;

                try
                {
                    if (!t.IsConnected)
                    {
                        var callFuture             = new CallFuture <T>(callback);
                        IList <MemoryStream> bytes = request.GetBytes(Local, this);
                        var transceiverCallback    = new TransceiverCallback <T>(this, request, callFuture, Local);

                        t.Transceive(bytes, transceiverCallback);

                        // Block until handshake complete
                        callFuture.Wait();
                        Message message = GetMessage(request);
                        if (message.Oneway.GetValueOrDefault())
                        {
                            Exception error = callFuture.Error;
                            if (error != null)
                            {
                                throw error;
                            }
                        }
                        return;
                    }
                }
                finally
                {
                    if (Thread.CurrentThread == handshakeThread)
                    {
                        handshakeThread = null;
                        Monitor.Exit(handshakeLock);
                    }
                }
            }

            if (GetMessage(request).Oneway.GetValueOrDefault())
            {
                t.LockChannel();
                try
                {
                    IList <MemoryStream> bytes = request.GetBytes(Local, this);
                    t.WriteBuffers(bytes);
                    if (callback != null)
                    {
                        callback.HandleResult(default(T));
                    }
                }
                finally
                {
                    t.UnlockChannel();
                }
            }
            else
            {
                IList <MemoryStream> bytes = request.GetBytes(Local, this);
                var transceiverCallback    = new TransceiverCallback <T>(this, request, callback, Local);

                t.Transceive(bytes, transceiverCallback);

                //if (Thread.CurrentThread == handshakeThread)
                //{
                //    Monitor.Exit(handshakeLock);
                //}
            }
        }