Skip to content
Snippets Groups Projects
Commit 75321c3e authored by Frederik Carlier's avatar Frederik Carlier
Browse files

SendAsync: Add the ability to cancel the async task

parent d36f44b3
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ namespace MasterDevs.ChromeDevTools ...@@ -17,7 +17,7 @@ namespace MasterDevs.ChromeDevTools
private IEventFactory _eventFactory; private IEventFactory _eventFactory;
private ManualResetEvent _openEvent = new ManualResetEvent(false); private ManualResetEvent _openEvent = new ManualResetEvent(false);
private ManualResetEvent _publishEvent = new ManualResetEvent(false); private ManualResetEvent _publishEvent = new ManualResetEvent(false);
private ConcurrentDictionary<long, ManualResetEvent> _requestWaitHandles = new ConcurrentDictionary<long, ManualResetEvent>(); private ConcurrentDictionary<long, ManualResetEventSlim> _requestWaitHandles = new ConcurrentDictionary<long, ManualResetEventSlim>();
private ICommandResponseFactory _responseFactory; private ICommandResponseFactory _responseFactory;
private ConcurrentDictionary<long, ICommandResponse> _responses = new ConcurrentDictionary<long, ICommandResponse>(); private ConcurrentDictionary<long, ICommandResponse> _responses = new ConcurrentDictionary<long, ICommandResponse>();
private WebSocket _webSocket; private WebSocket _webSocket;
...@@ -73,16 +73,16 @@ namespace MasterDevs.ChromeDevTools ...@@ -73,16 +73,16 @@ namespace MasterDevs.ChromeDevTools
}); });
} }
public Task<ICommandResponse> SendAsync<T>() public Task<ICommandResponse> SendAsync<T>(CancellationToken cancellationToken)
{ {
var command = _commandFactory.Create<T>(); var command = _commandFactory.Create<T>();
return SendCommand(command); return SendCommand(command, cancellationToken);
} }
public Task<ICommandResponse> SendAsync<T>(T parameter) public Task<ICommandResponse> SendAsync<T>(T parameter, CancellationToken cancellationToken)
{ {
var command = _commandFactory.Create(parameter); var command = _commandFactory.Create(parameter);
return SendCommand(command); return SendCommand(command, cancellationToken);
} }
public void Subscribe<T>(Action<T> handler) where T : class public void Subscribe<T>(Action<T> handler) where T : class
...@@ -136,7 +136,7 @@ namespace MasterDevs.ChromeDevTools ...@@ -136,7 +136,7 @@ namespace MasterDevs.ChromeDevTools
private void HandleResponse(ICommandResponse response) private void HandleResponse(ICommandResponse response)
{ {
if (null == response) return; if (null == response) return;
ManualResetEvent requestMre; ManualResetEventSlim requestMre;
if (_requestWaitHandles.TryGetValue(response.Id, out requestMre)) if (_requestWaitHandles.TryGetValue(response.Id, out requestMre))
{ {
_responses.AddOrUpdate(response.Id, id => response, (key, value) => response); _responses.AddOrUpdate(response.Id, id => response, (key, value) => response);
...@@ -156,7 +156,7 @@ namespace MasterDevs.ChromeDevTools ...@@ -156,7 +156,7 @@ namespace MasterDevs.ChromeDevTools
} }
} }
private Task<ICommandResponse> SendCommand(Command command) private Task<ICommandResponse> SendCommand(Command command, CancellationToken cancellationToken)
{ {
var settings = new JsonSerializerSettings var settings = new JsonSerializerSettings
{ {
...@@ -164,13 +164,13 @@ namespace MasterDevs.ChromeDevTools ...@@ -164,13 +164,13 @@ namespace MasterDevs.ChromeDevTools
NullValueHandling = NullValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore,
}; };
var requestString = JsonConvert.SerializeObject(command, settings); var requestString = JsonConvert.SerializeObject(command, settings);
var requestResetEvent = new ManualResetEvent(false); var requestResetEvent = new ManualResetEventSlim(false);
_requestWaitHandles.AddOrUpdate(command.Id, requestResetEvent, (id, r) => requestResetEvent); _requestWaitHandles.AddOrUpdate(command.Id, requestResetEvent, (id, r) => requestResetEvent);
return Task.Run(() => return Task.Run(() =>
{ {
EnsureInit(); EnsureInit();
_webSocket.Send(requestString); _webSocket.Send(requestString);
requestResetEvent.WaitOne(); requestResetEvent.Wait(cancellationToken);
ICommandResponse response = null; ICommandResponse response = null;
_responses.TryRemove(command.Id, out response); _responses.TryRemove(command.Id, out response);
_requestWaitHandles.TryRemove(command.Id, out requestResetEvent); _requestWaitHandles.TryRemove(command.Id, out requestResetEvent);
......
using System.Threading;
using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools
{
public static class ChromeSessionExtensions
{
public static Task<ICommandResponse> SendAsync<T>(this IChromeSession session, T parameter)
{
return session.SendAsync<T>(parameter, CancellationToken.None);
}
public static Task<ICommandResponse> SendAsync<T>(this IChromeSession session)
{
return session.SendAsync<T>(CancellationToken.None);
}
}
}
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools namespace MasterDevs.ChromeDevTools
{ {
public interface IChromeSession public interface IChromeSession
{ {
Task<ICommandResponse> SendAsync<T>(T parameter); Task<ICommandResponse> SendAsync<T>(T parameter, CancellationToken cancellationToken);
Task<ICommandResponse> SendAsync<T>(); Task<ICommandResponse> SendAsync<T>(CancellationToken cancellationToken);
void Subscribe<T>(Action<T> handler) where T : class; void Subscribe<T>(Action<T> handler) where T : class;
} }
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
<Compile Include="ChromeProcessFactory.cs" /> <Compile Include="ChromeProcessFactory.cs" />
<Compile Include="ChromeProcess.cs" /> <Compile Include="ChromeProcess.cs" />
<Compile Include="ChromeSession.cs" /> <Compile Include="ChromeSession.cs" />
<Compile Include="ChromeSessionExtensions.cs" />
<Compile Include="ChromeSessionFactory.cs" /> <Compile Include="ChromeSessionFactory.cs" />
<Compile Include="Command.cs" /> <Compile Include="Command.cs" />
<Compile Include="CommandAttribute.cs" /> <Compile Include="CommandAttribute.cs" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment