示例#1
0
        public void HandleBlockingRequestArgsReturns()
        {
            const int num            = 10;
            const int expectedResult = 55;
            var       mock           = new Mock <ITestService> (MockBehavior.Strict);

            mock.Setup(x => x.BlockingProcedureReturns(It.IsAny <int> (), It.IsAny <int> ()))
            .Returns((int n, int sum) => BlockingProcedureReturnsFn(n, sum));
            TestService.Service = mock.Object;
            var request = Req("TestService", "BlockingProcedureReturns", Arg(0, num));

            BlockingProcedureReturnsFnCount = 0;
            Response response = null;
            Continuation <Response> continuation = new RequestContinuation(null, request);

            while (response == null)
            {
                try {
                    response = continuation.Run();
                } catch (YieldException e) {
                    continuation = (Continuation <Response>)e.Continuation;
                }
            }
            response.Time = 0;
            Assert.AreEqual(string.Empty, response.Error);
            Assert.AreEqual(expectedResult, (int)response.ReturnValue);
            // Verify the KRPCProcedure is called once, but the handler function is called multiple times
            mock.Verify(x => x.BlockingProcedureReturns(It.IsAny <int> (), It.IsAny <int> ()), Times.Once());
            Assert.AreEqual(num + 1, BlockingProcedureReturnsFnCount);
        }
示例#2
0
        public void HandleBlockingRequestArgsNoReturn()
        {
            const int num  = 42;
            var       mock = new Mock <ITestService> (MockBehavior.Strict);

            mock.Setup(x => x.BlockingProcedureNoReturn(It.IsAny <int> ()))
            .Callback((int n) => BlockingProcedureNoReturnFn(n));
            TestService.Service = mock.Object;
            var request = Req("TestService", "BlockingProcedureNoReturn",
                              Arg(0, ProtocolBuffers.WriteValue(num, typeof(int))));

            BlockingProcedureNoReturnFnCount = 0;
            Response response = null;
            Continuation <Response> continuation = new RequestContinuation(null, request);

            while (response == null)
            {
                try {
                    response = continuation.Run();
                } catch (YieldException e) {
                    continuation = (Continuation <Response>)e.Continuation;
                }
            }
            response.Time = 0;
            Assert.AreEqual("", response.Error);
            // Verify the KRPCProcedure is called once, but the handler function is called multiple times
            mock.Verify(x => x.BlockingProcedureNoReturn(It.IsAny <int> ()), Times.Once());
            Assert.AreEqual(num + 1, BlockingProcedureNoReturnFnCount);
        }
