]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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.IO; | |
19 | using System.Threading; | |
20 | using System.Threading.Tasks; | |
21 | ||
22 | namespace Thrift.Transports.Client | |
23 | { | |
24 | // ReSharper disable once InconsistentNaming | |
25 | public class TStreamClientTransport : TClientTransport | |
26 | { | |
27 | private bool _isDisposed; | |
28 | ||
29 | protected TStreamClientTransport() | |
30 | { | |
31 | } | |
32 | ||
33 | public TStreamClientTransport(Stream inputStream, Stream outputStream) | |
34 | { | |
35 | InputStream = inputStream; | |
36 | OutputStream = outputStream; | |
37 | } | |
38 | ||
39 | protected Stream OutputStream { get; set; } | |
40 | ||
41 | protected Stream InputStream { get; set; } | |
42 | ||
43 | public override bool IsOpen => true; | |
44 | ||
45 | public override async Task OpenAsync(CancellationToken cancellationToken) | |
46 | { | |
47 | if (cancellationToken.IsCancellationRequested) | |
48 | { | |
49 | await Task.FromCanceled(cancellationToken); | |
50 | } | |
51 | } | |
52 | ||
53 | public override void Close() | |
54 | { | |
55 | if (InputStream != null) | |
56 | { | |
57 | InputStream.Dispose(); | |
58 | InputStream = null; | |
59 | } | |
60 | ||
61 | if (OutputStream != null) | |
62 | { | |
63 | OutputStream.Dispose(); | |
64 | OutputStream = null; | |
65 | } | |
66 | } | |
67 | ||
68 | public override async Task<int> ReadAsync(byte[] buffer, int offset, int length, | |
69 | CancellationToken cancellationToken) | |
70 | { | |
71 | if (InputStream == null) | |
72 | { | |
73 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, | |
74 | "Cannot read from null inputstream"); | |
75 | } | |
76 | ||
77 | return await InputStream.ReadAsync(buffer, offset, length, cancellationToken); | |
78 | } | |
79 | ||
80 | public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) | |
81 | { | |
82 | if (OutputStream == null) | |
83 | { | |
84 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, | |
85 | "Cannot write to null outputstream"); | |
86 | } | |
87 | ||
88 | await OutputStream.WriteAsync(buffer, offset, length, cancellationToken); | |
89 | } | |
90 | ||
91 | public override async Task FlushAsync(CancellationToken cancellationToken) | |
92 | { | |
93 | await OutputStream.FlushAsync(cancellationToken); | |
94 | } | |
95 | ||
96 | // IDisposable | |
97 | protected override void Dispose(bool disposing) | |
98 | { | |
99 | if (!_isDisposed) | |
100 | { | |
101 | if (disposing) | |
102 | { | |
103 | InputStream?.Dispose(); | |
104 | OutputStream?.Dispose(); | |
105 | } | |
106 | } | |
107 | _isDisposed = true; | |
108 | } | |
109 | } | |
110 | } |