@@ -20,7 +20,7 @@ Bleeding edge builds are available using our MyGet feed (`https://www.myget.org/ | |||
In order to compile Discord.Net, you require the following: | |||
### Using Visual Studio | |||
- [Visual Studio 2017 RC](https://www.microsoft.com/net/core#windowsvs2017) | |||
- [Visual Studio 2017 RC Build 26014.0](https://www.microsoft.com/net/core#windowsvs2017) | |||
The .NET Core and Docker (Preview) workload is required during Visual Studio installation. | |||
@@ -28,8 +28,8 @@ namespace Discord.Commands | |||
public static Dictionary<Type, ModuleInfo> Build(CommandService service, params TypeInfo[] validTypes) => Build(validTypes, service); | |||
public static Dictionary<Type, ModuleInfo> Build(IEnumerable<TypeInfo> validTypes, CommandService service) | |||
{ | |||
if (!validTypes.Any()) | |||
throw new InvalidOperationException("Could not find any valid modules from the given selection"); | |||
/*if (!validTypes.Any()) | |||
throw new InvalidOperationException("Could not find any valid modules from the given selection");*/ | |||
var topLevelGroups = validTypes.Where(x => x.DeclaringType == null); | |||
var subGroups = validTypes.Intersect(topLevelGroups); | |||
@@ -65,7 +65,8 @@ namespace Discord.Commands | |||
if (builtTypes.Contains(typeInfo)) | |||
continue; | |||
builder.AddModule((module) => { | |||
builder.AddModule((module) => | |||
{ | |||
BuildModule(module, typeInfo, service); | |||
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service); | |||
}); | |||
@@ -106,7 +107,8 @@ namespace Discord.Commands | |||
foreach (var method in validCommands) | |||
{ | |||
builder.AddCommand((command) => { | |||
builder.AddCommand((command) => | |||
{ | |||
BuildCommand(command, typeInfo, method, service); | |||
}); | |||
} | |||
@@ -147,21 +149,24 @@ namespace Discord.Commands | |||
int pos = 0, count = parameters.Length; | |||
foreach (var paramInfo in parameters) | |||
{ | |||
builder.AddParameter((parameter) => { | |||
builder.AddParameter((parameter) => | |||
{ | |||
BuildParameter(parameter, paramInfo, pos++, count, service); | |||
}); | |||
} | |||
var createInstance = ReflectionUtils.CreateBuilder<ModuleBase>(typeInfo, service); | |||
builder.Callback = (ctx, args, map) => { | |||
builder.Callback = (ctx, args, map) => | |||
{ | |||
var instance = createInstance(map); | |||
instance.Context = ctx; | |||
try | |||
{ | |||
return method.Invoke(instance, args) as Task ?? Task.CompletedTask; | |||
return method.Invoke(instance, args) as Task ?? Task.Delay(0); | |||
} | |||
finally{ | |||
finally | |||
{ | |||
(instance as IDisposable)?.Dispose(); | |||
} | |||
}; | |||
@@ -1,41 +1,25 @@ | |||
<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 Discord.Net extension adding support for bot commands.</Description> | |||
<VersionPrefix>1.0.0-beta2</VersionPrefix> | |||
<TargetFramework>netstandard1.3</TargetFramework> | |||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks> | |||
<AssemblyName>Discord.Net.Commands</AssemblyName> | |||
<PackageTags>discord;discordapp</PackageTags> | |||
<PackageProjectUrl>https://github.com/RogueException/Discord.Net</PackageProjectUrl> | |||
<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" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.NET.Sdk"> | |||
<Version>1.0.0-alpha-20161104-2</Version> | |||
<PrivateAssets>All</PrivateAssets> | |||
</PackageReference> | |||
</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> |
@@ -32,12 +32,7 @@ | |||
}, | |||
"frameworks": { | |||
"netstandard1.3": { | |||
"imports": [ | |||
"dotnet5.4", | |||
"dnxcore50", | |||
"portable-net45+win8" | |||
] | |||
} | |||
"netstandard1.1": {}, | |||
"netstandard1.3": {} | |||
} | |||
} |
@@ -160,8 +160,8 @@ namespace Discord.API | |||
LoginState = LoginState.LoggedOut; | |||
} | |||
internal virtual Task ConnectInternalAsync() => Task.CompletedTask; | |||
internal virtual Task DisconnectInternalAsync() => Task.CompletedTask; | |||
internal virtual Task ConnectInternalAsync() => Task.Delay(0); | |||
internal virtual Task DisconnectInternalAsync() => Task.Delay(0); | |||
//Core | |||
internal Task SendAsync(string method, Expression<Func<string>> endpointExpr, BucketIds ids, | |||
@@ -1,60 +1,30 @@ | |||
<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 .Net API wrapper and bot framework for Discord.</Description> | |||
<VersionPrefix>1.0.0-beta2</VersionPrefix> | |||
<TargetFramework>netstandard1.3</TargetFramework> | |||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks> | |||
<AssemblyName>Discord.Net.Core</AssemblyName> | |||
<PackageTags>discord;discordapp</PackageTags> | |||
<PackageProjectUrl>https://github.com/RogueException/Discord.Net</PackageProjectUrl> | |||
<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> | |||
<PackageReference Include="Microsoft.NET.Sdk"> | |||
<Version>1.0.0-alpha-20161104-2</Version> | |||
<PrivateAssets>All</PrivateAssets> | |||
</PackageReference> | |||
<PackageReference Include="Microsoft.Win32.Primitives"> | |||
<Version>4.3.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="Newtonsoft.Json"> | |||
<Version>9.0.1</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.Collections.Concurrent"> | |||
<Version>4.3.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.Collections.Immutable"> | |||
<Version>1.3.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.Interactive.Async"> | |||
<Version>3.1.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.Net.Http"> | |||
<Version>4.3.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.Net.WebSockets.Client"> | |||
<Version>4.3.0</Version> | |||
<PrivateAssets>All</PrivateAssets> | |||
</PackageReference> | |||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" /> | |||
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" /> | |||
<PackageReference Include="System.Collections.Immutable" Version="1.3.0" /> | |||
<PackageReference Include="System.Interactive.Async" Version="3.1.0" /> | |||
<PackageReference Include="System.Net.Http" Version="4.3.0" /> | |||
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" /> | |||
</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> |
@@ -9,8 +9,10 @@ namespace Discord | |||
{ | |||
/// <summary> Sends a message to this message channel. </summary> | |||
Task<IUserMessage> 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> | |||
Task<IUserMessage> 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> | |||
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, RequestOptions options = null); | |||
@@ -8,24 +8,30 @@ namespace Discord | |||
{ | |||
internal static class CollectionExtensions | |||
{ | |||
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source) | |||
=> new ConcurrentDictionaryWrapper<TValue>(source.Select(x => x.Value), () => source.Count); | |||
//public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue>(this IReadOnlyCollection<TValue> source) | |||
// => new CollectionWrapper<TValue>(source, () => source.Count); | |||
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue>(this ICollection<TValue> source) | |||
=> new CollectionWrapper<TValue>(source, () => source.Count); | |||
//public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source) | |||
// => new CollectionWrapper<TValue>(source.Select(x => x.Value), () => source.Count); | |||
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TKey, TValue>(this IDictionary<TKey, TValue> source) | |||
=> new CollectionWrapper<TValue>(source.Select(x => x.Value), () => source.Count); | |||
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue, TSource>(this IEnumerable<TValue> query, IReadOnlyCollection<TSource> source) | |||
=> new ConcurrentDictionaryWrapper<TValue>(query, () => source.Count); | |||
=> new CollectionWrapper<TValue>(query, () => source.Count); | |||
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue>(this IEnumerable<TValue> query, Func<int> countFunc) | |||
=> new ConcurrentDictionaryWrapper<TValue>(query, countFunc); | |||
=> new CollectionWrapper<TValue>(query, countFunc); | |||
} | |||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] | |||
internal struct ConcurrentDictionaryWrapper<TValue> : IReadOnlyCollection<TValue> | |||
internal struct CollectionWrapper<TValue> : IReadOnlyCollection<TValue> | |||
{ | |||
private readonly IEnumerable<TValue> _query; | |||
private readonly Func<int> _countFunc; | |||
//It's okay that this count is affected by race conditions - we're wrapping a concurrent collection and that's to be expected | |||
public int Count => _countFunc(); | |||
public ConcurrentDictionaryWrapper(IEnumerable<TValue> query, Func<int> countFunc) | |||
public CollectionWrapper(IEnumerable<TValue> query, Func<int> countFunc) | |||
{ | |||
_query = query; | |||
_countFunc = countFunc; | |||
@@ -17,56 +17,68 @@ namespace Discord.Logging | |||
ClientLogger = new Logger(this, "Discord"); | |||
} | |||
public async Task LogAsync(LogSeverity severity, string source, Exception ex) | |||
{ | |||
if (severity <= Level) | |||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, null, ex)).ConfigureAwait(false); | |||
} | |||
public async Task LogAsync(LogSeverity severity, string source, string message, Exception ex = null) | |||
{ | |||
if (severity <= Level) | |||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, message, ex)).ConfigureAwait(false); | |||
} | |||
#if NETSTANDARD1_3 | |||
public async Task LogAsync(LogSeverity severity, string source, FormattableString message, Exception ex = null) | |||
{ | |||
if (severity <= Level) | |||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, message.ToString(), ex)).ConfigureAwait(false); | |||
} | |||
public async Task LogAsync(LogSeverity severity, string source, Exception ex) | |||
{ | |||
if (severity <= Level) | |||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, null, ex)).ConfigureAwait(false); | |||
} | |||
#endif | |||
public Task ErrorAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Error, source, ex); | |||
public Task ErrorAsync(string source, string message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Error, source, message, ex); | |||
#if NETSTANDARD1_3 | |||
public Task ErrorAsync(string source, FormattableString message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Error, source, message, ex); | |||
public Task ErrorAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Error, source, ex); | |||
#endif | |||
public Task WarningAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Warning, source, ex); | |||
public Task WarningAsync(string source, string message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Warning, source, message, ex); | |||
#if NETSTANDARD1_3 | |||
public Task WarningAsync(string source, FormattableString message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Warning, source, message, ex); | |||
public Task WarningAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Warning, source, ex); | |||
#endif | |||
public Task InfoAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Info, source, ex); | |||
public Task InfoAsync(string source, string message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Info, source, message, ex); | |||
#if NETSTANDARD1_3 | |||
public Task InfoAsync(string source, FormattableString message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Info, source, message, ex); | |||
public Task InfoAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Info, source, ex); | |||
#endif | |||
public Task VerboseAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Verbose, source, ex); | |||
public Task VerboseAsync(string source, string message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | |||
#if NETSTANDARD1_3 | |||
public Task VerboseAsync(string source, FormattableString message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | |||
public Task VerboseAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Verbose, source, ex); | |||
#endif | |||
public Task DebugAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Debug, source, ex); | |||
public Task DebugAsync(string source, string message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Debug, source, message, ex); | |||
#if NETSTANDARD1_3 | |||
public Task DebugAsync(string source, FormattableString message, Exception ex = null) | |||
=> LogAsync(LogSeverity.Debug, source, message, ex); | |||
public Task DebugAsync(string source, Exception ex) | |||
=> LogAsync(LogSeverity.Debug, source, ex); | |||
#endif | |||
public Logger CreateLogger(string name) => new Logger(this, name); | |||
@@ -20,42 +20,54 @@ namespace Discord.Logging | |||
=> _manager.LogAsync(severity, Name, exception); | |||
public Task LogAsync(LogSeverity severity, string message, Exception exception = null) | |||
=> _manager.LogAsync(severity, Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task LogAsync(LogSeverity severity, FormattableString message, Exception exception = null) | |||
=> _manager.LogAsync(severity, Name, message, exception); | |||
#endif | |||
public Task ErrorAsync(Exception exception) | |||
=> _manager.ErrorAsync(Name, exception); | |||
public Task ErrorAsync(string message, Exception exception = null) | |||
=> _manager.ErrorAsync(Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task ErrorAsync(FormattableString message, Exception exception = null) | |||
=> _manager.ErrorAsync(Name, message, exception); | |||
public Task ErrorAsync(Exception exception) | |||
=> _manager.ErrorAsync(Name, exception); | |||
#endif | |||
public Task WarningAsync(Exception exception) | |||
=> _manager.WarningAsync(Name, exception); | |||
public Task WarningAsync(string message, Exception exception = null) | |||
=> _manager.WarningAsync(Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task WarningAsync(FormattableString message, Exception exception = null) | |||
=> _manager.WarningAsync(Name, message, exception); | |||
public Task WarningAsync(Exception exception) | |||
=> _manager.WarningAsync(Name, exception); | |||
#endif | |||
public Task InfoAsync(Exception exception) | |||
=> _manager.InfoAsync(Name, exception); | |||
public Task InfoAsync(string message, Exception exception = null) | |||
=> _manager.InfoAsync(Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task InfoAsync(FormattableString message, Exception exception = null) | |||
=> _manager.InfoAsync(Name, message, exception); | |||
public Task InfoAsync(Exception exception) | |||
=> _manager.InfoAsync(Name, exception); | |||
#endif | |||
public Task VerboseAsync(Exception exception) | |||
=> _manager.VerboseAsync(Name, exception); | |||
public Task VerboseAsync(string message, Exception exception = null) | |||
=> _manager.VerboseAsync(Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task VerboseAsync(FormattableString message, Exception exception = null) | |||
=> _manager.VerboseAsync(Name, message, exception); | |||
public Task VerboseAsync(Exception exception) | |||
=> _manager.VerboseAsync(Name, exception); | |||
#endif | |||
public Task DebugAsync(Exception exception) | |||
=> _manager.DebugAsync(Name, exception); | |||
public Task DebugAsync(string message, Exception exception = null) | |||
=> _manager.DebugAsync(Name, message, exception); | |||
#if NETSTANDARD1_3 | |||
public Task DebugAsync(FormattableString message, Exception exception = null) | |||
=> _manager.DebugAsync(Name, message, exception); | |||
public Task DebugAsync(Exception exception) | |||
=> _manager.DebugAsync(Name, exception); | |||
#endif | |||
} | |||
} |
@@ -209,7 +209,7 @@ namespace Discord.Net.Queue | |||
#endif | |||
} | |||
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | |||
var now = DateTimeUtils.ToUnixSeconds(DateTimeOffset.UtcNow); | |||
DateTimeOffset? resetTick = null; | |||
//Using X-RateLimit-Remaining causes a race condition | |||
@@ -17,7 +17,7 @@ namespace Discord.Net | |||
IsGlobal = headers.TryGetValue("X-RateLimit-Global", out temp) ? bool.Parse(temp) : false; | |||
Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) ? int.Parse(temp) : (int?)null; | |||
Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) ? int.Parse(temp) : (int?)null; | |||
Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) ? DateTimeOffset.FromUnixTimeSeconds(int.Parse(temp)) : (DateTimeOffset?)null; | |||
Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) ? DateTimeUtils.FromUnixSeconds(int.Parse(temp)) : (DateTimeOffset?)null; | |||
RetryAfter = headers.TryGetValue("Retry-After", out temp) ? int.Parse(temp) : (int?)null; | |||
} | |||
} | |||
@@ -0,0 +1,19 @@ | |||
using System; | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
namespace Discord.Net.Udp | |||
{ | |||
public interface IUdpSocket | |||
{ | |||
event Func<byte[], int, int, Task> ReceivedDatagram; | |||
void SetCancelToken(CancellationToken cancelToken); | |||
void SetDestination(string host, int port); | |||
Task StartAsync(); | |||
Task StopAsync(); | |||
Task SendAsync(byte[] data, int index, int count); | |||
} | |||
} |
@@ -0,0 +1,4 @@ | |||
namespace Discord.Net.Udp | |||
{ | |||
public delegate IUdpSocket UdpSocketProvider(); | |||
} |
@@ -2,14 +2,58 @@ | |||
namespace Discord | |||
{ | |||
internal static class DateTimeUtils | |||
{ | |||
public static DateTimeOffset FromSnowflake(ulong value) | |||
=> DateTimeOffset.FromUnixTimeMilliseconds((long)((value >> 22) + 1420070400000UL)); | |||
internal static class DateTimeUtils | |||
{ | |||
#if !NETSTANDARD1_3 | |||
//https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/DateTimeOffset.cs | |||
private const long UnixEpochTicks = 621355968000000000; | |||
private const long UnixEpochSeconds = 62135596800; | |||
#endif | |||
public static DateTimeOffset FromTicks(long ticks) | |||
=> new DateTimeOffset(ticks, TimeSpan.Zero); | |||
public static DateTimeOffset? FromTicks(long? ticks) | |||
=> ticks != null ? new DateTimeOffset(ticks.Value, TimeSpan.Zero) : (DateTimeOffset?)null; | |||
} | |||
public static DateTimeOffset FromSnowflake(ulong value) | |||
=> FromUnixMilliseconds((long)((value >> 22) + 1420070400000UL)); | |||
public static DateTimeOffset FromTicks(long ticks) | |||
=> new DateTimeOffset(ticks, TimeSpan.Zero); | |||
public static DateTimeOffset? FromTicks(long? ticks) | |||
=> ticks != null ? new DateTimeOffset(ticks.Value, TimeSpan.Zero) : (DateTimeOffset?)null; | |||
public static DateTimeOffset FromUnixSeconds(long seconds) | |||
{ | |||
#if NETSTANDARD1_3 | |||
return DateTimeOffset.FromUnixTimeSeconds(seconds); | |||
#else | |||
long ticks = seconds * TimeSpan.TicksPerSecond + UnixEpochTicks; | |||
return new DateTimeOffset(ticks, TimeSpan.Zero); | |||
#endif | |||
} | |||
public static DateTimeOffset FromUnixMilliseconds(long seconds) | |||
{ | |||
#if NETSTANDARD1_3 | |||
return DateTimeOffset.FromUnixTimeMilliseconds(seconds); | |||
#else | |||
long ticks = seconds * TimeSpan.TicksPerMillisecond + UnixEpochTicks; | |||
return new DateTimeOffset(ticks, TimeSpan.Zero); | |||
#endif | |||
} | |||
public static long ToUnixSeconds(DateTimeOffset dto) | |||
{ | |||
#if NETSTANDARD1_3 | |||
return dto.ToUnixTimeSeconds(); | |||
#else | |||
long seconds = dto.UtcDateTime.Ticks / TimeSpan.TicksPerSecond; | |||
return seconds - UnixEpochSeconds; | |||
#endif | |||
} | |||
public static long ToUnixMilliseconds(DateTimeOffset dto) | |||
{ | |||
#if NETSTANDARD1_3 | |||
return dto.ToUnixTimeMilliseconds(); | |||
#else | |||
long seconds = dto.UtcDateTime.Ticks / TimeSpan.TicksPerMillisecond; | |||
return seconds - UnixEpochSeconds; | |||
#endif | |||
} | |||
} | |||
} |
@@ -26,25 +26,16 @@ | |||
}, | |||
"dependencies": { | |||
"Microsoft.Win32.Primitives": "4.3.0", | |||
"Newtonsoft.Json": "9.0.1", | |||
"System.Collections.Concurrent": "4.3.0", | |||
"System.Collections.Immutable": "1.3.0", | |||
"System.Interactive.Async": "3.1.0", | |||
"System.Net.Http": "4.3.0", | |||
"System.Net.WebSockets.Client": { | |||
"version": "4.3.0", | |||
"type": "build" | |||
} | |||
"System.Runtime.Serialization.Primitives": "4.1.1" | |||
}, | |||
"frameworks": { | |||
"netstandard1.3": { | |||
"imports": [ | |||
"dotnet5.4", | |||
"dnxcore50", | |||
"portable-net45+win8" | |||
] | |||
} | |||
"netstandard1.1": {}, | |||
"netstandard1.3": {} | |||
} | |||
} |
@@ -86,7 +86,7 @@ namespace Discord.Rest | |||
await _loggedInEvent.InvokeAsync().ConfigureAwait(false); | |||
} | |||
protected virtual Task OnLoginAsync(TokenType tokenType, string token) { return Task.CompletedTask; } | |||
protected virtual Task OnLoginAsync(TokenType tokenType, string token) { return Task.Delay(0); } | |||
/// <inheritdoc /> | |||
public async Task LogoutAsync() | |||
@@ -111,7 +111,7 @@ namespace Discord.Rest | |||
await _loggedOutEvent.InvokeAsync().ConfigureAwait(false); | |||
} | |||
protected virtual Task OnLogoutAsync() { return Task.CompletedTask; } | |||
protected virtual Task OnLogoutAsync() { return Task.Delay(0); } | |||
internal virtual void Dispose(bool disposing) | |||
{ | |||
@@ -1,44 +1,28 @@ | |||
<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 REST client and models.</Description> | |||
<VersionPrefix>1.0.0-beta2</VersionPrefix> | |||
<TargetFramework>netstandard1.3</TargetFramework> | |||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks> | |||
<AssemblyName>Discord.Net.Rest</AssemblyName> | |||
<PackageTags>discord;discordapp</PackageTags> | |||
<PackageProjectUrl>https://github.com/RogueException/Discord.Net</PackageProjectUrl> | |||
<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" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.NET.Sdk"> | |||
<Version>1.0.0-alpha-20161104-2</Version> | |||
<PrivateAssets>All</PrivateAssets> | |||
</PackageReference> | |||
<PackageReference Include="System.IO.FileSystem"> | |||
<Version>4.3.0</Version> | |||
</PackageReference> | |||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' "> | |||
<PackageReference Include="System.IO.FileSystem" 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> |
@@ -21,12 +21,12 @@ namespace Discord.Rest | |||
protected override Task OnLoginAsync(TokenType tokenType, string token) | |||
{ | |||
base.CurrentUser = RestSelfUser.Create(this, ApiClient.CurrentUser); | |||
return Task.CompletedTask; | |||
return Task.Delay(0); | |||
} | |||
protected override Task OnLogoutAsync() | |||
{ | |||
_applicationInfo = null; | |||
return Task.CompletedTask; | |||
return Task.Delay(0); | |||
} | |||
/// <inheritdoc /> | |||
@@ -139,6 +139,7 @@ namespace Discord.Rest | |||
return RestUserMessage.Create(client, channel, client.CurrentUser, model); | |||
} | |||
#if NETSTANDARD1_3 | |||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | |||
string filePath, string text, bool isTTS, RequestOptions options) | |||
{ | |||
@@ -146,6 +147,7 @@ namespace Discord.Rest | |||
using (var file = File.OpenRead(filePath)) | |||
return await SendFileAsync(channel, client, file, filename, text, isTTS, options).ConfigureAwait(false); | |||
} | |||
#endif | |||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | |||
Stream stream, string filename, string text, bool isTTS, RequestOptions options) | |||
{ | |||
@@ -8,8 +8,10 @@ namespace Discord.Rest | |||
{ | |||
/// <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); | |||
@@ -65,8 +65,10 @@ namespace Discord.Rest | |||
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); | |||
@@ -122,8 +124,10 @@ namespace Discord.Rest | |||
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) | |||
@@ -78,8 +78,10 @@ namespace Discord.Rest | |||
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); | |||
@@ -132,8 +134,10 @@ namespace Discord.Rest | |||
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) | |||
@@ -57,8 +57,10 @@ namespace Discord.Rest | |||
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); | |||
@@ -104,8 +106,10 @@ namespace Discord.Rest | |||
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) | |||
@@ -35,8 +35,10 @@ namespace Discord.Rest | |||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, 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, RequestOptions options = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options); | |||
#endif | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options); | |||
@@ -82,8 +84,10 @@ namespace Discord.Rest | |||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) | |||
=> await GetPinnedMessagesAsync(options); | |||
#if NETSTANDARD1_3 | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options) | |||
=> await SendFileAsync(filePath, text, isTTS, options); | |||
#endif | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options) | |||
=> await SendFileAsync(stream, filename, text, isTTS, options); | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options) | |||
@@ -69,7 +69,7 @@ namespace Discord.Rest | |||
public static ImmutableArray<ITag> ParseTags(string text, IMessageChannel channel, IGuild guild, ImmutableArray<IUser> userMentions) | |||
{ | |||
var tags = new SortedList<int, ITag>(); | |||
var tags = ImmutableArray.CreateBuilder<ITag>(); | |||
int index = 0; | |||
while (true) | |||
@@ -94,27 +94,27 @@ namespace Discord.Rest | |||
break; | |||
} | |||
} | |||
tags.Add(index, new Tag<IUser>(TagType.UserMention, index, content.Length, id, mentionedUser)); | |||
tags.Add(new Tag<IUser>(TagType.UserMention, index, content.Length, id, mentionedUser)); | |||
} | |||
else if (MentionUtils.TryParseChannel(content, out id)) | |||
{ | |||
IChannel mentionedChannel = null; | |||
if (guild != null) | |||
mentionedChannel = guild.GetChannelAsync(id, CacheMode.CacheOnly).GetAwaiter().GetResult(); | |||
tags.Add(index, new Tag<IChannel>(TagType.ChannelMention, index, content.Length, id, mentionedChannel)); | |||
tags.Add(new Tag<IChannel>(TagType.ChannelMention, index, content.Length, id, mentionedChannel)); | |||
} | |||
else if (MentionUtils.TryParseRole(content, out id)) | |||
{ | |||
IRole mentionedRole = null; | |||
if (guild != null) | |||
mentionedRole = guild.GetRole(id); | |||
tags.Add(index, new Tag<IRole>(TagType.RoleMention, index, content.Length, id, mentionedRole)); | |||
tags.Add(new Tag<IRole>(TagType.RoleMention, index, content.Length, id, mentionedRole)); | |||
} | |||
else | |||
{ | |||
Emoji emoji; | |||
if (Emoji.TryParse(content, out emoji)) | |||
tags.Add(index, new Tag<Emoji>(TagType.Emoji, index, content.Length, id, emoji)); | |||
tags.Add(new Tag<Emoji>(TagType.Emoji, index, content.Length, id, emoji)); | |||
} | |||
index = endIndex + 1; | |||
} | |||
@@ -125,7 +125,7 @@ namespace Discord.Rest | |||
index = text.IndexOf("@everyone", index); | |||
if (index == -1) break; | |||
tags.Add(index, new Tag<object>(TagType.EveryoneMention, index, "@everyone".Length, 0, null)); | |||
tags.Add(new Tag<object>(TagType.EveryoneMention, index, "@everyone".Length, 0, null)); | |||
index++; | |||
} | |||
@@ -135,11 +135,11 @@ namespace Discord.Rest | |||
index = text.IndexOf("@here", index); | |||
if (index == -1) break; | |||
tags.Add(index, new Tag<object>(TagType.HereMention, index, "@here".Length, 0, null)); | |||
tags.Add(new Tag<object>(TagType.HereMention, index, "@here".Length, 0, null)); | |||
index++; | |||
} | |||
return tags.Values.ToImmutableArray(); | |||
return tags.ToImmutable(); | |||
} | |||
public static ImmutableArray<ulong> FilterTagsByKey(TagType type, ImmutableArray<ITag> tags) | |||
{ | |||
@@ -12,7 +12,7 @@ using System.Threading.Tasks; | |||
namespace Discord.Net.Rest | |||
{ | |||
public sealed class DefaultRestClient : IRestClient | |||
internal sealed class DefaultRestClient : IRestClient | |||
{ | |||
private const int HR_SECURECHANNELFAILED = -2146233079; | |||
@@ -29,16 +29,14 @@ | |||
"Discord.Net.Core": { | |||
"target": "project" | |||
}, | |||
"System.IO.FileSystem": "4.3.0" | |||
}, | |||
"frameworks": { | |||
"netstandard1.1": {}, | |||
"netstandard1.3": { | |||
"imports": [ | |||
"dotnet5.4", | |||
"dnxcore50", | |||
"portable-net45+win8" | |||
] | |||
"dependencies": { | |||
"System.IO.FileSystem": "4.3.0" | |||
} | |||
} | |||
} | |||
} |
@@ -218,9 +218,6 @@ namespace Discord.API | |||
private async Task<TResponse> SendRpcAsyncInternal<TResponse>(string cmd, object payload, Optional<string> evt, RequestOptions options) | |||
where TResponse : class | |||
{ | |||
if (!options.IgnoreState) | |||
CheckState(); | |||
byte[] bytes = null; | |||
var guid = Guid.NewGuid(); | |||
payload = new API.Rpc.RpcFrame { Cmd = cmd, Event = evt, Args = payload, Nonce = guid }; | |||
@@ -1,48 +1,40 @@ | |||
<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 RPC client and models.</Description> | |||
<VersionPrefix>1.0.0-beta2</VersionPrefix> | |||
<TargetFramework>netstandard1.3</TargetFramework> | |||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks> | |||
<AssemblyName>Discord.Net.Rpc</AssemblyName> | |||
<PackageTags>discord;discordapp</PackageTags> | |||
<PackageProjectUrl>https://github.com/RogueException/Discord.Net</PackageProjectUrl> | |||
<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> | |||
<Compile Include="..\Discord.Net.WebSocket\Net\DefaultWebSocketClient.cs"> | |||
<Link>Net\DefaultWebSocketClient.cs</Link> | |||
</Compile> | |||
</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.WebSockets.Client"> | |||
<Version>4.3.0</Version> | |||
</PackageReference> | |||
<PackageReference Include="System.IO.Compression" Version="4.3.0" /> | |||
</ItemGroup> | |||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' "> | |||
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Folder Include="Net\" /> | |||
</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> |
@@ -1,5 +1,6 @@ | |||
using Discord.Net.WebSockets; | |||
using Discord.Rest; | |||
using System; | |||
namespace Discord.Rpc | |||
{ | |||
@@ -14,6 +15,19 @@ namespace Discord.Rpc | |||
public int ConnectionTimeout { get; set; } = 30000; | |||
/// <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; } | |||
public DiscordRpcConfig() | |||
{ | |||
#if NETSTANDARD1_3 | |||
WebSocketProvider = () => new DefaultWebSocketClient(); | |||
#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+."); | |||
}; | |||
#endif | |||
} | |||
} | |||
} |
@@ -46,8 +46,10 @@ namespace Discord.Rpc | |||
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); | |||
@@ -100,8 +102,10 @@ namespace Discord.Rpc | |||
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) | |||
@@ -48,8 +48,10 @@ namespace Discord.Rpc | |||
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); | |||
@@ -99,8 +101,10 @@ namespace Discord.Rpc | |||
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) | |||
@@ -51,8 +51,10 @@ namespace Discord.Rpc | |||
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); | |||
@@ -101,8 +103,10 @@ namespace Discord.Rpc | |||
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) | |||
@@ -32,17 +32,15 @@ | |||
"Discord.Net.Rest": { | |||
"target": "project" | |||
}, | |||
"System.IO.Compression": "4.3.0", | |||
"System.Net.WebSockets.Client": "4.3.0" | |||
"System.IO.Compression": "4.3.0" | |||
}, | |||
"frameworks": { | |||
"netstandard1.1": {}, | |||
"netstandard1.3": { | |||
"imports": [ | |||
"dotnet5.4", | |||
"dnxcore50", | |||
"portable-net45+win8" | |||
] | |||
"dependencies": { | |||
"System.Net.WebSockets.Client": "4.3.0" | |||
} | |||
} | |||
} | |||
} |
@@ -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) | |||
@@ -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 |
@@ -1,4 +1,5 @@ | |||
using System; | |||
#if NETSTANDARD1_3 | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.IO; | |||
@@ -9,7 +10,7 @@ using System.Threading.Tasks; | |||
namespace Discord.Net.WebSockets | |||
{ | |||
public class DefaultWebSocketClient : IWebSocketClient | |||
internal class DefaultWebSocketClient : IWebSocketClient | |||
{ | |||
public const int ReceiveChunkSize = 16 * 1024; //16KB | |||
public const int SendChunkSize = 4 * 1024; //4KB | |||
@@ -19,7 +20,7 @@ namespace Discord.Net.WebSockets | |||
public event Func<string, Task> TextMessage; | |||
public event Func<Exception, Task> Closed; | |||
private readonly SemaphoreSlim _sendLock; | |||
private readonly SemaphoreSlim _lock; | |||
private readonly Dictionary<string, string> _headers; | |||
private ClientWebSocket _client; | |||
private Task _task; | |||
@@ -29,7 +30,7 @@ namespace Discord.Net.WebSockets | |||
public DefaultWebSocketClient() | |||
{ | |||
_sendLock = new SemaphoreSlim(1, 1); | |||
_lock = new SemaphoreSlim(1, 1); | |||
_cancelTokenSource = new CancellationTokenSource(); | |||
_cancelToken = CancellationToken.None; | |||
_parentToken = CancellationToken.None; | |||
@@ -40,7 +41,7 @@ namespace Discord.Net.WebSockets | |||
if (!_isDisposed) | |||
{ | |||
if (disposing) | |||
_client.Dispose(); | |||
DisconnectInternalAsync(true).GetAwaiter().GetResult(); | |||
_isDisposed = true; | |||
} | |||
} | |||
@@ -51,14 +52,14 @@ namespace Discord.Net.WebSockets | |||
public async Task ConnectAsync(string host) | |||
{ | |||
await _sendLock.WaitAsync().ConfigureAwait(false); | |||
await _lock.WaitAsync().ConfigureAwait(false); | |||
try | |||
{ | |||
await ConnectInternalAsync(host).ConfigureAwait(false); | |||
} | |||
finally | |||
{ | |||
_sendLock.Release(); | |||
_lock.Release(); | |||
} | |||
} | |||
private async Task ConnectInternalAsync(string host) | |||
@@ -83,27 +84,33 @@ namespace Discord.Net.WebSockets | |||
public async Task DisconnectAsync() | |||
{ | |||
await _sendLock.WaitAsync().ConfigureAwait(false); | |||
await _lock.WaitAsync().ConfigureAwait(false); | |||
try | |||
{ | |||
await DisconnectInternalAsync().ConfigureAwait(false); | |||
} | |||
finally | |||
{ | |||
_sendLock.Release(); | |||
_lock.Release(); | |||
} | |||
} | |||
private async Task DisconnectInternalAsync() | |||
private async Task DisconnectInternalAsync(bool isDisposing = false) | |||
{ | |||
try { _cancelTokenSource.Cancel(false); } catch { } | |||
await (_task ?? Task.CompletedTask).ConfigureAwait(false); | |||
if (!isDisposing) | |||
await (_task ?? Task.Delay(0)).ConfigureAwait(false); | |||
if (_client != null && _client.State == WebSocketState.Open) | |||
{ | |||
var token = new CancellationToken(); | |||
await _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", token); | |||
_client.Dispose(); | |||
if (!isDisposing) | |||
{ | |||
try { await _client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", token); } | |||
catch { } | |||
} | |||
try { _client.Dispose(); } | |||
catch { } | |||
_client = null; | |||
} | |||
} | |||
@@ -120,7 +127,7 @@ namespace Discord.Net.WebSockets | |||
public async Task SendAsync(byte[] data, int index, int count, bool isText) | |||
{ | |||
await _sendLock.WaitAsync().ConfigureAwait(false); | |||
await _lock.WaitAsync().ConfigureAwait(false); | |||
try | |||
{ | |||
if (_client == null) return; | |||
@@ -143,7 +150,7 @@ namespace Discord.Net.WebSockets | |||
} | |||
finally | |||
{ | |||
_sendLock.Release(); | |||
_lock.Release(); | |||
} | |||
} | |||
@@ -181,11 +188,15 @@ namespace Discord.Net.WebSockets | |||
//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 | |||
@@ -217,3 +228,4 @@ namespace Discord.Net.WebSockets | |||
} | |||
} | |||
} | |||
#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" | |||
} | |||
} | |||
} | |||
} |
@@ -1,23 +1,19 @@ | |||
<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>An aynchronous API wrapper for Discord. This metapackage includes all of the optional Discord.Net components.</Description> | |||
<VersionPrefix>1.0.0-beta2</VersionPrefix> | |||
<TargetFramework>netstandard1.3</TargetFramework> | |||
<TargetFrameworks>netstandard1.1;netstandard1.3</TargetFrameworks> | |||
<AssemblyName>Discord.Net</AssemblyName> | |||
<PackageTags>discord;discordapp</PackageTags> | |||
<PackageProjectUrl>https://github.com/RogueException/Discord.Net</PackageProjectUrl> | |||
<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" /> | |||
@@ -25,18 +21,4 @@ | |||
<ProjectReference Include="..\Discord.Net.Rpc\Discord.Net.Rpc.csproj" /> | |||
<ProjectReference Include="..\Discord.Net.Commands\Discord.Net.Commands.csproj" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.NET.Sdk"> | |||
<Version>1.0.0-alpha-20161104-2</Version> | |||
<PrivateAssets>All</PrivateAssets> | |||
</PackageReference> | |||
</ItemGroup> | |||
<ItemGroup /> | |||
<PropertyGroup Label="Configuration"> | |||
<SignAssembly>False</SignAssembly> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | |||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> | |||
</PropertyGroup> | |||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | |||
</Project> |
@@ -32,12 +32,7 @@ | |||
}, | |||
"frameworks": { | |||
"netstandard1.3": { | |||
"imports": [ | |||
"dotnet5.4", | |||
"dnxcore50", | |||
"portable-net45+win8" | |||
] | |||
} | |||
"netstandard1.1": {}, | |||
"netstandard1.3": {} | |||
} | |||
} |