@@ -48,7 +48,7 @@ public ValueTask<int> WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior
48
48
return ProtocolUtility . WritePayloadAsync ( m_uncompressedStreamByteHandler ! , GetNextUncompressedSequenceNumber , payload , ioBehavior ) . ContinueWith ( _ =>
49
49
{
50
50
if ( m_uncompressedStream ! . Length == 0 )
51
- return default ( ValueTask < int > ) ;
51
+ return default ;
52
52
53
53
if ( ! m_uncompressedStream . TryGetBuffer ( out var uncompressedData ) )
54
54
throw new InvalidOperationException ( "Couldn't get uncompressed stream buffer." ) ;
@@ -81,20 +81,20 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
81
81
if ( headerReadBytes . Count < 7 )
82
82
{
83
83
return protocolErrorBehavior == ProtocolErrorBehavior . Ignore ?
84
- default ( ValueTask < int > ) :
84
+ default :
85
85
ValueTaskExtensions . FromException < int > ( new EndOfStreamException ( "Wanted to read 7 bytes but only read {0} when reading compressed packet header" . FormatInvariant ( headerReadBytes . Count ) ) ) ;
86
86
}
87
87
88
88
var payloadLength = ( int ) SerializationUtility . ReadUInt32 ( headerReadBytes . Array ! , headerReadBytes . Offset , 3 ) ;
89
- int packetSequenceNumber = headerReadBytes . Array ! [ headerReadBytes . Offset + 3 ] ;
89
+ var packetSequenceNumber = headerReadBytes . Array ! [ headerReadBytes . Offset + 3 ] ;
90
90
var uncompressedLength = ( int ) SerializationUtility . ReadUInt32 ( headerReadBytes . Array , headerReadBytes . Offset + 4 , 3 ) ;
91
91
92
92
// verify the compressed packet sequence number
93
- var expectedSequenceNumber = GetNextCompressedSequenceNumber ( ) % 256 ;
93
+ var expectedSequenceNumber = GetNextCompressedSequenceNumber ( ) ;
94
94
if ( packetSequenceNumber != expectedSequenceNumber )
95
95
{
96
96
if ( protocolErrorBehavior == ProtocolErrorBehavior . Ignore )
97
- return default ( ValueTask < int > ) ;
97
+ return default ;
98
98
99
99
var exception = MySqlProtocolException . CreateForPacketOutOfOrder ( expectedSequenceNumber , packetSequenceNumber ) ;
100
100
return ValueTaskExtensions . FromException < int > ( exception ) ;
@@ -116,7 +116,7 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
116
116
if ( payloadReadBytes . Count < payloadLength )
117
117
{
118
118
return protocolErrorBehavior == ProtocolErrorBehavior . Ignore ?
119
- default ( ValueTask < int > ) :
119
+ default :
120
120
ValueTaskExtensions . FromException < int > ( new EndOfStreamException ( "Wanted to read {0} bytes but only read {1} when reading compressed payload" . FormatInvariant ( payloadLength , payloadReadBytes . Count ) ) ) ;
121
121
}
122
122
@@ -136,7 +136,7 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
136
136
// FLG & 0x40: has preset dictionary (not supported)
137
137
// CMF*256+FLG is a multiple of 31: header checksum
138
138
return protocolErrorBehavior == ProtocolErrorBehavior . Ignore ?
139
- default ( ValueTask < int > ) :
139
+ default :
140
140
ValueTaskExtensions . FromException < int > ( new NotSupportedException ( "Unsupported zlib header: {0:X2}{1:X2}" . FormatInvariant ( cmf , flg ) ) ) ;
141
141
}
142
142
@@ -151,14 +151,14 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
151
151
m_remainingData = new ( uncompressedData , 0 , bytesRead ) ;
152
152
153
153
var checksum = ComputeAdler32Checksum ( uncompressedData , 0 , bytesRead ) ;
154
- int adlerStartOffset = payloadReadBytes . Offset + payloadReadBytes . Count - 4 ;
154
+ var adlerStartOffset = payloadReadBytes . Offset + payloadReadBytes . Count - 4 ;
155
155
if ( payloadReadBytes . Array [ adlerStartOffset + 0 ] != ( ( checksum >> 24 ) & 0xFF ) ||
156
156
payloadReadBytes . Array [ adlerStartOffset + 1 ] != ( ( checksum >> 16 ) & 0xFF ) ||
157
157
payloadReadBytes . Array [ adlerStartOffset + 2 ] != ( ( checksum >> 8 ) & 0xFF ) ||
158
158
payloadReadBytes . Array [ adlerStartOffset + 3 ] != ( checksum & 0xFF ) )
159
159
{
160
160
return protocolErrorBehavior == ProtocolErrorBehavior . Ignore ?
161
- default ( ValueTask < int > ) :
161
+ default :
162
162
ValueTaskExtensions . FromException < int > ( new NotSupportedException ( "Invalid Adler-32 checksum of uncompressed data." ) ) ;
163
163
}
164
164
}
@@ -171,7 +171,7 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
171
171
} ) ;
172
172
}
173
173
174
- private int GetNextCompressedSequenceNumber ( ) => m_compressedSequenceNumber ++ ;
174
+ private byte GetNextCompressedSequenceNumber ( ) => m_compressedSequenceNumber ++ ;
175
175
176
176
private int GetNextUncompressedSequenceNumber ( ) => m_uncompressedSequenceNumber ++ ;
177
177
@@ -205,7 +205,7 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
205
205
throw new InvalidOperationException ( "Couldn't get compressed stream buffer." ) ;
206
206
}
207
207
208
- uint uncompressedLength = ( uint ) remainingUncompressedBytes ;
208
+ var uncompressedLength = ( uint ) remainingUncompressedBytes ;
209
209
if ( compressedData . Array is null || compressedData . Count >= remainingUncompressedBytes )
210
210
{
211
211
// setting the length to 0 indicates sending uncompressed data
@@ -215,20 +215,20 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
215
215
216
216
var buffer = new byte [ compressedData . Count + 7 ] ;
217
217
SerializationUtility . WriteUInt32 ( ( uint ) compressedData . Count , buffer , 0 , 3 ) ;
218
- buffer [ 3 ] = ( byte ) GetNextCompressedSequenceNumber ( ) ;
218
+ buffer [ 3 ] = GetNextCompressedSequenceNumber ( ) ;
219
219
SerializationUtility . WriteUInt32 ( uncompressedLength , buffer , 4 , 3 ) ;
220
220
Buffer . BlockCopy ( compressedData . Array ! , compressedData . Offset , buffer , 7 , compressedData . Count ) ;
221
221
222
222
remainingUncompressedData = remainingUncompressedData . Slice ( remainingUncompressedBytes ) ;
223
223
return m_byteHandler ! . WriteBytesAsync ( new ArraySegment < byte > ( buffer , 0 , buffer . Length ) , ioBehavior )
224
- . ContinueWith ( _ => remainingUncompressedData . Count == 0 ? default ( ValueTask < int > ) :
224
+ . ContinueWith ( _ => remainingUncompressedData . Count == 0 ? default :
225
225
CompressAndWrite ( remainingUncompressedData , ioBehavior ) ) ;
226
226
}
227
227
228
228
private uint ComputeAdler32Checksum ( byte [ ] data , int offset , int length )
229
229
{
230
230
int s1 = 1 , s2 = 0 ;
231
- for ( int i = 0 ; i < length ; i ++ )
231
+ for ( var i = 0 ; i < length ; i ++ )
232
232
{
233
233
s1 = ( s1 + data [ offset + i ] ) % 65521 ;
234
234
s2 = ( s2 + s1 ) % 65521 ;
@@ -269,8 +269,8 @@ public ValueTask<int> ReadBytesAsync(Memory<byte> buffer, IOBehavior ioBehavior)
269
269
IByteHandler ? m_byteHandler ;
270
270
readonly BufferedByteReader m_bufferedByteReader ;
271
271
readonly BufferedByteReader m_compressedBufferedByteReader ;
272
- int m_compressedSequenceNumber ;
273
- int m_uncompressedSequenceNumber ;
272
+ byte m_compressedSequenceNumber ;
273
+ byte m_uncompressedSequenceNumber ;
274
274
ArraySegment < byte > m_remainingData ;
275
275
bool m_isContinuationPacket ;
276
276
}
0 commit comments