Cleaned up connect methods

This commit is contained in:
Brandon Smith
2015-09-06 05:13:23 -03:00
parent 43e54f0042
commit 2aa099d9c8

View File

@@ -497,106 +497,80 @@ namespace Discord
//Connection //Connection
/// <summary> Connects to the Discord server with the provided token. </summary> /// <summary> Connects to the Discord server with the provided token. </summary>
public Task<string> Connect(string token) public async Task<string> Connect(string token)
=> ConnectInternal(null, null, token); {
await Disconnect();
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket is using cached token.");
return await ConnectInternal(token);
}
/// <summary> Connects to the Discord server with the provided email and password. </summary> /// <summary> Connects to the Discord server with the provided email and password. </summary>
/// <returns> Returns a token for future connections. </returns> /// <returns> Returns a token for future connections. </returns>
public Task<string> Connect(string email, string password) public async Task<string> Connect(string email, string password)
=> ConnectInternal(email, password, null); {
await Disconnect();
var response = await _api.Login(email, password);
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
return await ConnectInternal(response.Token);
}
/// <summary> Connects to the Discord server with the provided token, and will fall back to username and password. </summary> /// <summary> Connects to the Discord server with the provided token, and will fall back to username and password. </summary>
/// <returns> Returns a token for future connections. </returns> /// <returns> Returns a token for future connections. </returns>
public Task<string> Connect(string email, string password, string token) /*public Task<string> Connect(string email, string password, string token)
=> ConnectInternal(email, password, token); => ConnectInternal(email, password, token);*/
/// <summary> Connects to the Discord server as an anonymous user with the provided username. </summary> /// <summary> Connects to the Discord server as an anonymous user with the provided username. </summary>
/// <returns> Returns a token for future connections. </returns> /// <returns> Returns a token for future connections. </returns>
public Task<string> ConnectAnonymous(string username) public async Task<string> ConnectAnonymous(string username)
=> ConnectInternal(username, null, null);
public async Task<string> ConnectInternal(string emailOrUsername, string password, string token)
{ {
bool success = false; var response = await _api.LoginAnonymous(username);
string url = null; if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got anonymous token.");
_http.Token = response.Token;
return await ConnectInternal(response.Token);
}
private async Task<string> ConnectInternal(string token)
{
_http.Token = token;
string url = (await _api.GetWebSocketEndpoint()).Url;
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got endpoint.");
await _webSocket.ConnectAsync(url);
await _webSocket.Login(token);
await Disconnect();
_blockEvent.Reset();
_disconnectToken = new CancellationTokenSource(); _disconnectToken = new CancellationTokenSource();
if (_config.UseMessageQueue)
if (token != null) _mainTask = MessageQueueLoop();
else
_mainTask = _disconnectToken.Wait();
_mainTask = _mainTask.ContinueWith(async x =>
{ {
try await _webSocket.DisconnectAsync();
{
//Login using cached token
_http.Token = token;
url = (await _api.GetWebSocketEndpoint()).Url;
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
success = true;
}
catch (InvalidOperationException) //Bad Token
{
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket had a bad token.");
if (password == null) //If we don't have an alternate login, throw this error
throw;
}
}
if (!success)
{
if (password != null) //Normal Login
{
var response = await _api.Login(emailOrUsername, password);
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
token = response.Token;
}
else //Anonymous login
{
var response = await _api.LoginAnonymous(emailOrUsername);
if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket generated anonymous token.");
token = response.Token;
}
_http.Token = token;
url = (await _api.GetWebSocketEndpoint()).Url;
success = true;
}
if (success)
{
await _webSocket.ConnectAsync(url);
await _webSocket.Login(token);
if (_config.UseMessageQueue)
_mainTask = MessageQueueLoop();
else
_mainTask = _disconnectToken.Wait();
_mainTask = _mainTask.ContinueWith(async x =>
{
await _webSocket.DisconnectAsync();
#if !DNXCORE50 #if !DNXCORE50
if (_config.EnableVoice) if (_config.EnableVoice)
await _voiceWebSocket.DisconnectAsync(); await _voiceWebSocket.DisconnectAsync();
#endif #endif
//Clear send queue //Clear send queue
Message ignored; Message ignored;
while (_pendingMessages.TryDequeue(out ignored)) { } while (_pendingMessages.TryDequeue(out ignored)) { }
_channels.Clear(); _channels.Clear();
_messages.Clear(); _messages.Clear();
_roles.Clear(); _roles.Clear();
_servers.Clear(); _servers.Clear();
_users.Clear(); _users.Clear();
_blockEvent.Set(); _blockEvent.Set();
_mainTask = null; _mainTask = null;
}).Unwrap(); }).Unwrap();
_isConnected = true; _isConnected = true;
}
else
{
token = null;
_http.Token = null;
}
return token; return token;
} }
/// <summary> Disconnects from the Discord server, canceling any pending requests. </summary> /// <summary> Disconnects from the Discord server, canceling any pending requests. </summary>