#region Copyright notice and license // Copyright 2019 The gRPC Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #endregion using System; using System.Threading.Tasks; namespace Grpc.Core { /// /// Provides an abstraction over the callback providers /// used by AsyncUnaryCall, AsyncDuplexStreamingCall, etc /// internal struct AsyncCallState { readonly object responseHeadersAsync; // Task or Func> readonly object getStatusFunc; // Func or Func readonly object getTrailersFunc; // Func or Func readonly object disposeAction; // Action or Action readonly object? callbackState; // arg0 for the callbacks above, if needed internal AsyncCallState( Func> responseHeadersAsync, Func getStatusFunc, Func getTrailersFunc, Action disposeAction, object callbackState) { this.responseHeadersAsync = responseHeadersAsync; this.getStatusFunc = getStatusFunc; this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; this.callbackState = callbackState; } internal AsyncCallState( Task responseHeadersAsync, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { this.responseHeadersAsync = responseHeadersAsync; this.getStatusFunc = getStatusFunc; this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; this.callbackState = null; } internal Task ResponseHeadersAsync() { var withState = responseHeadersAsync as Func>; return withState != null ? withState(callbackState!) : (Task)responseHeadersAsync; } internal Status GetStatus() { var withState = getStatusFunc as Func; return withState != null ? withState(callbackState!) : ((Func)getStatusFunc)(); } internal Metadata GetTrailers() { var withState = getTrailersFunc as Func; return withState != null ? withState(callbackState!) : ((Func)getTrailersFunc)(); } internal void Dispose() { var withState = disposeAction as Action; if (withState != null) { withState(callbackState!); } else { ((Action)disposeAction)(); } } } }