@@ -27,6 +27,7 @@ public class HubEndPoint<THub> : IInvocationBinder where THub : Hub
27
27
{
28
28
private static readonly Base64Encoder Base64Encoder = new Base64Encoder ( ) ;
29
29
private static readonly PassThroughEncoder PassThroughEncoder = new PassThroughEncoder ( ) ;
30
+ private static readonly TimeSpan NegotiateTimeout = TimeSpan . FromSeconds ( 5 ) ;
30
31
31
32
private readonly Dictionary < string , HubMethodDescriptor > _methods = new Dictionary < string , HubMethodDescriptor > ( StringComparer . OrdinalIgnoreCase ) ;
32
33
@@ -108,35 +109,46 @@ async Task WriteToTransport()
108
109
109
110
private async Task < bool > ProcessNegotiate ( HubConnectionContext connection )
110
111
{
111
- while ( await connection . Input . WaitToReadAsync ( ) )
112
+ try
112
113
{
113
- while ( connection . Input . TryRead ( out var buffer ) )
114
+ using ( var cts = new CancellationTokenSource ( ) )
114
115
{
115
- if ( NegotiationProtocol . TryParseMessage ( buffer , out var negotiationMessage ) )
116
+ cts . CancelAfter ( NegotiateTimeout ) ;
117
+ while ( await connection . Input . WaitToReadAsync ( cts . Token ) )
116
118
{
117
- var protocol = _protocolResolver . GetProtocol ( negotiationMessage . Protocol , connection ) ;
119
+ while ( connection . Input . TryRead ( out var buffer ) )
120
+ {
121
+ if ( NegotiationProtocol . TryParseMessage ( buffer , out var negotiationMessage ) )
122
+ {
123
+ var protocol = _protocolResolver . GetProtocol ( negotiationMessage . Protocol , connection ) ;
118
124
119
- var transportCapabilities = connection . Features . Get < IConnectionTransportFeature > ( ) ? . TransportCapabilities
120
- ?? throw new InvalidOperationException ( "Unable to read transport capabilities." ) ;
125
+ var transportCapabilities = connection . Features . Get < IConnectionTransportFeature > ( ) ? . TransportCapabilities
126
+ ?? throw new InvalidOperationException ( "Unable to read transport capabilities." ) ;
121
127
122
- var dataEncoder = ( protocol . Type == ProtocolType . Binary && ( transportCapabilities & TransferMode . Binary ) == 0 )
123
- ? ( IDataEncoder ) Base64Encoder
124
- : PassThroughEncoder ;
128
+ var dataEncoder = ( protocol . Type == ProtocolType . Binary && ( transportCapabilities & TransferMode . Binary ) == 0 )
129
+ ? ( IDataEncoder ) Base64Encoder
130
+ : PassThroughEncoder ;
125
131
126
- var transferModeFeature = connection . Features . Get < ITransferModeFeature > ( ) ??
127
- throw new InvalidOperationException ( "Unable to read transfer mode." ) ;
132
+ var transferModeFeature = connection . Features . Get < ITransferModeFeature > ( ) ??
133
+ throw new InvalidOperationException ( "Unable to read transfer mode." ) ;
128
134
129
- transferModeFeature . TransferMode =
130
- ( protocol . Type == ProtocolType . Binary && ( transportCapabilities & TransferMode . Binary ) != 0 )
131
- ? TransferMode . Binary
132
- : TransferMode . Text ;
135
+ transferModeFeature . TransferMode =
136
+ ( protocol . Type == ProtocolType . Binary && ( transportCapabilities & TransferMode . Binary ) != 0 )
137
+ ? TransferMode . Binary
138
+ : TransferMode . Text ;
133
139
134
- connection . ProtocolReaderWriter = new HubProtocolReaderWriter ( protocol , dataEncoder ) ;
140
+ connection . ProtocolReaderWriter = new HubProtocolReaderWriter ( protocol , dataEncoder ) ;
135
141
136
- return true ;
142
+ return true ;
143
+ }
144
+ }
137
145
}
138
146
}
139
147
}
148
+ catch ( OperationCanceledException )
149
+ {
150
+ _logger . LogDebug ( "Negotiate was canceled." ) ;
151
+ }
140
152
141
153
return false ;
142
154
}
0 commit comments