@@ -1403,107 +1403,87 @@ internal bool TryReadChar(out char value)
1403
1403
{
1404
1404
Debug . Assert ( _syncOverAsync || ! _asyncReadWithoutSnapshot , "This method is not safe to call when doing sync over async" ) ;
1405
1405
1406
- byte [ ] buffer ;
1407
- int offset ;
1406
+ Span < byte > buffer = stackalloc byte [ 2 ] ;
1408
1407
if ( ( ( _inBytesUsed + 2 ) > _inBytesRead ) || ( _inBytesPacket < 2 ) )
1409
1408
{
1410
1409
// If the char isn't fully in the buffer, or if it isn't fully in the packet,
1411
1410
// then use ReadByteArray since the logic is there to take care of that.
1412
-
1413
- if ( ! TryReadByteArray ( _bTmp , 2 ) )
1411
+ if ( ! TryReadByteArray ( buffer , 2 ) )
1414
1412
{
1415
1413
value = '\0 ' ;
1416
1414
return false ;
1417
1415
}
1418
-
1419
- buffer = _bTmp ;
1420
- offset = 0 ;
1421
1416
}
1422
1417
else
1423
1418
{
1424
1419
// The entire char is in the packet and in the buffer, so just return it
1425
1420
// and take care of the counters.
1426
-
1427
- buffer = _inBuff ;
1428
- offset = _inBytesUsed ;
1429
-
1421
+ buffer = _inBuff . AsSpan ( _inBytesUsed , 2 ) ;
1430
1422
_inBytesUsed += 2 ;
1431
1423
_inBytesPacket -= 2 ;
1432
1424
}
1433
1425
1434
1426
AssertValidState ( ) ;
1435
- value = ( char ) ( ( buffer [ offset + 1 ] << 8 ) + buffer [ offset ] ) ;
1427
+ value = ( char ) ( ( buffer [ 1 ] << 8 ) + buffer [ 0 ] ) ;
1436
1428
return true ;
1437
1429
}
1438
1430
1439
1431
internal bool TryReadInt16 ( out short value )
1440
1432
{
1441
1433
Debug . Assert ( _syncOverAsync || ! _asyncReadWithoutSnapshot , "This method is not safe to call when doing sync over async" ) ;
1442
1434
1443
- byte [ ] buffer ;
1444
- int offset ;
1435
+ Span < byte > buffer = stackalloc byte [ 2 ] ;
1445
1436
if ( ( ( _inBytesUsed + 2 ) > _inBytesRead ) || ( _inBytesPacket < 2 ) )
1446
1437
{
1447
1438
// If the int16 isn't fully in the buffer, or if it isn't fully in the packet,
1448
1439
// then use ReadByteArray since the logic is there to take care of that.
1449
-
1450
- if ( ! TryReadByteArray ( _bTmp , 2 ) )
1440
+ if ( ! TryReadByteArray ( buffer , 2 ) )
1451
1441
{
1452
1442
value = default ;
1453
1443
return false ;
1454
1444
}
1455
-
1456
- buffer = _bTmp ;
1457
- offset = 0 ;
1458
1445
}
1459
1446
else
1460
1447
{
1461
1448
// The entire int16 is in the packet and in the buffer, so just return it
1462
1449
// and take care of the counters.
1463
-
1464
- buffer = _inBuff ;
1465
- offset = _inBytesUsed ;
1466
-
1450
+ buffer = _inBuff . AsSpan ( _inBytesUsed , 2 ) ;
1467
1451
_inBytesUsed += 2 ;
1468
1452
_inBytesPacket -= 2 ;
1469
1453
}
1470
1454
1471
1455
AssertValidState ( ) ;
1472
- value = ( short ) ( ( buffer [ offset + 1 ] << 8 ) + buffer [ offset ] ) ;
1456
+ value = ( short ) ( ( buffer [ 1 ] << 8 ) + buffer [ 0 ] ) ;
1473
1457
return true ;
1474
1458
}
1475
1459
1476
1460
internal bool TryReadInt32 ( out int value )
1477
1461
{
1478
1462
Debug . Assert ( _syncOverAsync || ! _asyncReadWithoutSnapshot , "This method is not safe to call when doing sync over async" ) ;
1463
+ Span < byte > buffer = stackalloc byte [ 4 ] ;
1479
1464
if ( ( ( _inBytesUsed + 4 ) > _inBytesRead ) || ( _inBytesPacket < 4 ) )
1480
1465
{
1481
1466
// If the int isn't fully in the buffer, or if it isn't fully in the packet,
1482
1467
// then use ReadByteArray since the logic is there to take care of that.
1483
-
1484
- if ( ! TryReadByteArray ( _bTmp , 4 ) )
1468
+ if ( ! TryReadByteArray ( buffer , 4 ) )
1485
1469
{
1486
1470
value = 0 ;
1487
1471
return false ;
1488
1472
}
1489
-
1490
- AssertValidState ( ) ;
1491
- value = BitConverter . ToInt32 ( _bTmp , 0 ) ;
1492
- return true ;
1493
1473
}
1494
1474
else
1495
1475
{
1496
1476
// The entire int is in the packet and in the buffer, so just return it
1497
1477
// and take care of the counters.
1498
-
1499
- value = BitConverter . ToInt32 ( _inBuff , _inBytesUsed ) ;
1500
-
1478
+ buffer = _inBuff . AsSpan ( _inBytesUsed , 4 ) ;
1501
1479
_inBytesUsed += 4 ;
1502
1480
_inBytesPacket -= 4 ;
1503
-
1504
- AssertValidState ( ) ;
1505
- return true ;
1506
1481
}
1482
+
1483
+ AssertValidState ( ) ;
1484
+ value = ( buffer [ 3 ] << 24 ) + ( buffer [ 2 ] << 16 ) + ( buffer [ 1 ] << 8 ) + buffer [ 0 ] ;
1485
+ return true ;
1486
+
1507
1487
}
1508
1488
1509
1489
// This method is safe to call when doing async without snapshot
@@ -1559,36 +1539,28 @@ internal bool TryReadUInt16(out ushort value)
1559
1539
{
1560
1540
Debug . Assert ( _syncOverAsync || ! _asyncReadWithoutSnapshot , "This method is not safe to call when doing sync over async" ) ;
1561
1541
1562
- byte [ ] buffer ;
1563
- int offset ;
1542
+ Span < byte > buffer = stackalloc byte [ 2 ] ;
1564
1543
if ( ( ( _inBytesUsed + 2 ) > _inBytesRead ) || ( _inBytesPacket < 2 ) )
1565
1544
{
1566
1545
// If the uint16 isn't fully in the buffer, or if it isn't fully in the packet,
1567
1546
// then use ReadByteArray since the logic is there to take care of that.
1568
-
1569
- if ( ! TryReadByteArray ( _bTmp , 2 ) )
1547
+ if ( ! TryReadByteArray ( buffer , 2 ) )
1570
1548
{
1571
1549
value = default ;
1572
1550
return false ;
1573
1551
}
1574
-
1575
- buffer = _bTmp ;
1576
- offset = 0 ;
1577
1552
}
1578
1553
else
1579
1554
{
1580
1555
// The entire uint16 is in the packet and in the buffer, so just return it
1581
1556
// and take care of the counters.
1582
-
1583
- buffer = _inBuff ;
1584
- offset = _inBytesUsed ;
1585
-
1557
+ buffer = _inBuff . AsSpan ( _inBytesUsed , 2 ) ;
1586
1558
_inBytesUsed += 2 ;
1587
1559
_inBytesPacket -= 2 ;
1588
1560
}
1589
1561
1590
1562
AssertValidState ( ) ;
1591
- value = ( ushort ) ( ( buffer [ offset + 1 ] << 8 ) + buffer [ offset ] ) ;
1563
+ value = ( ushort ) ( ( buffer [ 1 ] << 8 ) + buffer [ 0 ] ) ;
1592
1564
return true ;
1593
1565
}
1594
1566
@@ -3627,8 +3599,8 @@ private void SniWriteStatisticsAndTracing()
3627
3599
statistics. RequestNetworkServerTimer ( ) ;
3628
3600
}
3629
3601
}
3630
- [ Conditional ( "DEBUG" ) ]
3631
3602
3603
+ [ Conditional ( "DEBUG" ) ]
3632
3604
private void AssertValidState ( )
3633
3605
{
3634
3606
if ( _inBytesUsed < 0 || _inBytesRead < 0 )
0 commit comments