Exemple #1
0
        public async Task Process(JToken content)
        {
            // Log("IN PROCESS");
            if (content == null)
            {
                // Log("BAD CONTENT");
                return;
            }
            if (content is JObject)
            {
                Log($"RECV '{content}'");

                var jobject = content as JObject;
                try {
                    if (jobject.Properties().Any(each => each.Name == "method"))
                    {
                        var method = jobject.Property("method").Value.ToString();
                        var id     = jobject.Property("id")?.Value.ToString();
                        // this is a method call.
                        // pass it to the service that is listening...
                        //Log($"Dispatching: {method}");
                        if (_dispatch.TryGetValue(method, out Func <JToken, Task <string> > fn))
                        {
                            var parameters = jobject.Property("params").Value;
                            var result     = await fn(parameters);

                            if (id != null)
                            {
                                // if this is a request, send the response.
                                await this.Send(ProtocolExtensions.Response(id, result));
                            }
                            //
                        }
                        return;
                    }

                    // this is a result from a previous call.
                    if (jobject.Properties().Any(each => each.Name == "result"))
                    {
                        var id = jobject.Property("id")?.Value.ToString();
                        if (!string.IsNullOrEmpty(id))
                        {
                            var f = _tasks[id];
                            _tasks.Remove(id);
                            //Log($"result data: {jobject.Property("result").Value.ToString()}");
                            f.SetCompleted(jobject.Property("result").Value);
                            //Log("Should have unblocked?");
                        }
                    }
                } catch (Exception e) {
                    //Log($"[PROCESS ERROR]: {e.GetType().FullName}/{e.Message}/{e.StackTrace}");
                    //Log($"[LISTEN CONTENT]: {jobject.ToString()}");
                }
            }
            if (content is JArray)
            {
                //Log("TODO: Batch");
                return;
            }
        }
Exemple #2
0
        public void Process(JToken content)
        {
            if (content == null)
            {
                return;
            }
            if (content is JObject)
            {
                Task.Factory.StartNew(async() => {
                    var jobject = content as JObject;
                    try
                    {
                        if (jobject.Properties().Any(each => each.Name == "method"))
                        {
                            var method = jobject.Property("method").Value.ToString();
                            var id     = jobject.Property("id")?.Value.ToString();
                            // this is a method call.
                            // pass it to the service that is listening...
                            if (_dispatch.TryGetValue(method, out Func <JToken, Task <string> > fn))
                            {
                                var parameters = jobject.Property("params").Value;
                                var result     = await fn(parameters);
                                if (id != null)
                                {
                                    // if this is a request, send the response.
                                    await this.Send(ProtocolExtensions.Response(id, result));
                                }
                                //
                            }
                            return;
                        }

                        // this is a result from a previous call.
                        if (jobject.Properties().Any(each => each.Name == "result"))
                        {
                            var id = jobject.Property("id")?.Value.ToString();
                            if (!string.IsNullOrEmpty(id))
                            {
                                ICallerResponse f = null;
                                lock ( _tasks ) {
                                    f = _tasks[id];
                                    _tasks.Remove(id);
                                }
                                f.SetCompleted(jobject.Property("result").Value);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                });
            }
            if (content is JArray)
            {
                Console.Error.WriteLine("Unhandled: Batch Request");
            }
        }
Exemple #3
0
 public async Task Respond(string request, string value)
 {
     await Send(ProtocolExtensions.Response(request, value)).ConfigureAwait(false);
 }