Visual Studio C#7 suggestions

This commit is contained in:
Christopher F
2017-04-23 15:23:06 -04:00
parent 6000b15c4d
commit 431b7fbd9f
21 changed files with 80 additions and 165 deletions

View File

@@ -131,8 +131,7 @@ namespace Discord.Audio
await keepaliveTask.ConfigureAwait(false);
_keepaliveTask = null;
long time;
while (_heartbeatTimes.TryDequeue(out time)) { }
while (_heartbeatTimes.TryDequeue(out long time)) { }
_lastMessageTime = 0;
await ClearInputStreamsAsync().ConfigureAwait(false);
@@ -186,8 +185,7 @@ namespace Discord.Audio
}
internal AudioInStream GetInputStream(ulong id)
{
StreamPair streamPair;
if (_streams.TryGetValue(id, out streamPair))
if (_streams.TryGetValue(id, out StreamPair streamPair))
return streamPair.Reader;
return null;
}
@@ -254,8 +252,7 @@ namespace Discord.Audio
{
await _audioLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false);
long time;
if (_heartbeatTimes.TryDequeue(out time))
if (_heartbeatTimes.TryDequeue(out long time))
{
int latency = (int)(Environment.TickCount - time);
int before = Latency;

View File

@@ -85,8 +85,7 @@ namespace Discord.Audio.Streams
long dist = nextTick - tick;
if (dist <= 0)
{
Frame frame;
if (_queuedFrames.TryDequeue(out frame))
if (_queuedFrames.TryDequeue(out Frame frame))
{
await _client.SetSpeakingAsync(true).ConfigureAwait(false);
_next.WriteHeader(seq++, timestamp, false);
@@ -100,7 +99,7 @@ namespace Discord.Audio.Streams
var _ = _logger?.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)");
#endif
}
else
else
{
while ((nextTick - tick) <= 0)
{
@@ -135,8 +134,7 @@ namespace Discord.Audio.Streams
cancelToken = _cancelToken;
await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false);
byte[] buffer;
if (!_bufferPool.TryDequeue(out buffer))
if (!_bufferPool.TryDequeue(out byte[] buffer))
{
#if DEBUG
var _ = _logger?.DebugAsync($"Buffer overflow"); //Should never happen because of the queueLock
@@ -166,10 +164,9 @@ namespace Discord.Audio.Streams
}
public override Task ClearAsync(CancellationToken cancelToken)
{
Frame ignored;
do
cancelToken.ThrowIfCancellationRequested();
while (_queuedFrames.TryDequeue(out ignored));
while (_queuedFrames.TryDequeue(out Frame ignored));
return Task.Delay(0);
}
}

View File

@@ -55,9 +55,8 @@ namespace Discord.Audio.Streams
{
cancelToken.ThrowIfCancellationRequested();
RTPFrame frame;
await _signal.WaitAsync(cancelToken).ConfigureAwait(false);
_frames.TryDequeue(out frame);
_frames.TryDequeue(out RTPFrame frame);
return frame;
}

View File

@@ -94,8 +94,7 @@ namespace Discord.Audio.Streams
continue;
}
Frame frame;
if (_queuedFrames.TryPeek(out frame))
if (_queuedFrames.TryPeek(out Frame frame))
{
silenceFrames = 0;
uint distance = (uint)(frame.Timestamp - _timestamp);
@@ -201,7 +200,6 @@ namespace Discord.Audio.Streams
return; //This is an old frame, ignore
}
byte[] buffer;
if (!await _queueLock.WaitAsync(0).ConfigureAwait(false))
{
#if DEBUG
@@ -209,7 +207,7 @@ namespace Discord.Audio.Streams
#endif
return;
}
_bufferPool.TryDequeue(out buffer);
_bufferPool.TryDequeue(out byte[] buffer);
Buffer.BlockCopy(data, offset, buffer, 0, count);
#if DEBUG
@@ -239,10 +237,9 @@ namespace Discord.Audio.Streams
}
public override Task ClearAsync(CancellationToken cancelToken)
{
Frame ignored;
do
cancelToken.ThrowIfCancellationRequested();
while (_queuedFrames.TryDequeue(out ignored));
while (_queuedFrames.TryDequeue(out Frame ignored));
return Task.Delay(0);
}
}

