1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. 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,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 using System;
19 using System.Threading;
20 using System.Threading.Tasks;
21 using Thrift.Protocol;
22 
23 namespace Thrift
24 {
25     // ReSharper disable once InconsistentNaming
26     /// <summary>
27     ///     TBaseClient.
28     ///     Base client for generated clients.
29     ///     Do not change this class without checking generated code (namings, etc.)
30     /// </summary>
31     public abstract class TBaseClient
32     {
33         private readonly TProtocol _inputProtocol;
34         private readonly TProtocol _outputProtocol;
35         private bool _isDisposed;
36         private int _seqId;
37         public readonly Guid ClientId = Guid.NewGuid();
38 
TBaseClient(TProtocol inputProtocol, TProtocol outputProtocol)39         protected TBaseClient(TProtocol inputProtocol, TProtocol outputProtocol)
40         {
41             _inputProtocol = inputProtocol ?? throw new ArgumentNullException(nameof(inputProtocol));
42             _outputProtocol = outputProtocol ?? throw new ArgumentNullException(nameof(outputProtocol));
43         }
44 
45         public TProtocol InputProtocol => _inputProtocol;
46 
47         public TProtocol OutputProtocol => _outputProtocol;
48 
49         public int SeqId
50         {
51             get { return ++_seqId; }
52         }
53 
OpenTransportAsync()54         public virtual async Task OpenTransportAsync()
55         {
56             await OpenTransportAsync(CancellationToken.None);
57         }
58 
OpenTransportAsync(CancellationToken cancellationToken)59         public virtual async Task OpenTransportAsync(CancellationToken cancellationToken)
60         {
61             if (!_inputProtocol.Transport.IsOpen)
62             {
63                 await _inputProtocol.Transport.OpenAsync(cancellationToken);
64             }
65 
66             if (!_outputProtocol.Transport.IsOpen)
67             {
68                 await _outputProtocol.Transport.OpenAsync(cancellationToken);
69             }
70         }
71 
Dispose()72         public void Dispose()
73         {
74             Dispose(true);
75         }
76 
Dispose(bool disposing)77         protected virtual void Dispose(bool disposing)
78         {
79             if (!_isDisposed)
80             {
81                 if (disposing)
82                 {
83                     _inputProtocol?.Dispose();
84                     _outputProtocol?.Dispose();
85                 }
86             }
87 
88             _isDisposed = true;
89         }
90     }
91 }
92