Skip to content

Commit 49837d0

Browse files
committed
real async workflow
1 parent 2f2b562 commit 49837d0

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

Json-Rpc/Handler.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ private void RemoveRpcException()
149149
/// </summary>
150150
public SMD MetaData { get; set; }
151151

152+
private const string THREAD_CALLBACK_SLOT_NAME ="Callback";
153+
152154
#region Public Methods
153155

154156
/// <summary>
@@ -199,7 +201,9 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
199201
Error = preProcessingException,
200202
Id = Rpc.Id
201203
};
204+
//callback is called - if it is empty then nothing will be done
202205
callback.Invoke(response);
206+
//return response always- if callback is empty or not
203207
return response;
204208
}
205209

@@ -339,11 +343,12 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
339343
try
340344
{
341345
//callback is stored to thread's local storage in order to get it directly from concrete JsonRpcService method implementation
342-
if (null != callback)
343-
{
344-
Thread.SetData(Thread.GetNamedDataSlot("Callback"), callback);
345-
}
346+
//where callback is just returned from method
347+
Thread.SetData(Thread.GetNamedDataSlot(THREAD_CALLBACK_SLOT_NAME), callback);
348+
349+
346350
var results = handle.DynamicInvoke(parameters);
351+
347352
var last = parameters.LastOrDefault();
348353
JsonRpcException contextException;
349354
if (Task.CurrentId.HasValue && RpcExceptions.TryRemove(Task.CurrentId.Value, out contextException))
@@ -358,7 +363,8 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
358363
callback.Invoke(response);
359364
return response;
360365
}
361-
366+
//return response, if callback is set (method is asynchronous) - result could be empty string and future result operations
367+
//will be processed in the callback
362368
return new JsonResponse() { Result = results };
363369
}
364370
catch (Exception ex)
@@ -401,6 +407,26 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null, Action<Jso
401407
}
402408
}
403409

410+
/// <summary>
411+
/// Method returns the actual callback set to this thread in Handle() method.
412+
/// If callback is not set, then empty callback is returned.
413+
/// </summary>
414+
/// <returns></returns>
415+
internal Action<JsonResponse> GetAsyncCallback()
416+
{
417+
object o = Thread.GetData(Thread.GetNamedDataSlot(THREAD_CALLBACK_SLOT_NAME));
418+
Action<JsonResponse> callback;
419+
if(o is Action<JsonResponse>)
420+
{
421+
callback = o as Action<JsonResponse>;
422+
}
423+
else
424+
{
425+
callback = delegate(JsonResponse a) { };
426+
}
427+
return callback;
428+
}
429+
404430
private void AddRpcContext(object RpcContext)
405431
{
406432
if (Task.CurrentId != null)

Json-Rpc/JsonRpcProcessor.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ public static void AsyncProcess(string jsonRpc, Action<string> callback, object
1818
Task.Factory.StartNew(() => AsyncProcessInternal(Handler.DefaultSessionId(), jsonRpc, context, callback));
1919
}
2020

21+
public static void AsyncProcess(string sessionId,string jsonRpc, Action<string> callback, object context = null)
22+
{
23+
Task.Factory.StartNew(() => AsyncProcessInternal(sessionId, jsonRpc, context, callback));
24+
}
25+
26+
/// <summary>
27+
/// The callback will be returned to the user, who needs to invoke it in concrete
28+
/// service implementation. Call should be made directly from same thread as
29+
/// service method is executed.
30+
/// </summary>
31+
/// <param name="sessionId">Handler session id</param>
32+
/// <returns></returns>
33+
public static Action<JsonResponse> GetAsyncProcessCallback(string sessionId = "")
34+
{
35+
Handler handler;
36+
if ("" == sessionId)
37+
{
38+
handler = Handler.GetSessionHandler(Handler.DefaultSessionId());
39+
}
40+
else
41+
{
42+
handler = Handler.GetSessionHandler(sessionId);
43+
}
44+
45+
return handler.GetAsyncCallback();
46+
}
47+
2148
private static void AsyncProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext, Action<string> callback)
2249
{
2350
Handler handler = Handler.GetSessionHandler(sessionId);

0 commit comments

Comments
 (0)