]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/netcore/Thrift/Transports/Client/TMemoryBufferClientTransport.cs
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / netcore / Thrift / Transports / Client / TMemoryBufferClientTransport.cs
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 TMemoryBufferClientTransport : TClientTransport
26 {
27 private readonly MemoryStream _byteStream;
28 private bool _isDisposed;
29
30 public TMemoryBufferClientTransport()
31 {
32 _byteStream = new MemoryStream();
33 }
34
35 public TMemoryBufferClientTransport(byte[] buf)
36 {
37 _byteStream = new MemoryStream(buf);
38 }
39
40 public override bool IsOpen => true;
41
42 public override async Task OpenAsync(CancellationToken cancellationToken)
43 {
44 if (cancellationToken.IsCancellationRequested)
45 {
46 await Task.FromCanceled(cancellationToken);
47 }
48 }
49
50 public override void Close()
51 {
52 /** do nothing **/
53 }
54
55 public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
56 CancellationToken cancellationToken)
57 {
58 return await _byteStream.ReadAsync(buffer, offset, length, cancellationToken);
59 }
60
61 public override async Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
62 {
63 await _byteStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
64 }
65
66 public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
67 {
68 await _byteStream.WriteAsync(buffer, offset, length, cancellationToken);
69 }
70
71 public override async Task FlushAsync(CancellationToken cancellationToken)
72 {
73 if (cancellationToken.IsCancellationRequested)
74 {
75 await Task.FromCanceled(cancellationToken);
76 }
77 }
78
79 public byte[] GetBuffer()
80 {
81 return _byteStream.ToArray();
82 }
83
84 // IDisposable
85 protected override void Dispose(bool disposing)
86 {
87 if (!_isDisposed)
88 {
89 if (disposing)
90 {
91 _byteStream?.Dispose();
92 }
93 }
94 _isDisposed = true;
95 }
96 }
97 }