Browse Source

Fixed DiscordClient.Disconnect

tags/docs-0.9
RogueException 9 years ago
parent
commit
3a074c4c04
2 changed files with 50 additions and 46 deletions
  1. +42
    -38
      src/Discord.Net/DiscordClient.cs
  2. +8
    -8
      src/Discord.Net/Net/Rest/SharpRestEngine.cs

+ 42
- 38
src/Discord.Net/DiscordClient.cs View File

@@ -24,7 +24,7 @@ namespace Discord
private readonly Semaphore _connectionLock; private readonly Semaphore _connectionLock;
private readonly ManualResetEvent _disconnectedEvent; private readonly ManualResetEvent _disconnectedEvent;
private readonly ManualResetEventSlim _connectedEvent; private readonly ManualResetEventSlim _connectedEvent;
private readonly TaskManager _taskManager;
private readonly TaskManager _taskManager;
private readonly ConcurrentDictionary<ulong, Server> _servers; private readonly ConcurrentDictionary<ulong, Server> _servers;
private readonly ConcurrentDictionary<ulong, Channel> _channels; private readonly ConcurrentDictionary<ulong, Channel> _channels;
private readonly ConcurrentDictionary<ulong, Channel> _privateChannels; //Key = RecipientId private readonly ConcurrentDictionary<ulong, Channel> _privateChannels; //Key = RecipientId
@@ -72,16 +72,16 @@ namespace Discord
public IEnumerable<Channel> PrivateChannels => _privateChannels.Select(x => x.Value); public IEnumerable<Channel> PrivateChannels => _privateChannels.Select(x => x.Value);
/// <summary> Gets a collection of all voice regions currently offered by Discord. </summary> /// <summary> Gets a collection of all voice regions currently offered by Discord. </summary>
public IEnumerable<Region> Regions => _regions.Select(x => x.Value); public IEnumerable<Region> Regions => _regions.Select(x => x.Value);
/// <summary> Initializes a new instance of the DiscordClient class. </summary>
public DiscordClient(DiscordConfig config = null)
{
Config = config ?? new DiscordConfig();
/// <summary> Initializes a new instance of the DiscordClient class. </summary>
public DiscordClient(DiscordConfig config = null)
{
Config = config ?? new DiscordConfig();
Config.Lock(); Config.Lock();
State = (int)ConnectionState.Disconnected;
Status = UserStatus.Online;
State = (int)ConnectionState.Disconnected;
Status = UserStatus.Online;
//Logging //Logging
Log = new LogManager(this); Log = new LogManager(this);
Logger = Log.CreateLogger("Discord"); Logger = Log.CreateLogger("Discord");
@@ -89,8 +89,8 @@ namespace Discord
//Async //Async
_taskManager = new TaskManager(Cleanup); _taskManager = new TaskManager(Cleanup);
_connectionLock = new Semaphore(1, 1); _connectionLock = new Semaphore(1, 1);
_disconnectedEvent = new ManualResetEvent(true);
_connectedEvent = new ManualResetEventSlim(false);
_disconnectedEvent = new ManualResetEvent(true);
_connectedEvent = new ManualResetEventSlim(false);
CancelToken = new CancellationToken(true); CancelToken = new CancellationToken(true);


//Cache //Cache
@@ -102,8 +102,8 @@ namespace Discord
_serializer = new JsonSerializer(); _serializer = new JsonSerializer();
_serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; _serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
#if TEST_RESPONSES #if TEST_RESPONSES
_serializer.CheckAdditionalContent = true;
_serializer.MissingMemberHandling = MissingMemberHandling.Error;
_serializer.CheckAdditionalContent = true;
_serializer.MissingMemberHandling = MissingMemberHandling.Error;
#else #else
_serializer.Error += (s, e) => _serializer.Error += (s, e) =>
{ {
@@ -123,14 +123,14 @@ namespace Discord
}; };
GatewaySocket.Disconnected += (s, e) => OnDisconnected(e.WasUnexpected, e.Exception); GatewaySocket.Disconnected += (s, e) => OnDisconnected(e.WasUnexpected, e.Exception);
GatewaySocket.ReceivedDispatch += (s, e) => OnReceivedEvent(e); GatewaySocket.ReceivedDispatch += (s, e) => OnReceivedEvent(e);
if (Config.UseMessageQueue)
MessageQueue = new MessageQueue(this);
Connected += async (s, e) =>
{
if (Config.UseMessageQueue)
MessageQueue = new MessageQueue(this);
Connected += async (s, e) =>
{
ClientAPI.CancelToken = CancelToken; ClientAPI.CancelToken = CancelToken;
await SendStatus().ConfigureAwait(false);
};
await SendStatus().ConfigureAwait(false);
};


//Extensibility //Extensibility
Services = new ServiceManager(this); Services = new ServiceManager(this);
@@ -140,18 +140,18 @@ namespace Discord
//_messageImporter.ContractResolver = new Message.ImportResolver(); //_messageImporter.ContractResolver = new Message.ImportResolver();
} }


