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
9 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System.Threading;
20 using System.Threading.Tasks;
22 namespace Thrift.Transports.Client
24 // ReSharper disable once InconsistentNaming
25 public class TStreamClientTransport : TClientTransport
27 private bool _isDisposed;
29 protected TStreamClientTransport()
33 public TStreamClientTransport(Stream inputStream, Stream outputStream)
35 InputStream = inputStream;
36 OutputStream = outputStream;
39 protected Stream OutputStream { get; set; }
41 protected Stream InputStream { get; set; }
43 public override bool IsOpen => true;
45 public override async Task OpenAsync(CancellationToken cancellationToken)
47 if (cancellationToken.IsCancellationRequested)
49 await Task.FromCanceled(cancellationToken);
53 public override void Close()
55 if (InputStream != null)
57 InputStream.Dispose();
61 if (OutputStream != null)
63 OutputStream.Dispose();
68 public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
69 CancellationToken cancellationToken)
71 if (InputStream == null)
73 throw new TTransportException(TTransportException.ExceptionType.NotOpen,
74 "Cannot read from null inputstream");
77 return await InputStream.ReadAsync(buffer, offset, length, cancellationToken);
80 public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
82 if (OutputStream == null)
84 throw new TTransportException(TTransportException.ExceptionType.NotOpen,
85 "Cannot write to null outputstream");
88 await OutputStream.WriteAsync(buffer, offset, length, cancellationToken);
91 public override async Task FlushAsync(CancellationToken cancellationToken)
93 await OutputStream.FlushAsync(cancellationToken);
97 protected override void Dispose(bool disposing)
103 InputStream?.Dispose();
104 OutputStream?.Dispose();