-
Notifications
You must be signed in to change notification settings - Fork 98
/
ServerModel.cs
132 lines (113 loc) · 3.26 KB
/
ServerModel.cs
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
using Engine.Api;
using Engine.Api.Server;
using Engine.Helpers;
using Engine.Model.Common;
using Engine.Model.Server.Entities;
using Engine.Network;
using Engine.Plugins.Server;
using System;
using System.Security;
using System.Threading;
namespace Engine.Model.Server
{
[SecurityCritical]
public static class ServerModel
{
#region static model
private static ServerChat _chat;
/// <summary>
/// Серверный API
/// </summary>
public static IApi Api
{
[SecurityCritical]
get;
[SecurityCritical]
private set;
}
/// <summary>
/// Сервер
/// </summary>
public static AsyncServer Server
{
[SecurityCritical]
get;
[SecurityCritical]
private set;
}
/// <summary>
/// Менеджер плагинов.
/// </summary>
public static ServerPluginManager Plugins
{
[SecurityCritical]
get;
[SecurityCritical]
private set;
}
/// <summary>
/// Уведомитель.
/// </summary>
public static IServerNotifier Notifier { [SecurityCritical] get; } = NotifierGenerator.MakeInvoker<IServerNotifier>();
/// <summary>
/// Logger.
/// </summary>
public static Logger Logger { [SecurityCritical] get; } = new Logger("Server.log");
/// <summary>
/// Исользовать только с конструкцией using
/// </summary>
/// <example>using (var server = SeeverModel.Get()) { ... }</example>
/// <returns>Возвращает и блокирует модель.</returns>
[SecurityCritical]
public static ServerGuard Get()
{
if (Interlocked.CompareExchange(ref _chat, null, null) == null)
throw new ArgumentException("model do not inited yet");
return new ServerGuard(_chat);
}
#endregion
#region static methods
public static bool IsInited
{
[SecurityCritical]
get { return Interlocked.CompareExchange(ref _chat, null, null) != null; }
}
[SecurityCritical]
public static void Init(ServerInitializer initializer)
{
if (!initializer.Certificate.HasPrivateKey)
throw new ArgumentException("Initializer should have certificate with private key.");
if (Interlocked.CompareExchange(ref _chat, new ServerChat(), null) != null)
throw new InvalidOperationException("model already inited");
Api = new ServerApi(initializer.AdminPassword);
Server = new AsyncServer(initializer.Certificate, Api, Notifier, Logger);
Plugins = new ServerPluginManager(initializer.PluginsPath);
Plugins.LoadPlugins(initializer.ExcludedPlugins);
}
[SecurityCritical]
public static void Reset()
{
if (Interlocked.Exchange(ref _chat, null) == null)
throw new InvalidOperationException("model not yet inited");
Dispose(Server);
Dispose(Plugins);
Dispose(Api);
Server = null;
Plugins = null;
Api = null;
}
[SecurityCritical]
private static void Dispose(IDisposable disposable)
{
if (disposable != null)
disposable.Dispose();
}
[SecurityCritical]
public static void Check()
{
if (!IsInited)
throw new InvalidOperationException("Server not inited");
}
#endregion
}
}