Skip to content

Commit

Permalink
Make GbClient connect finish on formal connected
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
ermau committed Jun 28, 2014
1 parent ed0c657 commit 8fa0419
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Gablarski/Client/GablarskiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public int ReconnectAttemptFrequency

public async Task<ClientConnectionResult> ConnectAsync (Target target)
{
var tcs = new TaskCompletionSource<ClientConnectionResult>();
var oldTcs = Interlocked.Exchange (ref this.connectTcs, tcs);
if (oldTcs != null)
oldTcs.TrySetCanceled();

this.previousTarget = target;
this.running = true;
ClientConnectionResult result = await this.client.ConnectAsync (target).ConfigureAwait (false);
Expand All @@ -259,7 +264,7 @@ public async Task<ClientConnectionResult> ConnectAsync (Target target)
Audio.Start();
}

return result;
return await tcs.Task.ConfigureAwait (false);
}

public Task DisconnectAsync()
Expand Down Expand Up @@ -304,6 +309,7 @@ private void Setup (IClientConnection connection, IAudioEngine audioEngine, ICli
private int reconnectAttemptFrequency = 2000;
private int reconnectAttempt;
private bool connecting, running;
private TaskCompletionSource<ClientConnectionResult> connectTcs;

private TempestClient client;

Expand Down Expand Up @@ -364,6 +370,10 @@ private void OnServerInfoReceivedMessage (MessageEventArgs<ServerInfoMessage> e)
ServerInfo = e.Message.ServerInfo;
IsConnected = true;

var tcs = Interlocked.Exchange (ref this.connectTcs, null);
if (tcs != null)
tcs.TrySetResult (new ClientConnectionResult (ConnectionResult.Success, this.client.Connection.RemoteKey));

OnConnected (this, EventArgs.Empty);
}

Expand Down

0 comments on commit 8fa0419

Please sign in to comment.