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); } }
public void Start() { if (Connect != null) { var exception = Connect(this); if (exception != null) { throw exception; } } ConcurrentApp.AddSpawnListener(onSpawn); ConcurrentApp.Start(); }
public void RegisterClass(Type type) { var publicMethods = type .GetMethods() .Where(method => isConcurrent(method)); var methodFuncs = new Dictionary <string, MethodFunc>(); foreach (var method in publicMethods) { var parameters = method .GetParameters(); var paramCount = parameters.Length - 3; var paramNames = parameters .Take(paramCount) .Select(param => param.Name) .ToArray(); var paramTypes = parameters .Take(paramCount) .Select(param => param.ParameterType) .ToArray(); methodFuncs[method.Name] = (@object, args, success, failure) => { var arguments = new object[paramCount + 3]; for (int i = 0; i < paramCount; i++) { var property = args.Property(paramNames[i]); arguments[i] = property.Value.ToObject(paramTypes[i]); } arguments[paramCount] = default(CancellationToken); arguments[paramCount + 1] = success; arguments[paramCount + 2] = failure; method.Invoke(@object, arguments); }; } _methods[type] = methodFuncs; ConcurrentApp.RegisterClass(type); }
public void AwaitCompletion() { ConcurrentApp.AwaitCompletion(); }
public void Stop() { ConcurrentApp.Stop(); }