View File

@@ -41,15 +41,13 @@ namespace Discord.WebSocket
internal SocketChannel GetChannel(ulong id)
{
SocketChannel channel;
if (_channels.TryGetValue(id, out channel))
if (_channels.TryGetValue(id, out SocketChannel channel))
return channel;
return null;
}
internal SocketDMChannel GetDMChannel(ulong userId)
{
SocketDMChannel channel;
if (_dmChannels.TryGetValue(userId, out channel))
if (_dmChannels.TryGetValue(userId, out SocketDMChannel channel))
return channel;
return null;
}
@@ -57,31 +55,25 @@ namespace Discord.WebSocket
{
_channels[channel.Id] = channel;
var dmChannel = channel as SocketDMChannel;
if (dmChannel != null)
if (channel is SocketDMChannel dmChannel)
_dmChannels[dmChannel.Recipient.Id] = dmChannel;
else
{
var groupChannel = channel as SocketGroupChannel;
if (groupChannel != null)
if (channel is SocketGroupChannel groupChannel)
_groupChannels.TryAdd(groupChannel.Id);
}
}
internal SocketChannel RemoveChannel(ulong id)
{
SocketChannel channel;
if (_channels.TryRemove(id, out channel))
if (_channels.TryRemove(id, out SocketChannel channel))
{
var dmChannel = channel as SocketDMChannel;
if (dmChannel != null)
if (channel is SocketDMChannel dmChannel)
{
SocketDMChannel ignored;
_dmChannels.TryRemove(dmChannel.Recipient.Id, out ignored);
_dmChannels.TryRemove(dmChannel.Recipient.Id, out SocketDMChannel ignored);
}
else
{
var groupChannel = channel as SocketGroupChannel;
if (groupChannel != null)
if (channel is SocketGroupChannel groupChannel)
_groupChannels.TryRemove(id);
}
return channel;
@@ -91,8 +83,7 @@ namespace Discord.WebSocket
internal SocketGuild GetGuild(ulong id)
{
SocketGuild guild;
if (_guilds.TryGetValue(id, out guild))
if (_guilds.TryGetValue(id, out SocketGuild guild))
return guild;
return null;
}
@@ -102,16 +93,14 @@ namespace Discord.WebSocket
}
internal SocketGuild RemoveGuild(ulong id)
{
SocketGuild guild;
if (_guilds.TryRemove(id, out guild))
if (_guilds.TryRemove(id, out SocketGuild guild))
return guild;
return null;
}
internal SocketGlobalUser GetUser(ulong id)
{
SocketGlobalUser user;
if (_users.TryGetValue(id, out user))
if (_users.TryGetValue(id, out SocketGlobalUser user))
return user;
return null;
}
@@ -121,8 +110,7 @@ namespace Discord.WebSocket
}
internal SocketGlobalUser RemoveUser(ulong id)
{
SocketGlobalUser user;
if (_users.TryRemove(id, out user))
if (_users.TryRemove(id, out SocketGlobalUser user))
return user;
return null;
}

View File

