forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlReadBuffer.cs
More file actions
567 lines (499 loc) · 20.1 KB
/
NpgsqlReadBuffer.cs
File metadata and controls
567 lines (499 loc) · 20.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#region License
// The PostgreSQL License
//
// Copyright (C) 2017 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Npgsql
{
/// <summary>
/// A buffer used by Npgsql to read data from the socket efficiently.
/// Provides methods which decode different values types and tracks the current position.
/// </summary>
public sealed class NpgsqlReadBuffer
{
#region Fields and Properties
public NpgsqlConnection Connection => Connector.Connection;
internal readonly NpgsqlConnector Connector;
internal Stream Underlying { private get; set; }
/// <summary>
/// The total byte length of the buffer.
/// </summary>
internal int Size { get; }
internal Encoding TextEncoding { get; }
internal int ReadPosition { get; private set; }
internal int ReadBytesLeft => _filledBytes - ReadPosition;
internal byte[] Buffer { get; }
int _filledBytes;
readonly Decoder _textDecoder;
readonly byte[] _workspace;
/// <summary>
/// Used for internal temporary purposes
/// </summary>
[CanBeNull]
char[] _tempCharBuf;
/// <summary>
/// The minimum buffer size possible.
/// </summary>
internal const int MinimumSize = 4096;
internal const int DefaultSize = 8192;
#endregion
#region Constructors
internal NpgsqlReadBuffer([CanBeNull] NpgsqlConnector connector, Stream stream, int size, Encoding textEncoding)
{
if (size < MinimumSize) {
throw new ArgumentOutOfRangeException(nameof(size), size, "Buffer size must be at least " + MinimumSize);
}
Connector = connector;
Underlying = stream;
Size = size;
Buffer = new byte[Size];
TextEncoding = textEncoding;
_textDecoder = TextEncoding.GetDecoder();
_workspace = new byte[8];
}
#endregion
#region I/O
/// <summary>
/// Ensures that <paramref name="count"/> bytes are available in the buffer, and if
/// not, reads from the socket until enough is available.
/// </summary>
public Task Ensure(int count, bool async) => Ensure(count, async, false);
internal void Ensure(int count)
{
if (count <= ReadBytesLeft)
return;
Ensure(count, false).GetAwaiter().GetResult();
}
internal Task Ensure(int count, bool async, bool dontBreakOnTimeouts)
=> count <= ReadBytesLeft ? PGUtil.CompletedTask : EnsureLong(count, async, dontBreakOnTimeouts);
async Task EnsureLong(int count, bool async, bool dontBreakOnTimeouts=false)
{
Debug.Assert(count <= Size);
Debug.Assert(count > ReadBytesLeft);
count -= ReadBytesLeft;
if (count <= 0) { return; }
if (ReadPosition == _filledBytes) {
Clear();
} else if (count > Size - _filledBytes) {
Array.Copy(Buffer, ReadPosition, Buffer, 0, ReadBytesLeft);
_filledBytes = ReadBytesLeft;
ReadPosition = 0;
}
try
{
while (count > 0)
{
var toRead = Size - _filledBytes;
var read = async
? await Underlying.ReadAsync(Buffer, _filledBytes, toRead)
: Underlying.Read(Buffer, _filledBytes, toRead);
if (read == 0)
throw new EndOfStreamException();
count -= read;
_filledBytes += read;
}
}
// We have a special case when reading async notifications - a timeout may be normal
// shouldn't be fatal
// Note that mono throws SocketException with the wrong error (see #1330)
catch (IOException e) when (
dontBreakOnTimeouts && (e.InnerException as SocketException)?.SocketErrorCode ==
(Type.GetType("Mono.Runtime") == null ? SocketError.TimedOut : SocketError.WouldBlock)
)
{
throw new TimeoutException("Timeout while reading from stream");
}
catch (Exception e)
{
Connector.Break();
throw new NpgsqlException("Exception while reading from stream", e);
}
}
internal Task ReadMore(bool async) => Ensure(ReadBytesLeft + 1, async);
internal NpgsqlReadBuffer AllocateOversize(int count)
{
Debug.Assert(count > Size);
var tempBuf = new NpgsqlReadBuffer(Connector, Underlying, count, TextEncoding);
CopyTo(tempBuf);
Clear();
return tempBuf;
}
/// <summary>
/// Does not perform any I/O - assuming that the bytes to be skipped are in the memory buffer.
/// </summary>
/// <param name="len"></param>
internal void Skip(long len)
{
Debug.Assert(ReadBytesLeft >= len);
ReadPosition += (int)len;
}
internal async Task Skip(long len, bool async)
{
Debug.Assert(len >= 0);
if (len > ReadBytesLeft)
{
len -= ReadBytesLeft;
while (len > Size)
{
Clear();
await Ensure(Size, async);
len -= Size;
}
Clear();
await Ensure((int)len, async);
}
ReadPosition += (int)len;
}
#endregion
#region Read Simple
public byte ReadByte()
{
Debug.Assert(ReadBytesLeft >= sizeof(byte));
return Buffer[ReadPosition++];
}
public short ReadInt16()
{
Debug.Assert(ReadBytesLeft >= sizeof(short));
var result = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Buffer, ReadPosition));
ReadPosition += 2;
return result;
}
public ushort ReadUInt16()
{
Debug.Assert(ReadBytesLeft >= sizeof(short));
var result = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Buffer, ReadPosition));
ReadPosition += 2;
return result;
}
public int ReadInt32()
{
Debug.Assert(ReadBytesLeft >= sizeof(int));
var result = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(Buffer, ReadPosition));
ReadPosition += 4;
return result;
}
public uint ReadUInt32()
{
Debug.Assert(ReadBytesLeft >= sizeof(int));
var result = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(Buffer, ReadPosition));
ReadPosition += 4;
return result;
}
public long ReadInt64()
{
Debug.Assert(ReadBytesLeft >= sizeof(long));
var result = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Buffer, ReadPosition));
ReadPosition += 8;
return result;
}
public float ReadSingle()
{
Debug.Assert(ReadBytesLeft >= sizeof(float));
if (BitConverter.IsLittleEndian)
{
_workspace[3] = Buffer[ReadPosition++];
_workspace[2] = Buffer[ReadPosition++];
_workspace[1] = Buffer[ReadPosition++];
_workspace[0] = Buffer[ReadPosition++];
return BitConverter.ToSingle(_workspace, 0);
}
else
{
var result = BitConverter.ToSingle(Buffer, ReadPosition);
ReadPosition += 4;
return result;
}
}
public double ReadDouble()
{
Debug.Assert(ReadBytesLeft >= sizeof(double));
if (BitConverter.IsLittleEndian)
{
_workspace[7] = Buffer[ReadPosition++];
_workspace[6] = Buffer[ReadPosition++];
_workspace[5] = Buffer[ReadPosition++];
_workspace[4] = Buffer[ReadPosition++];
_workspace[3] = Buffer[ReadPosition++];
_workspace[2] = Buffer[ReadPosition++];
_workspace[1] = Buffer[ReadPosition++];
_workspace[0] = Buffer[ReadPosition++];
return BitConverter.ToDouble(_workspace, 0);
}
else
{
var result = BitConverter.ToDouble(Buffer, ReadPosition);
ReadPosition += 8;
return result;
}
}
public string ReadString(int byteLen)
{
Debug.Assert(byteLen <= ReadBytesLeft);
var result = TextEncoding.GetString(Buffer, ReadPosition, byteLen);
ReadPosition += byteLen;
return result;
}
public char[] ReadChars(int byteLen)
{
Debug.Assert(byteLen <= ReadBytesLeft);
var result = TextEncoding.GetChars(Buffer, ReadPosition, byteLen);
ReadPosition += byteLen;
return result;
}
public void ReadBytes(byte[] output, int outputOffset, int len)
{
Debug.Assert(len <= ReadBytesLeft);
System.Buffer.BlockCopy(Buffer, ReadPosition, output, outputOffset, len);
ReadPosition += len;
}
#endregion
#region Read Complex
internal async ValueTask<int> ReadAllBytes(byte[] output, int outputOffset, int len, bool readOnce, bool async)
{
if (len <= ReadBytesLeft)
{
Array.Copy(Buffer, ReadPosition, output, outputOffset, len);
ReadPosition += len;
return len;
}
Array.Copy(Buffer, ReadPosition, output, outputOffset, ReadBytesLeft);
var offset = outputOffset + ReadBytesLeft;
var totalRead = ReadBytesLeft;
Clear();
try
{
while (totalRead < len)
{
var read = async
? await Underlying.ReadAsync(output, offset, len - totalRead)
: Underlying.Read(output, offset, len - totalRead);
if (read == 0)
throw new EndOfStreamException();
totalRead += read;
if (readOnce)
return totalRead;
offset += read;
}
}
catch (Exception e)
{
Connector.Break();
throw new NpgsqlException("Exception while reading from stream", e);
}
return len;
}
/// <summary>
/// Seeks the first null terminator (\0) and returns the string up to it. The buffer must already
/// contain the entire string and its terminator.
/// </summary>
public string ReadNullTerminatedString() => ReadNullTerminatedString(TextEncoding);
/// <summary>
/// Seeks the first null terminator (\0) and returns the string up to it. The buffer must already
/// contain the entire string and its terminator.
/// </summary>
/// <param name="encoding">Decodes the messages with this encoding.</param>
internal string ReadNullTerminatedString(Encoding encoding)
{
int i;
for (i = ReadPosition; Buffer[i] != 0; i++)
{
Debug.Assert(i <= ReadPosition + ReadBytesLeft);
}
Debug.Assert(i >= ReadPosition);
var result = encoding.GetString(Buffer, ReadPosition, i - ReadPosition);
ReadPosition = i + 1;
return result;
}
/// <summary>
/// Note that unlike the primitive readers, this reader can read any length, looping internally
/// and reading directly from the underlying stream.
/// </summary>
/// <param name="output">output buffer to fill</param>
/// <param name="outputOffset">offset in the output buffer in which to start writing</param>
/// <param name="charCount">number of character to be read into the output buffer</param>
/// <param name="byteCount">number of bytes left in the field. This method will not read bytes
/// beyond this count</param>
/// <param name="bytesRead">The number of bytes actually read.</param>
/// <param name="charsRead">The number of characters actually read.</param>
/// <returns>the number of bytes read</returns>
internal void ReadAllChars(char[] output, int outputOffset, int charCount, int byteCount, out int bytesRead, out int charsRead)
{
Debug.Assert(charCount <= output.Length - outputOffset);
bytesRead = 0;
charsRead = 0;
if (charCount == 0) { return; }
try
{
while (true)
{
Ensure(1); // Make sure we have at least some data
int bytesUsed, charsUsed;
bool completed;
var maxBytes = Math.Min(byteCount - bytesRead, ReadBytesLeft);
_textDecoder.Convert(Buffer, ReadPosition, maxBytes, output, outputOffset, charCount - charsRead, false,
out bytesUsed, out charsUsed, out completed);
ReadPosition += bytesUsed;
bytesRead += bytesUsed;
charsRead += charsUsed;
if (charsRead == charCount || bytesRead == byteCount)
return;
outputOffset += charsUsed;
Clear();
}
}
finally
{
_textDecoder.Reset();
}
}
/// <summary>
/// Skips over characters in the buffer, reading from the underlying stream as necessary.
/// </summary>
/// <param name="charCount">the number of characters to skip over.
/// int.MaxValue means all available characters (limited only by <paramref name="byteCount"/>).
/// </param>
/// <param name="byteCount">the maximal number of bytes to process</param>
/// <param name="bytesSkipped">The number of bytes actually skipped.</param>
/// <param name="charsSkipped">The number of characters actually skipped.</param>
/// <returns>the number of bytes read</returns>
internal void SkipChars(int charCount, int byteCount, out int bytesSkipped, out int charsSkipped)
{
if (_tempCharBuf == null)
_tempCharBuf = new char[1024];
charsSkipped = bytesSkipped = 0;
while (charsSkipped < charCount && bytesSkipped < byteCount)
{
ReadAllChars(_tempCharBuf, 0, Math.Min(charCount, _tempCharBuf.Length), byteCount, out var bSkipped, out var cSkipped);
charsSkipped += cSkipped;
bytesSkipped += bSkipped;
}
}
#endregion
#region Read PostGIS
internal int ReadInt32(ByteOrder bo)
{
Debug.Assert(ReadBytesLeft >= sizeof(int));
int result;
if (BitConverter.IsLittleEndian == (bo == ByteOrder.LSB))
{
result = BitConverter.ToInt32(Buffer, ReadPosition);
ReadPosition += 4;
}
else
{
_workspace[3] = Buffer[ReadPosition++];
_workspace[2] = Buffer[ReadPosition++];
_workspace[1] = Buffer[ReadPosition++];
_workspace[0] = Buffer[ReadPosition++];
result = BitConverter.ToInt32(_workspace, 0);
}
return result;
}
internal uint ReadUInt32(ByteOrder bo)
{
Debug.Assert(ReadBytesLeft >= sizeof(int));
uint result;
if (BitConverter.IsLittleEndian == (bo == ByteOrder.LSB))
{
result = BitConverter.ToUInt32(Buffer, ReadPosition);
ReadPosition += 4;
}
else
{
_workspace[3] = Buffer[ReadPosition++];
_workspace[2] = Buffer[ReadPosition++];
_workspace[1] = Buffer[ReadPosition++];
_workspace[0] = Buffer[ReadPosition++];
result = BitConverter.ToUInt32(_workspace, 0);
}
return result;
}
internal double ReadDouble(ByteOrder bo)
{
Debug.Assert(ReadBytesLeft >= sizeof(double));
if (BitConverter.IsLittleEndian == (ByteOrder.LSB == bo))
{
var result = BitConverter.ToDouble(Buffer, ReadPosition);
ReadPosition += 8;
return result;
}
else
{
_workspace[7] = Buffer[ReadPosition++];
_workspace[6] = Buffer[ReadPosition++];
_workspace[5] = Buffer[ReadPosition++];
_workspace[4] = Buffer[ReadPosition++];
_workspace[3] = Buffer[ReadPosition++];
_workspace[2] = Buffer[ReadPosition++];
_workspace[1] = Buffer[ReadPosition++];
_workspace[0] = Buffer[ReadPosition++];
return BitConverter.ToDouble(_workspace, 0);
}
}
#endregion
#region Misc
/// <summary>
/// Seeks within the current in-memory data. Does not read any data from the underlying.
/// </summary>
/// <param name="offset"></param>
/// <param name="origin"></param>
internal void Seek(int offset, SeekOrigin origin)
{
int absoluteOffset;
switch (origin)
{
case SeekOrigin.Begin:
absoluteOffset = offset;
break;
case SeekOrigin.Current:
absoluteOffset = ReadPosition + offset;
break;
case SeekOrigin.End:
throw new NotImplementedException();
default:
throw new ArgumentOutOfRangeException(nameof(origin));
}
Debug.Assert(absoluteOffset >= 0 && absoluteOffset <= _filledBytes);
ReadPosition = absoluteOffset;
}
internal void Clear()
{
ReadPosition = 0;
_filledBytes = 0;
}
internal void CopyTo(NpgsqlReadBuffer other)
{
Debug.Assert(other.Size - other._filledBytes >= ReadBytesLeft);
Array.Copy(Buffer, ReadPosition, other.Buffer, other._filledBytes, ReadBytesLeft);
other._filledBytes += ReadBytesLeft;
}
#endregion
}
}