@@ -48,6 +48,7 @@ internal class BinaryConnection : IConnection
48
48
private EndPoint _endPoint ;
49
49
private ConnectionDescription _description ;
50
50
private readonly Dropbox _dropbox = new Dropbox ( ) ;
51
+ private bool _failedEventHasBeenRaised ;
51
52
private DateTime _lastUsedAtUtc ;
52
53
private DateTime _openedAtUtc ;
53
54
private readonly object _openLock = new object ( ) ;
@@ -151,8 +152,18 @@ public ConnectionSettings Settings
151
152
// methods
152
153
private void ConnectionFailed ( Exception exception )
153
154
{
154
- if ( _state . TryChange ( State . Open , State . Failed ) )
155
+ if ( ! _state . TryChange ( State . Open , State . Failed ) )
155
156
{
157
+ var currentState = _state . Value ;
158
+ if ( currentState != State . Failed && currentState != State . Disposed )
159
+ {
160
+ throw new InvalidOperationException ( $ "Invalid BinaryConnection state transition from { currentState } to Failed.") ;
161
+ }
162
+ }
163
+
164
+ if ( ! _failedEventHasBeenRaised )
165
+ {
166
+ _failedEventHasBeenRaised = true ;
156
167
if ( _failedEventHandler != null )
157
168
{
158
169
_failedEventHandler ( new ConnectionFailedEvent ( _connectionId , exception ) ) ;
@@ -661,7 +672,14 @@ public OpenConnectionHelper(BinaryConnection connection)
661
672
662
673
public void FailedOpeningConnection ( Exception wrappedException )
663
674
{
664
- _connection . _state . TryChange ( State . Failed ) ;
675
+ if ( ! _connection . _state . TryChange ( State . Connecting , State . Failed ) && ! _connection . _state . TryChange ( State . Initializing , State . Failed ) )
676
+ {
677
+ var currentState = _connection. _state. Value;
678
+ if ( currentState ! = State . Disposed )
679
+ {
680
+ throw new InvalidOperationException( $"Invalid BinaryConnection state transition from {currentState} to Failed." ) ;
681
+ }
682
+ }
665
683
666
684
var handler = _connection. _failedOpeningEventHandler ;
667
685
if ( handler != null )
@@ -672,14 +690,37 @@ public void FailedOpeningConnection(Exception wrappedException)
672
690
673
691
public void InitializingConnection( )
674
692
{
675
- _connection. _state. TryChange( State . Initializing ) ;
693
+ if ( ! _connection . _state . TryChange ( State . Connecting , State . Initializing ) )
694
+ {
695
+ var currentState = _connection. _state . Value ;
696
+ if ( currentState == State . Disposed )
697
+ {
698
+ throw new ObjectDisposedException( typeof ( BinaryConnection ) . Name ) ;
699
+ }
700
+ else
701
+ {
702
+ throw new InvalidOperationException( $ "Invalid BinaryConnection state transition from { currentState } to Initializing.") ;
703
+ }
704
+ }
676
705
}
677
706
678
707
public void OpenedConnection( )
679
708
{
680
709
_stopwatch. Stop ( ) ;
681
710
_connection. _connectionId = _connection . _description . ConnectionId ;
682
- _connection . _state . TryChange ( State . Open ) ;
711
+
712
+ if ( ! _connection . _state . TryChange ( State . Initializing , State . Open ) )
713
+ {
714
+ var currentState = _connection. _state . Value ;
715
+ if ( currentState == State . Disposed )
716
+ {
717
+ throw new ObjectDisposedException( typeof ( BinaryConnection ) . Name ) ;
718
+ }
719
+ else
720
+ {
721
+ throw new InvalidOperationException( $ "Invalid BinaryConnection state transition from { currentState } to Open.") ;
722
+ }
723
+ }
683
724
684
725
var handler = _connection. _openedEventHandler ;
685
726
if ( handler != null )
@@ -883,6 +924,7 @@ public void SentMessages(int bufferLength)
883
924
884
925
private static class State
885
926
{
927
+ // note: the numeric values matter because sometimes we compare their magnitudes
886
928
public static int Initial = 0 ;
887
929
public static int Connecting = 1 ;
888
930
public static int Initializing = 2 ;
0 commit comments