-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystem.cs
More file actions
323 lines (286 loc) · 11.5 KB
/
System.cs
File metadata and controls
323 lines (286 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using Org.Reddragonit.MultiDomain.Controllers;
using Org.Reddragonit.MultiDomain.Interfaces;
using Org.Reddragonit.MultiDomain.Interfaces.EventSystem;
using Org.Reddragonit.MultiDomain.Interfaces.Logging;
using Org.Reddragonit.MultiDomain.Interfaces.Messaging;
using Org.Reddragonit.MultiDomain.Messages;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
namespace Org.Reddragonit.MultiDomain
{
public static class System
{
private delegate void delAppendEntry(StackFrame sf,LogLevels level, string message);
internal struct sDomain
{
private AppDomain _domain;
public AppDomain Domain { get { return _domain; } }
private Core _core;
public Core Core { get { return _core; } }
private delProcessEvent _processEvent;
public void ProcessEvent(Messages.Event Event)
{
if (Event.IsSynchronous)
_processEvent.Invoke(Event);
else
_processEvent.BeginInvoke(Event, null, null);
}
public sDomain(string name)
: this(name,null) { }
public sDomain(string name,string configFile)
{
if (configFile != null)
{
AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
setup.ConfigurationFile = configFile;
_domain = AppDomain.CreateDomain((name == null ? System._rand.NextString(32) : name), AppDomain.CurrentDomain.Evidence, setup);
}else
_domain = AppDomain.CreateDomain((name == null ? System._rand.NextString(32) : name));
_domain.Load(typeof(System).Assembly.FullName);
_core = (Core)_domain.CreateInstanceAndUnwrap(typeof(System).Assembly.FullName, typeof(Core).FullName);
_processEvent = new delProcessEvent(_core.ProcessEvent);
}
}
private static Core _core;
private static Mutex _mut;
private static MT19937 _rand;
private static delAppendEntry _appendEntry;
private static List<sDomain> _domains;
internal static sDomain[] Domains
{
get
{
_mut.WaitOne();
sDomain[] ret = new sDomain[_domains.Count];
_domains.CopyTo(ret);
_mut.ReleaseMutex();
return ret;
}
}
static System()
{
_core = new Core();
_core.Init();
_mut = new Mutex(false);
_rand = new MT19937(DateTime.Now.Ticks);
_domains = new List<sDomain>();
_appendEntry = new delAppendEntry(_AppendEntry);
}
#region Events
public static string RegisterHandler(IEventHandler handler) { return _core.RegisterHandler(handler); }
public static void UnRegisterEventHandler(string id) { _core.UnRegistereventHandler(id); }
public static void TriggerEvent(IEvent Event) {
Messages.Event evnt = new Messages.Event(Event);
_core.AbsoluteParent.ProcessEvent(evnt);
}
#endregion
#region InterDomainMessages
public static InterDomainMessageResponse ProcessInterDomainMessage(IInterDomainMessage message)
{
DateTime start = DateTime.Now;
message = new RoutedInterDomainMessage(message);
message = _core.AbsoluteParent.InterceptMessage(message);
Debug("Time to intercept interdomain message {0}:{1}ms", message.Name, DateTime.Now.Subtract(start).TotalMilliseconds);
start = DateTime.Now;
InterDomainMessageResponse ret = _core.AbsoluteParent.ProcessMessage(message);
Debug("Time to handle interdomain message {0}:{1}ms", message.Name, DateTime.Now.Subtract(start).TotalMilliseconds);
start = DateTime.Now;
if (ret != null)
{
_core.AbsoluteParent.InterceptResponse(ref ret);
Debug("Time to intercept interdomain response {0}:{1}ms", message.Name, DateTime.Now.Subtract(start).TotalMilliseconds);
}
return ret;
}
#endregion
#region Logging
public static void Error(Exception e)
{
StackFrame sf = new StackFrame(1);
AssemblyName assName = new AssemblyName(sf.GetMethod().DeclaringType.Assembly.FullName);
MethodBase method = sf.GetMethod();
Exception ex = e;
while (ex != null)
{
_AppendEntry(LogLevels.Error, ex.Message);
_AppendEntry(LogLevels.Error, ex.StackTrace);
ex = ex.InnerException;
}
}
public static void Error(string message)
{
_AppendEntry(LogLevels.Error, message);
}
public static void Error(string message, object arg0)
{
_AppendEntry(LogLevels.Error, string.Format(message, arg0));
}
public static void Error(string message, object arg0, object arg1)
{
_AppendEntry(LogLevels.Error, string.Format(message, arg0, arg1));
}
public static void Error(string message, object[] args)
{
_AppendEntry(LogLevels.Error, string.Format(message, args));
}
public static void Fatal(string message)
{
_AppendEntry(LogLevels.Fatal, message);
}
public static void Fatal(string message, object arg0)
{
_AppendEntry(LogLevels.Fatal, string.Format(message, arg0));
}
public static void Fatal(string message, object arg0, object arg1)
{
_AppendEntry(LogLevels.Fatal, string.Format(message, arg0, arg1));
}
public static void Fatal(string message, object[] args)
{
_AppendEntry(LogLevels.Fatal, string.Format(message, args));
}
public static void Warn(string message)
{
_AppendEntry(LogLevels.Warn, message);
}
public static void Warn(string message, object arg0)
{
_AppendEntry(LogLevels.Warn, string.Format(message, arg0));
}
public static void Warn(string message, object arg0, object arg1)
{
_AppendEntry(LogLevels.Warn, string.Format(message, arg0, arg1));
}
public static void Warn(string message, object[] args)
{
_AppendEntry(LogLevels.Warn, string.Format(message, args));
}
public static void Info(string message)
{
_AppendEntry(LogLevels.Info, message);
}
public static void Info(string message, object arg0)
{
_AppendEntry(LogLevels.Info, string.Format(message, arg0));
}
public static void Info(string message, object arg0, object arg1)
{
_AppendEntry(LogLevels.Info, string.Format(message, arg0, arg1));
}
public static void Info(string message, object[] args)
{
_AppendEntry(LogLevels.Info, string.Format(message, args));
}
public static void Debug(string message)
{
_AppendEntry(LogLevels.Debug, message);
}
public static void Debug(string message, object arg0)
{
_AppendEntry(LogLevels.Debug, string.Format(message, arg0));
}
public static void Debug(string message, object arg0, object arg1)
{
_AppendEntry(LogLevels.Debug, string.Format(message, arg0, arg1));
}
public static void Debug(string message, object[] args)
{
_AppendEntry(LogLevels.Debug, string.Format(message, args));
}
private static void _AppendEntry(LogLevels level, string message)
{
int index = 0;
StackFrame sf = new StackFrame(index);
while (sf.GetMethod().DeclaringType.FullName == typeof(System).FullName)
{
index++;
sf = new StackFrame(index);
}
_appendEntry.BeginInvoke(sf, level, message, null, null);
}
private static void _AppendEntry(StackFrame sf, LogLevels level, string message)
{
MethodBase method = sf.GetMethod();
_core.AbsoluteParent.AppendEntry(AppDomain.CurrentDomain.FriendlyName, new AssemblyName(method.DeclaringType.Assembly.FullName), method.DeclaringType.FullName, method.Name, level, DateTime.Now, message);
}
#endregion
public static bool ProduceNewDomain(object[] assemblies)
{
return ProduceNewDomain(null, assemblies);
}
public static bool ProduceNewDomain(string name, object[] assemblies)
{
_mut.WaitOne();
sDomain? dom = null;
try
{
foreach (object obj in assemblies)
{
if (obj is string)
{
if (((string)obj).EndsWith(".config"))
{
dom = new sDomain(name, (string)obj);
break;
}
}
}
if (dom == null)
dom = new sDomain(name);
dom.Value.Core.LoadAssemblies(assemblies);
dom.Value.Core.EstablishParent(_core);
_domains.Add(dom.Value);
dom.Value.Core.Startup();
_mut.ReleaseMutex();
dom.Value.Domain.DomainUnload += Domain_DomainUnload;
}
catch (Exception e)
{
if (dom.HasValue)
{
for (int x = 0; x < _domains.Count; x++)
{
if (_domains[x].Domain.FriendlyName == dom.Value.Domain.FriendlyName)
{
System.Error("Unloading {0} due to error in startup for application domain.", dom.Value.Domain.FriendlyName);
_domains.RemoveAt(x);
break;
}
}
_mut.ReleaseMutex();
try { AppDomain.Unload(dom.Value.Domain); }
catch (Exception ex) { }
}
dom = null;
}
return dom.HasValue;
}
internal static void Domain_DomainUnload(object sender, EventArgs e)
{
AppDomain dom = (AppDomain)sender;
_mut.WaitOne();
for (int x = 0; x < _domains.Count; x++)
{
if (_domains[x].Domain.FriendlyName == dom.FriendlyName)
{
_domains.RemoveAt(x);
break;
}
}
_mut.ReleaseMutex();
}
internal static void _RegsiterMessageHandlerRoute(sRoute[] routes)
{
_core._RegsiterMessageHandlerRoute(routes);
}
internal static void _UnRegisterMessageHandlerRoute(sRoute[] routes)
{
_core._UnRegsiterMessageHandlerRoute(routes);
}
}
}