Browse Source

Fixed several websocket and datastore issues

tags/1.0-rc
RogueException 9 years ago
parent
commit
5f43369220
5 changed files with 30 additions and 20 deletions
  1. +3
    -1
      src/Discord.Net/Net/Queue/RestRequest.cs
  2. +3
    -1
      src/Discord.Net/Net/Rest/DefaultRestClient.cs
  3. +8
    -1
      src/Discord.Net/Net/WebSockets/DefaultWebsocketClient.cs
  4. +15
    -16
      src/Discord.Net/WebSocket/DiscordClient.cs
  5. +1
    -1
      src/Discord.Net/WebSocket/Entities/Guilds/Guild.cs

+ 3
- 1
src/Discord.Net/Net/Queue/RestRequest.cs View File

@@ -46,8 +46,10 @@ namespace Discord.Net.Queue
{
if (IsMultipart)
return await Client.Send(Method, Endpoint, MultipartParams, HeaderOnly).ConfigureAwait(false);
else
else if (Json != null)
return await Client.Send(Method, Endpoint, Json, HeaderOnly).ConfigureAwait(false);
else
return await Client.Send(Method, Endpoint, HeaderOnly).ConfigureAwait(false);
}
}
}

+ 3
- 1
src/Discord.Net/Net/Rest/DefaultRestClient.cs View File

@@ -32,8 +32,10 @@ namespace Discord.Net.Rest
UseProxy = false,
PreAuthenticate = false
});

SetHeader("accept-encoding", "gzip, deflate");

_cancelTokenSource = new CancellationTokenSource();
_cancelToken = CancellationToken.None;
_parentToken = CancellationToken.None;
}
protected virtual void Dispose(bool disposing)


+ 8
- 1
src/Discord.Net/Net/WebSockets/DefaultWebsocketClient.cs View File

@@ -28,6 +28,9 @@ namespace Discord.Net.WebSockets
_client = new ClientWebSocket();
_client.Options.Proxy = null;
_client.Options.KeepAliveInterval = TimeSpan.Zero;

_cancelTokenSource = new CancellationTokenSource();
_cancelToken = CancellationToken.None;
_parentToken = CancellationToken.None;
}
private void Dispose(bool disposing)
@@ -46,6 +49,7 @@ namespace Discord.Net.WebSockets

public async Task Connect(string host)
{
//Assume locked
await Disconnect().ConfigureAwait(false);

_cancelTokenSource = new CancellationTokenSource();
@@ -56,9 +60,11 @@ namespace Discord.Net.WebSockets
}
public async Task Disconnect()
{
//Assume locked
_cancelTokenSource.Cancel();

_client.Abort();
if (_client.State == WebSocketState.Open)
try { await _client?.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } catch { }
await (_task ?? Task.CompletedTask).ConfigureAwait(false);
}
@@ -75,6 +81,7 @@ namespace Discord.Net.WebSockets

public async Task Send(byte[] data, int offset, int count, bool isText)
{
//TODO: If connection is temporarily down, retry?
int frameCount = (int)Math.Ceiling((double)count / SendChunkSize);
for (int i = 0; i < frameCount; i++, offset += SendChunkSize)


+ 15
- 16
src/Discord.Net/WebSocket/DiscordClient.cs View File

@@ -279,7 +279,7 @@ namespace Discord.WebSocket
return models.Select(x => new Connection(x));
}

public async Task<Channel> GetChannel(ulong id)
public Channel GetChannel(ulong id)
{
return DataStore.GetChannel(id);
}
@@ -292,7 +292,7 @@ namespace Discord.WebSocket
return null;
}

public async Task<Guild> GetGuild(ulong id)
public Guild GetGuild(ulong id)
{
return DataStore.GetGuild(id);
}
@@ -303,11 +303,11 @@ namespace Discord.WebSocket
return new Guild(this, model);
}

