示例#1
0
        public void SetUp ()
        {
            // Create a response object and get the binary representation of it
            var streamMessage = new StreamMessage ();

            var response1 = new Response ();
            response1.Time = 42;
            response1.Error = "Foo";

            var streamResponse1 = new StreamResponse (1263);
            streamResponse1.Response = response1;

            var response2 = new Response ();
            response2.Time = 123;
            response2.Error = "Bar";

            var streamResponse2 = new StreamResponse (3443);
            streamResponse2.Response = response2;

            streamMessage.Responses.Add (streamResponse1);
            streamMessage.Responses.Add (streamResponse2);

            expectedMessage = streamMessage;
            using (var stream = new MemoryStream ()) {
                expectedMessage.ToProtobufMessage ().WriteDelimitedTo (stream);
                messageBytes = stream.ToArray ();
            }
        }
示例#2
0
 public Response HandleRequest (ProcedureSignature procedure, object[] arguments)
 {
     if ((CallContext.GameScene & procedure.GameScene) == 0)
         throw new RPCException (procedure, "Procedure not available in game scene '" + CallContext.GameScene + "'");
     object returnValue;
     try {
         returnValue = procedure.Handler.Invoke (arguments);
     } catch (TargetInvocationException e) {
         if (e.InnerException is YieldException)
             throw e.InnerException;
         throw new RPCException (procedure, e.InnerException);
     }
     var response = new Response ();
     if (procedure.HasReturnType) {
         CheckReturnValue (procedure, returnValue);
         response.ReturnValue = returnValue;
     }
     return response;
 }
示例#3
0
        public void SetUp ()
        {
            // Create a request object and get the binary representation of it
            expectedRequest = new Request ("TestService", "ProcedureNoArgsNoReturn");
            using (var stream = new MemoryStream ()) {
                var codedStream = new CodedOutputStream (stream, true);
                codedStream.WriteInt32 (expectedRequest.ToProtobufMessage ().CalculateSize ());
                expectedRequest.ToProtobufMessage ().WriteTo (codedStream);
                codedStream.Flush ();
                requestBytes = stream.ToArray ();
            }

            // Create a response object and get the binary representation of it
            expectedResponse = new Response ();
            expectedResponse.Error = "SomeErrorMessage";
            expectedResponse.Time = 42;
            using (var stream = new MemoryStream ()) {
                var codedStream = new CodedOutputStream (stream, true);
                codedStream.WriteInt32 (expectedResponse.ToProtobufMessage ().CalculateSize ());
                expectedResponse.ToProtobufMessage ().WriteTo (codedStream);
                codedStream.Flush ();
                responseBytes = stream.ToArray ();
            }
        }
示例#4
0
 public static Response ProcedureSingleArgReturns (Response data)
 {
     return Service.ProcedureSingleArgReturns (data);
 }
示例#5
0
 public static void ProcedureThreeArgsNoReturn (Response x, Request y, Response z)
 {
     Service.ProcedureThreeArgsNoReturn (x, y, z);
 }
示例#6
0
 public static void ProcedureSingleArgNoReturn (Response data)
 {
     Service.ProcedureSingleArgNoReturn (data);
 }
示例#7
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);
            }
        }
示例#8
0
文件: Core.cs 项目: paperclip/krpc
 void PollRequests (IList<RequestContinuation> yieldedContinuations)
 {
     if (clientScheduler.Empty)
         return;
     pollRequestsCurrentClients.Clear ();
     for (int i = 0; i < continuations.Count; i++)
         pollRequestsCurrentClients.Add (continuations [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))
                     Logger.WriteLine ("Received request from client " + client.Address + " (" + request.Service + "." + request.Procedure + ")", Logger.Severity.Debug);
                 continuations.Add (new RequestContinuation (client, request));
             }
         } catch (ServerException e) {
             Logger.WriteLine ("Error receiving request from client " + client.Address + ": " + e.Message, Logger.Severity.Error);
             client.Stream.Close ();
             continue;
         } catch (Exception e) {
             var response = new Response ();
             response.HasError = true;
             response.Error = "Error receiving message" + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace;
             response.Time = GetUniversalTime ();
             if (Logger.ShouldLog (Logger.Severity.Debug))
                 Logger.WriteLine (e.Message + Environment.NewLine + e.StackTrace, Logger.Severity.Error);
             Logger.WriteLine ("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug);
             client.Stream.Write (response);
         }
         item = item.Next;
     }
 }
