Ejemplo n.º 1
0
        ///<Summary>
        /// Generates a Request JSON object (array syntax)
        ///</Summary>
        public static string Request(string id, string methodName, params object[] values) =>
        values == null || values.Length == 0 ?
        // without any values, this doesn't need parameters
        ProtocolExtensions.JsonObject(Protocol, methodName.Method(), Id(id)):

        // with values
        ProtocolExtensions.JsonObject(Protocol, methodName.Method(), Params(values), Id(id));
Ejemplo n.º 2
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;
            }
        }
Ejemplo n.º 3
0
        ///<Summary>
        /// Generates a Request JSON object (object parameter syntax)
        ///</Summary>
        public static string RequestWithObject(string id, string methodName, object parameter) =>
        parameter == null || parameter is string || parameter.IsPrimitive() ?
        // if you pass in null, a string, or a primitive
        // this has to be passed as an array
        ProtocolExtensions.JsonObject(Protocol, methodName.Method(), Params(new[] { parameter }), Id(id)):

        // pass as an object
        ProtocolExtensions.JsonObject(Protocol, methodName.Method(), Params(parameter.ToJsonValue()), Id(id));
Ejemplo n.º 4
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");
            }
        }
Ejemplo n.º 5
0
        public async Task <T> RequestWithObject <T>(string methodName, object parameter)
        {
            var id       = Interlocked.Increment(ref _requestId).ToString();
            var response = new CallerResponse <T>(id);

            _tasks.Add(id, response);
            await Send(ProtocolExtensions.Request(id, methodName, parameter)).ConfigureAwait(false);

            return(await response.Task.ConfigureAwait(false));
        }
        public async Task <T> Request <T>(string methodName, params object[] values)
        {
            var id       = Interlocked.Decrement(ref _requestId).ToString();
            var response = new CallerResponse <T>(id);

            lock (_tasks) { _tasks.Add(id, response); }
            await Send(ProtocolExtensions.Request(id, methodName, values)).ConfigureAwait(false);

            return(await response.Task.ConfigureAwait(false));
        }
Ejemplo n.º 7
0
 public void Dispatch(string path, Func <Task <string> > method)
 {
     _dispatch.Add(path, async(input) => {
         var result = await method();
         if (result == null)
         {
             return("null");
         }
         return(ProtocolExtensions.Quote(result));
     });
 }
Ejemplo n.º 8
0
        public void Dispatch <P1, P2, T>(string path, Func <P1, P2, Task <T> > method)
        {
            _dispatch.Add(path, async(input) => {
                var args = ReadArguments(input, 2);
                var a1   = args[0].Value <P1>();
                var a2   = args[1].Value <P2>();

                var result = await method(a1, a2);
                if (result == null)
                {
                    return("null");
                }
                return(ProtocolExtensions.ToJsonValue(result));
            });
        }
Ejemplo n.º 9
0
 public async Task NotifyWithObject(string methodName, object parameter) =>
 await Send(ProtocolExtensions.NotificationWithObject(methodName, parameter)).ConfigureAwait(false);
Ejemplo n.º 10
0
 public async Task Notify(string methodName, params object[] values) =>
 await Send(ProtocolExtensions.Notification(methodName, values)).ConfigureAwait(false);
Ejemplo n.º 11
0
 public async Task Respond(string request, string value)
 {
     await Send(ProtocolExtensions.Response(request, value)).ConfigureAwait(false);
 }
Ejemplo n.º 12
0
 public async Task SendError(string id, int code, string message)
 {
     await Send(ProtocolExtensions.Error(id, code, message)).ConfigureAwait(false);
 }