From 2aa099d9c8b53d98acb0196cf1ba1d39ab83b446 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Sun, 6 Sep 2015 05:13:23 -0300 Subject: [PATCH] Cleaned up connect methods --- src/Discord.Net/DiscordClient.cs | 142 +++++++++++++------------------ 1 file changed, 58 insertions(+), 84 deletions(-) diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index c50cae918..770e641ae 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -497,106 +497,80 @@ namespace Discord //Connection /// Connects to the Discord server with the provided token. - public Task Connect(string token) - => ConnectInternal(null, null, token); + public async Task Connect(string token) + { + await Disconnect(); + + if (_isDebugMode) + RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket is using cached token."); + + return await ConnectInternal(token); + } /// Connects to the Discord server with the provided email and password. /// Returns a token for future connections. - public Task Connect(string email, string password) - => ConnectInternal(email, password, null); + public async Task Connect(string email, string password) + { + await Disconnect(); + + var response = await _api.Login(email, password); + if (_isDebugMode) + RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token."); + + return await ConnectInternal(response.Token); + } /// Connects to the Discord server with the provided token, and will fall back to username and password. /// Returns a token for future connections. - public Task Connect(string email, string password, string token) - => ConnectInternal(email, password, token); + /*public Task Connect(string email, string password, string token) + => ConnectInternal(email, password, token);*/ /// Connects to the Discord server as an anonymous user with the provided username. /// Returns a token for future connections. - public Task ConnectAnonymous(string username) - => ConnectInternal(username, null, null); - public async Task ConnectInternal(string emailOrUsername, string password, string token) + public async Task ConnectAnonymous(string username) { - bool success = false; - string url = null; + var response = await _api.LoginAnonymous(username); + if (_isDebugMode) + RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got anonymous token."); + _http.Token = response.Token; - await Disconnect(); - _blockEvent.Reset(); - _disconnectToken = new CancellationTokenSource(); + return await ConnectInternal(response.Token); + } - if (token != null) - { - try - { - //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; - } + private async Task ConnectInternal(string token) + { + _http.Token = token; + string url = (await _api.GetWebSocketEndpoint()).Url; + if (_isDebugMode) + RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got endpoint."); - _http.Token = token; - url = (await _api.GetWebSocketEndpoint()).Url; - success = true; - } - if (success) + await _webSocket.ConnectAsync(url); + await _webSocket.Login(token); + + _disconnectToken = new CancellationTokenSource(); + if (_config.UseMessageQueue) + _mainTask = MessageQueueLoop(); + else + _mainTask = _disconnectToken.Wait(); + _mainTask = _mainTask.ContinueWith(async x => { - 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(); + await _webSocket.DisconnectAsync(); #if !DNXCORE50 - if (_config.EnableVoice) - await _voiceWebSocket.DisconnectAsync(); + if (_config.EnableVoice) + await _voiceWebSocket.DisconnectAsync(); #endif - //Clear send queue - Message ignored; - while (_pendingMessages.TryDequeue(out ignored)) { } + //Clear send queue + Message ignored; + while (_pendingMessages.TryDequeue(out ignored)) { } - _channels.Clear(); - _messages.Clear(); - _roles.Clear(); - _servers.Clear(); - _users.Clear(); + _channels.Clear(); + _messages.Clear(); + _roles.Clear(); + _servers.Clear(); + _users.Clear(); - _blockEvent.Set(); - _mainTask = null; - }).Unwrap(); - _isConnected = true; - } - else - { - token = null; - _http.Token = null; - } + _blockEvent.Set(); + _mainTask = null; + }).Unwrap(); + _isConnected = true; return token; } /// Disconnects from the Discord server, canceling any pending requests.