@@ -16,8 +16,9 @@ public class WampSessionClient<TMessage> : IWampSessionClientExtended,
16
16
private static readonly AuthenticateExtraData EmptyAuthenticateDetails = new AuthenticateExtraData ( ) ;
17
17
private readonly IWampServerProxy mServerProxy ;
18
18
private TaskCompletionSource < bool > mOpenTask = new TaskCompletionSource < bool > ( ) ;
19
+ private TaskCompletionSource < GoodbyeMessage > mCloseTask ;
20
+ private GoodbyeMessage mGoodbyeMessage ;
19
21
private readonly IWampFormatter < TMessage > mFormatter ;
20
- private readonly object mLock = new object ( ) ;
21
22
private bool mGoodbyeSent ;
22
23
private readonly IWampClientAuthenticator mAuthenticator ;
23
24
private HelloDetails mSentDetails ;
@@ -115,6 +116,10 @@ public void Goodbye(GoodbyeDetails details, string reason)
115
116
{
116
117
mServerProxy . Goodbye ( new GoodbyeDetails ( ) , WampErrors . GoodbyeAndOut ) ;
117
118
}
119
+ else
120
+ {
121
+ mGoodbyeMessage = new GoodbyeMessage ( ) { Details = details , Reason = reason } ;
122
+ }
118
123
}
119
124
120
125
TrySetCloseEventArgs ( SessionCloseType . Goodbye , details , reason ) ;
@@ -126,11 +131,21 @@ private void RaiseConnectionBroken()
126
131
127
132
WampSessionCloseEventArgs closeEventArgs = mCloseEventArgs ;
128
133
129
- SetOpenTaskErrorIfNeeded ( new WampConnectionBrokenException ( mCloseEventArgs ) ) ;
130
-
131
134
Interlocked . CompareExchange ( ref mIsConnected , 0 , 1 ) ;
135
+
136
+ GoodbyeMessage goodbyeMessage = mGoodbyeMessage ;
137
+
138
+ if ( goodbyeMessage != null )
139
+ {
140
+ mCloseTask ? . SetResult ( goodbyeMessage ) ;
141
+ }
142
+
143
+ SetTasksErrorsIfNeeded ( new WampConnectionBrokenException ( mCloseEventArgs ) ) ;
144
+
132
145
mOpenTask = new TaskCompletionSource < bool > ( ) ;
146
+ mCloseTask = null ;
133
147
mCloseEventArgs = null ;
148
+ mGoodbyeMessage = null ;
134
149
135
150
OnConnectionBroken ( closeEventArgs ) ;
136
151
}
@@ -154,13 +169,20 @@ private void TrySetCloseEventArgs(SessionCloseType sessionCloseType,
154
169
155
170
public Task OpenTask => mOpenTask . Task ;
156
171
157
- public void Close ( string reason , GoodbyeDetails details )
172
+ public Task < GoodbyeMessage > Close ( string reason , GoodbyeDetails details )
158
173
{
159
174
reason = reason ?? WampErrors . CloseNormal ;
160
175
details = details ?? EmptyGoodbyeDetails ;
161
176
177
+ TaskCompletionSource < GoodbyeMessage > closeTask =
178
+ new TaskCompletionSource < GoodbyeMessage > ( ) ;
179
+
180
+ mCloseTask = closeTask ;
181
+
162
182
mGoodbyeSent = true ;
163
183
mServerProxy . Goodbye ( details , reason ) ;
184
+
185
+ return closeTask . Task ;
164
186
}
165
187
166
188
public void OnConnectionOpen ( )
@@ -191,19 +213,15 @@ public void OnConnectionClosed()
191
213
192
214
public void OnConnectionError ( Exception exception )
193
215
{
194
- SetOpenTaskErrorIfNeeded ( exception ) ;
216
+ SetTasksErrorsIfNeeded ( exception ) ;
195
217
196
218
OnConnectionError ( new WampConnectionErrorEventArgs ( exception ) ) ;
197
219
}
198
220
199
- private void SetOpenTaskErrorIfNeeded ( Exception exception )
221
+ private void SetTasksErrorsIfNeeded ( Exception exception )
200
222
{
201
- TaskCompletionSource < bool > openTask = mOpenTask ;
202
-
203
- if ( openTask != null )
204
- {
205
- openTask . TrySetException ( exception ) ;
206
- }
223
+ mOpenTask ? . TrySetException ( exception ) ;
224
+ mCloseTask ? . TrySetException ( exception ) ;
207
225
}
208
226
209
227
public event EventHandler < WampSessionCreatedEventArgs > ConnectionEstablished ;
0 commit comments