Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Examples/MathServiceImpl.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2
3 // Copyright 2015, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #endregion
33
34 using System;
35 using System.Collections.Generic;
36 using System.Threading;
37 using System.Threading.Tasks;
38 using Grpc.Core;
39 using Grpc.Core.Utils;
40
41 namespace Math
42 {
43 /// <summary>
44 /// Implementation of MathService server
45 /// </summary>
46 public class MathServiceImpl : Math.IMath
47 {
48 public Task<DivReply> Div(DivArgs request, ServerCallContext context)
49 {
50 return Task.FromResult(DivInternal(request));
51 }
52
53 public async Task Fib(FibArgs request, IServerStreamWriter<Num> response Stream, ServerCallContext context)
54 {
55 if (request.Limit <= 0)
56 {
57 // keep streaming the sequence until cancelled.
58 IEnumerator<Num> fibEnumerator = FibInternal(long.MaxValue).GetE numerator();
59 while (!context.CancellationToken.IsCancellationRequested && fib Enumerator.MoveNext())
60 {
61 await responseStream.WriteAsync(fibEnumerator.Current);
62 await Task.Delay(100);
63 }
64 }
65
66 if (request.Limit > 0)
67 {
68 foreach (var num in FibInternal(request.Limit))
69 {
70 await responseStream.WriteAsync(num);
71 }
72 }
73 }
74
75 public async Task<Num> Sum(IAsyncStreamReader<Num> requestStream, Server CallContext context)
76 {
77 long sum = 0;
78 await requestStream.ForEachAsync(async num =>
79 {
80 sum += num.Num_;
81 });
82 return new Num { Num_ = sum };
83 }
84
85 public async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, ISe rverStreamWriter<DivReply> responseStream, ServerCallContext context)
86 {
87 await requestStream.ForEachAsync(async divArgs => await responseStre am.WriteAsync(DivInternal(divArgs)));
88 }
89
90 static DivReply DivInternal(DivArgs args)
91 {
92 long quotient = args.Dividend / args.Divisor;
93 long remainder = args.Dividend % args.Divisor;
94 return new DivReply { Quotient = quotient, Remainder = remainder };
95 }
96
97 static IEnumerable<Num> FibInternal(long n)
98 {
99 long a = 1;
100 yield return new Num { Num_ = a };
101
102 long b = 1;
103 for (long i = 0; i < n - 1; i++)
104 {
105 long temp = a;
106 a = b;
107 b = temp + b;
108 yield return new Num { Num_ = a };
109 }
110 }
111 }
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698