public static IpcChannel CreateRecieverChannel <TBackend>(string name, Action <IpcConfiguration> configurator = null) where TBackend : ISignalBackend, new() { var channel = new IpcChannel { event_communicator = new TBackend(), func_communicator = new TBackend() }; if (NLog.LogManager.Configuration == null) { var config = new NLog.Config.LoggingConfiguration(); var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); // Rules for mapping loggers to targets config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, logconsole); // Apply config NLog.LogManager.Configuration = config; } channel.event_communicator.Initialize(name + ".events", 4096, true); channel.func_communicator.Initialize(name + ".funcs", 4096, true); if (configurator != null) { var config = new IpcConfiguration(); configurator(config); foreach (var func in config.shared_functions) { channel.shared_functions.Add(func.Key, func.Value); } } return(channel); }
public static void CollectShared(IpcChannel channel, params Type[] types) { Logger.Trace("Collecting Shared Functions"); channel.func_communicator.OnNewMessage += (data) => { var obj = Serializer.Deserialize <FunctionCallRequest>(data); Optional <string> error = false; object res = null; object[] args = null; var methodInfo = channel.shared_functions[obj.ID]; var parameterInfo = methodInfo.GetParameters(); if (!IsArgumentMismatch(parameterInfo, obj.ParameterRaw)) { args = GetDeserializedParameters(parameterInfo, obj.ParameterRaw); } var id = methodInfo.GetCustomAttribute <SharedFunctionAttribute>()?.ID; try { try { if (methodInfo.IsStatic) { res = methodInfo.Invoke(null, args); } else { res = methodInfo.Invoke(channel, args); } } catch (Exception ex) { error = ex.Message.ToOptional(); } } catch (Exception ex) { error = ex.Message.ToOptional(); } var resp = new FunctionCallResponse { ID = obj.ID }; if (!error) { resp.ReturnValue = Serializer.Serialize(res); } else { resp.ErrorMessage = error; } var raw = Serializer.Serialize(resp); channel.func_communicator.Write(raw); }; foreach (var t in types) { var attr = t.GetCustomAttribute <SharedAttribute>(); if (attr != null) { foreach (var m in t.GetMethods()) { var mattr = m.GetCustomAttribute <SharedFunctionAttribute>(); if (mattr != null) { if (!channel.shared_functions.ContainsKey(mattr.ID)) { channel.shared_functions.Add(mattr.ID, m); } } } } } }
public static void CollectAllShared(IpcChannel channel) { var assembly = Assembly.GetCallingAssembly(); CollectShared(channel, assembly.GetTypes()); }
public static T CallMethod <T>(IpcChannel channel, int id, params object[] arg) { return(Task.Run(() => CallMethodAsync <T>(channel, id, arg)).Result); }
public static void Send(IpcChannel channel, IpcMessage msg) { var json = JsonConvert.SerializeObject(msg); channel.communicator.Write(json); }