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
463 lines (385 loc) · 15.5 KB
/
NpgsqlReadBuffer.cs
File metadata and controls
463 lines (385 loc) · 15.5 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
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
#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 partial 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; }
/// <summary>
/// Same as <see cref="TextEncoding"/>, except that it does not throw an exception if an invalid char is
/// encountered (exception fallback), but rather replaces it with a question mark character (replacement
/// fallback).
/// </summary>
internal Encoding RelaxedTextEncoding { get; }
internal int ReadPosition { get; set; }
internal int ReadBytesLeft => FilledBytes - ReadPosition;
internal readonly byte[] Buffer;
internal int FilledBytes;
ColumnStream? _columnStream;
/// <summary>
/// The minimum buffer size possible.
/// </summary>
internal const int MinimumSize = 4096;
internal const int DefaultSize = 8192;
#endregion
#region Constructors
internal NpgsqlReadBuffer(
NpgsqlConnector connector,
Stream stream,
int size,
Encoding textEncoding,
Encoding relaxedTextEncoding)
{
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;
RelaxedTextEncoding = relaxedTextEncoding;
}
#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)
{
return count <= ReadBytesLeft ? Task.CompletedTask : EnsureLong();
async Task EnsureLong()
{
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
{
var totalRead = 0;
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;
totalRead += read;
}
NpgsqlEventSource.Log.BytesRead(totalRead);
}
// 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, RelaxedTextEncoding);
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;
}
/// <summary>
/// Skip a given number of bytes.
/// </summary>
public 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
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => Read<sbyte>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => Read<byte>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadInt16()
=> ReadInt16(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadInt16(bool littleEndian)
{
var result = Read<short>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUInt16()
=> ReadUInt16(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUInt16(bool littleEndian)
{
var result = Read<ushort>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt32()
=> ReadInt32(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt32(bool littleEndian)
{
var result = Read<int>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt32()
=> ReadUInt32(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt32(bool littleEndian)
{
var result = Read<uint>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadInt64()
=> ReadInt64(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadInt64(bool littleEndian)
{
var result = Read<long>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadUInt64()
=> ReadUInt64(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadUInt64(bool littleEndian)
{
var result = Read<ulong>();
return littleEndian == BitConverter.IsLittleEndian
? result : BinaryPrimitives.ReverseEndianness(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadSingle()
=> ReadSingle(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadSingle(bool littleEndian)
{
var result = ReadInt32(littleEndian);
return Unsafe.As<int, float>(ref result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble()
=> ReadDouble(false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble(bool littleEndian)
{
var result = ReadInt64(littleEndian);
return Unsafe.As<long, double>(ref result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
T Read<T>()
{
if (Unsafe.SizeOf<T>() > ReadBytesLeft)
ThrowNotSpaceLeft();
var result = Unsafe.ReadUnaligned<T>(ref Buffer[ReadPosition]);
ReadPosition += Unsafe.SizeOf<T>();
return result;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowNotSpaceLeft()
=> throw new InvalidOperationException("There is not enough space left in the buffer.");
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(Span<byte> output)
{
Debug.Assert(output.Length <= ReadBytesLeft);
new Span<byte>(Buffer, ReadPosition, output.Length).CopyTo(output);
ReadPosition += output.Length;
}
public void ReadBytes(byte[] output, int outputOffset, int len)
=> ReadBytes(new Span<byte>(output, outputOffset, len));
public ReadOnlySpan<byte> ReadSpan(int len)
{
Debug.Assert(len <= ReadBytesLeft);
return new ReadOnlySpan<byte>(Buffer, ReadPosition, len);
}
public ReadOnlyMemory<byte> ReadMemory(int len)
{
Debug.Assert(len <= ReadBytesLeft);
return new ReadOnlyMemory<byte>(Buffer, ReadPosition, len);
}
#endregion
#region Read Complex
public ValueTask<int> ReadBytes(byte[] output, int outputOffset, int len, bool async)
{
var readFromBuffer = Math.Min(ReadBytesLeft, len);
if (readFromBuffer > 0)
{
System.Buffer.BlockCopy(Buffer, ReadPosition, output, outputOffset, readFromBuffer);
ReadPosition += len;
return new ValueTask<int>(readFromBuffer);
}
return new ValueTask<int>(ReadBytesLong());
async Task<int> ReadBytesLong()
{
Debug.Assert(ReadPosition == 0);
Clear();
try
{
var read = async
? await Underlying.ReadAsync(output, outputOffset, len)
: Underlying.Read(output, outputOffset, len);
if (read == 0)
throw new EndOfStreamException();
return read;
}
catch (Exception e)
{
Connector.Break();
throw new NpgsqlException("Exception while reading from stream", e);
}
}
}
public Stream GetStream(int len, bool canSeek)
{
if (_columnStream == null)
_columnStream = new ColumnStream(this);
_columnStream.Init(len, canSeek);
return _columnStream;
}
/// <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. If any character could not be decoded, a question
/// mark character is returned instead of throwing an exception.
/// </summary>
public string ReadNullTerminatedStringRelaxed() => ReadNullTerminatedString(RelaxedTextEncoding);
/// <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>
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;
}
public ReadOnlySpan<byte> GetNullTerminatedBytes()
{
int i;
for (i = ReadPosition; Buffer[i] != 0; i++)
Debug.Assert(i <= ReadPosition + ReadBytesLeft);
Debug.Assert(i >= ReadPosition);
var result = new ReadOnlySpan<byte>(Buffer, ReadPosition, i - ReadPosition);
ReadPosition = i + 1;
return result;
}
#endregion
#region Misc
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
}
}