public async Task<User> GetUser(ulong id)
public User GetUser(ulong id)
{
return DataStore.GetUser(id);
}
public async Task<User> GetUser(string username, ushort discriminator)
public User GetUser(string username, ushort discriminator)
{
return DataStore.Users.Where(x => x.Discriminator == discriminator && x.Username == username).FirstOrDefault();
}
@@ -317,7 +317,7 @@ namespace Discord.WebSocket
return models.Select(x => new User(this, x));
}

public async Task<VoiceRegion> GetVoiceRegion(string id)
public VoiceRegion GetVoiceRegion(string id)
{
VoiceRegion region;
if (_voiceRegions.TryGetValue(id, out region))
@@ -335,7 +335,6 @@ namespace Discord.WebSocket
//Global
case "READY":
{
//TODO: None of this is really threadsafe - should only replace the cache collections when they have been fully populated
//TODO: Store guilds even if they're unavailable
//TODO: Make downloading large guilds optional

@@ -866,31 +865,31 @@ namespace Discord.WebSocket
}
}

async Task<IChannel> IDiscordClient.GetChannel(ulong id)
=> await GetChannel(id).ConfigureAwait(false);
Task<IChannel> IDiscordClient.GetChannel(ulong id)
=> Task.FromResult<IChannel>(GetChannel(id));
Task<IEnumerable<IDMChannel>> IDiscordClient.GetDMChannels()
=> Task.FromResult<IEnumerable<IDMChannel>>(DMChannels.ToImmutableArray());
async Task<IEnumerable<IConnection>> IDiscordClient.GetConnections()
=> await GetConnections().ConfigureAwait(false);
async Task<IInvite> IDiscordClient.GetInvite(string inviteIdOrXkcd)
=> await GetInvite(inviteIdOrXkcd).ConfigureAwait(false);
async Task<IGuild> IDiscordClient.GetGuild(ulong id)
=> await GetGuild(id).ConfigureAwait(false);
Task<IGuild> IDiscordClient.GetGuild(ulong id)
=> Task.FromResult<IGuild>(GetGuild(id));
Task<IEnumerable<IUserGuild>> IDiscordClient.GetGuilds()
=> Task.FromResult<IEnumerable<IUserGuild>>(Guilds.ToImmutableArray());
async Task<IGuild> IDiscordClient.CreateGuild(string name, IVoiceRegion region, Stream jpegIcon)
=> await CreateGuild(name, region, jpegIcon).ConfigureAwait(false);
async Task<IUser> IDiscordClient.GetUser(ulong id)
=> await GetUser(id).ConfigureAwait(false);
async Task<IUser> IDiscordClient.GetUser(string username, ushort discriminator)
=> await GetUser(username, discriminator).ConfigureAwait(false);
Task<IUser> IDiscordClient.GetUser(ulong id)
=> Task.FromResult<IUser>(GetUser(id));
Task<IUser> IDiscordClient.GetUser(string username, ushort discriminator)
=> Task.FromResult<IUser>(GetUser(username, discriminator));
Task<ISelfUser> IDiscordClient.GetCurrentUser()
=> Task.FromResult<ISelfUser>(CurrentUser);
async Task<IEnumerable<IUser>> IDiscordClient.QueryUsers(string query, int limit)
=> await QueryUsers(query, limit).ConfigureAwait(false);
Task<IEnumerable<IVoiceRegion>> IDiscordClient.GetVoiceRegions()
=> Task.FromResult<IEnumerable<IVoiceRegion>>(VoiceRegions.ToImmutableArray());
async Task<IVoiceRegion> IDiscordClient.GetVoiceRegion(string id)
=> await GetVoiceRegion(id).ConfigureAwait(false);
Task<IVoiceRegion> IDiscordClient.GetVoiceRegion(string id)
=> Task.FromResult<IVoiceRegion>(GetVoiceRegion(id));
}
}

+ 1
- 1
src/Discord.Net/WebSocket/Entities/Guilds/Guild.cs View File

@@ -88,7 +88,7 @@ namespace Discord.WebSocket

Update(model);
}
private void Update(Model model)
private async void Update(Model model)
{
_afkChannelId = model.AFKChannelId;
AFKTimeout = model.AFKTimeout;


Loading…
Cancel
Save