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 static void StartServer(IDistributedApp app, string url, int expectedClients = 0, Action <Exception> connected = null) { Task.Run(() => { using (var context = NetMQContext.Create()) using (var input = context.CreateDealerSocket()) { Dictionary <string, DealerSocket> clients = null; try { input.Bind(url); if (expectedClients > 0) { clients = new Dictionary <string, DealerSocket>(); awaitClients(input, expectedClients, clients, context, app); app.SendToClient = (client, msg) => writeMessage(clients[client], msg); } connected(null); } catch (Exception ex) { connected(ex); return; } //loop var message = new NetMQMessage(expectedFrameCount: 7); var appMessage = new DistributedAppMessage(); for (;;) { message = input.ReceiveMultipartMessage(expectedFrameCount: 7); try { if (!readMessage(message, appMessage)) { continue; } app.Receive(appMessage); } catch { //td: log? } finally { message.Clear(); } } } }); }
private static void writeMessage(DealerSocket output, DistributedAppMessage appMessage) { output.SendFrame(appMessage.Id.ToString(), more: true); output.SendFrameEmpty(more: true); output.SendFrame(appMessage.Method ?? string.Empty, more: true); output.SendFrameEmpty(more: true); output.SendFrame(appMessage.Data, more: true); output.SendFrameEmpty(more: true); output.SendFrame(appMessage.RequestId.ToString()); }
private void sendFailure(DistributedAppMessage msg, Exception ex) { if (msg.Failure == null && msg.RequestId == Guid.Empty) { return; // no one is waiting for this } if (msg.Failure != null) { msg.Failure(ex); } else { var jsonMessage = $"{{\"__ex\": \"{ex.Message}\"}}"; outgoingMessage(Guid.Empty, string.Empty, jsonMessage, msg.RequestId); } }
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); } }
private static bool readMessage(NetMQMessage message, DistributedAppMessage appMessage) { try { appMessage.Id = Guid.Parse(message.Pop().ConvertToString()); message.Pop(); appMessage.Method = message.Pop().ConvertToString(); message.Pop(); appMessage.Data = message.Pop().ConvertToString(); message.Pop(); appMessage.RequestId = Guid.Parse(message.Pop().ConvertToString()); } catch { //td: what? return(false); } return(true); }
private void sendFailure(DistributedAppMessage msg, string message) { sendFailure(msg, new InvalidOperationException(message)); }