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 TMemoryBufferClientTransport : TClientTransport
27 private readonly MemoryStream _byteStream;
28 private bool _isDisposed;
30 public TMemoryBufferClientTransport()
32 _byteStream = new MemoryStream();
35 public TMemoryBufferClientTransport(byte[] buf)
37 _byteStream = new MemoryStream(buf);
40 public override bool IsOpen => true;
42 public override async Task OpenAsync(CancellationToken cancellationToken)
44 if (cancellationToken.IsCancellationRequested)
46 await Task.FromCanceled(cancellationToken);
50 public override void Close()
55 public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
56 CancellationToken cancellationToken)
58 return await _byteStream.ReadAsync(buffer, offset, length, cancellationToken);
61 public override async Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
63 await _byteStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
66 public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
68 await _byteStream.WriteAsync(buffer, offset, length, cancellationToken);
71 public override async Task FlushAsync(CancellationToken cancellationToken)
73 if (cancellationToken.IsCancellationRequested)
75 await Task.FromCanceled(cancellationToken);
79 public byte[] GetBuffer()
81 return _byteStream.ToArray();
85 protected override void Dispose(bool disposing)
91 _byteStream?.Dispose();