Browse Source

Reverted to old style Rest/WebSocket engines

tags/docs-0.9
RogueException 9 years ago
parent
commit
41b337b273
9 changed files with 67 additions and 39 deletions
  1. +10
    -4
      src/Discord.Net.Net45/Discord.Net.csproj
  2. +1
    -1
      src/Discord.Net/DiscordAPIClient.cs
  3. +13
    -0
      src/Discord.Net/Net/Rest/IRestEngine.cs
  4. +2
    -2
      src/Discord.Net/Net/Rest/RestClient.Events.cs
  5. +8
    -8
      src/Discord.Net/Net/Rest/RestClient.cs
  6. +7
    -6
      src/Discord.Net/Net/Rest/SharpRestEngine.cs
  7. +23
    -0
      src/Discord.Net/Net/WebSockets/IWebSocketEngine.cs
  8. +1
    -16
      src/Discord.Net/Net/WebSockets/WebSocket.cs
  9. +2
    -2
      src/Discord.Net/Net/WebSockets/WebSocketSharpEngine.cs

+ 10
- 4
src/Discord.Net.Net45/Discord.Net.csproj View File

@@ -247,14 +247,17 @@
<Compile Include="..\Discord.Net\Net\HttpException.cs">
<Link>Net\HttpException.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\Rest\IRestEngine.cs">
<Link>Net\Rest\IRestEngine.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\Rest\RestClient.cs">
<Link>Net\Rest\RestClient.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\Rest\RestClient.Events.cs">
<Link>Net\Rest\RestClient.Events.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\Rest\SharpRestRestClient.cs">
<Link>Net\Rest\SharpRestRestClient.cs</Link>
<Compile Include="..\Discord.Net\Net\Rest\SharpRestEngine.cs">
<Link>Net\Rest\SharpRestEngine.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\Voice\IDiscordVoiceBuffer.cs">
<Link>Net\Voice\IDiscordVoiceBuffer.cs</Link>
@@ -271,6 +274,9 @@
<Compile Include="..\Discord.Net\Net\WebSockets\DataWebSockets.Events.cs">
<Link>Net\WebSockets\DataWebSockets.Events.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\IWebSocketEngine.cs">
<Link>Net\WebSockets\IWebSocketEngine.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\VoiceWebSocket.cs">
<Link>Net\WebSockets\VoiceWebSocket.cs</Link>
</Compile>
@@ -283,8 +289,8 @@
<Compile Include="..\Discord.Net\Net\WebSockets\WebSocket.Events.cs">
<Link>Net\WebSockets\WebSocket.Events.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Net\WebSockets\WebSocket.WebSocketSharp.cs">
<Link>Net\WebSockets\WebSocket.WebSocketSharp.cs</Link>
<Compile Include="..\Discord.Net\Net\WebSockets\WebSocketSharpEngine.cs">
<Link>Net\WebSockets\WebSocketSharpEngine.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>


+ 1
- 1
src/Discord.Net/DiscordAPIClient.cs View File

@@ -20,7 +20,7 @@ namespace Discord
public DiscordAPIClient(DiscordAPIClientConfig config = null)
{
_config = config ?? new DiscordAPIClientConfig();
_rest = new SharpRestRestClient(_config);
_rest = new RestClient(_config);
}

private string _token;


+ 13
- 0
src/Discord.Net/Net/Rest/IRestEngine.cs View File

@@ -0,0 +1,13 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Discord.Net.Rest
{
internal interface IRestEngine
{
void SetToken(string token);
Task<string> Send(HttpMethod method, string path, string json, CancellationToken cancelToken);
Task<string> SendFile(HttpMethod method, string path, string filePath, CancellationToken cancelToken);
}
}

+ 2
- 2
src/Discord.Net/Net/Rest/RestClient.Events.cs View File

@@ -3,7 +3,7 @@ using System.Net.Http;

