1
1
using System ;
2
+ using System . Globalization ;
2
3
using System . Threading ;
3
4
4
5
namespace Renci . SshNet
@@ -19,36 +20,36 @@ private ForwardedPortStatus(int value, string name)
19
20
_name = name ;
20
21
}
21
22
22
- public override bool Equals ( object obj )
23
+ public override bool Equals ( object other )
23
24
{
24
- if ( ReferenceEquals ( obj , null ) )
25
+ if ( ReferenceEquals ( other , null ) )
25
26
return false ;
26
27
27
- if ( ReferenceEquals ( this , obj ) )
28
+ if ( ReferenceEquals ( this , other ) )
28
29
return true ;
29
30
30
- var forwardedPortStatus = obj as ForwardedPortStatus ;
31
+ var forwardedPortStatus = other as ForwardedPortStatus ;
31
32
if ( forwardedPortStatus == null )
32
33
return false ;
33
34
34
35
return forwardedPortStatus . _value == _value ;
35
36
}
36
37
37
- public static bool operator == ( ForwardedPortStatus c1 , ForwardedPortStatus c2 )
38
+ public static bool operator == ( ForwardedPortStatus left , ForwardedPortStatus right )
38
39
{
39
40
// check if lhs is null
40
- if ( ReferenceEquals ( c1 , null ) )
41
+ if ( ReferenceEquals ( left , null ) )
41
42
{
42
43
// check if both lhs and rhs are null
43
- return ( ReferenceEquals ( c2 , null ) ) ;
44
+ return ( ReferenceEquals ( right , null ) ) ;
44
45
}
45
46
46
- return c1 . Equals ( c2 ) ;
47
+ return left . Equals ( right ) ;
47
48
}
48
49
49
- public static bool operator != ( ForwardedPortStatus c1 , ForwardedPortStatus c2 )
50
+ public static bool operator != ( ForwardedPortStatus left , ForwardedPortStatus right )
50
51
{
51
- return ! ( c1 == c2 ) ;
52
+ return ! ( left == right ) ;
52
53
}
53
54
54
55
public override int GetHashCode ( )
@@ -61,13 +62,25 @@ public override string ToString()
61
62
return _name ;
62
63
}
63
64
65
+ /// <summary>
66
+ /// Returns a value indicating whether <paramref name="status"/> has been changed to <see cref="Stopping"/>.
67
+ /// </summary>
68
+ /// <param name="status">The status to transition from.</param>
69
+ /// <returns>
70
+ /// <c>true</c> if <paramref name="status"/> has been changed to <see cref="Stopping"/>; otherwise, <c>false</c>.
71
+ /// </returns>
72
+ /// <exception cref="InvalidOperationException">Cannot transition <paramref name="status"/> to <see cref="Stopping"/>.</exception>
73
+ /// <remarks>
74
+ /// While a transition from <see cref="Stopped"/> to <see cref="Stopping"/> is not possible, this method will
75
+ /// return <c>false</c> for any such attempts. This is related to concurrency.
76
+ /// </remarks>
64
77
public static bool ToStopping ( ref ForwardedPortStatus status )
65
78
{
66
79
// attempt to transition from Started to Stopping
67
80
var previousStatus = Interlocked . CompareExchange ( ref status , Stopping , Started ) ;
68
81
if ( previousStatus == Stopping || previousStatus == Stopped )
69
82
{
70
- // status is already Stopping or Stopped, so no transition to Stopped is necessary
83
+ // status is already Stopping or Stopped, so no transition to Stopping is necessary
71
84
return false ;
72
85
}
73
86
@@ -79,14 +92,32 @@ public static bool ToStopping(ref ForwardedPortStatus status)
79
92
previousStatus = Interlocked . CompareExchange ( ref status , Stopping , Starting ) ;
80
93
if ( previousStatus == Stopping || previousStatus == Stopped )
81
94
{
82
- // status is already Stopping or Stopped, so no transition to Stopped is necessary
95
+ // status is already Stopping or Stopped, so no transition to Stopping is necessary
83
96
return false ;
84
97
}
85
98
86
99
// we've successfully transitioned from Starting to Stopping
87
- return status == Stopping ;
100
+ if ( status == Stopping )
101
+ return true ;
102
+
103
+ // there's no valid transition from status to Stopping
104
+ throw new InvalidOperationException ( string . Format ( "Forwarded port cannot transition from '{0}' to '{1}'." ,
105
+ previousStatus ,
106
+ Stopping ) ) ;
88
107
}
89
108
109
+ /// <summary>
110
+ /// Returns a value indicating whether <paramref name="status"/> has been changed to <see cref="Starting"/>.
111
+ /// </summary>
112
+ /// <param name="status">The status to transition from.</param>
113
+ /// <returns>
114
+ /// <c>true</c> if <paramref name="status"/> has been changed to <see cref="Starting"/>; otherwise, <c>false</c>.
115
+ /// </returns>
116
+ /// <exception cref="InvalidOperationException">Cannot transition <paramref name="status"/> to <see cref="Starting"/>.</exception>
117
+ /// <remarks>
118
+ /// While a transition from <see cref="Started"/> to <see cref="Starting"/> is not possible, this method will
119
+ /// return <c>false</c> for any such attempts. This is related to concurrency.
120
+ /// </remarks>
90
121
public static bool ToStarting ( ref ForwardedPortStatus status )
91
122
{
92
123
// attemp to transition from Stopped to Starting
@@ -101,8 +132,10 @@ public static bool ToStarting(ref ForwardedPortStatus status)
101
132
if ( status == Starting )
102
133
return true ;
103
134
104
- // there's no valid transition from Stopping to Starting
105
- throw new InvalidOperationException ( "Forwarded port is stopping." ) ;
135
+ // there's no valid transition from status to Starting
136
+ throw new InvalidOperationException ( string . Format ( "Forwarded port cannot transition from '{0}' to '{1}'." ,
137
+ previousStatus ,
138
+ Starting ) ) ;
106
139
}
107
140
}
108
141
}
0 commit comments