Exemplo n.º 1
0
        private ITikSentence GetOne(string tag)
        {
            do
            {
                ITikSentence result;
                // try to find in in _readSentences
                if (_readSentences.TryDequeue(tag, out result)) // found => removed from _readSentences and return as result
                {
                    return(result);
                }

                lock (_readLockObj)
                {
                    // again - try to find in in _readSentences (could be added between last try and lock)  (see double check lock pattern)
                    if (_readSentences.TryDequeue(tag, out result)) // found => removed from _readSentences and return as result
                    {
                        return(result);
                    }

                    ITikSentence sentenceFromTcp = ReadSentence();
                    if (sentenceFromTcp.Tag == tag)
                    {
                        return(sentenceFromTcp);
                    }
                    else // another tag => add to _readSentences for another reading thread
                    {
                        _readSentences.Enqueue(sentenceFromTcp);
                    }
                }
            } while (true); //TODO max attempts???  //repeat until get any response for specific tag
        }
Exemplo n.º 2
0
        internal bool TryDequeue(string tag, out ITikSentence sentence)
        {
            if (tag == "")
                tag = EMPTY_TAG_KEY;
            lock(_lockObj)
            {
                List<ITikSentence> list;
                if (_sentencesForTags.TryGetValue(tag, out list))
                {
                    if (list.Count > 0)
                    {
                        sentence = list[0];
                        list.RemoveAt(0);

                        if (list.Count <= 0)
                            _sentencesForTags.Remove(tag); //free memory

                        return true;
                    }
                    else
                    {
                        //empty list - should not happen
                        sentence = null;
                        return false;
                    }

                }
                else
                {
                    sentence = null;
                    return false;
                }
            }
        }
Exemplo n.º 3
0
        internal void Enqueue(ITikSentence sentence)
        {
            lock (_lockObj)
            {
                List <ITikSentence> list;
                string sentenceTag = string.IsNullOrWhiteSpace(sentence.Tag) ? EMPTY_TAG_KEY : sentence.Tag;
                if (!_sentencesForTags.TryGetValue(sentenceTag, out list))
                {
                    list = new List <ITikSentence>();
                    _sentencesForTags.Add(sentenceTag, list);
                }

                list.Add(sentence);
            }
        }
Exemplo n.º 4
0
        internal void Enqueue(ITikSentence sentence)
        {
            lock(_lockObj)
            {
                List<ITikSentence> list;
                string sentenceTag = string.IsNullOrWhiteSpace(sentence.Tag) ? EMPTY_TAG_KEY : sentence.Tag;
                if (!_sentencesForTags.TryGetValue(sentenceTag, out list))
                {
                    list = new List<ITikSentence>();
                    _sentencesForTags.Add(sentenceTag, list);
                }

                list.Add(sentence);
            }
        }
Exemplo n.º 5
0
        private ITikSentence GetOne(string tag)
        {
            do
            {
                if (!_tcpConnection.Connected)
                {
                    _isOpened = false;
                }

                if (!_isOpened)
                {
                    return(new ApiTrapSentence(new string[] { "=category=-1", "=message=connection closed" }));
                }

                ITikSentence result;
                // try to find in in _readSentences
                if (_readSentences.TryDequeue(tag, out result)) // found => removed from _readSentences and return as result
                {
                    return(result);
                }

                lock (_readLockObj)
                {
                    // again - try to find in in _readSentences (could be added between last try and lock)  (see double check lock pattern)
                    if (_readSentences.TryDequeue(tag, out result)) // found => removed from _readSentences and return as result
                    {
                        return(result);
                    }

                    ITikSentence sentenceFromTcp = ReadSentence();
                    if (sentenceFromTcp.Tag == tag)
                    {
                        return(sentenceFromTcp);
                    }
                    else // another tag => add to _readSentences for another reading thread
                    {
                        _readSentences.Enqueue(sentenceFromTcp);
                    }
                }
            } while (true); //TODO max attempts???  //repeat until get any response for specific tag
        }
Exemplo n.º 6
0
        internal bool TryDequeue(string tag, out ITikSentence sentence)
        {
            if (tag == "")
            {
                tag = EMPTY_TAG_KEY;
            }
            lock (_lockObj)
            {
                List <ITikSentence> list;
                if (_sentencesForTags.TryGetValue(tag, out list))
                {
                    if (list.Count > 0)
                    {
                        sentence = list[0];
                        list.RemoveAt(0);

                        if (list.Count <= 0)
                        {
                            _sentencesForTags.Remove(tag); //free memory
                        }
                        return(true);
                    }
                    else
                    {
                        //empty list - should not happen
                        sentence = null;
                        return(false);
                    }
                }
                else
                {
                    sentence = null;
                    return(false);
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TikConnectionException"/> class.
 /// </summary>
 /// <param name="message">The exception message.</param>
 /// <param name="command">The command sent to target.</param>
 /// <param name="response">The response from target.</param>
 public TikConnectionException(string message, ITikCommand command, ITikSentence response)
     : this(FormatMessage(message, command, new ITikSentence[] { response }))
 {
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TikConnectionException"/> class.
 /// </summary>
 /// <param name="message">The exception message.</param>
 /// <param name="command">The command sent to target.</param>
 /// <param name="response">The response from target.</param>
 public TikCommandUnexpectedResponseException(string message, ITikCommand command, ITikSentence response)
     : base(command, FormatMessage(message, command, new ITikSentence[] { response }))
 {
 }
Exemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TikConnectionException"/> class.
 /// </summary>
 /// <param name="message">The exception message.</param>
 /// <param name="command">The command sent to target.</param>
 /// <param name="response">The response from target.</param>
 public TikConnectionException(string message, ITikCommand command, ITikSentence response)
     : this(FormatMessage(message, command, new ITikSentence[] { response }))
 {
 }
Exemplo n.º 10
0
 /// <summary>
 /// ctor.
 /// </summary>
 /// <param name="message">Exception message.</param>
 /// <param name="sentecne">Sentence with error - not proper format.</param>
 public TikSentenceException(string message, ITikSentence sentecne)
     : base(message)
 {
     _sentence = sentecne;
 }
Exemplo n.º 11
0
 /// <summary>
 /// ctor.
 /// </summary>
 /// <param name="message">Exception message.</param>
 /// <param name="sentecne">Sentence with error - not proper format.</param>
 public TikSentenceException(string message, ITikSentence sentecne)
     : base(message)
 {
     _sentence = sentecne;
 }