Reverted to old style Rest/WebSocket engines

This commit is contained in:
RogueException
2015-10-18 19:31:49 -03:00
parent ee44caf6ab
commit 41b337b273
9 changed files with 67 additions and 39 deletions

View File

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

View File

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

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);
}
}

View File

@@ -3,7 +3,7 @@ using System.Net.Http;
namespace Discord.Net.Rest namespace Discord.Net.Rest
{ {
internal abstract partial class RestClient internal sealed partial class RestClient
{ {
public class RequestEventArgs : EventArgs public class RequestEventArgs : EventArgs
{ {
@@ -21,7 +21,7 @@ namespace Discord.Net.Rest
} }
public event EventHandler<RequestEventArgs> OnRequest; 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) if (OnRequest != null)
OnRequest(this, new RequestEventArgs(method, path, payload, milliseconds)); OnRequest(this, new RequestEventArgs(method, path, payload, milliseconds));

View File

@@ -8,19 +8,19 @@ using System.Threading.Tasks;
namespace Discord.Net.Rest namespace Discord.Net.Rest
{ {
internal abstract partial class RestClient internal sealed partial class RestClient
{ {
protected readonly DiscordAPIClientConfig _config; private readonly DiscordAPIClientConfig _config;
protected CancellationToken _cancelToken; private readonly IRestEngine _engine;
private CancellationToken _cancelToken;
public RestClient(DiscordAPIClientConfig config) public RestClient(DiscordAPIClientConfig config)
{ {
_config = config; _config = config;
_engine = new SharpRestEngine(config);
} }
protected internal abstract void SetToken(string token); public void SetToken(string token) => _engine.SetToken(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);
//DELETE //DELETE
private static readonly HttpMethod _delete = HttpMethod.Delete; private static readonly HttpMethod _delete = HttpMethod.Delete;
@@ -95,7 +95,7 @@ namespace Discord.Net.Rest
if (_config.LogLevel >= LogMessageSeverity.Verbose) if (_config.LogLevel >= LogMessageSeverity.Verbose)
stopwatch = Stopwatch.StartNew(); 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 TEST_RESPONSES
if (!hasResponse && !string.IsNullOrEmpty(responseJson)) if (!hasResponse && !string.IsNullOrEmpty(responseJson))
@@ -134,7 +134,7 @@ namespace Discord.Net.Rest
if (_config.LogLevel >= LogMessageSeverity.Verbose) if (_config.LogLevel >= LogMessageSeverity.Verbose)
stopwatch = Stopwatch.StartNew(); 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 TEST_RESPONSES
if (!hasResponse && !string.IsNullOrEmpty(responseJson)) if (!hasResponse && !string.IsNullOrEmpty(responseJson))

View File

@@ -8,13 +8,14 @@ using System.Threading.Tasks;
namespace Discord.Net.Rest namespace Discord.Net.Rest
{ {
internal sealed class SharpRestRestClient : RestClient internal sealed class SharpRestEngine : IRestEngine
{ {
private readonly DiscordAPIClientConfig _config;
private readonly RestSharp.RestClient _client; private readonly RestSharp.RestClient _client;
public SharpRestRestClient(DiscordAPIClientConfig config) public SharpRestEngine(DiscordAPIClientConfig config)
: base(config)
{ {
_config = config;
_client = new RestSharp.RestClient(Endpoints.BaseApi) _client = new RestSharp.RestClient(Endpoints.BaseApi)
{ {
PreAuthenticate = false, PreAuthenticate = false,
@@ -27,20 +28,20 @@ namespace Discord.Net.Rest
_client.AddDefaultHeader("accept-encoding", "gzip,deflate"); _client.AddDefaultHeader("accept-encoding", "gzip,deflate");
} }
protected internal override void SetToken(string token) public void SetToken(string token)
{ {
_client.RemoveDefaultParameter("authorization"); _client.RemoveDefaultParameter("authorization");
if (token != null) if (token != null)
_client.AddDefaultHeader("authorization", token); _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)); var request = new RestRequest(path, GetMethod(method));
request.AddParameter("application/json", json, ParameterType.RequestBody); request.AddParameter("application/json", json, ParameterType.RequestBody);
return Send(request, cancelToken); 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); var request = new RestRequest(path, Method.POST);
request.AddFile("file", filePath); request.AddFile("file", filePath);

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);
}
}

View File

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

View File

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