Refactoring and fixed a few stylecop errors

This commit is contained in:
RogueException
2015-12-09 00:56:09 -04:00
parent a44e08bba1
commit 82746e9207
25 changed files with 574 additions and 505 deletions

View File

@@ -67,18 +67,21 @@
<Compile Include="..\Discord.Net.Audio\Net\WebSockets\VoiceWebSocket.Events.cs">
<Link>Net\WebSockets\VoiceWebSocket.Events.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\Opus.cs">
<Link>Opus.cs</Link>
<Compile Include="..\Discord.Net.Audio\Opus\Enums.cs">
<Link>Opus\Enums.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\OpusDecoder.cs">
<Link>OpusDecoder.cs</Link>
<Compile Include="..\Discord.Net.Audio\Opus\OpusDecoder.cs">
<Link>Opus\OpusDecoder.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\OpusEncoder.cs">
<Link>OpusEncoder.cs</Link>
<Compile Include="..\Discord.Net.Audio\Opus\OpusEncoder.cs">
<Link>Opus\OpusEncoder.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\Sodium.cs">
<Link>Sodium.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\Sodium\SecretBox.cs">
<Link>Sodium\SecretBox.cs</Link>
</Compile>
<Compile Include="..\Discord.Net.Audio\VoiceBuffer.cs">
<Link>VoiceBuffer.cs</Link>
</Compile>

View File

@@ -145,13 +145,13 @@ namespace Discord.Audio
return Task.FromResult(_defaultClient);
}
var client = _voiceClients.GetOrAdd(server.Id, _ =>
var client = _voiceClients.GetOrAdd(server.Id, (Func<long, DiscordAudioClient>)(_ =>
{
int id = unchecked(++_nextClientId);
var logger = Client.Log().CreateLogger($"Voice #{id}");
GatewayWebSocket gatewaySocket = null;
Net.WebSockets.GatewaySocket gatewaySocket = null;
var voiceSocket = new VoiceWebSocket(Client.Config, _config, logger);
var voiceClient = new DiscordAudioClient(this, id, logger, gatewaySocket, voiceSocket);
var voiceClient = new DiscordAudioClient((AudioService)(this), (int)id, (Logger)logger, (Net.WebSockets.GatewaySocket)gatewaySocket, (VoiceWebSocket)voiceSocket);
voiceClient.SetServerId(server.Id);
voiceSocket.OnPacket += (s, e) =>
@@ -165,7 +165,7 @@ namespace Discord.Audio
};
return voiceClient;
});
}));
//await client.Connect(gatewaySocket.Host, _client.Token).ConfigureAwait(false);
return Task.FromResult(client);
}

View File

@@ -37,6 +37,10 @@ namespace Discord.Audio
public int? Bitrate { get { return _bitrate; } set { SetValue(ref _bitrate, value); } }
private int? _bitrate = null;
/// <summary> Gets or sets the number of channels (1 or 2) used for outgoing audio. </summary>
public int Channels { get { return _channels; } set { SetValue(ref _channels, value); } }
private int _channels = 1;
//Lock
protected bool _isLocked;
internal void Lock() { _isLocked = true; }

View File

