Exemple #1
0
        private void incomingMessage(DistributedAppMessage msg)
        {
            try
            {
                var @object   = null as IConcurrentObject;
                var @continue = null as Action <DistributedAppMessage>;
                var @remote   = null as Action <DistributedAppMessage>;
                if (_objects.TryGetValue(msg.Id, out @object))
                {
                    var methods = null as Dictionary <string, MethodFunc>;
                    if (!_methods.TryGetValue(@object.GetType(), out methods))
                    {
                        throw new ArgumentException("type");
                    }

                    var method = null as MethodFunc;
                    if (!methods.TryGetValue(msg.Method, out method))
                    {
                        throw new ArgumentException("method");
                    }

                    var json = JObject.Parse(msg.Data);
                    if (json == null)
                    {
                        throw new ArgumentException("args");
                    }

                    ConcurrentApp.Schedule(@object,
                                           () => method(@object, json,
                                                        response => sendResponse(msg, response),
                                                        ex => sendFailure(msg, ex)),
                                           ex => sendFailure(msg, ex));
                }
                else if (_remotes.TryGetValue(msg.Id, out @remote))
                {
                    Debug.Assert(msg.RequestId == Guid.Empty);

                    msg.RequestId            = Guid.NewGuid();
                    _requests[msg.RequestId] = __res => msg.Success(__res.Data); //td: failure

                    @remote(msg);
                }
                else if (_requests.TryRemove(msg.RequestId, out @continue))
                {
                    @continue(msg);
                }
                else if (msg.Id == Guid.Empty && msg.RequestId == Guid.Empty)
                {
                    internalCommand(msg.Method, msg.Data);
                }
                else
                {
                    throw new ArgumentException("id");
                }
            }
            catch (Exception ex)
            {
                sendFailure(msg, ex);
            }
        }
Exemple #2
0
        private void sendResponse(DistributedAppMessage msg, object response)
        {
            if (msg.Success == null && msg.RequestId == Guid.Empty)
            {
                return; // no one is waiting for this
            }
            var jsonMessage = $"{{\"__res\": {JsonConvert.SerializeObject(response)}}}";

            if (msg.Success != null)
            {
                msg.Success(jsonMessage);
            }
            else
            {
                outgoingMessage(Guid.Empty, string.Empty, jsonMessage, msg.RequestId);
            }
        }