• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2019 The 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;
20 using System.Threading.Tasks;
21 using Grpc.Core.Utils;
22 
23 namespace Grpc.Core
24 {
25     /// <summary>
26     /// Base class for gRPC channel. Channels are an abstraction of long-lived connections to remote servers.
27     /// </summary>
28     public abstract class ChannelBase
29     {
30         private readonly string target;
31 
32         /// <summary>
33         /// Initializes a new instance of <see cref="ChannelBase"/> class that connects to a specific host.
34         /// </summary>
35         /// <param name="target">Target of the channel.</param>
ChannelBase(string target)36         protected ChannelBase(string target)
37         {
38             this.target = GrpcPreconditions.CheckNotNull(target, nameof(target));
39         }
40 
41         /// <summary>The original target used to create the channel.</summary>
42         public string Target
43         {
44             get { return this.target; }
45         }
46 
47         /// <summary>
48         /// Create a new <see cref="CallInvoker"/> for the channel.
49         /// </summary>
50         /// <returns>A new <see cref="CallInvoker"/>.</returns>
CreateCallInvoker()51         public abstract CallInvoker CreateCallInvoker();
52 
53         /// <summary>
54         /// Shuts down the channel cleanly. It is strongly recommended to shutdown
55         /// the channel once you stopped using it.
56         /// </summary>
57         /// <remarks>
58         /// Guidance for implementors:
59         /// This method doesn't wait for all calls on this channel to finish (nor does
60         /// it have to explicitly cancel all outstanding calls). It is user's responsibility to make sure
61         /// all the calls on this channel have finished (successfully or with an error)
62         /// before shutting down the channel to ensure channel shutdown won't impact
63         /// the outcome of those remote calls.
64         /// </remarks>
ShutdownAsync()65         public Task ShutdownAsync()
66         {
67             return ShutdownAsyncCore();
68         }
69 
70         /// <summary>Provides implementation of a non-virtual public member.</summary>
71         #pragma warning disable 1998
ShutdownAsyncCore()72         protected virtual async Task ShutdownAsyncCore()
73         {
74             // default implementation is no-op for backwards compatibility, but all implementations
75             // are expected to override this method.
76 
77             // warning 1998 is disabled to avoid needing TaskUtils.CompletedTask, which is
78             // only available in Grpc.Core
79         }
80         #pragma warning restore 1998
81     }
82 }
83