@@ -10,14 +10,14 @@ namespace Discord.Audio
public int Id => _id;
private readonly AudioService _service;
private readonly GatewayWebSocket _gatewaySocket;
private readonly GatewaySocket _gatewaySocket;
private readonly VoiceWebSocket _voiceSocket;
private readonly Logger _logger;
public long? ServerId => _voiceSocket.ServerId;
public long? ChannelId => _voiceSocket.ChannelId;
public DiscordAudioClient(AudioService service, int id, Logger logger, GatewayWebSocket gatewaySocket, VoiceWebSocket voiceSocket)
public DiscordAudioClient(AudioService service, int id, Logger logger, GatewaySocket gatewaySocket, VoiceWebSocket voiceSocket)
{
_service = service;
_id = id;

View File

@@ -1,5 +1,7 @@
using Discord.API;
using Discord.Audio;
using Discord.Audio.Opus;
using Discord.Audio.Sodium;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
@@ -54,7 +56,7 @@ namespace Discord.Net.WebSockets
_targetAudioBufferLength = _audioConfig.BufferLength / 20; //20 ms frames
_encodingBuffer = new byte[MaxOpusSize];
_ssrcMapping = new ConcurrentDictionary<uint, long>();
_encoder = new OpusEncoder(48000, 1, 20, _audioConfig.Bitrate, Opus.Application.Audio);
_encoder = new OpusEncoder(48000, _audioConfig.Channels, 20, _audioConfig.Bitrate, OpusApplication.Audio);
_sendBuffer = new VoiceBuffer((int)Math.Ceiling(_audioConfig.BufferLength / (double)_encoder.FrameLength), _encoder.FrameSize);
}
@@ -223,7 +225,7 @@ namespace Discord.Net.WebSockets
return;
Buffer.BlockCopy(packet, 0, nonce, 0, 12);
int ret = Sodium.Decrypt(packet, 12, packetLength - 12, decodingBuffer, nonce, _secretKey);
int ret = SecretBox.Decrypt(packet, 12, packetLength - 12, decodingBuffer, nonce, _secretKey);
if (ret != 0)
continue;
result = decodingBuffer;
@@ -294,7 +296,7 @@ namespace Discord.Net.WebSockets
if (_isEncrypted)
{
Buffer.BlockCopy(pingPacket, 0, nonce, 0, 8);
int ret = Sodium.Encrypt(pingPacket, 8, encodedFrame, 0, nonce, _secretKey);
int ret = SecretBox.Encrypt(pingPacket, 8, encodedFrame, 0, nonce, _secretKey);
if (ret != 0)
throw new InvalidOperationException("Failed to encrypt ping packet");
pingPacket = new byte[pingPacket.Length + 16];
@@ -333,7 +335,7 @@ namespace Discord.Net.WebSockets
if (_isEncrypted)
{
Buffer.BlockCopy(voicePacket, 2, nonce, 2, 6); //Update nonce
int ret = Sodium.Encrypt(encodedFrame, encodedLength, voicePacket, 12, nonce, _secretKey);
int ret = SecretBox.Encrypt(encodedFrame, encodedLength, voicePacket, 12, nonce, _secretKey);
if (ret != 0)
continue;
rtpPacketLength = encodedLength + 12 + 16;

View File

@@ -1,71 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Discord.Audio
{
internal unsafe static class Opus
{
[DllImport("opus", EntryPoint = "opus_encoder_create", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateEncoder(int Fs, int channels, int application, out Error error);
[DllImport("opus", EntryPoint = "opus_encoder_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyEncoder(IntPtr encoder);
[DllImport("opus", EntryPoint = "opus_encode", CallingConvention = CallingConvention.Cdecl)]
public static extern int Encode(IntPtr st, byte* pcm, int frame_size, byte[] data, int max_data_bytes);
[DllImport("opus", EntryPoint = "opus_decoder_create", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateDecoder(int Fs, int channels, out Error error);
[DllImport("opus", EntryPoint = "opus_decoder_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyDecoder(IntPtr decoder);
[DllImport("opus", EntryPoint = "opus_decode", CallingConvention = CallingConvention.Cdecl)]
public static extern int Decode(IntPtr st, byte* data, int len, byte[] pcm, int frame_size, int decode_fec);
[DllImport("opus", EntryPoint = "opus_encoder_ctl", CallingConvention = CallingConvention.Cdecl)]
public static extern int EncoderCtl(IntPtr st, Ctl request, int value);
public enum Ctl : int
{
SetBitrateRequest = 4002,
GetBitrateRequest = 4003,
SetInbandFECRequest = 4012,
GetInbandFECRequest = 4013
}
/// <summary>Supported coding modes.</summary>
public enum Application : int
{
/// <summary>
/// Gives best quality at a given bitrate for voice signals. It enhances the input signal by high-pass filtering and emphasizing formants and harmonics.
/// Optionally it includes in-band forward error correction to protect against packet loss. Use this mode for typical VoIP applications.
/// Because of the enhancement, even at high bitrates the output may sound different from the input.
/// </summary>
Voip = 2048,
/// <summary>
/// Gives best quality at a given bitrate for most non-voice signals like music.
/// Use this mode for music and mixed (music/voice) content, broadcast, and applications requiring less than 15 ms of coding delay.
/// </summary>
Audio = 2049,
/// <summary> Low-delay mode that disables the speech-optimized mode in exchange for slightly reduced delay. </summary>
Restricted_LowLatency = 2051
}
public enum Error : int
{
/// <summary> No error. </summary>
OK = 0,
/// <summary> One or more invalid/out of range arguments. </summary>
BadArg = -1,
/// <summary> The mode struct passed is invalid. </summary>
BufferToSmall = -2,
/// <summary> An internal error was detected. </summary>
InternalError = -3,
/// <summary> The compressed data passed is corrupted. </summary>
InvalidPacket = -4,
/// <summary> Invalid/unsupported request number. </summary>
Unimplemented = -5,
/// <summary> An encoder or decoder structure is invalid or already freed. </summary>
InvalidState = -6,
/// <summary> Memory allocation has failed. </summary>
AllocFail = -7
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Discord.Audio.Opus
{
internal enum OpusCtl : int
{
SetBitrateRequest = 4002,
GetBitrateRequest = 4003,
SetInbandFECRequest = 4012,
GetInbandFECRequest = 4013
}
/// <summary>Supported coding modes.</summary>
internal enum OpusApplication : int
{
/// <summary>
/// Gives best quality at a given bitrate for voice signals. It enhances the input signal by high-pass filtering and emphasizing formants and harmonics.
/// Optionally it includes in-band forward error correction to protect against packet loss. Use this mode for typical VoIP applications.
/// Because of the enhancement, even at high bitrates the output may sound different from the input.
/// </summary>
Voip = 2048,
/// <summary>
/// Gives best quality at a given bitrate for most non-voice signals like music.
/// Use this mode for music and mixed (music/voice) content, broadcast, and applications requiring less than 15 ms of coding delay.
/// </summary>
Audio = 2049,
/// <summary> Low-delay mode that disables the speech-optimized mode in exchange for slightly reduced delay. </summary>
Restricted_LowLatency = 2051
}
internal enum OpusError : int
{
/// <summary> No error. </summary>
OK = 0,
/// <summary> One or more invalid/out of range arguments. </summary>
BadArg = -1,
/// <summary> The mode struct passed is invalid. </summary>
BufferToSmall = -2,
/// <summary> An internal error was detected. </summary>
InternalError = -3,
/// <summary> The compressed data passed is corrupted. </summary>
InvalidPacket = -4,
/// <summary> Invalid/unsupported request number. </summary>
Unimplemented = -5,
/// <summary> An encoder or decoder structure is invalid or already freed. </summary>
InvalidState = -6,
/// <summary> Memory allocation has failed. </summary>
AllocFail = -7
}
}

View File

@@ -1,10 +1,25 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Discord.Audio
namespace Discord.Audio.Opus
{
/// <summary> Opus codec wrapper. </summary>
internal class OpusDecoder : IDisposable
{
#if NET45
[SuppressUnmanagedCodeSecurity]
#endif
private unsafe static class UnsafeNativeMethods
{
[DllImport("opus", EntryPoint = "opus_decoder_create", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateDecoder(int Fs, int channels, out OpusError error);
[DllImport("opus", EntryPoint = "opus_decoder_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyDecoder(IntPtr decoder);
[DllImport("opus", EntryPoint = "opus_decode", CallingConvention = CallingConvention.Cdecl)]
public static extern int Decode(IntPtr st, byte* data, int len, byte[] pcm, int frame_size, int decode_fec);
}
private readonly IntPtr _ptr;
/// <summary> Gets the bit rate of the encoder. </summary>
@@ -22,7 +37,7 @@ namespace Discord.Audio
/// <summary> Gets the bytes per frame. </summary>
public int FrameSize { get; private set; }
/// <summary> Creates a new Opus encoder. </summary>
/// <summary> Creates a new Opus decoder. </summary>
/// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values: 8000, 12000, 16000, 24000, or 48000.</param>
/// <param name="channels">Number of channels (1 or 2) in input signal.</param>
/// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
@@ -44,19 +59,17 @@ namespace Discord.Audio
SamplesPerFrame = samplingRate / 1000 * FrameLength;
FrameSize = SamplesPerFrame * SampleSize;
Opus.Error error;
_ptr = Opus.CreateDecoder(samplingRate, channels, out error);
if (error != Opus.Error.OK)
OpusError error;
_ptr = UnsafeNativeMethods.CreateDecoder(samplingRate, channels, out error);
if (error != OpusError.OK)
throw new InvalidOperationException($"Error occured while creating decoder: {error}");
SetForwardErrorCorrection(true);
}
/// <summary> Produces Opus encoded audio from PCM samples. </summary>
/// <param name="input">PCM samples to encode.</param>
/// <param name="inputOffset">Offset of the frame in pcmSamples.</param>
/// <param name="output">Buffer to store the encoded frame.</param>
/// <returns>Length of the frame contained in outputBuffer.</returns>
/// <summary> Produces PCM samples from Opus-encoded audio. </summary>
/// <param name="input">PCM samples to decode.</param>
/// <param name="inputOffset">Offset of the frame in input.</param>
/// <param name="output">Buffer to store the decoded frame.</param>
/// <returns>Length of the frame contained in output.</returns>
public unsafe int DecodeFrame(byte[] input, int inputOffset, byte[] output)
{
if (disposed)
@@ -64,24 +77,13 @@ namespace Discord.Audio
int result = 0;
fixed (byte* inPtr = input)
result = Opus.Encode(_ptr, inPtr + inputOffset, SamplesPerFrame, output, output.Length);
result = UnsafeNativeMethods.Decode(_ptr, inPtr + inputOffset, SamplesPerFrame, output, output.Length, 0);
if (result < 0)
throw new Exception("Decoding failed: " + ((Opus.Error)result).ToString());
throw new Exception("Decoding failed: " + ((OpusError)result).ToString());
return result;
}
/// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
public void SetForwardErrorCorrection(bool value)
{
if (disposed)
throw new ObjectDisposedException(nameof(OpusDecoder));
var result = Opus.EncoderCtl(_ptr, Opus.Ctl.SetInbandFECRequest, value ? 1 : 0);
if (result < 0)
throw new Exception("Decoder error: " + ((Opus.Error)result).ToString());
}
#region IDisposable
private bool disposed;
public void Dispose()
@@ -92,7 +94,7 @@ namespace Discord.Audio
GC.SuppressFinalize(this);
if (_ptr != IntPtr.Zero)
Opus.DestroyEncoder(_ptr);
UnsafeNativeMethods.DestroyDecoder(_ptr);
disposed = true;
}

View File

@@ -1,10 +1,27 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Discord.Audio
namespace Discord.Audio.Opus
{
/// <summary> Opus codec wrapper. </summary>
internal class OpusEncoder : IDisposable
{
#if NET45
[SuppressUnmanagedCodeSecurity]
#endif
private unsafe static class UnsafeNativeMethods
{
[DllImport("opus", EntryPoint = "opus_encoder_create", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateEncoder(int Fs, int channels, int application, out OpusError error);
[DllImport("opus", EntryPoint = "opus_encoder_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyEncoder(IntPtr encoder);
[DllImport("opus", EntryPoint = "opus_encode", CallingConvention = CallingConvention.Cdecl)]
public static extern int Encode(IntPtr st, byte* pcm, int frame_size, byte[] data, int max_data_bytes);
[DllImport("opus", EntryPoint = "opus_encoder_ctl", CallingConvention = CallingConvention.Cdecl)]
public static extern int EncoderCtl(IntPtr st, OpusCtl request, int value);
}
private readonly IntPtr _ptr;
/// <summary> Gets the bit rate of the encoder. </summary>
@@ -24,7 +41,7 @@ namespace Discord.Audio
/// <summary> Gets the bit rate in kbit/s. </summary>
public int? BitRate { get; private set; }
/// <summary> Gets the coding mode of the encoder. </summary>
public Opus.Application Application { get; private set; }
public OpusApplication Application { get; private set; }
/// <summary> Creates a new Opus encoder. </summary>
/// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values: 8000, 12000, 16000, 24000, or 48000.</param>
@@ -33,7 +50,7 @@ namespace Discord.Audio
/// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
/// <param name="application">Coding mode.</param>
/// <returns>A new <c>OpusEncoder</c></returns>
public OpusEncoder(int samplingRate, int channels, int frameLength, int? bitrate, Opus.Application application)
public OpusEncoder(int samplingRate, int channels, int frameLength, int? bitrate, OpusApplication application)
{
if (samplingRate != 8000 && samplingRate != 12000 &&
samplingRate != 16000 && samplingRate != 24000 &&
@@ -53,9 +70,9 @@ namespace Discord.Audio
FrameSize = SamplesPerFrame * SampleSize;
BitRate = bitrate;
Opus.Error error;
_ptr = Opus.CreateEncoder(samplingRate, channels, (int)application, out error);
if (error != Opus.Error.OK)
OpusError error;
_ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
if (error != OpusError.OK)
throw new InvalidOperationException($"Error occured while creating encoder: {error}");
SetForwardErrorCorrection(true);
@@ -75,10 +92,10 @@ namespace Discord.Audio
int result = 0;
fixed (byte* inPtr = input)
result = Opus.Encode(_ptr, inPtr + inputOffset, SamplesPerFrame, output, output.Length);
result = UnsafeNativeMethods.Encode(_ptr, inPtr + inputOffset, SamplesPerFrame, output, output.Length);
if (result < 0)
throw new Exception("Encoding failed: " + ((Opus.Error)result).ToString());
throw new Exception("Encoding failed: " + ((OpusError)result).ToString());
return result;
}
@@ -88,9 +105,9 @@ namespace Discord.Audio
if (disposed)
throw new ObjectDisposedException(nameof(OpusEncoder));
var result = Opus.EncoderCtl(_ptr, Opus.Ctl.SetInbandFECRequest, value ? 1 : 0);
var result = UnsafeNativeMethods.EncoderCtl(_ptr, OpusCtl.SetInbandFECRequest, value ? 1 : 0);
if (result < 0)
throw new Exception("Encoder error: " + ((Opus.Error)result).ToString());
throw new Exception("Encoder error: " + ((OpusError)result).ToString());
}
/// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
@@ -99,9 +116,9 @@ namespace Discord.Audio
if (disposed)
throw new ObjectDisposedException(nameof(OpusEncoder));
var result = Opus.EncoderCtl(_ptr, Opus.Ctl.SetBitrateRequest, value * 1000);
var result = UnsafeNativeMethods.EncoderCtl(_ptr, OpusCtl.SetBitrateRequest, value * 1000);
if (result < 0)
throw new Exception("Encoder error: " + ((Opus.Error)result).ToString());
throw new Exception("Encoder error: " + ((OpusError)result).ToString());
}
#region IDisposable
@@ -114,7 +131,7 @@ namespace Discord.Audio
GC.SuppressFinalize(this);
if (_ptr != IntPtr.Zero)
Opus.DestroyEncoder(_ptr);
UnsafeNativeMethods.DestroyEncoder(_ptr);
disposed = true;
}

View File

@@ -2,25 +2,4 @@
namespace Discord.Audio
{
internal unsafe static class Sodium
{
[DllImport("libsodium", EntryPoint = "crypto_secretbox_easy", CallingConvention = CallingConvention.Cdecl)]
private static extern int SecretBoxEasy(byte* output, byte[] input, long inputLength, byte[] nonce, byte[] secret);
public static int Encrypt(byte[] input, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
{
fixed (byte* outPtr = output)
return SecretBoxEasy(outPtr + outputOffset, input, inputLength, nonce, secret);
}
[DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)]
private static extern int SecretBoxOpenEasy(byte[] output, byte* input, long inputLength, byte[] nonce, byte[] secret);
public static int Decrypt(byte[] input, int inputOffset, long inputLength, byte[] output, byte[] nonce, byte[] secret)
{
fixed (byte* inPtr = input)
return SecretBoxOpenEasy(output, inPtr + inputLength, inputLength, nonce, secret);
}
}
}

View File

@@ -0,0 +1,30 @@
using System.Runtime.InteropServices;
using System.Security;
namespace Discord.Audio.Sodium
{
public unsafe static class SecretBox
{
#if NET45
[SuppressUnmanagedCodeSecurity]
#endif
private static class SafeNativeMethods
{
[DllImport("libsodium", EntryPoint = "crypto_secretbox_easy", CallingConvention = CallingConvention.Cdecl)]
public static extern int SecretBoxEasy(byte* output, byte[] input, long inputLength, byte[] nonce, byte[] secret);
[DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)]
public static extern int SecretBoxOpenEasy(byte[] output, byte* input, long inputLength, byte[] nonce, byte[] secret);
}
public static int Encrypt(byte[] input, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
{
fixed (byte* outPtr = output)
return SafeNativeMethods.SecretBoxEasy(outPtr + outputOffset, input, inputLength, nonce, secret);
}
public static int Decrypt(byte[] input, int inputOffset, long inputLength, byte[] output, byte[] nonce, byte[] secret)
{
fixed (byte* inPtr = input)
return SafeNativeMethods.SecretBoxOpenEasy(output, inPtr + inputLength, inputLength, nonce, secret);
}
}
}

View File

@@ -2,7 +2,7 @@
namespace Discord.Commands
{
public class CommandEventArgs
public class CommandEventArgs : EventArgs
{
private readonly string[] _args;

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Discord.Commands
{
/// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary>
public partial class CommandService : IService
public sealed partial class CommandService : IService
{
private const string DefaultPermissionError = "You do not have permission to access this command.";

View File

@@ -224,11 +224,11 @@
<Compile Include="..\Discord.Net\Net\TimeoutException.cs">
<Link>Net\TimeoutException.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\GatewayWebSocket.cs">
<Link>Net\WebSockets\GatewayWebSocket.cs</Link>
<Compile Include="..\Discord.Net\Net\WebSockets\GatewaySocket.cs">
<Link>Net\WebSockets\GatewaySocket.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\GatewayWebSockets.Events.cs">
<Link>Net\WebSockets\GatewayWebSockets.Events.cs</Link>
<Compile Include="..\Discord.Net\Net\WebSockets\GatewaySocket.Events.cs">
<Link>Net\WebSockets\GatewaySocket.Events.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\IWebSocketEngine.cs">
<Link>Net\WebSockets\IWebSocketEngine.cs</Link>

View File

@@ -58,7 +58,7 @@ namespace Discord
}
}
public partial class DiscordClient
public partial class DiscordClient : IDisposable
{
public event EventHandler<UserEventArgs> UserJoined;
private void RaiseUserJoined(User user)

View File

@@ -4,7 +4,6 @@ using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
@@ -82,8 +81,8 @@ namespace Discord
private readonly DiscordAPIClient _api;
/// <summary> Returns the internal websocket object. </summary>
public GatewayWebSocket WebSocket => _webSocket;
private readonly GatewayWebSocket _webSocket;
public GatewaySocket WebSocket => _webSocket;
private readonly GatewaySocket _webSocket;
public string GatewayUrl => _gateway;
private string _gateway;
@@ -140,11 +139,24 @@ namespace Discord
CreateCacheLogger();
//Networking
_webSocket = CreateWebSocket();
_webSocket = new GatewaySocket(_config, _log.CreateLogger("WebSocket"));
var settings = new JsonSerializerSettings();
_webSocket.Connected += (s, e) =>
{
if (_state == (int)DiscordClientState.Connecting)
EndConnect();
};
_webSocket.Disconnected += (s, e) =>
{
RaiseDisconnected(e);
};
_webSocket.ReceivedDispatch += async (s, e) => await OnReceivedEvent(e).ConfigureAwait(false);
_api = new DiscordAPIClient(_config);
if (Config.UseMessageQueue)
_pendingMessages = new ConcurrentQueue<Message>();
this.Connected += async (s, e) =>
Connected += async (s, e) =>
{
_api.CancelToken = _cancelToken;
await SendStatus().ConfigureAwait(false);
@@ -257,24 +269,6 @@ namespace Discord
}
}
private GatewayWebSocket CreateWebSocket()
{
var socket = new GatewayWebSocket(_config, _log.CreateLogger("WebSocket"));
var settings = new JsonSerializerSettings();
socket.Connected += (s, e) =>
{
if (_state == (int)DiscordClientState.Connecting)
CompleteConnect();
};
socket.Disconnected += (s, e) =>
{
RaiseDisconnected(e);
};
socket.ReceivedDispatch += async (s, e) => await OnReceivedEvent(e).ConfigureAwait(false);
return socket;
}
/// <summary> Connects to the Discord server with the provided email and password. </summary>
/// <returns> Returns a token for future connections. </returns>
public async Task<string> Connect(string email, string password)
@@ -285,19 +279,15 @@ namespace Discord
if (State != DiscordClientState.Disconnected)
await Disconnect().ConfigureAwait(false);
string token;
try
{
var response = await _api.Login(email, password)
.ConfigureAwait(false);
token = response.Token;
_token = response.Token;
_api.Token = response.Token;
if (_config.LogLevel >= LogSeverity.Verbose)
_logger.Log(LogSeverity.Verbose, "Login successful, got token.");
await Connect(token);
return token;
}
catch (TaskCanceledException) { throw new TimeoutException(); }
await BeginConnect();
return response.Token;
}
/// <summary> Connects to the Discord server with the provided token. </summary>
public async Task Connect(string token)
@@ -308,26 +298,32 @@ namespace Discord
if (State != (int)DiscordClientState.Disconnected)
await Disconnect().ConfigureAwait(false);
_token = token;
_api.Token = token;
await BeginConnect();
}
private async Task BeginConnect()
{
try
{
_state = (int)DiscordClientState.Connecting;
var gatewayResponse = await _api.Gateway().ConfigureAwait(false);
string gateway = gatewayResponse.Url;
if (_config.LogLevel >= LogSeverity.Verbose)
_logger.Log(LogSeverity.Verbose, $"Websocket endpoint: {gateway}");
try
{
_state = (int)DiscordClientState.Connecting;
_disconnectedEvent.Reset();
_gateway = gateway;
_token = token;
_cancelTokenSource = new CancellationTokenSource();
_cancelToken = _cancelTokenSource.Token;
_webSocket.Host = gateway;
_webSocket.ParentCancelToken = _cancelToken;
await _webSocket.Connect(token).ConfigureAwait(false);
await _webSocket.Connect(_token).ConfigureAwait(false);
_runTask = RunTasks();
@@ -342,8 +338,6 @@ namespace Discord
_webSocket.ThrowError(); //Throws data socket's internal error if any occured
throw;
}
//_state = (int)DiscordClientState.Connected;
}
catch
{
@@ -351,7 +345,7 @@ namespace Discord
throw;
}
}
private void CompleteConnect()
private void EndConnect()
{
_state = (int)DiscordClientState.Connected;
_connectedEvent.Set();
@@ -359,8 +353,8 @@ namespace Discord
}
/// <summary> Disconnects from the Discord server, canceling any pending requests. </summary>
public Task Disconnect() => DisconnectInternal(new Exception("Disconnect was requested by user."), isUnexpected: false);
private async Task DisconnectInternal(Exception ex = null, bool isUnexpected = true, bool skipAwait = false)
public Task Disconnect() => SignalDisconnect(new Exception("Disconnect was requested by user."), isUnexpected: false);
private async Task SignalDisconnect(Exception ex = null, bool isUnexpected = true, bool wait = false)
{
int oldState;
bool hasWriterLock;
@@ -386,7 +380,7 @@ namespace Discord
await Cleanup().ConfigureAwait(false);*/
}
if (!skipAwait)
if (wait)
{
Task task = _runTask;
if (_runTask != null)
@@ -407,10 +401,10 @@ namespace Discord
//Wait until the first task ends/errors and capture the error
try { await firstTask.ConfigureAwait(false); }
catch (Exception ex) { await DisconnectInternal(ex: ex, skipAwait: true).ConfigureAwait(false); }
catch (Exception ex) { await SignalDisconnect(ex: ex, wait: true).ConfigureAwait(false); }
//Ensure all other tasks are signaled to end.
await DisconnectInternal(skipAwait: true).ConfigureAwait(false);
await SignalDisconnect(wait: true).ConfigureAwait(false);
//Wait for the remaining tasks to complete
try { await allTasks.ConfigureAwait(false); }
@@ -454,35 +448,6 @@ namespace Discord
_privateUser = null;
}
public T AddSingleton<T>(T obj)
where T : class
{
_singletons.Add(typeof(T), obj);
return obj;
}
public T GetSingleton<T>(bool required = true)
where T : class
{
object singleton;
T singletonT = null;
if (_singletons.TryGetValue(typeof(T), out singleton))
singletonT = singleton as T;
if (singletonT == null && required)
throw new InvalidOperationException($"This operation requires {typeof(T).Name} to be added to {nameof(DiscordClient)}.");
return singletonT;
}
public T AddService<T>(T obj)
where T : class, IService
{
AddSingleton(obj);
obj.Install(this);
return obj;
}
public T GetService<T>(bool required = true)
where T : class, IService
=> GetSingleton<T>(required);
private async Task OnReceivedEvent(WebSocketEventEventArgs e)
{
try
@@ -851,8 +816,7 @@ namespace Discord
_sentInitialLog = true;
}
//Helpers
#region Async Wrapper
/// <summary> Blocking call that will not return until client has been stopped. This is mainly intended for use in console applications. </summary>
public void Run(Func<Task> asyncAction)
{
@@ -868,7 +832,62 @@ namespace Discord
{
_disconnectedEvent.WaitOne();
}
#endregion
#region Services
public T AddSingleton<T>(T obj)
where T : class
{
_singletons.Add(typeof(T), obj);
return obj;
}
public T GetSingleton<T>(bool required = true)
where T : class
{
object singleton;
T singletonT = null;
if (_singletons.TryGetValue(typeof(T), out singleton))
singletonT = singleton as T;
if (singletonT == null && required)
throw new InvalidOperationException($"This operation requires {typeof(T).Name} to be added to {nameof(DiscordClient)}.");
return singletonT;
}
public T AddService<T>(T obj)
where T : class, IService
{
AddSingleton(obj);
obj.Install(this);
return obj;
}
public T GetService<T>(bool required = true)
where T : class, IService
=> GetSingleton<T>(required);
#endregion
#region IDisposable
private bool _isDisposed = false;
protected virtual void Dispose(bool isDisposing)
{
if (!_isDisposed)
{
if (isDisposing)
{
_disconnectedEvent.Dispose();
_connectedEvent.Dispose();
}
_isDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
//Helpers
private void CheckReady()
{
switch (_state)

View File

@@ -42,6 +42,7 @@ namespace Discord
public bool IsServerDeafened { get; private set; }
public bool IsServerSuppressed { get; private set; }
public bool IsPrivate => _server.Id == null;
public bool IsOwner => _server.Value.OwnerId == Id;
public string SessionId { get; private set; }
public string Token { get; private set; }
@@ -101,11 +102,25 @@ namespace Discord
{
if (_server.Id != null)
{
if (_client.Config.UsePermissionsCache)
{
return Server.Channels
.Where(x => (x.Type == ChannelType.Text && x.GetPermissions(this).ReadMessages) ||
(x.Type == ChannelType.Voice && x.GetPermissions(this).Connect));
}
else
{
ChannelPermissions perms = new ChannelPermissions();
return Server.Channels
.Where(x =>
{
x.UpdatePermissions(this, perms);
return (x.Type == ChannelType.Text && perms.ReadMessages) ||
(x.Type == ChannelType.Voice && perms.Connect);
});
}
}
else
{
var privateChannel = Global.PrivateChannel;
if (privateChannel != null)

View File

@@ -1,8 +1,12 @@
using System;
using System.Net;
using System.Runtime.Serialization;
namespace Discord.Net
{
#if NET45
[Serializable]
#endif
public class HttpException : Exception
{
public HttpStatusCode StatusCode { get; }
@@ -12,5 +16,9 @@ namespace Discord.Net
{
StatusCode = statusCode;
}
#if NET45
public override void GetObjectData(SerializationInfo info, StreamingContext context)
=> base.GetObjectData(info, context);
#endif
}
}

View File

@@ -2,6 +2,9 @@
namespace Discord.Net
{
#if NET45
[Serializable]
#endif
public sealed class TimeoutException : OperationCanceledException
{
public TimeoutException()

View File

@@ -14,7 +14,7 @@ namespace Discord.Net.WebSockets
}
}
public partial class GatewayWebSocket
public partial class GatewaySocket
{
public event EventHandler<WebSocketEventEventArgs> ReceivedDispatch;
private void RaiseReceivedDispatch(string type, JToken payload)

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Discord.Net.WebSockets
{
public partial class GatewayWebSocket : WebSocket
public partial class GatewaySocket : WebSocket
{
public int LastSequence => _lastSeq;
private int _lastSeq;
@@ -17,7 +17,7 @@ namespace Discord.Net.WebSockets
public string SessionId => _sessionId;
private string _sessionId;
public GatewayWebSocket(DiscordConfig config, Logger logger)
public GatewaySocket(DiscordConfig config, Logger logger)
: base(config, logger)
{
Disconnected += async (s, e) =>
@@ -29,12 +29,16 @@ namespace Discord.Net.WebSockets
public async Task Connect(string token)
{
await SignalDisconnect(wait: true).ConfigureAwait(false);
_token = token;
await BeginConnect().ConfigureAwait(false);
SendIdentify(token);
}
private async Task Redirect(string server)
{
await SignalDisconnect(wait: true).ConfigureAwait(false);
await BeginConnect().ConfigureAwait(false);
SendResume();
}

View File

@@ -114,7 +114,6 @@ namespace Discord.Net.WebSockets
{
try
{
await SignalDisconnect(wait: true).ConfigureAwait(false);
_state = (int)WebSocketState.Connecting;
if (ParentCancelToken == null)
@@ -173,7 +172,7 @@ namespace Discord.Net.WebSockets
await Cleanup().ConfigureAwait(false);
}
if (!wait)
if (wait)
{
Task task = _runTask;
if (_runTask != null)

View File

@@ -0,0 +1 @@
<StyleCopSettings Version="105" />

View File

@@ -29,7 +29,8 @@
},
"dependencies": {
"Newtonsoft.Json": "7.0.1"
"Newtonsoft.Json": "7.0.1",
"StyleCop.Analyzers": "1.0.0-rc2"
},
"frameworks": {