示例#9
0
文件: Core.cs 项目: paperclip/krpc
        void StreamServerUpdate ()
        {
            streamTimer.Reset ();
            streamTimer.Start ();
            uint rpcsExecuted = 0;

            for (int i = 0; i < servers.Count; i++)
                servers [i].StreamServer.Update ();

            // Run streaming requests
            if (streamRequests.Count > 0) {
                foreach (var entry in streamRequests) {
                    var streamClient = entry.Key;
                    var id = streamClient.Guid;
                    var requests = entry.Value;
                    if (requests.Count == 0)
                        continue;
                    if (!rpcClients.ContainsKey (id))
                        continue;
                    CallContext.Set (rpcClients [id]);
                    var streamMessage = new StreamMessage ();
                    foreach (var request in requests) {
                        // Run the RPC
                        Response response;
                        try {
                            response = KRPC.Service.Services.Instance.HandleRequest (request.Procedure, request.Arguments);
                        } catch (RPCException e) {
                            response = new Response ();
                            response.HasError = true;
                            response.Error = e.ToString ();
                        } catch (YieldException e) {
                            //FIXME: handle yields correctly
                            response = new Response ();
                            response.HasError = true;
                            response.Error = e.ToString ();
                        }
                        rpcsExecuted++;
                        // Don't send an update if it is the previous one
                        //FIXME: does the following comparison work?!? The objects have not been serialized
                        if (response.ReturnValue == streamResultCache [request.Identifier])
                            continue;
                        // Add the update to the response message
                        streamResultCache [request.Identifier] = response.ReturnValue;
                        response.Time = GetUniversalTime ();
                        var streamResponse = request.Response;
                        streamResponse.Response = response;
                        streamMessage.Responses.Add (streamResponse);
                    }
                    if (streamMessage.Responses.Count > 0)
                        streamClient.Stream.Write (streamMessage);
                }
            }

            streamTimer.Stop ();
            StreamRPCs = rpcsExecuted;
            StreamRPCsExecuted += rpcsExecuted;
            TimePerStreamUpdate = (float)streamTimer.ElapsedSeconds ();
        }
示例#10
0
 public override void Write (Response value)
 {
     codedOutputStream.WriteMessage (value.ToProtobufMessage ());
     codedOutputStream.Flush ();
 }
示例#11
0
 public void HandleRequestArgsReturn ()
 {
     var expectedResponse = new Response { Error = "bar", Time = 42 };
     var mock = new Mock<ITestService> (MockBehavior.Strict);
     mock.Setup (x => x.ProcedureSingleArgReturns (It.IsAny<Response> ()))
         .Returns ((Response x) => x);
     TestService.Service = mock.Object;
     var request = Req ("TestService", "ProcedureSingleArgReturns", Arg (0, expectedResponse));
     Response response = Run (request);
     response.Time = 42;
     mock.Verify (x => x.ProcedureSingleArgReturns (It.IsAny<Response> ()), Times.Once ());
     var innerResponse = (Response)response.ReturnValue;
     Assert.AreEqual (expectedResponse.Error, innerResponse.Error);
 }
示例#12
0
 public Response HandleRequest (ProcedureSignature procedure, IContinuation continuation)
 {
     object returnValue;
     try {
         returnValue = continuation.RunUntyped ();
     } catch (YieldException) {
         throw;
     } catch (Exception e) {
         throw new RPCException (procedure, e);
     }
     var response = new Response ();
     if (procedure.HasReturnType) {
         CheckReturnValue (procedure, returnValue);
         response.ReturnValue = returnValue;
     }
     return response;
 }