Added support for .NET Standard 1.1 and 1.2
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Discord.API;
|
||||
using Discord.API.Voice;
|
||||
using Discord.Net.Converters;
|
||||
using Discord.Net.Udp;
|
||||
using Discord.Net.WebSockets;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
@@ -9,8 +10,6 @@ using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -42,19 +41,27 @@ namespace Discord.Audio
|
||||
private readonly IWebSocketClient _webSocketClient;
|
||||
private readonly SemaphoreSlim _connectionLock;
|
||||
private CancellationTokenSource _connectCancelToken;
|
||||
private UdpClient _udp;
|
||||
private IPEndPoint _udpEndpoint;
|
||||
private Task _udpRecieveTask;
|
||||
private IUdpSocket _udp;
|
||||
private bool _isDisposed;
|
||||
|
||||
public ulong GuildId { get; }
|
||||
public ConnectionState ConnectionState { get; private set; }
|
||||
|
||||
internal DiscordVoiceAPIClient(ulong guildId, WebSocketProvider webSocketProvider, JsonSerializer serializer = null)
|
||||
internal DiscordVoiceAPIClient(ulong guildId, WebSocketProvider webSocketProvider, UdpSocketProvider udpSocketProvider, JsonSerializer serializer = null)
|
||||
{
|
||||
GuildId = guildId;
|
||||
_connectionLock = new SemaphoreSlim(1, 1);
|
||||
_udp = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
|
||||
_udp = udpSocketProvider();
|
||||
_udp.ReceivedDatagram += async (data, index, count) =>
|
||||
{
|
||||
if (index != 0)
|
||||
{
|
||||
var newData = new byte[count];
|
||||
Buffer.BlockCopy(data, index, newData, 0, count);
|
||||
data = newData;
|
||||
}
|
||||
await _receivedPacketEvent.InvokeAsync(data).ConfigureAwait(false);
|
||||
};
|
||||
|
||||
_webSocketClient = webSocketProvider();
|
||||
//_gatewayClient.SetHeader("user-agent", DiscordConfig.UserAgent); (Causes issues in .Net 4.6+)
|
||||
@@ -93,6 +100,7 @@ namespace Discord.Audio
|
||||
if (disposing)
|
||||
{
|
||||
_connectCancelToken?.Dispose();
|
||||
(_udp as IDisposable)?.Dispose();
|
||||
(_webSocketClient as IDisposable)?.Dispose();
|
||||
}
|
||||
_isDisposed = true;
|
||||
@@ -111,18 +119,14 @@ namespace Discord.Audio
|
||||
}
|
||||
public async Task SendAsync(byte[] data, int bytes)
|
||||
{
|
||||
if (_udpEndpoint != null)
|
||||
{
|
||||
await _udp.SendAsync(data, bytes, _udpEndpoint).ConfigureAwait(false);
|
||||
await _sentDataEvent.InvokeAsync(bytes).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await _udp.SendAsync(data, 0, bytes).ConfigureAwait(false);
|
||||
await _sentDataEvent.InvokeAsync(bytes).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
//WebSocket
|
||||
public async Task SendHeartbeatAsync(RequestOptions options = null)
|
||||
{
|
||||
await SendAsync(VoiceOpCode.Heartbeat, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), options: options).ConfigureAwait(false);
|
||||
await SendAsync(VoiceOpCode.Heartbeat, DateTimeUtils.ToUnixMilliseconds(DateTimeOffset.UtcNow), options: options).ConfigureAwait(false);
|
||||
}
|
||||
public async Task SendIdentityAsync(ulong userId, string sessionId, string token)
|
||||
{
|
||||
@@ -171,9 +175,13 @@ namespace Discord.Audio
|
||||
try
|
||||
{
|
||||
_connectCancelToken = new CancellationTokenSource();
|
||||
_webSocketClient.SetCancelToken(_connectCancelToken.Token);
|
||||
var cancelToken = _connectCancelToken.Token;
|
||||
|
||||
_webSocketClient.SetCancelToken(cancelToken);
|
||||
await _webSocketClient.ConnectAsync(url).ConfigureAwait(false);
|
||||
_udpRecieveTask = ReceiveAsync(_connectCancelToken.Token);
|
||||
|
||||
_udp.SetCancelToken(cancelToken);
|
||||
await _udp.StartAsync().ConfigureAwait(false);
|
||||
|
||||
ConnectionState = ConnectionState.Connected;
|
||||
}
|
||||
@@ -202,8 +210,7 @@ namespace Discord.Audio
|
||||
catch { }
|
||||
|
||||
//Wait for tasks to complete
|
||||
await _udpRecieveTask.ConfigureAwait(false);
|
||||
|
||||
await _udp.StopAsync().ConfigureAwait(false);
|
||||
await _webSocketClient.DisconnectAsync().ConfigureAwait(false);
|
||||
|
||||
ConnectionState = ConnectionState.Disconnected;
|
||||
@@ -221,22 +228,9 @@ namespace Discord.Audio
|
||||
await _sentDiscoveryEvent.InvokeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public void SetUdpEndpoint(IPEndPoint endpoint)
|
||||
public void SetUdpEndpoint(string host, int port)
|
||||
{
|
||||
_udpEndpoint = endpoint;
|
||||
}
|
||||
private async Task ReceiveAsync(CancellationToken cancelToken)
|
||||
{
|
||||
var closeTask = Task.Delay(-1, cancelToken);
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
var receiveTask = _udp.ReceiveAsync();
|
||||
var task = await Task.WhenAny(closeTask, receiveTask).ConfigureAwait(false);
|
||||
if (task == closeTask)
|
||||
break;
|
||||
|
||||
await _receivedPacketEvent.InvokeAsync(receiveTask.Result.Buffer).ConfigureAwait(false);
|
||||
}
|
||||
_udp.SetDestination(host, port);
|
||||
}
|
||||
|
||||
//Helpers
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace Discord.Audio
|
||||
e.ErrorContext.Handled = true;
|
||||
};
|
||||
|
||||
ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider);
|
||||
ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider, Discord.UdpSocketProvider);
|
||||
|
||||
ApiClient.SentGatewayMessage += async opCode => await _audioLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);
|
||||
ApiClient.SentDiscovery += async () => await _audioLogger.DebugAsync($"Sent Discovery").ConfigureAwait(false);
|
||||
@@ -204,10 +204,8 @@ namespace Discord.Audio
|
||||
|
||||
_heartbeatTime = 0;
|
||||
_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _cancelToken.Token);
|
||||
|
||||
var entry = await Dns.GetHostEntryAsync(_url).ConfigureAwait(false);
|
||||
|
||||
ApiClient.SetUdpEndpoint(new IPEndPoint(entry.AddressList[0], data.Port));
|
||||
|
||||
ApiClient.SetUdpEndpoint(_url, data.Port);
|
||||
await ApiClient.SendDiscoveryAsync(_ssrc).ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
|
||||
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Description>A core Discord.Net library containing the WebSocket client and models.</Description>
|
||||
<VersionPrefix>1.0.0-beta2</VersionPrefix>
|
||||
<TargetFramework>netstandard1.3</TargetFramework>
|
||||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<AssemblyName>Discord.Net.WebSocket</AssemblyName>
|
||||
<PackageTags>discord;discordapp</PackageTags>
|
||||
@@ -11,48 +10,27 @@
|
||||
<PackageLicenseUrl>http://opensource.org/licenses/MIT</PackageLicenseUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>git://github.com/RogueException/Discord.Net</RepositoryUrl>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);dotnet5.4;dnxcore50;portable-net45+win8</PackageTargetFallback>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="**\*.cs" />
|
||||
<EmbeddedResource Include="**\*.resx" />
|
||||
<EmbeddedResource Include="compiler\resources\**\*" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
|
||||
<ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Sdk">
|
||||
<Version>1.0.0-alpha-20161104-2</Version>
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.IO.Compression">
|
||||
<Version>4.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.NameResolution">
|
||||
<Version>4.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Sockets">
|
||||
<Version>4.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.WebSockets.Client">
|
||||
<Version>4.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Runtime.InteropServices">
|
||||
<Version>4.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
|
||||
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
||||
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
|
||||
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
|
||||
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<SignAssembly>False</SignAssembly>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
|
||||
<NoWarn>$(NoWarn);CS1573;CS1591</NoWarn>
|
||||
<WarningsAsErrors>true</WarningsAsErrors>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -4,6 +4,7 @@ using Discord.Audio;
|
||||
using Discord.Logging;
|
||||
using Discord.Net.Converters;
|
||||
using Discord.Net.Queue;
|
||||
using Discord.Net.Udp;
|
||||
using Discord.Net.WebSockets;
|
||||
using Discord.Rest;
|
||||
using Newtonsoft.Json;
|
||||
@@ -56,6 +57,7 @@ namespace Discord.WebSocket
|
||||
internal AudioMode AudioMode { get; private set; }
|
||||
internal ClientState State { get; private set; }
|
||||
internal int ConnectionTimeout { get; private set; }
|
||||
internal UdpSocketProvider UdpSocketProvider { get; private set; }
|
||||
internal WebSocketProvider WebSocketProvider { get; private set; }
|
||||
internal bool DownloadUsersOnGuildAvailable { get; private set; }
|
||||
|
||||
@@ -76,6 +78,7 @@ namespace Discord.WebSocket
|
||||
MessageCacheSize = config.MessageCacheSize;
|
||||
LargeThreshold = config.LargeThreshold;
|
||||
AudioMode = config.AudioMode;
|
||||
UdpSocketProvider = config.UdpSocketProvider;
|
||||
WebSocketProvider = config.WebSocketProvider;
|
||||
DownloadUsersOnGuildAvailable = config.DownloadUsersOnGuildAvailable;
|
||||
ConnectionTimeout = config.ConnectionTimeout;
|
||||
@@ -115,7 +118,7 @@ namespace Discord.WebSocket
|
||||
GuildAvailable += g =>
|
||||
{
|
||||
var _ = g.DownloadUsersAsync();
|
||||
return Task.CompletedTask;
|
||||
return Task.Delay(0);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -512,7 +515,7 @@ namespace Discord.WebSocket
|
||||
await ApiClient.SendStatusUpdateAsync(
|
||||
status,
|
||||
status == UserStatus.AFK,
|
||||
statusSince != null ? _statusSince.Value.ToUnixTimeMilliseconds() : (long?)null,
|
||||
statusSince != null ? DateTimeUtils.ToUnixMilliseconds(_statusSince.Value) : (long?)null,
|
||||
gameModel).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Discord.Audio;
|
||||
using Discord.Net.Udp;
|
||||
using Discord.Net.WebSockets;
|
||||
using Discord.Rest;
|
||||
using System;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
@@ -27,9 +29,30 @@ namespace Discord.WebSocket
|
||||
public AudioMode AudioMode { get; set; } = AudioMode.Disabled;
|
||||
|
||||
/// <summary> Gets or sets the provider used to generate new websocket connections. </summary>
|
||||
public WebSocketProvider WebSocketProvider { get; set; } = () => new DefaultWebSocketClient();
|
||||
public WebSocketProvider WebSocketProvider { get; set; }
|
||||
/// <summary> Gets or sets the provider used to generate new udp sockets. </summary>
|
||||
public UdpSocketProvider UdpSocketProvider { get; set; }
|
||||
|
||||
/// <summary> Gets or sets whether or not all users should be downloaded as guilds come available. </summary>
|
||||
public bool DownloadUsersOnGuildAvailable { get; set; } = false;
|
||||
|
||||
public DiscordSocketConfig()
|
||||
{
|
||||
#if NETSTANDARD1_3
|
||||
WebSocketProvider = () => new DefaultWebSocketClient();
|
||||
UdpSocketProvider = () => new DefaultUdpSocket();
|
||||
#else
|
||||
WebSocketProvider = () =>
|
||||
{
|
||||
throw new InvalidOperationException("The default websocket provider is not supported on this platform.\n" +
|
||||
"You must specify a WebSocketProvider or target a runtime supporting .NET Standard 1.3, such as .NET Framework 4.6+.");
|
||||
};
|
||||
UdpSocketProvider = () =>
|
||||
{
|
||||
throw new InvalidOperationException("The default UDP provider is not supported on this platform.\n" +
|
||||
"You must specify a UdpSocketProvider or target a runtime supporting .NET Standard 1.3, such as .NET Framework 4.6+.");
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,10 @@ namespace Discord.WebSocket
|
||||
|
||||
/// <summary> Sends a message to this message channel. </summary>
|
||||
new Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null);
|
||||
#if NETSTANDARD1_3
|
||||
/// <summary> Sends a file to this text channel, with an optional caption. </summary>
|
||||
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, RequestOptions options = null);
|
||||
#endif
|
||||
/// <summary> Sends a file to this text channel, with an optional caption. </summary>
|
||||
new Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, RequestOptions options = null);
|
||||
|
||||
|
||||
@@ -68,8 +68,10 @@ namespace Discord.WebSocket
|
||||
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
|
||||
#if NETSTANDARD1_3
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
|
||||
#endif
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
|
||||
|
||||
@@ -130,8 +132,10 @@ namespace Discord.WebSocket
|
||||
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
|
||||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false);
|
||||
#if NETSTANDARD1_3
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
|
||||
#endif
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
|
||||
|
||||
@@ -91,8 +91,10 @@ namespace Discord.WebSocket
|
||||
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
|
||||
#if NETSTANDARD1_3
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
|
||||
#endif
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
|
||||
|
||||
@@ -193,8 +195,10 @@ namespace Discord.WebSocket
|
||||
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
|
||||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false);
|
||||
#if NETSTANDARD1_3
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
|
||||
#endif
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
|
||||
|
||||
@@ -74,8 +74,10 @@ namespace Discord.WebSocket
|
||||
|
||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
|
||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
|
||||
#if NETSTANDARD1_3
|
||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
|
||||
#endif
|
||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
|
||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
|
||||
|
||||
@@ -131,8 +133,10 @@ namespace Discord.WebSocket
|
||||
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
|
||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
|
||||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false);
|
||||
#if NETSTANDARD1_3
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
|
||||
#endif
|
||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
|
||||
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
|
||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
|
||||
|
||||
129
src/Discord.Net.WebSocket/Net/DefaultUdpSocket.cs
Normal file
129
src/Discord.Net.WebSocket/Net/DefaultUdpSocket.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
#if NETSTANDARD1_3
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Net.Udp
|
||||
{
|
||||
internal class DefaultUdpSocket : IUdpSocket
|
||||
{
|
||||
public event Func<byte[], int, int, Task> ReceivedDatagram;
|
||||
|
||||
private readonly SemaphoreSlim _lock;
|
||||
private UdpClient _udp;
|
||||
private IPEndPoint _destination;
|
||||
private CancellationTokenSource _cancelTokenSource;
|
||||
private CancellationToken _cancelToken, _parentToken;
|
||||
private Task _task;
|
||||
private bool _isDisposed;
|
||||
|
||||
public DefaultUdpSocket()
|
||||
{
|
||||
_lock = new SemaphoreSlim(1, 1);
|
||||
}
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
StopInternalAsync(true).GetAwaiter().GetResult();
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
await _lock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await StartInternalAsync(_cancelToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
public async Task StartInternalAsync(CancellationToken cancelToken)
|
||||
{
|
||||
await StopInternalAsync().ConfigureAwait(false);
|
||||
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
|
||||
|
||||
_udp = new UdpClient();
|
||||
|
||||
_task = RunAsync(_cancelToken);
|
||||
}
|
||||
public async Task StopAsync()
|
||||
{
|
||||
await _lock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await StopInternalAsync().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
public async Task StopInternalAsync(bool isDisposing = false)
|
||||
{
|
||||
try { _cancelTokenSource.Cancel(false); } catch { }
|
||||
|
||||
if (!isDisposing)
|
||||
await (_task ?? Task.Delay(0)).ConfigureAwait(false);
|
||||
|
||||
if (_udp != null)
|
||||
{
|
||||
try { _udp.Dispose(); }
|
||||
catch { }
|
||||
_udp = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDestination(string host, int port)
|
||||
{
|
||||
var entry = Dns.GetHostEntryAsync(host).GetAwaiter().GetResult();
|
||||
_destination = new IPEndPoint(entry.AddressList[0], port);
|
||||
}
|
||||
public void SetCancelToken(CancellationToken cancelToken)
|
||||
{
|
||||
_parentToken = cancelToken;
|
||||
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
|
||||
}
|
||||
|
||||
public async Task SendAsync(byte[] data, int index, int count)
|
||||
{
|
||||
if (index != 0) //Should never happen?
|
||||
{
|
||||
var newData = new byte[count];
|
||||
Buffer.BlockCopy(data, index, newData, 0, count);
|
||||
data = newData;
|
||||
}
|
||||
await _udp.SendAsync(data, count, _destination).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task RunAsync(CancellationToken cancelToken)
|
||||
{
|
||||
var closeTask = Task.Delay(-1, cancelToken);
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
var receiveTask = _udp.ReceiveAsync();
|
||||
var task = await Task.WhenAny(closeTask, receiveTask).ConfigureAwait(false);
|
||||
if (task == closeTask)
|
||||
break;
|
||||
|
||||
var result = receiveTask.Result;
|
||||
await ReceivedDatagram(result.Buffer, 0, result.Buffer.Length).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
231
src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs
Normal file
231
src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
#if NETSTANDARD1_3
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Net.WebSockets
|
||||
{
|
||||
internal class DefaultWebSocketClient : IWebSocketClient
|
||||
{
|
||||
public const int ReceiveChunkSize = 16 * 1024; //16KB
|
||||
public const int SendChunkSize = 4 * 1024; //4KB
|
||||
private const int HR_TIMEOUT = -2147012894;
|
||||
|
||||
public event Func<byte[], int, int, Task> BinaryMessage;
|
||||
public event Func<string, Task> TextMessage;
|
||||
public event Func<Exception, Task> Closed;
|
||||
|
||||
private readonly SemaphoreSlim _lock;
|
||||
private readonly Dictionary<string, string> _headers;
|
||||
private ClientWebSocket _client;
|
||||
private Task _task;
|
||||
private CancellationTokenSource _cancelTokenSource;
|
||||
private CancellationToken _cancelToken, _parentToken;
|
||||
private bool _isDisposed;
|
||||
|
||||
public DefaultWebSocketClient()
|
||||
{
|
||||
_lock = new SemaphoreSlim(1, 1);
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
_cancelToken = CancellationToken.None;
|
||||
_parentToken = CancellationToken.None;
|
||||
_headers = new Dictionary<string, string>();
|
||||
}
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
DisconnectInternalAsync(true).GetAwaiter().GetResult();
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
public async Task ConnectAsync(string host)
|
||||
{
|
||||
await _lock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await ConnectInternalAsync(host).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
private async Task ConnectInternalAsync(string host)
|
||||
{
|
||||
await DisconnectInternalAsync().ConfigureAwait(false);
|
||||
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
|
||||
|
||||
_client = new ClientWebSocket();
|
||||
_client.Options.Proxy = null;
|
||||
_client.Options.KeepAliveInterval = TimeSpan.Zero;
|
||||
foreach (var header in _headers)
|
||||
{
|
||||
if (header.Value != null)
|
||||
_client.Options.SetRequestHeader(header.Key, header.Value);
|
||||
}
|
||||
|
||||
await _client.ConnectAsync(new Uri(host), _cancelToken).ConfigureAwait(false);
|
||||
_task = RunAsync(_cancelToken);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
await _lock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await DisconnectInternalAsync().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
private async Task DisconnectInternalAsync(bool isDisposing = false)
|
||||
{
|
||||
try { _cancelTokenSource.Cancel(false); } catch { }
|
||||
|
||||
if (!isDisposing)
|
||||
await (_task ?? Task.Delay(0)).ConfigureAwait(false);
|
||||
|
||||
if (_client != null && _client.State == WebSocketState.Open)
|
||||
{
|
||||
var token = new CancellationToken();
|
||||
if (!isDisposing)
|
||||
{
|
||||
try { await _client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", token); }
|
||||
catch { }
|
||||
}
|
||||
try { _client.Dispose(); }
|
||||
catch { }
|
||||
_client = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetHeader(string key, string value)
|
||||
{
|
||||
_headers[key] = value;
|
||||
}
|
||||
public void SetCancelToken(CancellationToken cancelToken)
|
||||
{
|
||||
_parentToken = cancelToken;
|
||||
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
|
||||
}
|
||||
|
||||
public async Task SendAsync(byte[] data, int index, int count, bool isText)
|
||||
{
|
||||
await _lock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (_client == null) return;
|
||||
|
||||
int frameCount = (int)Math.Ceiling((double)count / SendChunkSize);
|
||||
|
||||
for (int i = 0; i < frameCount; i++, index += SendChunkSize)
|
||||
{
|
||||
bool isLast = i == (frameCount - 1);
|
||||
|
||||
int frameSize;
|
||||
if (isLast)
|
||||
frameSize = count - (i * SendChunkSize);
|
||||
else
|
||||
frameSize = SendChunkSize;
|
||||
|
||||
var type = isText ? WebSocketMessageType.Text : WebSocketMessageType.Binary;
|
||||
await _client.SendAsync(new ArraySegment<byte>(data, index, count), type, isLast, _cancelToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunAsync(CancellationToken cancelToken)
|
||||
{
|
||||
var buffer = new ArraySegment<byte>(new byte[ReceiveChunkSize]);
|
||||
|
||||
try
|
||||
{
|
||||
while (!cancelToken.IsCancellationRequested)
|
||||
{
|
||||
WebSocketReceiveResult socketResult = await _client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);
|
||||
byte[] result;
|
||||
int resultCount;
|
||||
|
||||
if (socketResult.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
var _ = Closed(new WebSocketClosedException((int)socketResult.CloseStatus, socketResult.CloseStatusDescription));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!socketResult.EndOfMessage)
|
||||
{
|
||||
//This is a large message (likely just READY), lets create a temporary expandable stream
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(buffer.Array, 0, socketResult.Count);
|
||||
do
|
||||
{
|
||||
if (cancelToken.IsCancellationRequested) return;
|
||||
socketResult = await _client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);
|
||||
stream.Write(buffer.Array, 0, socketResult.Count);
|
||||
}
|
||||
while (socketResult == null || !socketResult.EndOfMessage);
|
||||
|
||||
//Use the internal buffer if we can get it
|
||||
resultCount = (int)stream.Length;
|
||||
#if NETSTANDARD1_3
|
||||
ArraySegment<byte> streamBuffer;
|
||||
if (stream.TryGetBuffer(out streamBuffer))
|
||||
result = streamBuffer.Array;
|
||||
else
|
||||
result = stream.ToArray();
|
||||
#else
|
||||
result = stream.ToArray();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Small message
|
||||
resultCount = socketResult.Count;
|
||||
result = buffer.Array;
|
||||
}
|
||||
|
||||
if (socketResult.MessageType == WebSocketMessageType.Text)
|
||||
{
|
||||
string text = Encoding.UTF8.GetString(result, 0, resultCount);
|
||||
await TextMessage(text).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
await BinaryMessage(result, 0, resultCount).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Win32Exception ex) when (ex.HResult == HR_TIMEOUT)
|
||||
{
|
||||
var _ = Closed(new Exception("Connection timed out.", ex));
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
catch (Exception ex)
|
||||
{
|
||||
//This cannot be awaited otherwise we'll deadlock when DiscordApiClient waits for this task to complete.
|
||||
var _ = Closed(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -37,19 +37,17 @@
|
||||
"target": "project"
|
||||
},
|
||||
"System.IO.Compression": "4.3.0",
|
||||
"System.Net.NameResolution": "4.3.0",
|
||||
"System.Net.Sockets": "4.3.0",
|
||||
"System.Net.WebSockets.Client": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.1": {},
|
||||
"netstandard1.3": {
|
||||
"imports": [
|
||||
"dotnet5.4",
|
||||
"dnxcore50",
|
||||
"portable-net45+win8"
|
||||
]
|
||||
"dependencies": {
|
||||
"System.Net.NameResolution": "4.3.0",
|
||||
"System.Net.Sockets": "4.3.0",
|
||||
"System.Net.WebSockets.Client": "4.3.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user