Refactoring and fixed a few stylecop errors
This commit is contained in:
@@ -67,18 +67,21 @@
|
|||||||
<Compile Include="..\Discord.Net.Audio\Net\WebSockets\VoiceWebSocket.Events.cs">
|
<Compile Include="..\Discord.Net.Audio\Net\WebSockets\VoiceWebSocket.Events.cs">
|
||||||
<Link>Net\WebSockets\VoiceWebSocket.Events.cs</Link>
|
<Link>Net\WebSockets\VoiceWebSocket.Events.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net.Audio\Opus.cs">
|
<Compile Include="..\Discord.Net.Audio\Opus\Enums.cs">
|
||||||
<Link>Opus.cs</Link>
|
<Link>Opus\Enums.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net.Audio\OpusDecoder.cs">
|
<Compile Include="..\Discord.Net.Audio\Opus\OpusDecoder.cs">
|
||||||
<Link>OpusDecoder.cs</Link>
|
<Link>Opus\OpusDecoder.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net.Audio\OpusEncoder.cs">
|
<Compile Include="..\Discord.Net.Audio\Opus\OpusEncoder.cs">
|
||||||
<Link>OpusEncoder.cs</Link>
|
<Link>Opus\OpusEncoder.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net.Audio\Sodium.cs">
|
<Compile Include="..\Discord.Net.Audio\Sodium.cs">
|
||||||
<Link>Sodium.cs</Link>
|
<Link>Sodium.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="..\Discord.Net.Audio\Sodium\SecretBox.cs">
|
||||||
|
<Link>Sodium\SecretBox.cs</Link>
|
||||||
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net.Audio\VoiceBuffer.cs">
|
<Compile Include="..\Discord.Net.Audio\VoiceBuffer.cs">
|
||||||
<Link>VoiceBuffer.cs</Link>
|
<Link>VoiceBuffer.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@@ -145,13 +145,13 @@ namespace Discord.Audio
|
|||||||
return Task.FromResult(_defaultClient);
|
return Task.FromResult(_defaultClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = _voiceClients.GetOrAdd(server.Id, _ =>
|
var client = _voiceClients.GetOrAdd(server.Id, (Func<long, DiscordAudioClient>)(_ =>
|
||||||
{
|
{
|
||||||
int id = unchecked(++_nextClientId);
|
int id = unchecked(++_nextClientId);
|
||||||
var logger = Client.Log().CreateLogger($"Voice #{id}");
|
var logger = Client.Log().CreateLogger($"Voice #{id}");
|
||||||
GatewayWebSocket gatewaySocket = null;
|
Net.WebSockets.GatewaySocket gatewaySocket = null;
|
||||||
var voiceSocket = new VoiceWebSocket(Client.Config, _config, logger);
|
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);
|
voiceClient.SetServerId(server.Id);
|
||||||
|
|
||||||
voiceSocket.OnPacket += (s, e) =>
|
voiceSocket.OnPacket += (s, e) =>
|
||||||
@@ -165,7 +165,7 @@ namespace Discord.Audio
|
|||||||
};
|
};
|
||||||
|
|
||||||
return voiceClient;
|
return voiceClient;
|
||||||
});
|
}));
|
||||||
//await client.Connect(gatewaySocket.Host, _client.Token).ConfigureAwait(false);
|
//await client.Connect(gatewaySocket.Host, _client.Token).ConfigureAwait(false);
|
||||||
return Task.FromResult(client);
|
return Task.FromResult(client);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ namespace Discord.Audio
|
|||||||
public int? Bitrate { get { return _bitrate; } set { SetValue(ref _bitrate, value); } }
|
public int? Bitrate { get { return _bitrate; } set { SetValue(ref _bitrate, value); } }
|
||||||
private int? _bitrate = null;
|
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
|
//Lock
|
||||||
protected bool _isLocked;
|
protected bool _isLocked;
|
||||||
internal void Lock() { _isLocked = true; }
|
internal void Lock() { _isLocked = true; }
|
||||||
|
|||||||
@@ -10,14 +10,14 @@ namespace Discord.Audio
|
|||||||
public int Id => _id;
|
public int Id => _id;
|
||||||
|
|
||||||
private readonly AudioService _service;
|
private readonly AudioService _service;
|
||||||
private readonly GatewayWebSocket _gatewaySocket;
|
private readonly GatewaySocket _gatewaySocket;
|
||||||
private readonly VoiceWebSocket _voiceSocket;
|
private readonly VoiceWebSocket _voiceSocket;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
|
|
||||||
public long? ServerId => _voiceSocket.ServerId;
|
public long? ServerId => _voiceSocket.ServerId;
|
||||||
public long? ChannelId => _voiceSocket.ChannelId;
|
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;
|
_service = service;
|
||||||
_id = id;
|
_id = id;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using Discord.API;
|
using Discord.API;
|
||||||
using Discord.Audio;
|
using Discord.Audio;
|
||||||
|
using Discord.Audio.Opus;
|
||||||
|
using Discord.Audio.Sodium;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
@@ -54,7 +56,7 @@ namespace Discord.Net.WebSockets
|
|||||||
_targetAudioBufferLength = _audioConfig.BufferLength / 20; //20 ms frames
|
_targetAudioBufferLength = _audioConfig.BufferLength / 20; //20 ms frames
|
||||||
_encodingBuffer = new byte[MaxOpusSize];
|
_encodingBuffer = new byte[MaxOpusSize];
|
||||||
_ssrcMapping = new ConcurrentDictionary<uint, long>();
|
_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);
|
_sendBuffer = new VoiceBuffer((int)Math.Ceiling(_audioConfig.BufferLength / (double)_encoder.FrameLength), _encoder.FrameSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +225,7 @@ namespace Discord.Net.WebSockets
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
Buffer.BlockCopy(packet, 0, nonce, 0, 12);
|
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)
|
if (ret != 0)
|
||||||
continue;
|
continue;
|
||||||
result = decodingBuffer;
|
result = decodingBuffer;
|
||||||
@@ -294,7 +296,7 @@ namespace Discord.Net.WebSockets
|
|||||||
if (_isEncrypted)
|
if (_isEncrypted)
|
||||||
{
|
{
|
||||||
Buffer.BlockCopy(pingPacket, 0, nonce, 0, 8);
|
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)
|
if (ret != 0)
|
||||||
throw new InvalidOperationException("Failed to encrypt ping packet");
|
throw new InvalidOperationException("Failed to encrypt ping packet");
|
||||||
pingPacket = new byte[pingPacket.Length + 16];
|
pingPacket = new byte[pingPacket.Length + 16];
|
||||||
@@ -333,7 +335,7 @@ namespace Discord.Net.WebSockets
|
|||||||
if (_isEncrypted)
|
if (_isEncrypted)
|
||||||
{
|
{
|
||||||
Buffer.BlockCopy(voicePacket, 2, nonce, 2, 6); //Update nonce
|
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)
|
if (ret != 0)
|
||||||
continue;
|
continue;
|
||||||
rtpPacketLength = encodedLength + 12 + 16;
|
rtpPacketLength = encodedLength + 12 + 16;
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
53
src/Discord.Net.Audio/Opus/Enums.cs
Normal file
53
src/Discord.Net.Audio/Opus/Enums.cs
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +1,25 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security;
|
||||||
|
|
||||||
namespace Discord.Audio
|
namespace Discord.Audio.Opus
|
||||||
{
|
{
|
||||||
/// <summary> Opus codec wrapper. </summary>
|
/// <summary> Opus codec wrapper. </summary>
|
||||||
internal class OpusDecoder : IDisposable
|
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;
|
private readonly IntPtr _ptr;
|
||||||
|
|
||||||
/// <summary> Gets the bit rate of the encoder. </summary>
|
/// <summary> Gets the bit rate of the encoder. </summary>
|
||||||
@@ -22,7 +37,7 @@ namespace Discord.Audio
|
|||||||
/// <summary> Gets the bytes per frame. </summary>
|
/// <summary> Gets the bytes per frame. </summary>
|
||||||
public int FrameSize { get; private set; }
|
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="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="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>
|
/// <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;
|
SamplesPerFrame = samplingRate / 1000 * FrameLength;
|
||||||
FrameSize = SamplesPerFrame * SampleSize;
|
FrameSize = SamplesPerFrame * SampleSize;
|
||||||
|
|
||||||
Opus.Error error;
|
OpusError error;
|
||||||
_ptr = Opus.CreateDecoder(samplingRate, channels, out error);
|
_ptr = UnsafeNativeMethods.CreateDecoder(samplingRate, channels, out error);
|
||||||
if (error != Opus.Error.OK)
|
if (error != OpusError.OK)
|
||||||
throw new InvalidOperationException($"Error occured while creating decoder: {error}");
|
throw new InvalidOperationException($"Error occured while creating decoder: {error}");
|
||||||
|
|
||||||
SetForwardErrorCorrection(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Produces Opus encoded audio from PCM samples. </summary>
|
/// <summary> Produces PCM samples from Opus-encoded audio. </summary>
|
||||||
/// <param name="input">PCM samples to encode.</param>
|
/// <param name="input">PCM samples to decode.</param>
|
||||||
/// <param name="inputOffset">Offset of the frame in pcmSamples.</param>
|
/// <param name="inputOffset">Offset of the frame in input.</param>
|
||||||
/// <param name="output">Buffer to store the encoded frame.</param>
|
/// <param name="output">Buffer to store the decoded frame.</param>
|
||||||
/// <returns>Length of the frame contained in outputBuffer.</returns>
|
/// <returns>Length of the frame contained in output.</returns>
|
||||||
public unsafe int DecodeFrame(byte[] input, int inputOffset, byte[] output)
|
public unsafe int DecodeFrame(byte[] input, int inputOffset, byte[] output)
|
||||||
{
|
{
|
||||||
if (disposed)
|
if (disposed)
|
||||||
@@ -64,24 +77,13 @@ namespace Discord.Audio
|
|||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
fixed (byte* inPtr = input)
|
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)
|
if (result < 0)
|
||||||
throw new Exception("Decoding failed: " + ((Opus.Error)result).ToString());
|
throw new Exception("Decoding failed: " + ((OpusError)result).ToString());
|
||||||
return result;
|
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
|
#region IDisposable
|
||||||
private bool disposed;
|
private bool disposed;
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -92,7 +94,7 @@ namespace Discord.Audio
|
|||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
|
||||||
if (_ptr != IntPtr.Zero)
|
if (_ptr != IntPtr.Zero)
|
||||||
Opus.DestroyEncoder(_ptr);
|
UnsafeNativeMethods.DestroyDecoder(_ptr);
|
||||||
|
|
||||||
disposed = true;
|
disposed = true;
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,27 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security;
|
||||||
|
|
||||||
namespace Discord.Audio
|
namespace Discord.Audio.Opus
|
||||||
{
|
{
|
||||||
/// <summary> Opus codec wrapper. </summary>
|
/// <summary> Opus codec wrapper. </summary>
|
||||||
internal class OpusEncoder : IDisposable
|
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;
|
private readonly IntPtr _ptr;
|
||||||
|
|
||||||
/// <summary> Gets the bit rate of the encoder. </summary>
|
/// <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>
|
/// <summary> Gets the bit rate in kbit/s. </summary>
|
||||||
public int? BitRate { get; private set; }
|
public int? BitRate { get; private set; }
|
||||||
/// <summary> Gets the coding mode of the encoder. </summary>
|
/// <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>
|
/// <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>
|
/// <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="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>
|
/// <param name="application">Coding mode.</param>
|
||||||
/// <returns>A new <c>OpusEncoder</c></returns>
|
/// <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 &&
|
if (samplingRate != 8000 && samplingRate != 12000 &&
|
||||||
samplingRate != 16000 && samplingRate != 24000 &&
|
samplingRate != 16000 && samplingRate != 24000 &&
|
||||||
@@ -53,9 +70,9 @@ namespace Discord.Audio
|
|||||||
FrameSize = SamplesPerFrame * SampleSize;
|
FrameSize = SamplesPerFrame * SampleSize;
|
||||||
BitRate = bitrate;
|
BitRate = bitrate;
|
||||||
|
|
||||||
Opus.Error error;
|
OpusError error;
|
||||||
_ptr = Opus.CreateEncoder(samplingRate, channels, (int)application, out error);
|
_ptr = UnsafeNativeMethods.CreateEncoder(samplingRate, channels, (int)application, out error);
|
||||||
if (error != Opus.Error.OK)
|
if (error != OpusError.OK)
|
||||||
throw new InvalidOperationException($"Error occured while creating encoder: {error}");
|
throw new InvalidOperationException($"Error occured while creating encoder: {error}");
|
||||||
|
|
||||||
SetForwardErrorCorrection(true);
|
SetForwardErrorCorrection(true);
|
||||||
@@ -75,10 +92,10 @@ namespace Discord.Audio
|
|||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
fixed (byte* inPtr = input)
|
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)
|
if (result < 0)
|
||||||
throw new Exception("Encoding failed: " + ((Opus.Error)result).ToString());
|
throw new Exception("Encoding failed: " + ((OpusError)result).ToString());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,9 +105,9 @@ namespace Discord.Audio
|
|||||||
if (disposed)
|
if (disposed)
|
||||||
throw new ObjectDisposedException(nameof(OpusEncoder));
|
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)
|
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>
|
/// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
|
||||||
@@ -99,9 +116,9 @@ namespace Discord.Audio
|
|||||||
if (disposed)
|
if (disposed)
|
||||||
throw new ObjectDisposedException(nameof(OpusEncoder));
|
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)
|
if (result < 0)
|
||||||
throw new Exception("Encoder error: " + ((Opus.Error)result).ToString());
|
throw new Exception("Encoder error: " + ((OpusError)result).ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IDisposable
|
#region IDisposable
|
||||||
@@ -114,7 +131,7 @@ namespace Discord.Audio
|
|||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
|
||||||
if (_ptr != IntPtr.Zero)
|
if (_ptr != IntPtr.Zero)
|
||||||
Opus.DestroyEncoder(_ptr);
|
UnsafeNativeMethods.DestroyEncoder(_ptr);
|
||||||
|
|
||||||
disposed = true;
|
disposed = true;
|
||||||
}
|
}
|
||||||
@@ -2,25 +2,4 @@
|
|||||||
|
|
||||||
namespace Discord.Audio
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/Discord.Net.Audio/Sodium/SecretBox.cs
Normal file
30
src/Discord.Net.Audio/Sodium/SecretBox.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
public class CommandEventArgs
|
public class CommandEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
private readonly string[] _args;
|
private readonly string[] _args;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
|||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
/// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary>
|
/// <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.";
|
private const string DefaultPermissionError = "You do not have permission to access this command.";
|
||||||
|
|
||||||
|
|||||||
@@ -224,11 +224,11 @@
|
|||||||
<Compile Include="..\Discord.Net\Net\TimeoutException.cs">
|
<Compile Include="..\Discord.Net\Net\TimeoutException.cs">
|
||||||
<Link>Net\TimeoutException.cs</Link>
|
<Link>Net\TimeoutException.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net\Net\WebSockets\GatewayWebSocket.cs">
|
<Compile Include="..\Discord.Net\Net\WebSockets\GatewaySocket.cs">
|
||||||
<Link>Net\WebSockets\GatewayWebSocket.cs</Link>
|
<Link>Net\WebSockets\GatewaySocket.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net\Net\WebSockets\GatewayWebSockets.Events.cs">
|
<Compile Include="..\Discord.Net\Net\WebSockets\GatewaySocket.Events.cs">
|
||||||
<Link>Net\WebSockets\GatewayWebSockets.Events.cs</Link>
|
<Link>Net\WebSockets\GatewaySocket.Events.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net\Net\WebSockets\IWebSocketEngine.cs">
|
<Compile Include="..\Discord.Net\Net\WebSockets\IWebSocketEngine.cs">
|
||||||
<Link>Net\WebSockets\IWebSocketEngine.cs</Link>
|
<Link>Net\WebSockets\IWebSocketEngine.cs</Link>
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class DiscordClient
|
public partial class DiscordClient : IDisposable
|
||||||
{
|
{
|
||||||
public event EventHandler<UserEventArgs> UserJoined;
|
public event EventHandler<UserEventArgs> UserJoined;
|
||||||
private void RaiseUserJoined(User user)
|
private void RaiseUserJoined(User user)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using Newtonsoft.Json;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.ExceptionServices;
|
using System.Runtime.ExceptionServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -82,8 +81,8 @@ namespace Discord
|
|||||||
private readonly DiscordAPIClient _api;
|
private readonly DiscordAPIClient _api;
|
||||||
|
|
||||||
/// <summary> Returns the internal websocket object. </summary>
|
/// <summary> Returns the internal websocket object. </summary>
|
||||||
public GatewayWebSocket WebSocket => _webSocket;
|
public GatewaySocket WebSocket => _webSocket;
|
||||||
private readonly GatewayWebSocket _webSocket;
|
private readonly GatewaySocket _webSocket;
|
||||||
|
|
||||||
public string GatewayUrl => _gateway;
|
public string GatewayUrl => _gateway;
|
||||||
private string _gateway;
|
private string _gateway;
|
||||||
@@ -140,11 +139,24 @@ namespace Discord
|
|||||||
CreateCacheLogger();
|
CreateCacheLogger();
|
||||||
|
|
||||||
//Networking
|
//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);
|
_api = new DiscordAPIClient(_config);
|
||||||
if (Config.UseMessageQueue)
|
if (Config.UseMessageQueue)
|
||||||
_pendingMessages = new ConcurrentQueue<Message>();
|
_pendingMessages = new ConcurrentQueue<Message>();
|
||||||
this.Connected += async (s, e) =>
|
Connected += async (s, e) =>
|
||||||
{
|
{
|
||||||
_api.CancelToken = _cancelToken;
|
_api.CancelToken = _cancelToken;
|
||||||
await SendStatus().ConfigureAwait(false);
|
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>
|
/// <summary> Connects to the Discord server with the provided email and password. </summary>
|
||||||
/// <returns> Returns a token for future connections. </returns>
|
/// <returns> Returns a token for future connections. </returns>
|
||||||
public async Task<string> Connect(string email, string password)
|
public async Task<string> Connect(string email, string password)
|
||||||
@@ -285,19 +279,15 @@ namespace Discord
|
|||||||
if (State != DiscordClientState.Disconnected)
|
if (State != DiscordClientState.Disconnected)
|
||||||
await Disconnect().ConfigureAwait(false);
|
await Disconnect().ConfigureAwait(false);
|
||||||
|
|
||||||
string token;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var response = await _api.Login(email, password)
|
var response = await _api.Login(email, password)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
token = response.Token;
|
_token = response.Token;
|
||||||
|
_api.Token = response.Token;
|
||||||
if (_config.LogLevel >= LogSeverity.Verbose)
|
if (_config.LogLevel >= LogSeverity.Verbose)
|
||||||
_logger.Log(LogSeverity.Verbose, "Login successful, got token.");
|
_logger.Log(LogSeverity.Verbose, "Login successful, got token.");
|
||||||
|
|
||||||
await Connect(token);
|
await BeginConnect();
|
||||||
return token;
|
return response.Token;
|
||||||
}
|
|
||||||
catch (TaskCanceledException) { throw new TimeoutException(); }
|
|
||||||
}
|
}
|
||||||
/// <summary> Connects to the Discord server with the provided token. </summary>
|
/// <summary> Connects to the Discord server with the provided token. </summary>
|
||||||
public async Task Connect(string token)
|
public async Task Connect(string token)
|
||||||
@@ -308,26 +298,32 @@ namespace Discord
|
|||||||
if (State != (int)DiscordClientState.Disconnected)
|
if (State != (int)DiscordClientState.Disconnected)
|
||||||
await Disconnect().ConfigureAwait(false);
|
await Disconnect().ConfigureAwait(false);
|
||||||
|
|
||||||
|
_token = token;
|
||||||
_api.Token = token;
|
_api.Token = token;
|
||||||
|
await BeginConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task BeginConnect()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_state = (int)DiscordClientState.Connecting;
|
||||||
|
|
||||||
var gatewayResponse = await _api.Gateway().ConfigureAwait(false);
|
var gatewayResponse = await _api.Gateway().ConfigureAwait(false);
|
||||||
string gateway = gatewayResponse.Url;
|
string gateway = gatewayResponse.Url;
|
||||||
if (_config.LogLevel >= LogSeverity.Verbose)
|
if (_config.LogLevel >= LogSeverity.Verbose)
|
||||||
_logger.Log(LogSeverity.Verbose, $"Websocket endpoint: {gateway}");
|
_logger.Log(LogSeverity.Verbose, $"Websocket endpoint: {gateway}");
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_state = (int)DiscordClientState.Connecting;
|
|
||||||
_disconnectedEvent.Reset();
|
_disconnectedEvent.Reset();
|
||||||
|
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_token = token;
|
|
||||||
|
|
||||||
_cancelTokenSource = new CancellationTokenSource();
|
_cancelTokenSource = new CancellationTokenSource();
|
||||||
_cancelToken = _cancelTokenSource.Token;
|
_cancelToken = _cancelTokenSource.Token;
|
||||||
|
|
||||||
_webSocket.Host = gateway;
|
_webSocket.Host = gateway;
|
||||||
_webSocket.ParentCancelToken = _cancelToken;
|
_webSocket.ParentCancelToken = _cancelToken;
|
||||||
await _webSocket.Connect(token).ConfigureAwait(false);
|
await _webSocket.Connect(_token).ConfigureAwait(false);
|
||||||
|
|
||||||
_runTask = RunTasks();
|
_runTask = RunTasks();
|
||||||
|
|
||||||
@@ -342,8 +338,6 @@ namespace Discord
|
|||||||
_webSocket.ThrowError(); //Throws data socket's internal error if any occured
|
_webSocket.ThrowError(); //Throws data socket's internal error if any occured
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
//_state = (int)DiscordClientState.Connected;
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -351,7 +345,7 @@ namespace Discord
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void CompleteConnect()
|
private void EndConnect()
|
||||||
{
|
{
|
||||||
_state = (int)DiscordClientState.Connected;
|
_state = (int)DiscordClientState.Connected;
|
||||||
_connectedEvent.Set();
|
_connectedEvent.Set();
|
||||||
@@ -359,8 +353,8 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <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() => DisconnectInternal(new Exception("Disconnect was requested by user."), isUnexpected: false);
|
public Task Disconnect() => SignalDisconnect(new Exception("Disconnect was requested by user."), isUnexpected: false);
|
||||||
private async Task DisconnectInternal(Exception ex = null, bool isUnexpected = true, bool skipAwait = false)
|
private async Task SignalDisconnect(Exception ex = null, bool isUnexpected = true, bool wait = false)
|
||||||
{
|
{
|
||||||
int oldState;
|
int oldState;
|
||||||
bool hasWriterLock;
|
bool hasWriterLock;
|
||||||
@@ -386,7 +380,7 @@ namespace Discord
|
|||||||
await Cleanup().ConfigureAwait(false);*/
|
await Cleanup().ConfigureAwait(false);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!skipAwait)
|
if (wait)
|
||||||
{
|
{
|
||||||
Task task = _runTask;
|
Task task = _runTask;
|
||||||
if (_runTask != null)
|
if (_runTask != null)
|
||||||
@@ -407,10 +401,10 @@ namespace Discord
|
|||||||
|
|
||||||
//Wait until the first task ends/errors and capture the error
|
//Wait until the first task ends/errors and capture the error
|
||||||
try { await firstTask.ConfigureAwait(false); }
|
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.
|
//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
|
//Wait for the remaining tasks to complete
|
||||||
try { await allTasks.ConfigureAwait(false); }
|
try { await allTasks.ConfigureAwait(false); }
|
||||||
@@ -454,35 +448,6 @@ namespace Discord
|
|||||||
_privateUser = null;
|
_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)
|
private async Task OnReceivedEvent(WebSocketEventEventArgs e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -851,8 +816,7 @@ namespace Discord
|
|||||||
_sentInitialLog = true;
|
_sentInitialLog = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Async Wrapper
|
||||||
//Helpers
|
|
||||||
/// <summary> Blocking call that will not return until client has been stopped. This is mainly intended for use in console applications. </summary>
|
/// <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)
|
public void Run(Func<Task> asyncAction)
|
||||||
{
|
{
|
||||||
@@ -868,7 +832,62 @@ namespace Discord
|
|||||||
{
|
{
|
||||||
_disconnectedEvent.WaitOne();
|
_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()
|
private void CheckReady()
|
||||||
{
|
{
|
||||||
switch (_state)
|
switch (_state)
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ namespace Discord
|
|||||||
public bool IsServerDeafened { get; private set; }
|
public bool IsServerDeafened { get; private set; }
|
||||||
public bool IsServerSuppressed { get; private set; }
|
public bool IsServerSuppressed { get; private set; }
|
||||||
public bool IsPrivate => _server.Id == null;
|
public bool IsPrivate => _server.Id == null;
|
||||||
|
public bool IsOwner => _server.Value.OwnerId == Id;
|
||||||
|
|
||||||
public string SessionId { get; private set; }
|
public string SessionId { get; private set; }
|
||||||
public string Token { get; private set; }
|
public string Token { get; private set; }
|
||||||
@@ -101,11 +102,25 @@ namespace Discord
|
|||||||
{
|
{
|
||||||
if (_server.Id != null)
|
if (_server.Id != null)
|
||||||
{
|
{
|
||||||
|
if (_client.Config.UsePermissionsCache)
|
||||||
|
{
|
||||||
return Server.Channels
|
return Server.Channels
|
||||||
.Where(x => (x.Type == ChannelType.Text && x.GetPermissions(this).ReadMessages) ||
|
.Where(x => (x.Type == ChannelType.Text && x.GetPermissions(this).ReadMessages) ||
|
||||||
(x.Type == ChannelType.Voice && x.GetPermissions(this).Connect));
|
(x.Type == ChannelType.Voice && x.GetPermissions(this).Connect));
|
||||||
}
|
}
|
||||||
else
|
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;
|
var privateChannel = Global.PrivateChannel;
|
||||||
if (privateChannel != null)
|
if (privateChannel != null)
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace Discord.Net
|
namespace Discord.Net
|
||||||
{
|
{
|
||||||
|
#if NET45
|
||||||
|
[Serializable]
|
||||||
|
#endif
|
||||||
public class HttpException : Exception
|
public class HttpException : Exception
|
||||||
{
|
{
|
||||||
public HttpStatusCode StatusCode { get; }
|
public HttpStatusCode StatusCode { get; }
|
||||||
@@ -12,5 +16,9 @@ namespace Discord.Net
|
|||||||
{
|
{
|
||||||
StatusCode = statusCode;
|
StatusCode = statusCode;
|
||||||
}
|
}
|
||||||
|
#if NET45
|
||||||
|
public override void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||||
|
=> base.GetObjectData(info, context);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace Discord.Net
|
namespace Discord.Net
|
||||||
{
|
{
|
||||||
|
#if NET45
|
||||||
|
[Serializable]
|
||||||
|
#endif
|
||||||
public sealed class TimeoutException : OperationCanceledException
|
public sealed class TimeoutException : OperationCanceledException
|
||||||
{
|
{
|
||||||
public TimeoutException()
|
public TimeoutException()
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace Discord.Net.WebSockets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class GatewayWebSocket
|
public partial class GatewaySocket
|
||||||
{
|
{
|
||||||
public event EventHandler<WebSocketEventEventArgs> ReceivedDispatch;
|
public event EventHandler<WebSocketEventEventArgs> ReceivedDispatch;
|
||||||
private void RaiseReceivedDispatch(string type, JToken payload)
|
private void RaiseReceivedDispatch(string type, JToken payload)
|
||||||
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Discord.Net.WebSockets
|
namespace Discord.Net.WebSockets
|
||||||
{
|
{
|
||||||
public partial class GatewayWebSocket : WebSocket
|
public partial class GatewaySocket : WebSocket
|
||||||
{
|
{
|
||||||
public int LastSequence => _lastSeq;
|
public int LastSequence => _lastSeq;
|
||||||
private int _lastSeq;
|
private int _lastSeq;
|
||||||
@@ -17,7 +17,7 @@ namespace Discord.Net.WebSockets
|
|||||||
public string SessionId => _sessionId;
|
public string SessionId => _sessionId;
|
||||||
private string _sessionId;
|
private string _sessionId;
|
||||||
|
|
||||||
public GatewayWebSocket(DiscordConfig config, Logger logger)
|
public GatewaySocket(DiscordConfig config, Logger logger)
|
||||||
: base(config, logger)
|
: base(config, logger)
|
||||||
{
|
{
|
||||||
Disconnected += async (s, e) =>
|
Disconnected += async (s, e) =>
|
||||||
@@ -29,12 +29,16 @@ namespace Discord.Net.WebSockets
|
|||||||
|
|
||||||
public async Task Connect(string token)
|
public async Task Connect(string token)
|
||||||
{
|
{
|
||||||
|
await SignalDisconnect(wait: true).ConfigureAwait(false);
|
||||||
|
|
||||||
_token = token;
|
_token = token;
|
||||||
await BeginConnect().ConfigureAwait(false);
|
await BeginConnect().ConfigureAwait(false);
|
||||||
SendIdentify(token);
|
SendIdentify(token);
|
||||||
}
|
}
|
||||||
private async Task Redirect(string server)
|
private async Task Redirect(string server)
|
||||||
{
|
{
|
||||||
|
await SignalDisconnect(wait: true).ConfigureAwait(false);
|
||||||
|
|
||||||
await BeginConnect().ConfigureAwait(false);
|
await BeginConnect().ConfigureAwait(false);
|
||||||
SendResume();
|
SendResume();
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,6 @@ namespace Discord.Net.WebSockets
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await SignalDisconnect(wait: true).ConfigureAwait(false);
|
|
||||||
_state = (int)WebSocketState.Connecting;
|
_state = (int)WebSocketState.Connecting;
|
||||||
|
|
||||||
if (ParentCancelToken == null)
|
if (ParentCancelToken == null)
|
||||||
@@ -173,7 +172,7 @@ namespace Discord.Net.WebSockets
|
|||||||
await Cleanup().ConfigureAwait(false);
|
await Cleanup().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!wait)
|
if (wait)
|
||||||
{
|
{
|
||||||
Task task = _runTask;
|
Task task = _runTask;
|
||||||
if (_runTask != null)
|
if (_runTask != null)
|
||||||
|
|||||||
1
src/Discord.Net/Settings.StyleCop
Normal file
1
src/Discord.Net/Settings.StyleCop
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<StyleCopSettings Version="105" />
|
||||||
@@ -29,7 +29,8 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Newtonsoft.Json": "7.0.1"
|
"Newtonsoft.Json": "7.0.1",
|
||||||
|
"StyleCop.Analyzers": "1.0.0-rc2"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
|||||||
Reference in New Issue
Block a user