Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive RedisAsyncCommandTask from TaskCompletionSource. #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions CSRedis/Internal/RedisAsyncCommandToken.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
using CSRedis.Internal.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSRedis.Internal
{
interface IRedisAsyncCommandToken
{
Task Task { get; }
RedisCommand Command { get; }
void SetResult(RedisReader reader);
void SetException(Exception e);
bool TrySetException(Exception e);
}

class RedisAsyncCommandToken<T> : IRedisAsyncCommandToken
class RedisAsyncCommandToken<T> : TaskCompletionSource<T>, IRedisAsyncCommandToken
{
readonly TaskCompletionSource<T> _tcs;
readonly RedisCommand<T> _command;

public TaskCompletionSource<T> TaskSource { get { return _tcs; } }
public RedisCommand Command { get { return _command; } }
public Task Task { get { return _tcs.Task; } }

public RedisAsyncCommandToken(RedisCommand<T> command)
{
_tcs = new TaskCompletionSource<T>();
_command = command;
}

public void SetResult(RedisReader reader)
{
_tcs.SetResult(_command.Parse(reader));
}

public void SetException(Exception e)
{
_tcs.SetException(e);
SetResult(_command.Parse(reader));
}
}
}
4 changes: 2 additions & 2 deletions CSRedis/Internal/RedisConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Task<T> CallAsync<T>(RedisCommand<T> command)
var token = new RedisAsyncCommandToken<T>(command);
AsyncWriteQueue.Enqueue(token);
ConnectAsync().ContinueWith(CallAsyncDeferred);
return token.TaskSource.Task;
return token.Task;
}

public void Write(RedisCommand command)
Expand Down Expand Up @@ -316,7 +316,7 @@ void OnSocketSent(SocketAsyncEventArgs args)
}
catch (Exception e)
{
token.SetException(e);
token.TrySetException(e);
}
}
}
Expand Down