async/await

This commit is contained in:
Craig Roberts 2018-08-29 18:56:26 -04:00
parent 03f51ba65e
commit 6fda78301c
2 changed files with 81 additions and 5 deletions

View file

@ -9,6 +9,7 @@ using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using AsterNET.IO; using AsterNET.IO;
using System.Threading.Tasks;
namespace AsterNET.Manager namespace AsterNET.Manager
{ {
@ -1314,13 +1315,13 @@ namespace AsterNET.Manager
/// <param name="action">action to send</param> /// <param name="action">action to send</param>
/// <param name="timeout">timeout in milliseconds</param> /// <param name="timeout">timeout in milliseconds</param>
/// <returns></returns> /// <returns></returns>
public Response.ManagerResponse SendAction(ManagerAction action, int timeOut) public Response.ManagerResponse SendAction(ManagerAction action, int timeout)
{ {
AutoResetEvent autoEvent = new AutoResetEvent(false); AutoResetEvent autoEvent = new AutoResetEvent(false);
ResponseHandler handler = new ResponseHandler(action, autoEvent); ResponseHandler handler = new ResponseHandler(action, autoEvent);
int hash = SendAction(action, handler); int hash = SendAction(action, handler);
bool result = autoEvent.WaitOne(timeOut <= 0 ? -1 : timeOut, true); bool result = autoEvent.WaitOne(timeout <= 0 ? -1 : timeout, true);
RemoveResponseHandler(handler); RemoveResponseHandler(handler);
@ -1331,7 +1332,13 @@ namespace AsterNET.Manager
#endregion #endregion
#region SendAction(action, responseHandler) #region SendAction(action, responseHandler)
public int SendAction(ManagerAction action, ResponseHandler responseHandler) /// <summary>
/// Send action ans with timeout (milliseconds)
/// </summary>
/// <param name="action">action to send</param>
/// <param name="responseHandler">Response Handler</param>
/// <returns></returns>
public int SendAction(ManagerAction action, IResponseHandler responseHandler)
{ {
if (action == null) if (action == null)
throw new ArgumentException("Unable to send action: action is null."); throw new ArgumentException("Unable to send action: action is null.");
@ -1354,6 +1361,42 @@ namespace AsterNET.Manager
} }
#endregion #endregion
#region SendActionAsync(action)
/// <summary>
/// Asynchronously send Action async with default timeout.
/// </summary>
/// <param name="action">action to send</param>
public Task<ManagerResponse> SendActionAsync(ManagerAction action)
{
return SendActionAsync(action, null);
}
#endregion
#region SendActionAsync(action, timeout)
/// <summary>
/// Asynchronously send Action async.
/// </summary>
/// <param name="action">action to send</param>
/// <param name="cancellationToken">cancellation Token</param>
public Task<ManagerResponse> SendActionAsync(ManagerAction action, CancellationTokenSource cancellationToken)
{
var handler = new TaskResponseHandler(action);
var source = handler.TaskCompletionSource;
SendAction(action, handler);
if (cancellationToken != null)
cancellationToken.Token.Register(() => { source.TrySetCanceled(); });
return source.Task.ContinueWith(x =>
{
RemoveResponseHandler(handler);
return x.Result;
});
}
#endregion
#region SendEventGeneratingAction(action) #region SendEventGeneratingAction(action)
public ResponseEvents SendEventGeneratingAction(ManagerActionEvent action) public ResponseEvents SendEventGeneratingAction(ManagerActionEvent action)
{ {
@ -1420,7 +1463,11 @@ namespace AsterNET.Manager
responseEventHandlers[handler.Hash] = handler; responseEventHandlers[handler.Hash] = handler;
} }
internal void RemoveResponseHandler(IResponseHandler handler) /// <summary>
/// Delete an instance of a class <see cref="IResponseHandler"/> from handlers list.
/// </summary>
/// <param name="handler">Class instance <see cref="IResponseHandler"/>.</param>
public void RemoveResponseHandler(IResponseHandler handler)
{ {
int hash = handler.Hash; int hash = handler.Hash;
if (hash != 0) if (hash != 0)
@ -1437,7 +1484,6 @@ namespace AsterNET.Manager
if (responseEventHandlers.ContainsKey(hash)) if (responseEventHandlers.ContainsKey(hash))
responseEventHandlers.Remove(hash); responseEventHandlers.Remove(hash);
} }
private IResponseHandler GetRemoveResponseHandler(int hash) private IResponseHandler GetRemoveResponseHandler(int hash)
{ {
IResponseHandler handler = null; IResponseHandler handler = null;

View file

@ -0,0 +1,30 @@
using System.Threading.Tasks;
using AsterNET.Manager.Action;
namespace AsterNET.Manager.Response
{
/// <summary>
///
/// </summary>
public class TaskResponseHandler : IResponseHandler
{
public TaskResponseHandler(ManagerAction action)
{
TaskCompletionSource = new TaskCompletionSource<ManagerResponse>();
Action = action;
}
public TaskCompletionSource<ManagerResponse> TaskCompletionSource { get; }
public ManagerAction Action { get; }
public int Hash { get; set; }
public void HandleResponse(ManagerResponse response)
{
TaskCompletionSource.TrySetResult(response);
}
public void Free() { }
}
}