示例#3
0
文件: Core.cs 项目: dewiniaid/krpc
        void ExecuteContinuation(RequestContinuation continuation)
        {
            var client = continuation.Client;

            // Run the continuation, and either return a result, an error,
            // or throw a YieldException if the continuation has not completed
            Response response;

            try {
                CallContext.Set(client);
                response = continuation.Run();
            } catch (YieldException) {
                throw;
            } catch (RPCException e) {
                response          = new Response();
                response.HasError = true;
                response.Error    = e.Message;
                if (Logger.ShouldLog(Logger.Severity.Debug))
                {
                    Logger.WriteLine(response.Error, Logger.Severity.Debug);
                }
            } catch (Exception e) {
                response          = new Response();
                response.HasError = true;
                response.Error    = e.Message + Environment.NewLine + e.StackTrace;
                if (Logger.ShouldLog(Logger.Severity.Debug))
                {
                    Logger.WriteLine(response.Error, Logger.Severity.Debug);
                }
            } finally {
                CallContext.Clear();
            }

            // Send response to the client
            response.Time = GetUniversalTime();
            client.Stream.Write(response);
            if (Logger.ShouldLog(Logger.Severity.Debug))
            {
                if (response.HasError)
                {
                    Logger.WriteLine("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
                }
                else
                {
                    Logger.WriteLine("Sent response to client " + client.Address, Logger.Severity.Debug);
                }
            }
        }
示例#4
0
        static void ExecuteContinuation(RequestContinuation continuation)
        {
            var client = continuation.Client;

            // Run the continuation, and either return a result, an error,
            // or throw a YieldException if the continuation has not completed
            Response response;

            try {
                CallContext.Set(client);
                response = continuation.Run();
            } catch (YieldException) {
                throw;
            } catch (RPCException e) {
                response = new Response {
                    Error = HandleException(e)
                };
            } catch (System.Exception e) {
                response = new Response {
                    Error = HandleException(e)
                };
            } finally {
                CallContext.Clear();
            }

            // Send response to the client
            try {
                client.Stream.Write(response);
                if (Logger.ShouldLog(Logger.Severity.Debug))
                {
                    if (response.HasError)
                    {
                        Logger.WriteLine("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
                    }
                    else
                    {
                        Logger.WriteLine("Sent response to client " + client.Address, Logger.Severity.Debug);
                    }
                }
            } catch (ServerException exn) {
                Logger.WriteLine("Failed to send response to client " + client.Address + Environment.NewLine + exn, Logger.Severity.Error);
            }
        }
示例#5
0
文件: Core.cs 项目: paperclip/krpc
        void ExecuteContinuation (RequestContinuation continuation)
        {
            var client = continuation.Client;

            // Run the continuation, and either return a result, an error,
            // or throw a YieldException if the continuation has not completed
            Response response;
            try {
                CallContext.Set (client);
                response = continuation.Run ();
            } catch (YieldException) {
                throw;
            } catch (RPCException e) {
                response = new Response ();
                response.HasError = true;
                response.Error = e.Message;
                if (Logger.ShouldLog (Logger.Severity.Debug))
                    Logger.WriteLine (response.Error, Logger.Severity.Debug);
            } catch (Exception e) {
                response = new Response ();
                response.HasError = true;
                response.Error = e.Message + Environment.NewLine + e.StackTrace;
                if (Logger.ShouldLog (Logger.Severity.Debug))
                    Logger.WriteLine (response.Error, Logger.Severity.Debug);
            } finally {
                CallContext.Clear ();
            }

            // Send response to the client
            response.Time = GetUniversalTime ();
            client.Stream.Write (response);
            if (Logger.ShouldLog (Logger.Severity.Debug)) {
                if (response.HasError)
                    Logger.WriteLine ("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
                else
                    Logger.WriteLine ("Sent response to client " + client.Address, Logger.Severity.Debug);
            }
        }
示例#6
0
 public void HandleBlockingRequestArgsReturns ()
 {
     const int num = 10;
     const int expectedResult = 55;
     var mock = new Mock<ITestService> (MockBehavior.Strict);
     mock.Setup (x => x.BlockingProcedureReturns (It.IsAny<int> (), It.IsAny<int> ()))
         .Returns ((int n, int sum) => BlockingProcedureReturnsFn (n, sum));
     TestService.Service = mock.Object;
     var request = Req ("TestService", "BlockingProcedureReturns", Arg (0, num));
     BlockingProcedureReturnsFnCount = 0;
     Response response = null;
     Continuation<Response> continuation = new RequestContinuation (null, request);
     while (response == null) {
         try {
             response = continuation.Run ();
         } catch (YieldException e) {
             continuation = (Continuation<Response>)e.Continuation;
         }
     }
     response.Time = 0;
     Assert.AreEqual (String.Empty, response.Error);
     Assert.AreEqual (expectedResult, (int)response.ReturnValue);
     // Verify the KRPCProcedure is called once, but the handler function is called multiple times
     mock.Verify (x => x.BlockingProcedureReturns (It.IsAny<int> (), It.IsAny<int> ()), Times.Once ());
     Assert.AreEqual (num + 1, BlockingProcedureReturnsFnCount);
 }
示例#7
0
        void PollRequests(IList <RequestContinuation> yieldedContinuations)
        {
            if (clientScheduler.Empty)
            {
                return;
            }
            pollRequestsCurrentClients.Clear();
            for (int i = 0; i < rpcContinuations.Count; i++)
            {
                pollRequestsCurrentClients.Add(rpcContinuations [i].Client);
            }
            for (int i = 0; i < yieldedContinuations.Count; i++)
            {
                pollRequestsCurrentClients.Add(yieldedContinuations [i].Client);
            }
            var item = clientScheduler.Items.First;

            while (item != null)
            {
                var client = item.Value;
                var stream = client.Stream;
                try {
                    if (!pollRequestsCurrentClients.Contains(client) && stream.DataAvailable)
                    {
                        Request request = stream.Read();
                        EventHandlerExtensions.Invoke(OnClientActivity, this, new ClientActivityEventArgs(client));
                        if (Logger.ShouldLog(Logger.Severity.Debug))
                        {
                            var calls = string.Join(
                                ", ", request.Calls.Select(call => call.ServiceId > 0 ? call.ServiceId + "." + call.ProcedureId : call.Service + "." + call.Procedure).ToArray());
                            Logger.WriteLine("Received request from client " + client.Address + " (" + calls + ")", Logger.Severity.Debug);
                        }
                        var requestContinuation = new RequestContinuation(client, request);
                        rpcContinuations.Add(requestContinuation);
                        if (Logger.ShouldLog(Logger.Severity.Debug))
                        {
                            var calls = string.Join(", ", requestContinuation.Calls.Select(call => call.Procedure.FullyQualifiedName).ToArray());
                            Logger.WriteLine("Decoded request from client " + client.Address + " (" + calls + ")", Logger.Severity.Debug);
                        }
                    }
                } catch (ClientDisconnectedException) {
                    Logger.WriteLine("Client " + client.Address + " disconnected");
                    client.Stream.Close();
                    continue;
                } catch (ServerException e) {
                    Logger.WriteLine("Error receiving request from client " + client.Address + ": " + e.Message, Logger.Severity.Error);
                    client.Stream.Close();
                    continue;
                } catch (System.Exception e) {
                    var response = new Response();
                    response.Error = new Error("Error receiving message" + Environment.NewLine + e.Message, e.StackTrace);
                    if (Logger.ShouldLog(Logger.Severity.Debug))
                    {
                        Logger.WriteLine(e.Message + Environment.NewLine + e.StackTrace, Logger.Severity.Error);
                    }
                    try {
                        client.Stream.Write(response);
                        Logger.WriteLine("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
                    } catch (ServerException exn) {
                        Logger.WriteLine("Failed to send error response to client " + client.Address + Environment.NewLine + exn, Logger.Severity.Error);
                    }
                }
                item = item.Next;
            }
        }
示例#8
0
        /// <summary>
        /// Execute the continuation and send a response to the client,
        /// or throw a YieldException if the continuation is not complete.
        /// </summary>
        void ExecuteContinuation(RequestContinuation continuation)
        {
            var client = continuation.Client;

            // Run the continuation, and either return a result, an error,
            // or throw a YieldException if the continuation has not completed
            Response.Builder response;
            try {
                Context.Set (this, client);
                response = continuation.Run ();
            } catch (YieldException) {
                throw;
            } catch (Exception e) {
                response = Response.CreateBuilder ();
                response.Error = e.Message;
                if (Logger.ShouldLog (Logger.Severity.Debug))
                    Logger.WriteLine (e.Message, Logger.Severity.Debug);
            } finally {
                Context.Clear ();
            }

            // Send response to the client
            response.SetTime (GetUniversalTime ());
            var builtResponse = response.Build ();
            ((RPCClient)client).Stream.Write (builtResponse);
            if (Logger.ShouldLog (Logger.Severity.Debug)) {
                if (response.HasError)
                    Logger.WriteLine ("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
                else
                    Logger.WriteLine ("Sent response to client " + client.Address, Logger.Severity.Debug);
            }
        }