/// <summary> Connects to the Discord server with the provided email and password. </summary>
/// <returns> Returns a token that can be optionally stored for future connections. </returns>
public async Task<string> Connect(string email, string password)
{
/// <summary> Connects to the Discord server with the provided email and password. </summary>
/// <returns> Returns a token that can be optionally stored for future connections. </returns>
public async Task<string> Connect(string email, string password)
{
if (email == null) throw new ArgumentNullException(email); if (email == null) throw new ArgumentNullException(email);
if (password == null) throw new ArgumentNullException(password); if (password == null) throw new ArgumentNullException(password);


await BeginConnect(email, password, null).ConfigureAwait(false); await BeginConnect(email, password, null).ConfigureAwait(false);
return ClientAPI.Token; return ClientAPI.Token;
} }
/// <summary> Connects to the Discord server with the provided token. </summary>
public async Task Connect(string token)
/// <summary> Connects to the Discord server with the provided token. </summary>
public async Task Connect(string token)
{ {
if (token == null) throw new ArgumentNullException(token); if (token == null) throw new ArgumentNullException(token);


@@ -171,8 +171,8 @@ namespace Discord
_taskManager.ClearException(); _taskManager.ClearException();
State = ConnectionState.Connecting; State = ConnectionState.Connecting;
_disconnectedEvent.Reset(); _disconnectedEvent.Reset();
await Login(email, password, token).ConfigureAwait(false);
await Login(email, password, token).ConfigureAwait(false);
await GatewaySocket.Connect().ConfigureAwait(false); await GatewaySocket.Connect().ConfigureAwait(false);


List<Task> tasks = new List<Task>(); List<Task> tasks = new List<Task>();
@@ -258,22 +258,25 @@ namespace Discord
} }
} }
private void EndConnect() private void EndConnect()
{
State = ConnectionState.Connected;
_connectedEvent.Set();
OnConnected();
}
{
State = ConnectionState.Connected;
_connectedEvent.Set();
OnConnected();
}


/// <summary> Disconnects from the Discord server, canceling any pending requests. </summary> /// <summary> Disconnects from the Discord server, canceling any pending requests. </summary>
public Task Disconnect() => _taskManager.Stop(true);
private async Task Cleanup()
public async Task Disconnect()
{
if (State == ConnectionState.Connected)
await ClientAPI.Send(new LogoutRequest()).ConfigureAwait(false);
await _taskManager.Stop(true).ConfigureAwait(false);
}
private Task Cleanup()
{ {
State = ConnectionState.Disconnecting; State = ConnectionState.Disconnecting;
if (Config.UseMessageQueue) if (Config.UseMessageQueue)
MessageQueue.Clear(); MessageQueue.Clear();


await ClientAPI.Send(new LogoutRequest()).ConfigureAwait(false);

ClientAPI.Token = null; ClientAPI.Token = null;
GatewaySocket.Token = null; GatewaySocket.Token = null;


@@ -287,6 +290,7 @@ namespace Discord
State = (int)ConnectionState.Disconnected; State = (int)ConnectionState.Disconnected;
_connectedEvent.Reset(); _connectedEvent.Reset();
_disconnectedEvent.Set(); _disconnectedEvent.Set();
return TaskHelper.CompletedTask;
} }
public Task SetStatus(UserStatus status) public Task SetStatus(UserStatus status)


+ 8
- 8
src/Discord.Net/Net/Rest/SharpRestEngine.cs View File

@@ -64,10 +64,10 @@ namespace Discord.Net.Rest
private async Task<string> Send(RestRequest request, CancellationToken cancelToken) private async Task<string> Send(RestRequest request, CancellationToken cancelToken)
{ {
int retryCount = 0; int retryCount = 0;
while (true)
{
var response = await _client.ExecuteTaskAsync(request, cancelToken).ConfigureAwait(false);
int statusCode = (int)response.StatusCode;
while (true)
{
var response = await _client.ExecuteTaskAsync(request, cancelToken).ConfigureAwait(false);
int statusCode = (int)response.StatusCode;
if (statusCode == 0) //Internal Error if (statusCode == 0) //Internal Error
{ {
//The request was aborted: Could not create SSL/TLS secure channel. //The request was aborted: Could not create SSL/TLS secure channel.
@@ -99,11 +99,11 @@ namespace Discord.Net.Rest
} }
throw new HttpException(response.StatusCode); throw new HttpException(response.StatusCode);
} }
else if (statusCode < 200 || statusCode >= 300) //2xx = Success
throw new HttpException(response.StatusCode);
else if (statusCode < 200 || statusCode >= 300) //2xx = Success
throw new HttpException(response.StatusCode);
else else
return response.Content;
}
return response.Content;
}
} }


private Method GetMethod(string method) private Method GetMethod(string method)


Loading…
Cancel
Save