forked from kerryjiang/SuperSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicCommandLoaderBase.cs
More file actions
259 lines (215 loc) · 9.06 KB
/
Copy pathDynamicCommandLoaderBase.cs
File metadata and controls
259 lines (215 loc) · 9.06 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using AnyLog;
using Microsoft.Scripting.Hosting;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
namespace SuperSocket.Dlr
{
/// <summary>
/// Which is used for loading dynamic script file
/// </summary>
public abstract class DynamicCommandLoaderBase<TCommand> : CommandLoaderBase<TCommand>
where TCommand : ICommand
{
class CommandFileEntity
{
public IScriptSource Source { get; set; }
public bool Processed { get; set; }
}
class ServerCommandState
{
public List<IScriptSource> Sources { get; set; }
public Action<IEnumerable<CommandUpdateInfo<IScriptSource>>> CommandUpdater { get; set; }
}
/// <summary>
/// Gets the script runtime.
/// </summary>
protected ScriptRuntime ScriptRuntime { get; private set; }
private Timer m_ScriptSourceCheckingTimer;
private static readonly int m_ScriptSourceCheckingInterval = 1000 * 60 * 5;// 5 minutes
private ServerCommandState m_ServerCommandState;
/// <summary>
/// Initializes a new instance of the <see cref="DynamicCommandLoaderBase{TCommand}"/> class.
/// </summary>
public DynamicCommandLoaderBase()
: this(ScriptRuntime.CreateFromConfiguration())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DynamicCommandLoaderBase{TCommand}"/> class.
/// </summary>
/// <param name="scriptRuntime">The script runtime.</param>
public DynamicCommandLoaderBase(ScriptRuntime scriptRuntime)
{
ScriptRuntime = scriptRuntime;
m_ScriptSourceCheckingTimer = new Timer(OnScriptSourceCheckingTimerCallback, null, m_ScriptSourceCheckingInterval, m_ScriptSourceCheckingInterval);
}
/// <summary>
/// Gets the script sources.
/// </summary>
/// <param name="rootConfig">The root config.</param>
/// <param name="appServer">The app server.</param>
/// <returns></returns>
protected abstract IEnumerable<IScriptSource> GetScriptSources(IRootConfig rootConfig, IAppServer appServer);
private IEnumerable<CommandUpdateInfo<TCommand>> GetUpdatedCommands(IEnumerable<CommandUpdateInfo<IScriptSource>> updatedSources)
{
var updatedCommands = new List<CommandUpdateInfo<TCommand>>();
foreach (var source in updatedSources)
{
if (source.UpdateAction == CommandUpdateAction.Remove)
{
updatedCommands.Add(new CommandUpdateInfo<TCommand>
{
Command = (TCommand)Activator.CreateInstance(m_MockupCommandType, source),
UpdateAction = source.UpdateAction
});
}
try
{
var command = (TCommand)Activator.CreateInstance(m_DynamicCommandType, ScriptRuntime, source);
updatedCommands.Add(new CommandUpdateInfo<TCommand>
{
Command = command,
UpdateAction = source.UpdateAction
});
}
catch (Exception e)
{
OnError(new Exception("Failed to load command source: " + source.Command.Tag + "!", e));
continue;
}
}
return updatedCommands;
}
private IAppServer m_AppServer;
private IRootConfig m_RootConfig;
private Type m_DynamicCommandType;
private Type m_MockupCommandType;
/// <summary>
/// Initializes the command loader by the root config and the appserver instance.
/// </summary>
/// <param name="rootConfig">The root config.</param>
/// <param name="appServer">The app server.</param>
/// <returns></returns>
public override bool Initialize(IRootConfig rootConfig, IAppServer appServer)
{
m_RootConfig = rootConfig;
m_AppServer = appServer;
var genericParameterTypes = typeof(TCommand).GetGenericArguments();
m_DynamicCommandType = typeof(DynamicCommand<,>).MakeGenericType(genericParameterTypes);
m_MockupCommandType = typeof(MockupCommand<,>).MakeGenericType(genericParameterTypes);
return true;
}
/// <summary>
/// Tries to load commands.
/// </summary>
/// <param name="commands">The commands.</param>
/// <returns></returns>
public override bool TryLoadCommands(out IEnumerable<TCommand> commands)
{
var outputCommands = new List<TCommand>();
if (m_ServerCommandState != null)
{
OnError("This server's commands have been loaded already!");
commands = outputCommands;
return false;
}
var serverCommandState = new ServerCommandState
{
CommandUpdater = (o) => OnUpdated(GetUpdatedCommands(o)),
Sources = new List<IScriptSource>()
};
m_ServerCommandState = serverCommandState;
var scriptSources = GetScriptSources(m_RootConfig, m_AppServer);
foreach (var source in scriptSources)
{
TCommand command;
try
{
command = (TCommand)Activator.CreateInstance(m_DynamicCommandType, ScriptRuntime, source);
serverCommandState.Sources.Add(source);
outputCommands.Add(command);
}
catch (Exception e)
{
OnError(new Exception("Failed to load command source: " + source.Tag + "!", e));
}
}
commands = outputCommands;
return true;
}
private void OnScriptSourceCheckingTimerCallback(object state)
{
m_ScriptSourceCheckingTimer.Change(Timeout.Infinite, Timeout.Infinite);
try
{
var serverState = m_ServerCommandState;
var commandSourceDict = serverState.Sources.ToDictionary(c => c.Tag,
c => new CommandFileEntity { Source = c },
StringComparer.OrdinalIgnoreCase);
var commandSources = GetScriptSources(m_RootConfig, m_AppServer);
List<CommandUpdateInfo<IScriptSource>> updatedSources = new List<CommandUpdateInfo<IScriptSource>>();
foreach (var c in commandSources)
{
var lastUpdatedTime = c.LastUpdatedTime;
CommandFileEntity commandEntity;
if (commandSourceDict.TryGetValue(c.Tag, out commandEntity))
{
commandEntity.Processed = true;
if (commandEntity.Source.LastUpdatedTime != lastUpdatedTime)
{
//update command's last updated time in dictionary
commandEntity.Source = c;
updatedSources.Add(new CommandUpdateInfo<IScriptSource>
{
UpdateAction = CommandUpdateAction.Update,
Command = c
});
}
}
else
{
commandSourceDict.Add(c.Tag, new CommandFileEntity
{
Source = c,
Processed = true
});
updatedSources.Add(new CommandUpdateInfo<IScriptSource>
{
UpdateAction = CommandUpdateAction.Add,
Command = c
});
}
}
foreach (var cmd in commandSourceDict.Values.Where(e => !e.Processed))
{
updatedSources.Add(new CommandUpdateInfo<IScriptSource>
{
UpdateAction = CommandUpdateAction.Remove,
Command = cmd.Source
});
}
if (updatedSources.Count > 0)
{
serverState.Sources = commandSourceDict.Values.Where(e => e.Processed).Select(e => e.Source).ToList();
serverState.CommandUpdater(updatedSources);
}
}
catch (Exception e)
{
OnError(e);
}
finally
{
m_ScriptSourceCheckingTimer.Change(m_ScriptSourceCheckingInterval, m_ScriptSourceCheckingInterval);
}
}
}
}