@@ -197,7 +197,6 @@ namespace Discord.WebSocket
}
private async Task OnDisconnectingAsync(Exception ex)
{
ulong guildId;
await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false);
await ApiClient.DisconnectAsync().ConfigureAwait(false);
@@ -209,8 +208,7 @@ namespace Discord.WebSocket
await heartbeatTask.ConfigureAwait(false);
_heartbeatTask = null;
long time;
while (_heartbeatTimes.TryDequeue(out time)) { }
while (_heartbeatTimes.TryDequeue(out long time)) { }
_lastMessageTime = 0;
await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false);
@@ -221,7 +219,7 @@ namespace Discord.WebSocket
//Clear large guild queue
await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false);
while (_largeGuilds.TryDequeue(out guildId)) { }
while (_largeGuilds.TryDequeue(out ulong guildId)) { }
//Raise virtual GUILD_UNAVAILABLEs
await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false);
@@ -298,8 +296,7 @@ namespace Discord.WebSocket
/// <inheritdoc />
public RestVoiceRegion GetVoiceRegion(string id)
{
RestVoiceRegion region;
if (_voiceRegions.TryGetValue(id, out region))
if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region))
return region;
return null;
}
@@ -417,8 +414,7 @@ namespace Discord.WebSocket
{
await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false);
long time;
if (_heartbeatTimes.TryDequeue(out time))
if (_heartbeatTimes.TryDequeue(out long time))
{
int latency = (int)(Environment.TickCount - time);
int before = Latency;
@@ -903,8 +899,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
{
var user = channel.GetOrAddUser(data.User);
await TimedInvokeAsync(_recipientAddedEvent, nameof(RecipientAdded), user).ConfigureAwait(false);
@@ -921,8 +916,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
{
var user = channel.RemoveUser(data.User.Id);
if (user != null)
@@ -1094,12 +1088,11 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Message>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (guild != null && !guild.IsSynced)
{
{
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
}
@@ -1142,8 +1135,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Message>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (guild != null && !guild.IsSynced)
@@ -1174,9 +1166,9 @@ namespace Discord.WebSocket
after = SocketMessage.Create(this, State, author, channel, data);
}
var cacheableBefore = new Cacheable<IMessage, ulong>(before, data.Id, isCached , async () => await channel.GetMessageAsync(data.Id));
var cacheableBefore = new Cacheable<IMessage, ulong>(before, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id));
await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false);
await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false);
}
else
{
@@ -1190,8 +1182,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Message>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (!(guild?.IsSynced ?? true))
@@ -1218,8 +1209,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
bool isCached = cachedMsg != null;
@@ -1243,8 +1233,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
bool isCached = cachedMsg != null;
@@ -1268,8 +1257,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
bool isCached = cachedMsg != null;
@@ -1291,8 +1279,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<MessageDeleteBulkEvent>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (!(guild?.IsSynced ?? true))
@@ -1376,8 +1363,7 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer);
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;
if (channel != null)
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (!(guild?.IsSynced ?? true))

View File

