/// <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); Idle.Restart(); //You've done something, so you're technically no longer idle. return(await d.Task.ConfigureAwait(false)); }
private async Task CheckQueue() { if (_queue.Count > 0) { _currentCommand = _queue.Dequeue(); Trace.WriteLine(_currentCommand.SentText); await _writer.WriteLineAsync(_currentCommand.SentText).ConfigureAwait(false); await _writer.FlushAsync().ConfigureAwait(false); } }
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); } }
/// <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); }