@@ -25,64 +25,64 @@ public ValueTask<int> ReadBytesAsync(ArraySegment<byte> buffer, IOBehavior ioBeh
25
25
{
26
26
return ioBehavior == IOBehavior . Asynchronous ?
27
27
new ValueTask < int > ( DoReadBytesAsync ( buffer ) ) : DoReadBytesSync ( buffer ) ;
28
+ }
28
29
29
- ValueTask < int > DoReadBytesSync ( ArraySegment < byte > buffer_ )
30
+ private ValueTask < int > DoReadBytesSync ( ArraySegment < byte > buffer )
31
+ {
32
+ try
30
33
{
31
- try
32
- {
33
- if ( RemainingTimeout == Constants . InfiniteTimeout )
34
- return new ValueTask < int > ( m_socket . Receive ( buffer_ . Array , buffer_ . Offset , buffer_ . Count , SocketFlags . None ) ) ;
34
+ if ( RemainingTimeout == Constants . InfiniteTimeout )
35
+ return new ValueTask < int > ( m_socket . Receive ( buffer . Array , buffer . Offset , buffer . Count , SocketFlags . None ) ) ;
35
36
36
- while ( RemainingTimeout > 0 )
37
+ while ( RemainingTimeout > 0 )
38
+ {
39
+ var startTime = Environment . TickCount ;
40
+ if ( m_socket . Poll ( Math . Min ( int . MaxValue / 1000 , RemainingTimeout ) * 1000 , SelectMode . SelectRead ) )
37
41
{
38
- var startTime = Environment . TickCount ;
39
- if ( m_socket . Poll ( Math . Min ( int . MaxValue / 1000 , RemainingTimeout ) * 1000 , SelectMode . SelectRead ) )
40
- {
41
- var bytesRead = m_socket . Receive ( buffer_ . Array , buffer_ . Offset , buffer_ . Count , SocketFlags . None ) ;
42
- RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
43
- return new ValueTask < int > ( bytesRead ) ;
44
- }
42
+ var bytesRead = m_socket . Receive ( buffer . Array , buffer . Offset , buffer . Count , SocketFlags . None ) ;
45
43
RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
44
+ return new ValueTask < int > ( bytesRead ) ;
46
45
}
47
- return ValueTaskExtensions . FromException < int > ( MySqlException . CreateForTimeout ( ) ) ;
48
- }
49
- catch ( Exception ex )
50
- {
51
- return ValueTaskExtensions . FromException < int > ( ex ) ;
46
+ RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
52
47
}
48
+ return ValueTaskExtensions . FromException < int > ( MySqlException . CreateForTimeout ( ) ) ;
53
49
}
50
+ catch ( Exception ex )
51
+ {
52
+ return ValueTaskExtensions . FromException < int > ( ex ) ;
53
+ }
54
+ }
54
55
55
- async Task < int > DoReadBytesAsync ( ArraySegment < byte > buffer_ )
56
+ private async Task < int > DoReadBytesAsync ( ArraySegment < byte > buffer )
57
+ {
58
+ var startTime = RemainingTimeout == Constants . InfiniteTimeout ? 0 : Environment . TickCount ;
59
+ var timerId = RemainingTimeout == Constants . InfiniteTimeout ? 0 :
60
+ RemainingTimeout <= 0 ? throw MySqlException . CreateForTimeout ( ) :
61
+ TimerQueue . Instance . Add ( RemainingTimeout , m_closeSocket ) ;
62
+ m_socketAwaitable . EventArgs . SetBuffer ( buffer . Array , buffer . Offset , buffer . Count ) ;
63
+ int bytesRead ;
64
+ try
65
+ {
66
+ await m_socket . ReceiveAsync ( m_socketAwaitable ) ;
67
+ bytesRead = m_socketAwaitable . EventArgs . BytesTransferred ;
68
+ }
69
+ catch ( SocketException ex )
56
70
{
57
- var startTime = RemainingTimeout == Constants . InfiniteTimeout ? 0 : Environment . TickCount ;
58
- var timerId = RemainingTimeout == Constants . InfiniteTimeout ? 0 :
59
- RemainingTimeout <= 0 ? throw MySqlException . CreateForTimeout ( ) :
60
- TimerQueue . Instance . Add ( RemainingTimeout , m_closeSocket ) ;
61
- m_socketAwaitable . EventArgs . SetBuffer ( buffer_ . Array , buffer_ . Offset , buffer_ . Count ) ;
62
- int bytesRead ;
63
- try
64
- {
65
- await m_socket . ReceiveAsync ( m_socketAwaitable ) ;
66
- bytesRead = m_socketAwaitable . EventArgs . BytesTransferred ;
67
- }
68
- catch ( SocketException ex )
69
- {
70
- if ( RemainingTimeout != Constants . InfiniteTimeout )
71
- {
72
- RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
73
- if ( ! TimerQueue . Instance . Remove ( timerId ) )
74
- throw MySqlException . CreateForTimeout ( ex ) ;
75
- }
76
- throw ;
77
- }
78
71
if ( RemainingTimeout != Constants . InfiniteTimeout )
79
72
{
80
73
RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
81
74
if ( ! TimerQueue . Instance . Remove ( timerId ) )
82
- throw MySqlException . CreateForTimeout ( ) ;
75
+ throw MySqlException . CreateForTimeout ( ex ) ;
83
76
}
84
- return bytesRead ;
77
+ throw ;
78
+ }
79
+ if ( RemainingTimeout != Constants . InfiniteTimeout )
80
+ {
81
+ RemainingTimeout -= unchecked ( Environment . TickCount - startTime ) ;
82
+ if ( ! TimerQueue . Instance . Remove ( timerId ) )
83
+ throw MySqlException . CreateForTimeout ( ) ;
85
84
}
85
+ return bytesRead ;
86
86
}
87
87
88
88
public ValueTask < int > WriteBytesAsync ( ArraySegment < byte > data , IOBehavior ioBehavior )
@@ -99,13 +99,13 @@ public ValueTask<int> WriteBytesAsync(ArraySegment<byte> data, IOBehavior ioBeha
99
99
{
100
100
return ValueTaskExtensions . FromException < int > ( ex ) ;
101
101
}
102
+ }
102
103
103
- async Task < int > DoWriteBytesAsync ( ArraySegment < byte > data_ )
104
- {
105
- m_socketAwaitable . EventArgs . SetBuffer ( data_ . Array , data_ . Offset , data_ . Count ) ;
106
- await m_socket . SendAsync ( m_socketAwaitable ) ;
107
- return 0 ;
108
- }
104
+ private async Task < int > DoWriteBytesAsync ( ArraySegment < byte > data )
105
+ {
106
+ m_socketAwaitable . EventArgs . SetBuffer ( data . Array , data . Offset , data . Count ) ;
107
+ await m_socket . SendAsync ( m_socketAwaitable ) ;
108
+ return 0 ;
109
109
}
110
110
111
111
readonly Socket m_socket ;
0 commit comments