namespace Discord.Net.Rest
{
internal abstract partial class RestClient
internal sealed partial class RestClient
{
public class RequestEventArgs : EventArgs
{
@@ -21,7 +21,7 @@ namespace Discord.Net.Rest
}

public event EventHandler<RequestEventArgs> OnRequest;
protected void RaiseOnRequest(HttpMethod method, string path, string payload, double milliseconds)
private void RaiseOnRequest(HttpMethod method, string path, string payload, double milliseconds)
{
if (OnRequest != null)
OnRequest(this, new RequestEventArgs(method, path, payload, milliseconds));


+ 8
- 8
src/Discord.Net/Net/Rest/RestClient.cs View File

@@ -8,19 +8,19 @@ using System.Threading.Tasks;

namespace Discord.Net.Rest
{
internal abstract partial class RestClient
internal sealed partial class RestClient
{
protected readonly DiscordAPIClientConfig _config;
protected CancellationToken _cancelToken;
private readonly DiscordAPIClientConfig _config;
private readonly IRestEngine _engine;
private CancellationToken _cancelToken;

public RestClient(DiscordAPIClientConfig config)
{
_config = config;
_engine = new SharpRestEngine(config);
}

protected internal abstract void SetToken(string token);
protected abstract Task<string> SendInternal(HttpMethod method, string path, string json, CancellationToken cancelToken);
protected abstract Task<string> SendFileInternal(HttpMethod method, string path, string filePath, CancellationToken cancelToken);
public void SetToken(string token) => _engine.SetToken(token);

//DELETE
private static readonly HttpMethod _delete = HttpMethod.Delete;
@@ -95,7 +95,7 @@ namespace Discord.Net.Rest
if (_config.LogLevel >= LogMessageSeverity.Verbose)
stopwatch = Stopwatch.StartNew();
string responseJson = await SendInternal(method, path, requestJson, _cancelToken).ConfigureAwait(false);
string responseJson = await _engine.Send(method, path, requestJson, _cancelToken).ConfigureAwait(false);

#if TEST_RESPONSES
if (!hasResponse && !string.IsNullOrEmpty(responseJson))
@@ -134,7 +134,7 @@ namespace Discord.Net.Rest
if (_config.LogLevel >= LogMessageSeverity.Verbose)
stopwatch = Stopwatch.StartNew();
string responseJson = await SendFileInternal(method, path, filePath, _cancelToken).ConfigureAwait(false);
string responseJson = await _engine.SendFile(method, path, filePath, _cancelToken).ConfigureAwait(false);

#if TEST_RESPONSES
if (!hasResponse && !string.IsNullOrEmpty(responseJson))


src/Discord.Net/Net/Rest/SharpRestRestClient.cs → src/Discord.Net/Net/Rest/SharpRestEngine.cs View File

@@ -8,13 +8,14 @@ using System.Threading.Tasks;

namespace Discord.Net.Rest
{
internal sealed class SharpRestRestClient : RestClient
internal sealed class SharpRestEngine : IRestEngine
{
private readonly DiscordAPIClientConfig _config;
private readonly RestSharp.RestClient _client;

public SharpRestRestClient(DiscordAPIClientConfig config)
: base(config)
public SharpRestEngine(DiscordAPIClientConfig config)
{
_config = config;
_client = new RestSharp.RestClient(Endpoints.BaseApi)
{
PreAuthenticate = false,
@@ -27,20 +28,20 @@ namespace Discord.Net.Rest
_client.AddDefaultHeader("accept-encoding", "gzip,deflate");
}

protected internal override void SetToken(string token)
public void SetToken(string token)
{
_client.RemoveDefaultParameter("authorization");
if (token != null)
_client.AddDefaultHeader("authorization", token);
}

protected override Task<string> SendInternal(HttpMethod method, string path, string json, CancellationToken cancelToken)
public Task<string> Send(HttpMethod method, string path, string json, CancellationToken cancelToken)
{
var request = new RestRequest(path, GetMethod(method));
request.AddParameter("application/json", json, ParameterType.RequestBody);
return Send(request, cancelToken);
}
protected override Task<string> SendFileInternal(HttpMethod method, string path, string filePath, CancellationToken cancelToken)
public Task<string> SendFile(HttpMethod method, string path, string filePath, CancellationToken cancelToken)
{
var request = new RestRequest(path, Method.POST);
request.AddFile("file", filePath);

+ 23
- 0
src/Discord.Net/Net/WebSockets/IWebSocketEngine.cs View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Discord.Net.WebSockets
{
internal class WebSocketMessageEventArgs : EventArgs
{
public readonly string Message;
public WebSocketMessageEventArgs(string msg) { Message = msg; }
}

internal interface IWebSocketEngine
{
event EventHandler<WebSocketMessageEventArgs> ProcessMessage;

Task Connect(string host, CancellationToken cancelToken);
Task Disconnect();
void QueueMessage(string message);
IEnumerable<Task> GetTasks(CancellationToken cancelToken);
}
}

+ 1
- 16
src/Discord.Net/Net/WebSockets/WebSocket.cs View File

@@ -16,21 +16,6 @@ namespace Discord.Net.WebSockets
Disconnecting
}

internal class WebSocketMessageEventArgs : EventArgs
{
public readonly string Message;
public WebSocketMessageEventArgs(string msg) { Message = msg; }
}
internal interface IWebSocketEngine
{
event EventHandler<WebSocketMessageEventArgs> ProcessMessage;

Task Connect(string host, CancellationToken cancelToken);
Task Disconnect();
void QueueMessage(string message);
IEnumerable<Task> GetTasks(CancellationToken cancelToken);
}

internal abstract partial class WebSocket
{
protected readonly IWebSocketEngine _engine;
@@ -65,7 +50,7 @@ namespace Discord.Net.WebSockets
_cancelToken = new CancellationToken(true);
_connectedEvent = new ManualResetEventSlim(false);
_engine = new WSSharpWebSocketEngine(this, client.Config);
_engine = new WebSocketSharpEngine(this, client.Config);
_engine.ProcessMessage += async (s, e) =>
{
if (_logLevel >= LogMessageSeverity.Debug)


src/Discord.Net/Net/WebSockets/WebSocket.WebSocketSharp.cs → src/Discord.Net/Net/WebSockets/WebSocketSharpEngine.cs View File

@@ -8,7 +8,7 @@ using WSSharpNWebSocket = WebSocketSharp.WebSocket;

namespace Discord.Net.WebSockets
{
internal class WSSharpWebSocketEngine : IWebSocketEngine
internal class WebSocketSharpEngine : IWebSocketEngine
{
private readonly DiscordWebSocketClientConfig _config;
private readonly ConcurrentQueue<string> _sendQueue;
@@ -22,7 +22,7 @@ namespace Discord.Net.WebSockets
ProcessMessage(this, new WebSocketMessageEventArgs(msg));
}

internal WSSharpWebSocketEngine(WebSocket parent, DiscordWebSocketClientConfig config)
internal WebSocketSharpEngine(WebSocket parent, DiscordWebSocketClientConfig config)
{
_parent = parent;
_config = config;

Loading…
Cancel
Save