@@ -112,12 +112,12 @@ private ValueTask QueueMessage(BaseRequest<LiveRequestType> data) =>
112112
113113 public IAsyncUpdatable < WebsocketConnectionState > State => _state ;
114114
115- private async Task MessageLoop ( )
115+ private async Task MessageLoop ( CancellationToken cancellationToken )
116116 {
117117 try
118118 {
119- await foreach ( var msg in _channel . Reader . ReadAllAsync ( _linked . Token ) )
120- await JsonWebSocketUtils . SendFullMessage ( msg , _clientWebSocket ! , _linked . Token , JsonSerializerOptions ) ;
119+ await foreach ( var msg in _channel . Reader . ReadAllAsync ( cancellationToken ) )
120+ await JsonWebSocketUtils . SendFullMessage ( msg , _clientWebSocket ! , cancellationToken , JsonSerializerOptions ) ;
121121 }
122122 catch ( OperationCanceledException )
123123 {
@@ -128,11 +128,11 @@ private async Task MessageLoop()
128128 }
129129 }
130130
131- public struct Shutdown ;
131+ public readonly struct Shutdown ;
132132
133- public struct Reconnecting;
133+ public readonly struct Reconnecting;
134134
135- private async Task < OneOf < Success , NotFound , Shutdown , Reconnecting > > ConnectAsync ( )
135+ private async Task < OneOf < Success , Shutdown , Reconnecting > > ConnectAsync ( )
136136 {
137137 if ( _dispose . IsCancellationRequested )
138138 {
@@ -141,13 +141,21 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
141141 }
142142
143143 _state . Value = WebsocketConnectionState . Connecting ;
144+
144145#if NETSTANDARD2_1
145146 _currentConnectionClose ? . Cancel ( ) ;
146147#else
147148 if ( _currentConnectionClose != null ) await _currentConnectionClose . CancelAsync ( ) ;
148149#endif
150+
149151 _currentConnectionClose = new CancellationTokenSource ( ) ;
152+
153+ if ( _linked != _dispose )
154+ {
155+ _linked . Dispose ( ) ;
156+ }
150157 _linked = CancellationTokenSource . CreateLinkedTokenSource ( _dispose . Token , _currentConnectionClose . Token ) ;
158+ var cancellationToken = _linked . Token ;
151159
152160 _clientWebSocket ? . Abort ( ) ;
153161 _clientWebSocket ? . Dispose ( ) ;
@@ -160,32 +168,18 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
160168 _logger . LogInformation ( "Connecting to websocket...." ) ;
161169 try
162170 {
163- await _clientWebSocket . ConnectAsync ( new Uri ( $ "wss://{ Gateway } /1/ws/live/{ DeviceId } ") , _linked . Token ) ;
171+ await _clientWebSocket . ConnectAsync ( new Uri ( $ "wss://{ Gateway } /1/ws/live/{ DeviceId } ") , cancellationToken ) ;
164172
165173 _logger . LogInformation ( "Connected to websocket" ) ;
166174 _state . Value = WebsocketConnectionState . Connected ;
167175
168176#pragma warning disable CS4014
169- Run ( ReceiveLoop , _linked . Token ) ;
170- Run ( MessageLoop , _linked . Token ) ;
177+ Run ( ReceiveLoop ( cancellationToken ) , cancellationToken ) ;
178+ Run ( MessageLoop ( cancellationToken ) , cancellationToken ) ;
171179#pragma warning restore CS4014
172180
173181 return new Success ( ) ;
174182 }
175- catch ( WebSocketException e )
176- {
177- if ( e . Message . Contains ( "404" ) )
178- {
179- _logger . LogError ( "Device not found, shutting down" ) ;
180- _dispose . Dispose ( ) ;
181- #pragma warning disable CS4014
182- Run ( async ( ) => await _onDispose . InvokeAsyncParallel ( ) ) ;
183- #pragma warning restore CS4014
184- return new NotFound ( ) ;
185- }
186-
187- _logger . LogError ( e , "Error while connecting, retrying in 3 seconds" ) ;
188- }
189183 catch ( Exception e )
190184 {
191185 _logger . LogError ( e , "Error while connecting, retrying in 3 seconds" ) ;
@@ -229,9 +223,9 @@ private string GetUserAgent()
229223 }
230224
231225
232- private async Task ReceiveLoop ( )
226+ private async Task ReceiveLoop ( CancellationToken cancellationToken )
233227 {
234- while ( ! _linked . Token . IsCancellationRequested )
228+ while ( ! cancellationToken . IsCancellationRequested )
235229 {
236230 try
237231 {
@@ -244,7 +238,7 @@ private async Task ReceiveLoop()
244238 var message =
245239 await JsonWebSocketUtils
246240 . ReceiveFullMessageAsyncNonAlloc < LiveControlModels . BaseResponse < LiveResponseType > > (
247- _clientWebSocket , _linked . Token , JsonSerializerOptions ) ;
241+ _clientWebSocket , cancellationToken , JsonSerializerOptions ) ;
248242
249243 if ( message . IsT2 )
250244 {
@@ -257,7 +251,7 @@ await JsonWebSocketUtils
257251 try
258252 {
259253 await _clientWebSocket . CloseAsync ( WebSocketCloseStatus . NormalClosure , "Normal close" ,
260- _linked . Token ) ;
254+ cancellationToken ) ;
261255 }
262256 catch ( OperationCanceledException e )
263257 {
0 commit comments