@@ -120,15 +120,13 @@ namespace Discord.WebSocket
//Users
public new SocketGroupUser GetUser(ulong id)
{
SocketGroupUser user;
if (_users.TryGetValue(id, out user))
if (_users.TryGetValue(id, out SocketGroupUser user))
return user;
return null;
}
internal SocketGroupUser GetOrAddUser(UserModel model)
{
SocketGroupUser user;
if (_users.TryGetValue(model.Id, out user))
if (_users.TryGetValue(model.Id, out SocketGroupUser user))
return user as SocketGroupUser;
else
{
@@ -139,8 +137,7 @@ namespace Discord.WebSocket
}
internal SocketGroupUser RemoveUser(ulong id)
{
SocketGroupUser user;
if (_users.TryRemove(id, out user))
if (_users.TryRemove(id, out SocketGroupUser user))
{
user.GlobalUser.RemoveRef(Discord);
return user as SocketGroupUser;
@@ -158,15 +155,13 @@ namespace Discord.WebSocket
}
internal SocketVoiceState? GetVoiceState(ulong id)
{
SocketVoiceState voiceState;
if (_voiceStates.TryGetValue(id, out voiceState))
if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
return voiceState;
return null;
}
internal SocketVoiceState? RemoveVoiceState(ulong id)
{
SocketVoiceState voiceState;
if (_voiceStates.TryRemove(id, out voiceState))
if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
return voiceState;
return null;
}

View File

@@ -82,16 +82,7 @@ namespace Discord.WebSocket
=> Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray();
public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels
=> Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray();
public SocketGuildUser CurrentUser
{
get
{
SocketGuildUser member;
if (_members.TryGetValue(Discord.CurrentUser.Id, out member))
return member;
return null;
}
}
public SocketGuildUser CurrentUser => _members.TryGetValue(Discord.CurrentUser.Id, out SocketGuildUser member) ? member : null;
public SocketRole EveryoneRole => GetRole(Id);
public IReadOnlyCollection<SocketGuildChannel> Channels
{
@@ -162,8 +153,7 @@ namespace Discord.WebSocket
for (int i = 0; i < model.Presences.Length; i++)
{
SocketGuildUser member;
if (members.TryGetValue(model.Presences[i].User.Id, out member))
if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
member.Update(state, model.Presences[i], true);
else
Debug.Assert(false);
@@ -248,8 +238,7 @@ namespace Discord.WebSocket
for (int i = 0; i < model.Presences.Length; i++)
{
SocketGuildUser member;
if (members.TryGetValue(model.Presences[i].User.Id, out member))
if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
member.Update(state, model.Presences[i], true);
else
Debug.Assert(false);
@@ -343,8 +332,7 @@ namespace Discord.WebSocket
//Roles
public SocketRole GetRole(ulong id)
{
SocketRole value;
if (_roles.TryGetValue(id, out value))
if (_roles.TryGetValue(id, out SocketRole value))
return value;
return null;
}
@@ -359,8 +347,7 @@ namespace Discord.WebSocket
}
internal SocketRole RemoveRole(ulong id)
{
SocketRole role;
if (_roles.TryRemove(id, out role))
if (_roles.TryRemove(id, out SocketRole role))
return role;
return null;
}
@@ -368,8 +355,7 @@ namespace Discord.WebSocket
//Users
public SocketGuildUser GetUser(ulong id)
{
SocketGuildUser member;
if (_members.TryGetValue(id, out member))
if (_members.TryGetValue(id, out SocketGuildUser member))
return member;
return null;
}
@@ -378,8 +364,7 @@ namespace Discord.WebSocket
internal SocketGuildUser AddOrUpdateUser(UserModel model)
{
SocketGuildUser member;
if (_members.TryGetValue(model.Id, out member))
if (_members.TryGetValue(model.Id, out SocketGuildUser member))
member.GlobalUser?.Update(Discord.State, model);
else
{
@@ -391,8 +376,7 @@ namespace Discord.WebSocket
}
internal SocketGuildUser AddOrUpdateUser(MemberModel model)
{
SocketGuildUser member;
if (_members.TryGetValue(model.User.Id, out member))
if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
member.Update(Discord.State, model);
else
{
@@ -404,8 +388,7 @@ namespace Discord.WebSocket
}
internal SocketGuildUser AddOrUpdateUser(PresenceModel model)
{
SocketGuildUser member;
if (_members.TryGetValue(model.User.Id, out member))
if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
member.Update(Discord.State, model, false);
else
{
@@ -417,8 +400,7 @@ namespace Discord.WebSocket
}
internal SocketGuildUser RemoveUser(ulong id)
{
SocketGuildUser member;
if (_members.TryRemove(id, out member))
if (_members.TryRemove(id, out SocketGuildUser member))
{
DownloadedMemberCount--;
member.GlobalUser.RemoveRef(Discord);
@@ -466,15 +448,13 @@ namespace Discord.WebSocket
}
internal SocketVoiceState? GetVoiceState(ulong id)
{
SocketVoiceState voiceState;
if (_voiceStates.TryGetValue(id, out voiceState))
if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
return voiceState;
return null;
}
internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id)
{
SocketVoiceState voiceState;
if (_voiceStates.TryRemove(id, out voiceState))
if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
{
if (_audioClient != null)
await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream

View File

@@ -27,24 +27,20 @@ namespace Discord.WebSocket
{
_orderedMessages.Enqueue(message.Id);
ulong msgId;
SocketMessage msg;
while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out msgId))
_messages.TryRemove(msgId, out msg);
while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out ulong msgId))
_messages.TryRemove(msgId, out SocketMessage msg);
}
}
public SocketMessage Remove(ulong id)
{
SocketMessage msg;
_messages.TryRemove(id, out msg);
_messages.TryRemove(id, out SocketMessage msg);
return msg;
}
public SocketMessage Get(ulong id)
{
SocketMessage result;
if (_messages.TryGetValue(id, out result))
if (_messages.TryGetValue(id, out SocketMessage result))
return result;
return null;
}
@@ -67,8 +63,7 @@ namespace Discord.WebSocket
return cachedMessageIds
.Select(x =>
{
SocketMessage msg;
if (_messages.TryGetValue(x, out msg))
if (_messages.TryGetValue(x, out SocketMessage msg))
return msg;
return null;
})

View File

@@ -206,8 +206,7 @@ namespace Discord.Net.WebSockets
//Use the internal buffer if we can get it
resultCount = (int)stream.Length;
ArraySegment<byte> streamBuffer;
if (stream.TryGetBuffer(out streamBuffer))
if (stream.TryGetBuffer(out ArraySegment<byte> streamBuffer))
result = streamBuffer.Array;
else
result = stream.ToArray();