示例#1
0
        public MpdMessage(IMpcCommand <T> command, bool connected, IReadOnlyCollection <string> response)
        {
            this.Request = new MpdRequest <T>(command);

            var endLine = response.Skip(response.Count - 1).Single();

            this.rawResponse = response.Take(response.Count - 1).ToList();

            var values = this.Request.Command.Deserialize(this.GetValuesFromResponse());

            this.Response = new MpdResponse <T>(endLine, values, connected);
        }
示例#2
0
        public async Task <IMpdMessage <T> > SendAsync <T>(IMpcCommand <T> command)
        {
            await _semaphore.WaitAsync();

            try
            {
                return(await _connection.SendAsync(command));
            }
            finally
            {
                _semaphore.Release();
            }
        }
示例#3
0
        public async Task <IMpdMessage <T> > SendAsync <T>(IMpcCommand <T> command)
        {
            command.CheckNotNull();

            var connected = await CheckConnectionAsync();

            string[] response;

            try
            {
                _writer.WriteLine(command.Value);
                _writer.Flush();

                response = await ReadResponseAsync();
            }
            catch (Exception)
            {
                try { await DisconnectAsync(); } catch (Exception) { }
                return(null); // TODO: Create Null Object for MpdResponse
            }

            return(new MpdMessage <T>(command, connected, response));
        }
示例#4
0
        private static async Task SendCommand <T>(IMpcCommand <T> command)
        {
            var response = await Mpc.SendAsync(command);

            TestOutput.WriteLine(response);
        }
示例#5
0
        /// <summary>
        /// Sends the command asynchronously.
        /// </summary>
        /// <typeparam name="TResponse">The response type.</typeparam>
        /// <param name="mpcCommand">The MPC command.</param>
        /// <returns>
        /// The send task.
        /// </returns>
        public async Task <IMpdMessage <TResponse> > SendAsync <TResponse>(IMpcCommand <TResponse> mpcCommand)
        {
            if (this.tcpClient == null)
            {
                await this.ReconnectAsync(true);
            }

            if (mpcCommand == null)
            {
                throw new CommandNullException();
            }

            Exception lastException         = null;
            IReadOnlyList <string> response = new List <string>();
            var sendAttempter = new Attempter(3);
            var commandText   = mpcCommand.Serialize();

            this.mpcConnectionReporter?.Sending(commandText);
            while (sendAttempter.Attempt())
            {
                try
                {
                    using (var writer = new StreamWriter(this.networkStream, Encoding, 512, true)
                    {
                        NewLine = "\n"
                    })
                    {
                        await writer.WriteLineAsync(commandText);

                        await writer.FlushAsync();
                    }

                    response = await this.ReadResponseAsync();

                    if (response.Any())
                    {
                        lastException = null;
                        break;
                    }

                    throw new EmptyResponseException(commandText);
                }
                catch (Exception exception)
                {
                    lastException = exception;
                    this.mpcConnectionReporter?.SendException(commandText, sendAttempter.CurrentAttempt, exception);
                    await this.ReconnectAsync(true);

                    this.mpcConnectionReporter?.RetrySend(commandText, sendAttempter.CurrentAttempt);
                }
            }

            if (lastException != null)
            {
                try
                {
                    await this.DisconnectAsync(false);
                }
                catch (Exception)
                {
                }

                return(new ErrorMpdMessage <TResponse>(mpcCommand, new ErrorMpdResponse <TResponse>(lastException)));
            }

            return(new MpdMessage <TResponse>(mpcCommand, true, response));
        }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MpdRequest{T}"/> class.
 /// </summary>
 /// <param name="command">The command.</param>
 public MpdRequest(IMpcCommand <TContent> command)
 {
     this.Command = command;
 }
示例#7
0
 public ErrorMpdMessage(IMpcCommand <T> command, ErrorMpdResponse <T> errorResponse)
 {
     this.Request  = new MpdRequest <T>(command);
     this.Response = errorResponse;
 }
示例#8
0
 public MpdRequest(IMpcCommand <T> command)
 {
     Command = command;
 }