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

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core/Internal/AsyncCallServer.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-2016, 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.Diagnostics;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Threading;
39 using System.Threading.Tasks;
40 using Grpc.Core.Internal;
41 using Grpc.Core.Utils;
42
43 namespace Grpc.Core.Internal
44 {
45 /// <summary>
46 /// Manages server side native call lifecycle.
47 /// </summary>
48 internal class AsyncCallServer<TRequest, TResponse> : AsyncCallBase<TRespons e, TRequest>
49 {
50 readonly TaskCompletionSource<object> finishedServersideTcs = new TaskCo mpletionSource<object>();
51 readonly CancellationTokenSource cancellationTokenSource = new Cancellat ionTokenSource();
52 readonly Server server;
53
54 public AsyncCallServer(Func<TResponse, byte[]> serializer, Func<byte[], TRequest> deserializer, GrpcEnvironment environment, Server server) : base(seria lizer, deserializer, environment)
55 {
56 this.server = GrpcPreconditions.CheckNotNull(server);
57 }
58
59 public void Initialize(CallSafeHandle call)
60 {
61 call.Initialize(environment.CompletionRegistry, environment.Completi onQueue);
62
63 server.AddCallReference(this);
64 InitializeInternal(call);
65 }
66
67 /// <summary>
68 /// Starts a server side call.
69 /// </summary>
70 public Task ServerSideCallAsync()
71 {
72 lock (myLock)
73 {
74 GrpcPreconditions.CheckNotNull(call);
75
76 started = true;
77
78 call.StartServerSide(HandleFinishedServerside);
79 return finishedServersideTcs.Task;
80 }
81 }
82
83 /// <summary>
84 /// Sends a streaming response. Only one pending send action is allowed at any given time.
85 /// completionDelegate is called when the operation finishes.
86 /// </summary>
87 public void StartSendMessage(TResponse msg, WriteFlags writeFlags, Async CompletionDelegate<object> completionDelegate)
88 {
89 StartSendMessageInternal(msg, writeFlags, completionDelegate);
90 }
91
92 /// <summary>
93 /// Receives a streaming request. Only one pending read action is allowe d at any given time.
94 /// completionDelegate is called when the operation finishes.
95 /// </summary>
96 public void StartReadMessage(AsyncCompletionDelegate<TRequest> completio nDelegate)
97 {
98 StartReadMessageInternal(completionDelegate);
99 }
100
101 /// <summary>
102 /// Initiates sending a initial metadata.
103 /// Even though C-core allows sending metadata in parallel to sending me ssages, we will treat sending metadata as a send message operation
104 /// to make things simpler.
105 /// completionDelegate is invoked upon completion.
106 /// </summary>
107 public void StartSendInitialMetadata(Metadata headers, AsyncCompletionDe legate<object> completionDelegate)
108 {
109 lock (myLock)
110 {
111 GrpcPreconditions.CheckNotNull(headers, "metadata");
112 GrpcPreconditions.CheckNotNull(completionDelegate, "Completion d elegate cannot be null");
113
114 GrpcPreconditions.CheckState(!initialMetadataSent, "Response hea ders can only be sent once per call.");
115 GrpcPreconditions.CheckState(streamingWritesCounter == 0, "Respo nse headers can only be sent before the first write starts.");
116 CheckSendingAllowed();
117
118 GrpcPreconditions.CheckNotNull(completionDelegate, "Completion d elegate cannot be null");
119
120 using (var metadataArray = MetadataArraySafeHandle.Create(header s))
121 {
122 call.StartSendInitialMetadata(HandleSendFinished, metadataAr ray);
123 }
124
125 this.initialMetadataSent = true;
126 sendCompletionDelegate = completionDelegate;
127 }
128 }
129
130 /// <summary>
131 /// Sends call result status, also indicating server is done with stream ing responses.
132 /// Only one pending send action is allowed at any given time.
133 /// completionDelegate is called when the operation finishes.
134 /// </summary>
135 public void StartSendStatusFromServer(Status status, Metadata trailers, AsyncCompletionDelegate<object> completionDelegate)
136 {
137 lock (myLock)
138 {
139 GrpcPreconditions.CheckNotNull(completionDelegate, "Completion d elegate cannot be null");
140 CheckSendingAllowed();
141
142 using (var metadataArray = MetadataArraySafeHandle.Create(traile rs))
143 {
144 call.StartSendStatusFromServer(HandleHalfclosed, status, met adataArray, !initialMetadataSent);
145 }
146 halfcloseRequested = true;
147 readingDone = true;
148 sendCompletionDelegate = completionDelegate;
149 }
150 }
151
152 /// <summary>
153 /// Gets cancellation token that gets cancelled once close completion
154 /// is received and the cancelled flag is set.
155 /// </summary>
156 public CancellationToken CancellationToken
157 {
158 get
159 {
160 return cancellationTokenSource.Token;
161 }
162 }
163
164 public string Peer
165 {
166 get
167 {
168 return call.GetPeer();
169 }
170 }
171
172 protected override bool IsClient
173 {
174 get { return false; }
175 }
176
177 protected override void CheckReadingAllowed()
178 {
179 base.CheckReadingAllowed();
180 GrpcPreconditions.CheckArgument(!cancelRequested);
181 }
182
183 protected override void OnAfterReleaseResources()
184 {
185 server.RemoveCallReference(this);
186 }
187
188 /// <summary>
189 /// Handles the server side close completion.
190 /// </summary>
191 private void HandleFinishedServerside(bool success, bool cancelled)
192 {
193 lock (myLock)
194 {
195 finished = true;
196 ReleaseResourcesIfPossible();
197 }
198 // TODO(jtattermusch): handle error
199
200 if (cancelled)
201 {
202 cancellationTokenSource.Cancel();
203 }
204
205 finishedServersideTcs.SetResult(null);
206 }
207 }
208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698