summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/lib/netcore/Thrift/Transports/Client/TBufferedClientTransport.cs
blob: 761f1ac783c6558df5c599a87851f1951b2bf0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Licensed to the Apache Software Foundation(ASF) under one
// or more contributor license agreements.See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Thrift.Transports.Client
{
    // ReSharper disable once InconsistentNaming
    public class TBufferedClientTransport : TClientTransport
    {
        private readonly int _bufSize;
        private readonly MemoryStream _inputBuffer = new MemoryStream(0);
        private readonly MemoryStream _outputBuffer = new MemoryStream(0);
        private readonly TClientTransport _transport;
        private bool _isDisposed;

        //TODO: should support only specified input transport?
        public TBufferedClientTransport(TClientTransport transport, int bufSize = 1024)
        {
            if (bufSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufSize), "Buffer size must be a positive number.");
            }

            _transport = transport ?? throw new ArgumentNullException(nameof(transport));
            _bufSize = bufSize;
        }

        public TClientTransport UnderlyingTransport
        {
            get
            {
                CheckNotDisposed();

                return _transport;
            }
        }

        public override bool IsOpen => !_isDisposed && _transport.IsOpen;

        public override async Task OpenAsync(CancellationToken cancellationToken)
        {
            CheckNotDisposed();

            await _transport.OpenAsync(cancellationToken);
        }

        public override void Close()
        {
            CheckNotDisposed();

            _transport.Close();
        }

        public override async Task<int> ReadAsync(byte[] buffer, int offset, int length,
            CancellationToken cancellationToken)
        {
            //TODO: investigate how it should work correctly
            CheckNotDisposed();

            ValidateBufferArgs(buffer, offset, length);

            if (!IsOpen)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen);
            }

            if (_inputBuffer.Capacity < _bufSize)
            {
                _inputBuffer.Capacity = _bufSize;
            }

            var got = await _inputBuffer.ReadAsync(buffer, offset, length, cancellationToken);
            if (got > 0)
            {
                return got;
            }

            _inputBuffer.Seek(0, SeekOrigin.Begin);
            _inputBuffer.SetLength(_inputBuffer.Capacity);

            ArraySegment<byte> bufSegment;
            _inputBuffer.TryGetBuffer(out bufSegment);

            // investigate
            var filled = await _transport.ReadAsync(bufSegment.Array, 0, (int) _inputBuffer.Length, cancellationToken);
            _inputBuffer.SetLength(filled);

            if (filled == 0)
            {
                return 0;
            }

            return await ReadAsync(buffer, offset, length, cancellationToken);
        }

        public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
        {
            CheckNotDisposed();

            ValidateBufferArgs(buffer, offset, length);

            if (!IsOpen)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen);
            }

            // Relative offset from "off" argument
            var writtenCount = 0;
            if (_outputBuffer.Length > 0)
            {
                var capa = (int) (_outputBuffer.Capacity - _outputBuffer.Length);
                var writeSize = capa <= length ? capa : length;
                await _outputBuffer.WriteAsync(buffer, offset, writeSize, cancellationToken);

                writtenCount += writeSize;
                if (writeSize == capa)
                {
                    //ArraySegment<byte> bufSegment;
                    //_outputBuffer.TryGetBuffer(out bufSegment);
                    var data = _outputBuffer.ToArray();
                    //await _transport.WriteAsync(bufSegment.Array, cancellationToken);
                    await _transport.WriteAsync(data, cancellationToken);
                    _outputBuffer.SetLength(0);
                }
            }

            while (length - writtenCount >= _bufSize)
            {
                await _transport.WriteAsync(buffer, offset + writtenCount, _bufSize, cancellationToken);
                writtenCount += _bufSize;
            }

            var remain = length - writtenCount;
            if (remain > 0)
            {
                if (_outputBuffer.Capacity < _bufSize)
                {
                    _outputBuffer.Capacity = _bufSize;
                }
                await _outputBuffer.WriteAsync(buffer, offset + writtenCount, remain, cancellationToken);
            }
        }

        public override async Task FlushAsync(CancellationToken cancellationToken)
        {
            CheckNotDisposed();

            if (!IsOpen)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen);
            }

            if (_outputBuffer.Length > 0)
            {
                //ArraySegment<byte> bufSegment;
                var data = _outputBuffer.ToArray(); // TryGetBuffer(out bufSegment);

                await _transport.WriteAsync(data /*bufSegment.Array*/, cancellationToken);
                _outputBuffer.SetLength(0);
            }

            await _transport.FlushAsync(cancellationToken);
        }

        private void CheckNotDisposed()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(_transport));
            }
        }

        // IDisposable
        protected override void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _inputBuffer?.Dispose();
                    _outputBuffer?.Dispose();
                    _transport?.Dispose();
                }
            }
            _isDisposed = true;
        }
    }
}