1- #if NETCOREAPP2_1
1+ #if NETCOREAPP2_1 || NETSTANDARD2_1
22using System ;
33using System . Collections . Concurrent ;
44using System . Collections . Generic ;
@@ -24,8 +24,7 @@ internal class Http2Helper
2424 /// Task-based Asynchronous Pattern
2525 /// </summary>
2626 /// <returns></returns>
27- internal static async Task SendHttp2 ( Stream clientStream , Stream serverStream , int bufferSize ,
28- Action < byte [ ] , int , int > onDataSend , Action < byte [ ] , int , int > onDataReceive ,
27+ internal static async Task SendHttp2 ( Stream clientStream , Stream serverStream ,
2928 Func < SessionEventArgs > sessionFactory ,
3029 Func < SessionEventArgs , Task > onBeforeRequest , Func < SessionEventArgs , Task > onBeforeResponse ,
3130 CancellationTokenSource cancellationTokenSource , Guid connectionId ,
@@ -38,25 +37,25 @@ internal static async Task SendHttp2(Stream clientStream, Stream serverStream, i
3837
3938 // Now async relay all server=>client & client=>server data
4039 var sendRelay =
41- copyHttp2FrameAsync ( clientStream , serverStream , onDataSend , clientSettings , serverSettings ,
40+ copyHttp2FrameAsync ( clientStream , serverStream , clientSettings , serverSettings ,
4241 sessionFactory , sessions , onBeforeRequest ,
43- bufferSize , connectionId , true , cancellationTokenSource . Token , exceptionFunc ) ;
42+ connectionId , true , cancellationTokenSource . Token , exceptionFunc ) ;
4443 var receiveRelay =
45- copyHttp2FrameAsync ( serverStream , clientStream , onDataReceive , serverSettings , clientSettings ,
44+ copyHttp2FrameAsync ( serverStream , clientStream , serverSettings , clientSettings ,
4645 sessionFactory , sessions , onBeforeResponse ,
47- bufferSize , connectionId , false , cancellationTokenSource . Token , exceptionFunc ) ;
46+ connectionId , false , cancellationTokenSource . Token , exceptionFunc ) ;
4847
4948 await Task . WhenAny ( sendRelay , receiveRelay ) ;
5049 cancellationTokenSource . Cancel ( ) ;
5150
5251 await Task . WhenAll ( sendRelay , receiveRelay ) ;
5352 }
5453
55- private static async Task copyHttp2FrameAsync ( Stream input , Stream output , Action < byte [ ] , int , int > onCopy ,
54+ private static async Task copyHttp2FrameAsync ( Stream input , Stream output ,
5655 Http2Settings localSettings , Http2Settings remoteSettings ,
5756 Func < SessionEventArgs > sessionFactory , ConcurrentDictionary < int , SessionEventArgs > sessions ,
5857 Func < SessionEventArgs , Task > onBeforeRequestResponse ,
59- int bufferSize , Guid connectionId , bool isClient , CancellationToken cancellationToken ,
58+ Guid connectionId , bool isClient , CancellationToken cancellationToken ,
6059 ExceptionHandler exceptionFunc )
6160 {
6261 int headerTableSize = 0 ;
@@ -69,7 +68,6 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
6968 {
7069 var frameHeaderBuffer = frameHeader . Buffer ;
7170 int read = await forceRead ( input , frameHeaderBuffer , 0 , 9 , cancellationToken ) ;
72- onCopy ( frameHeaderBuffer , 0 , read ) ;
7371 if ( read != 9 )
7472 {
7573 return ;
@@ -92,7 +90,6 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
9290 }
9391
9492 read = await forceRead ( input , buffer , 0 , length , cancellationToken ) ;
95- onCopy ( buffer , 0 , read ) ;
9693 if ( read != length )
9794 {
9895 return ;
@@ -127,6 +124,11 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
127124 //System.Diagnostics.Debug.WriteLine("CONN: " + connectionId + ", CLIENT: " + isClient + ", STREAM: " + streamId + ", TYPE: " + type);
128125 if ( type == Http2FrameType . Data && args != null )
129126 {
127+ if ( isClient )
128+ args . OnDataSent ( buffer , 0 , read ) ;
129+ else
130+ args . OnDataReceived ( buffer , 0 , read ) ;
131+
130132 rr = isClient ? ( RequestResponseBase ) args . HttpClient . Request : args . HttpClient . Response ;
131133
132134 bool padded = ( flags & Http2FrameFlag . Padded ) != 0 ;
@@ -182,8 +184,10 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
182184 {
183185 args = sessionFactory ( ) ;
184186 args . IsPromise = true ;
185- sessions . TryAdd ( streamId , args ) ;
186- sessions . TryAdd ( promisedStreamId , args ) ;
187+ if ( ! sessions . TryAdd ( streamId , args ) )
188+ ;
189+ if ( ! sessions . TryAdd ( promisedStreamId , args ) )
190+ ;
187191 }
188192
189193 System . Diagnostics . Debug . WriteLine ( "PROMISE STREAM: " + streamId + ", " + promisedStreamId +
@@ -201,7 +205,8 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
201205 if ( ! sessions . TryGetValue ( streamId , out args ) )
202206 {
203207 args = sessionFactory ( ) ;
204- sessions . TryAdd ( streamId , args ) ;
208+ if ( ! sessions . TryAdd ( streamId , args ) )
209+ ;
205210 }
206211
207212 rr = isClient ? ( RequestResponseBase ) args . HttpClient . Request : args . HttpClient . Response ;
0 commit comments