-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.cs
298 lines (266 loc) · 9.22 KB
/
EventDispatcher.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
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
// TS3Client - A free TeamSpeak3 client implementation
// Copyright (C) 2017 TS3Client contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Open Software License v. 3.0
//
// You should have received a copy of the Open Software License along with this
// program. If not, see <https://opensource.org/licenses/OSL-3.0>.
namespace TS3Client
{
using System;
using System.Collections.Concurrent;
using System.Threading;
using Full;
using EvloopType = System.Action<object>;
internal static class EventDispatcherHelper
{
public const string DispatcherTitle = "TS3 Dispatcher";
public const string EventLoopTitle = "TS3 MessageLoop";
public static IEventDispatcher Create(EventDispatchType dispatcherType)
{
IEventDispatcher dispatcher;
switch (dispatcherType)
{
case EventDispatchType.None: dispatcher = new NoEventDispatcher(); break;
case EventDispatchType.CurrentThread: dispatcher = new CurrentThreadEventDisptcher(); break;
case EventDispatchType.ExtraDispatchThread: dispatcher = new ExtraThreadEventDispatcher(); break;
case EventDispatchType.DoubleThread: dispatcher = new DoubleThreadEventDispatcher(); break;
case EventDispatchType.AutoThreadPooled: dispatcher = new AutoThreadPooledEventDispatcher(); break;
case EventDispatchType.NewThreadEach: dispatcher = new NewThreadEachEventDispatcher(); break;
default: throw new NotSupportedException();
}
return dispatcher;
}
}
/// <summary> Provides a function to run a receiving loop and asynchronously
/// dispatch notifications.
/// </summary>
internal interface IEventDispatcher : IDisposable
{
/// <summary>Initializes the dispatcher.</summary>
/// <param name="eventLoop">The main loop which will be receiving packets.</param>
/// <param name="dispatcher">The method to call asynchronously when a new
/// notification comes in.</param>
void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx);
/// <summary>Dispatches the notification.</summary>
/// <param name="lazyNotification"></param>
void Invoke(LazyNotification lazyNotification);
/// <summary>Starts the eventLoop synchronously or asynchronous,
/// depending on the dispatcher type.
/// </summary>
void EnterEventLoop();
void DoWork();
}
internal sealed class CurrentThreadEventDisptcher : IEventDispatcher
{
private EvloopType eventLoop;
private ConnectionContext ctx;
private Action<LazyNotification> dispatcher;
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx)
{
this.eventLoop = eventLoop;
this.dispatcher = dispatcher;
this.ctx = ctx;
}
public void EnterEventLoop() => eventLoop(ctx);
public void Invoke(LazyNotification lazyNotification) => dispatcher.Invoke(lazyNotification);
public void DoWork() { }
public void Dispose() { }
}
internal sealed class ExtraThreadEventDispatcher : IEventDispatcher
{
private EvloopType eventLoop;
private ConnectionContext ctx;
private Action<LazyNotification> dispatcher;
private Thread dispatchThread;
private readonly ConcurrentQueue<LazyNotification> eventQueue = new ConcurrentQueue<LazyNotification>();
private readonly AutoResetEvent eventBlock = new AutoResetEvent(false);
private volatile bool run;
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx)
{
run = true;
this.eventLoop = eventLoop;
this.dispatcher = dispatcher;
this.ctx = ctx;
}
public void Invoke(LazyNotification lazyNotification)
{
eventQueue.Enqueue(lazyNotification);
eventBlock.Set();
}
public void EnterEventLoop()
{
dispatchThread = new Thread(DispatchLoop) { Name = EventDispatcherHelper.DispatcherTitle };
dispatchThread.Start();
eventLoop(ctx);
}
private void DispatchLoop()
{
while (run)
{
eventBlock.WaitOne();
while (!eventQueue.IsEmpty)
{
if (eventQueue.TryDequeue(out var lazyNotification))
dispatcher.Invoke(lazyNotification);
}
}
}
public void DoWork()
{
if (Thread.CurrentThread.ManagedThreadId != dispatchThread.ManagedThreadId)
return;
if (eventQueue.TryDequeue(out var lazyNotification))
dispatcher.Invoke(lazyNotification);
}
public void Dispose()
{
run = false;
eventBlock.Set();
}
}
internal sealed class DoubleThreadEventDispatcher : IEventDispatcher
{
private EvloopType eventLoop;
private ConnectionContext ctx;
private Action<LazyNotification> dispatcher;
private Thread eventLoopThread;
private Thread dispatchThread;
private readonly ConcurrentQueue<LazyNotification> eventQueue = new ConcurrentQueue<LazyNotification>();
private readonly AutoResetEvent eventBlock = new AutoResetEvent(false);
private volatile bool run;
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx)
{
run = true;
this.eventLoop = eventLoop;
this.dispatcher = dispatcher;
this.ctx = ctx;
}
public void Invoke(LazyNotification lazyNotification)
{
eventQueue.Enqueue(lazyNotification);
eventBlock.Set();
}
public void EnterEventLoop()
{
dispatchThread = new Thread(DispatchLoop) { Name = EventDispatcherHelper.DispatcherTitle };
dispatchThread.Start();
eventLoopThread = new Thread(() => eventLoop.Invoke(ctx)) { Name = EventDispatcherHelper.EventLoopTitle };
eventLoopThread.Start();
}
private void DispatchLoop()
{
while (run)
{
eventBlock.WaitOne();
while (!eventQueue.IsEmpty)
{
if (eventQueue.TryDequeue(out var lazyNotification))
dispatcher.Invoke(lazyNotification);
}
}
}
public void DoWork()
{
if (Thread.CurrentThread.ManagedThreadId != dispatchThread.ManagedThreadId)
return;
if (eventQueue.TryDequeue(out var lazyNotification))
dispatcher.Invoke(lazyNotification);
}
public void Dispose()
{
run = false;
eventBlock.Set();
}
}
internal sealed class NoEventDispatcher : IEventDispatcher
{
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx) { }
public void EnterEventLoop() { }
public void Invoke(LazyNotification lazyNotification) { }
public void DoWork() { }
public void Dispose() { }
}
internal sealed class AutoThreadPooledEventDispatcher : IEventDispatcher
{
private ConnectionContext ctx;
private EvloopType eventLoop;
private Action<LazyNotification> dispatcher;
private Thread eventLoopThread;
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx)
{
this.eventLoop = eventLoop;
this.dispatcher = dispatcher;
this.ctx = ctx;
}
public void EnterEventLoop()
{
eventLoopThread = new Thread(() => eventLoop.Invoke(ctx)) { Name = EventDispatcherHelper.EventLoopTitle };
eventLoopThread.Start();
}
public void Invoke(LazyNotification lazyNotification) => ThreadPool.QueueUserWorkItem(Call, lazyNotification);
private void Call(object obj) => dispatcher.Invoke((LazyNotification)obj);
public void DoWork() { }
public void Dispose() { }
}
internal sealed class NewThreadEachEventDispatcher : IEventDispatcher
{
private ConnectionContext ctx;
private EvloopType eventLoop;
private Action<LazyNotification> dispatcher;
private Thread eventLoopThread;
public void Init(EvloopType eventLoop, Action<LazyNotification> dispatcher, ConnectionContext ctx)
{
this.eventLoop = eventLoop;
this.dispatcher = dispatcher;
this.ctx = ctx;
}
public void EnterEventLoop()
{
eventLoopThread = new Thread(() => eventLoop.Invoke(ctx)) { Name = EventDispatcherHelper.EventLoopTitle };
eventLoopThread.Start();
}
public void Invoke(LazyNotification lazyNotification)
{
eventLoopThread = new Thread(() => dispatcher.Invoke(lazyNotification)) { Name = "TS3 MessageDispatcher" };
eventLoopThread.Start();
}
public void DoWork() { }
public void Dispose() { }
}
public enum EventDispatchType
{
/// <summary>
/// All events will be dropped.
/// </summary>
None,
/// <summary>
/// Will use the same thread that entered the <see cref="Ts3BaseFunctions.Connect"/>
/// for receiving and invoking all events. This method is not recommended since it mostly
/// only produces deadlocks. (Usually only for debugging)
/// </summary>
CurrentThread,
/// <summary>
/// Will use the thread that entered the <see cref="Ts3BaseFunctions.Connect"/> for
/// receiving and starts a second thread for invoking all events. This is the best method for
/// lightweight dipatching with no parallelization.
/// </summary>
ExtraDispatchThread,
/// <summary>
/// Will start one thread for receiving and a second thread for invoking all events.
/// This is the best method for lightweight asynchronous dipatching with no parallelization.
/// </summary>
DoubleThread,
/// <summary>
/// This method will use the <see cref="ThreadPool"/> from .NET to dispatch all events.
/// This is the best method for high parallelization with low overhead when using many instances.
/// </summary>
AutoThreadPooled,
/// <summary>
/// This method will create a new Thread for each event. This method is not recommended
/// due to high overhead and resource consumption. Only try it when all else fails.
/// </summary>
NewThreadEach,
}
}