-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
QueryClient.cs
413 lines (348 loc) · 16.6 KB
/
QueryClient.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace TeamSpeak3QueryApi.Net
{
/// <summary>Represents a client that can be used to access the TeamSpeak Query API on a remote server.</summary>
public class QueryClient : IDisposable
{
/// <summary>Gets the remote host of the Query API client.</summary>
/// <returns>The remote host of the Query API client.</returns>
public string Host { get; }
/// <summary>Gets the remote port of the Query API client.</summary>
/// <returns>The remote port of the Query API client.</returns>
public int Port { get; }
public bool IsConnected { get; private set; }
/// <summary>The default host which is used when no host is provided.</summary>
public const string DefaultHost = "localhost";
/// <summary>The default port which is used when no port is provided.</summary>
public const short DefaultPort = 10011;
public TcpClient Client { get; }
private StreamReader _reader;
private StreamWriter _writer;
private NetworkStream _ns;
private volatile bool _cancelTask;
private readonly Queue<QueryCommand> _queue = new Queue<QueryCommand>();
private readonly ConcurrentDictionary<string, List<Action<NotificationData>>> _subscriptions = new ConcurrentDictionary<string, List<Action<NotificationData>>>();
#region Ctors
/// <summary>Creates a new instance of <see cref="TeamSpeak3QueryApi.Net.QueryClient"/> using the <see cref="QueryClient.DefaultHost"/> and <see cref="QueryClient.DefaultPort"/>.</summary>
public QueryClient()
: this(DefaultHost, DefaultPort)
{ }
/// <summary>Creates a new instance of <see cref="TeamSpeak3QueryApi.Net.QueryClient"/> using the provided host and the <see cref="QueryClient.DefaultPort"/>.</summary>
/// <param name="hostName">The host name of the remote server.</param>
public QueryClient(string hostName)
: this(hostName, DefaultPort)
{ }
/// <summary>Creates a new instance of <see cref="TeamSpeak3QueryApi.Net.QueryClient"/> using the provided host TCP port.</summary>
/// <param name="hostName">The host name of the remote server.</param>
/// <param name="port">The TCP port of the Query API server.</param>
public QueryClient(string hostName, int port)
{
if (string.IsNullOrWhiteSpace(hostName))
throw new ArgumentNullException(nameof(hostName));
if (!ValidationHelper.ValidateTcpPort(port))
throw new ArgumentOutOfRangeException(nameof(port));
Host = hostName;
Port = port;
IsConnected = false;
Client = new TcpClient();
}
#endregion
/// <summary>Connects to the Query API server.</summary>
/// <returns>An awaitable <see cref="Task"/>.</returns>
public async Task Connect()
{
await Client.ConnectAsync(Host, Port).ConfigureAwait(false);
if (!Client.Connected)
throw new InvalidOperationException("Could not connect.");
_ns = Client.GetStream();
_reader = new StreamReader(_ns);
_writer = new StreamWriter(_ns) { NewLine = "\n" };
IsConnected = true;
await _reader.ReadLineAsync().ConfigureAwait(false);
await _reader.ReadLineAsync().ConfigureAwait(false); // Ignore welcome message
await _reader.ReadLineAsync().ConfigureAwait(false);
_cancelTask = false;
ResponseProcessingLoop();
}
#region Send
/// <summary>Sends a Query API command wihtout parameters to the server.</summary>
/// <param name="cmd">The command.</param>
/// <returns>An awaitable <see cref="T:System.Net.Threading.Tasks.Task{QueryResponseDictionary[]}"/>.</returns>
public Task<QueryResponseDictionary[]> Send(string cmd) => Send(cmd, null);
/// <summary>Sends a Query API command with parameters to the server.</summary>
/// <param name="cmd">The command.</param>
/// <param name="parameters">The parameters of the command.</param>
/// <returns>An awaitable <see cref="T:System.Net.Threading.Tasks.Task{QueryResponseDictionary[]}"/>.</returns>
public Task<QueryResponseDictionary[]> Send(string cmd, params Parameter[] parameters) => Send(cmd, parameters, null);
/// <summary>Sends a Query API command with parameters and options to the server.</summary>
/// <param name="cmd">The command.</param>
/// <param name="parameters">The parameters of the command.</param>
/// <param name="options">The options of the command.</param>
/// <returns>An awaitable <see cref="T:System.Net.Threading.Tasks.Task{QueryResponseDictionary[]}"/>.</returns>
public async Task<QueryResponseDictionary[]> Send(string cmd, Parameter[] parameters, string[] options)
{
if (string.IsNullOrWhiteSpace(cmd))
throw new ArgumentNullException(nameof(cmd)); //return Task.Run( () => throw new ArgumentNullException("cmd"));
options = options ?? new string[0];
var ps = parameters == null ? new List<Parameter>() : new List<Parameter>(parameters);
var toSend = new StringBuilder(cmd.TeamSpeakEscape());
for (int i = 0; i < options.Length; ++i)
toSend.Append(" -").Append(options[i].ToLowerInvariant().TeamSpeakEscape());
// Parameter arrays should be the last parameters in the list
var lastParamArray = ps.SingleOrDefault(p => p.Value is ParameterValueArray);
if (lastParamArray != null)
{
ps.MoveToBottom(lastParamArray);
}
foreach (var p in ps)
toSend.Append(' ').Append(p.GetEscapedRepresentation());
var d = new TaskCompletionSource<QueryResponseDictionary[]>();
var newItem = new QueryCommand(cmd, ps.AsReadOnly(), options, d, toSend.ToString());
_queue.Enqueue(newItem);
await CheckQueue().ConfigureAwait(false);
return await d.Task.ConfigureAwait(false);
}
#endregion
#region Subscriptions
/// <summary>Subscribes to a notification. If the subscribed notification is received, the callback is getting executed.</summary>
/// <param name="notificationName">The name of the notification (without the "notify" prefix).</param>
/// <param name="callback">The callback to execute on occurrence.</param>
public void Subscribe(string notificationName, Action<NotificationData> callback)
{
if (callback == null)
throw new ArgumentNullException(nameof(callback));
if (string.IsNullOrWhiteSpace(notificationName))
throw new ArgumentNullException(nameof(notificationName));
notificationName = NormalizeNotificationName(notificationName);
if (_subscriptions.ContainsKey(notificationName))
{
_subscriptions[notificationName].Add(callback);
}
else
{
_subscriptions[notificationName] = new List<Action<NotificationData>> { callback };
}
}
/// <summary>Unsubscribes all callbacks of a notification.</summary>
/// <param name="notificationName">The name of the notification to unsubscribe (without the "notify" prefix).</param>
public void Unsubscribe(string notificationName)
{
if (string.IsNullOrWhiteSpace(notificationName))
throw new ArgumentNullException(nameof(notificationName));
notificationName = NormalizeNotificationName(notificationName);
if (!_subscriptions.ContainsKey(notificationName))
return;
_subscriptions[notificationName].Clear();
_subscriptions[notificationName] = null;
_subscriptions.TryRemove(notificationName, out var _); // TODO: Revisit this
}
/// <summary>Unsubscribe a callback of a notification.</summary>
/// <param name="notificationName">The name of the notification to unsubscribe (without the "notify" prefix).</param>
/// <param name="callback">The callback to unsubscribe.</param>
public void Unsubscribe(string notificationName, Action<NotificationData> callback)
{
if (callback == null)
throw new ArgumentNullException(nameof(callback));
if (string.IsNullOrWhiteSpace(notificationName))
throw new ArgumentNullException(nameof(notificationName));
notificationName = NormalizeNotificationName(notificationName);
if (!_subscriptions.ContainsKey(notificationName))
return;
_subscriptions[notificationName].Remove(callback);
}
private static string NormalizeNotificationName(string name) => name.Trim().ToUpperInvariant();
#endregion
#region Parsing
private static QueryResponseDictionary[] ParseResponse(string rawResponse)
{
var records = rawResponse.Split('|');
var response = records.Select(s =>
{
var args = s.Split(' ');
var r = new QueryResponseDictionary();
foreach (var arg in args)
{
if (arg.Contains('='))
{
var eqIndex = arg.IndexOf('=');
var key = arg.Substring(0, eqIndex).TeamSpeakUnescape();
var value = arg.Remove(0, eqIndex + 1);
if (int.TryParse(value, out var intVal))
r[key] = intVal;
else
r[key] = value;
}
else
{
r[arg] = null;
}
}
return r;
});
return response.ToArray();
}
private static QueryError ParseError(string errorString)
{
// Ex:
// error id=2568 msg=insufficient\sclient\spermissions failed_permid=27
if (errorString == null)
throw new ArgumentNullException(nameof(errorString));
errorString = errorString.Remove(0, "error ".Length);
var errParams = errorString.Split(' ');
/*
id=2568
msg=insufficient\sclient\spermissions
failed_permid=27
*/
var parsedError = new QueryError { FailedPermissionId = -1 };
for (int i = 0; i < errParams.Length; ++i)
{
var errData = errParams[i].Split('=');
/*
id
2568
*/
var fieldName = errData[0].ToUpperInvariant();
switch (fieldName)
{
case "ID":
parsedError.Id = errData.Length > 1 ? int.Parse(errData[1], NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.CurrentCulture) : -1;
continue;
case "MSG":
parsedError.Message = errData.Length > 1 ? errData[1].TeamSpeakUnescape() : null;
continue;
case "FAILED_PERMID":
parsedError.FailedPermissionId = errData.Length > 1 ? int.Parse(errData[1], NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.CurrentCulture) : -1;
continue;
default:
throw new QueryProtocolException();
}
}
return parsedError;
}
private static QueryNotification ParseNotification(string notificationString)
{
Debug.Assert(!string.IsNullOrWhiteSpace(notificationString));
var index = notificationString.IndexOf(' ');
Debug.Assert(index > -1);
var notificationName = notificationString.Remove(index);
Debug.Assert(!string.IsNullOrWhiteSpace(notificationName));
var payload = notificationString.Substring(notificationName.Length + 1);
var qRes = ParseResponse(payload); // Not tested
var notData = new NotificationData(qRes);
return new QueryNotification(notificationName, notData);
}
private static void InvokeResponse(QueryCommand forCommand)
{
Debug.Assert(forCommand != null);
Debug.Assert(forCommand.Defer != null);
Debug.Assert(forCommand.Error != null);
if (forCommand.Error.Id != 0)
{
forCommand.Defer.TrySetException(new QueryException(forCommand.Error));
}
else
{
forCommand.Defer.TrySetResult(forCommand.ResponseDictionary);
}
}
#endregion
#region Invocation
private void InvokeNotification(QueryNotification notification)
{
Debug.Assert(notification != null);
Debug.Assert(notification.Name != null);
Debug.Assert(!string.IsNullOrWhiteSpace(notification.Name));
if (_subscriptions.Count == 0)
return; // going short here
var notName = NormalizeNotificationName(notification.Name);
if (_subscriptions.ContainsKey(notName))
{
var cbs = _subscriptions[notName];
for (int i = 0; i < cbs.Count; ++i)
{
cbs[i]?.Invoke(notification.Data);
}
}
}
private void ResponseProcessingLoop()
{
Task.Run(async () =>
{
while (!_cancelTask)
{
var line = await _reader.ReadLineAsync().ConfigureAwait(false);
Debug.WriteLine(line);
if (string.IsNullOrWhiteSpace(line))
continue;
var s = line.Trim();
if (s.StartsWith("error", StringComparison.OrdinalIgnoreCase))
{
Debug.Assert(_currentCommand != null);
var error = ParseError(s);
_currentCommand.Error = error;
InvokeResponse(_currentCommand);
}
else if (s.StartsWith("notify", StringComparison.OrdinalIgnoreCase))
{
s = s.Remove(0, "notify".Length);
var not = ParseNotification(s);
InvokeNotification(not);
}
else
{
Debug.Assert(_currentCommand != null);
_currentCommand.RawResponse = s;
_currentCommand.ResponseDictionary = ParseResponse(s);
}
}
});
}
private QueryCommand _currentCommand;
private async Task CheckQueue()
{
if (_queue.Count > 0)
{
_currentCommand = _queue.Dequeue();
Debug.WriteLine(_currentCommand.SentText);
await _writer.WriteLineAsync(_currentCommand.SentText).ConfigureAwait(false);
await _writer.FlushAsync().ConfigureAwait(false);
}
}
#endregion
#region IDisposable support
/// <summary>Finalizes the object.</summary>
~QueryClient()
{
Dispose(false);
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
/// <param name="disposing">A value indicating whether the object is disposing or finalizing.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Client?.Dispose();
_ns?.Dispose();
_reader?.Dispose();
_writer?.Dispose();
}
}
#endregion
}
}