Esempio n. 1
0
        /// <summary>
        /// Worker method for sending commands
        /// </summary>
        private async void CommandWorker()
        {
            while (!_commandCancellationToken.IsCancellationRequested)
            {
                CurrentCommand = null;

                if (_queueSize == 0)
                {
                    _queueEmptyEvent.Set();
                }

                try
                {
                    if (_commandQueue.TryTake(out CurrentCommand, 10, _commandCancellationToken.Token))
                    {
                        _queueSize--;

                        Logger?.WriteLine("Writing Command: '" + CurrentCommand.CommandText.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);

                        if (Connection.IsAsync)
                        {
                            await Connection.WriteAsync(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
                        }
                        else
                        {
                            Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
                        }

                        //wait for command to finish
                        _commandFinishedEvent.WaitOne();
                    }
                }
                catch (OperationCanceledException) { /*ignore, because it is thrown when the cancellation token is canceled*/ }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Worker method for sending commands
        /// </summary>
        private async void CommandWorker()
        {
            while (!_commandCancellationToken.IsCancellationRequested)
            {
                try
                {
                    CurrentCommand = null;

                    if (_queueSize == 0)
                    {
                        _queueEmptyEvent.Set();
                    }

                    try
                    {
                        if (_commandQueue.TryTake(out CurrentCommand, 10, _commandCancellationToken.Token))
                        {
                            Interlocked.Decrement(ref _queueSize);

                            Logger?.WriteLine("Writing Command: '" + CurrentCommand.CommandText.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);

                            if (Connection.IsAsync)
                            {
                                await Connection.WriteAsync(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
                            }
                            else
                            {
                                // ReSharper disable once MethodHasAsyncOverload
                                Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
                            }

                            //wait for command to finish
                            if (CurrentCommand.WaitForResponse)
                            {
                                if (CurrentCommand.Timeout.HasValue)
                                {
                                    _commandFinishedEvent.WaitOne(CurrentCommand.Timeout.Value);
                                }
                                else
                                {
                                    _commandFinishedEvent.WaitOne();
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        /*ignore, because it is thrown when the cancellation token is canceled*/
                    }
                }
                catch (Exception ex)
                {
                    SocketError?.Invoke(this, new SocketErrorEventArgs(ex));
                    Connection.Dispose();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Sends the command.
        /// </summary>
        /// <param name="command">command string</param>
        /// <exception cref="System.InvalidOperationException">Not connected</exception>
        protected virtual CommandResult SendCommand(string command)
        {
            if (!Connection.IsOpen)
            {
                throw new InvalidOperationException("Not connected");
            }

            command = PrepareCommand(command);
            Logger?.WriteLine("Queuing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);

            QueuedCommand cmd = new QueuedCommand(command);

            _queueEmptyEvent.Reset();
            _queueSize++;
            _commandQueue.Add(cmd);

            return(cmd.CommandResult);
        }
Esempio n. 4
0
        /// <summary>
        /// Sends the command.
        /// </summary>
        /// <param name="command">command string</param>
        /// <param name="waitForResponse"></param>
        /// <exception cref="System.InvalidOperationException">Not connected</exception>
        public virtual CommandResult SendCommand(string command, bool waitForResponse = true)
        {
            if (!Connection.IsOpen)
            {
                throw new InvalidOperationException("Not connected");
            }

            command = PrepareCommand(command);
            Logger?.WriteLine("Queuing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);

            QueuedCommand cmd = new QueuedCommand(command, CommandTimeout)
            {
                WaitForResponse = waitForResponse
            };

            _queueEmptyEvent.Reset();
            Interlocked.Increment(ref _queueSize);
            _commandQueue.Add(cmd);

            return(cmd.CommandResult);
        }