• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015-2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System.Collections.Generic;
20 using System.Threading.Tasks;
21 using Grpc.Core;
22 using Grpc.Core.Utils;
23 
24 namespace Math
25 {
26     /// <summary>
27     /// Implementation of MathService server
28     /// </summary>
29     public class MathServiceImpl : Math.MathBase
30     {
Div(DivArgs request, ServerCallContext context)31         public override Task<DivReply> Div(DivArgs request, ServerCallContext context)
32         {
33             return Task.FromResult(DivInternal(request));
34         }
35 
Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context)36         public override async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context)
37         {
38             var limit = request.Limit > 0 ? request.Limit : long.MaxValue;
39             var fibEnumerator = FibInternal(limit).GetEnumerator();
40 
41             // Keep streaming the sequence until the call is cancelled.
42             // Use CancellationToken from ServerCallContext to detect the cancellation.
43             while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext())
44             {
45                 await responseStream.WriteAsync(fibEnumerator.Current);
46                 await Task.Delay(100);
47             }
48         }
49 
Sum(IAsyncStreamReader<Num> requestStream, ServerCallContext context)50         public override async Task<Num> Sum(IAsyncStreamReader<Num> requestStream, ServerCallContext context)
51         {
52             long sum = 0;
53             await requestStream.ForEachAsync(num =>
54             {
55                 sum += num.Num_;
56                 return TaskUtils.CompletedTask;
57             });
58             return new Num { Num_ = sum };
59         }
60 
DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream, ServerCallContext context)61         public override async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream, ServerCallContext context)
62         {
63             await requestStream.ForEachAsync(async divArgs => await responseStream.WriteAsync(DivInternal(divArgs)));
64         }
65 
DivInternal(DivArgs args)66         static DivReply DivInternal(DivArgs args)
67         {
68             if (args.Divisor == 0)
69             {
70                 // One can finish the RPC with non-ok status by throwing RpcException instance.
71                 // Alternatively, resulting status can be set using ServerCallContext.Status
72                 throw new RpcException(new Status(StatusCode.InvalidArgument, "Division by zero"));
73             }
74 
75             long quotient = args.Dividend / args.Divisor;
76             long remainder = args.Dividend % args.Divisor;
77             return new DivReply { Quotient = quotient, Remainder = remainder };
78         }
79 
FibInternal(long n)80         static IEnumerable<Num> FibInternal(long n)
81         {
82             long a = 1;
83             yield return new Num { Num_ = a };
84 
85             long b = 1;
86             for (long i = 0; i < n - 1; i++)
87             {
88                 long temp = a;
89                 a = b;
90                 b = temp + b;
91                 yield return new Num { Num_ = a };
92             }
93         }
94     }
95 }
96