예제 #1
0
        /// <summary>
        /// public method to watch the reader and notify when new messages are available
        /// </summary>
        async Task Pump()
        {
            _log.LogInformation("LDAP pump started");
            // Need something to track if we should abandon existing tasks...etc.

            while (!_isClosed)
            {
                //if (_raw.DataAvailable)
                try
                {
                    // Ensure we can safely read the contents
                    if (await _reader.ReadAsync())
                    {
                        // read the next operation available
                        var message = await _reader.ReadResponseAsync();

                        if (message != null)
                        {
                            _log.LogInformation("Message received {0}", message.Operation);
                            if (_size == 0)     // new
                            {
                                _head.Message = message;
                                _tail         = _head;
                                _size         = 1;
                            }
                            else if (_size >= 16)     // shouldn't hardcode the 16
                            {
                                _head      = _head.Next;
                                _tail.Next = new MessagePumpNode {
                                    Message = message, Next = null
                                };
                                _tail = _tail.Next;
                            }
                            else
                            {
                                _tail.Next = new MessagePumpNode {
                                    Message = message, Next = null
                                };
                                _tail = _tail.Next;
                                _size++;
                            }

                            _event.Set();
                        }
                    }
                }
                catch (LDAPException ldapEx)
                {
                    _log.LogError(ldapEx.ToString());
                }
                catch (Exception ex)
                {
                    _log.LogError(ex.ToString());
                }
                // else
                // {
                //     await Task.Delay(250);
                // }
            }
        }
예제 #2
0
 public MessagePumpEnumerator(MessagePumpNode node, EventWaitHandle evt, int messageId, CancellationToken token)
 {
     _node      = node;
     _event     = evt;
     _messageId = messageId;
     _token     = token;
 }
예제 #3
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="reader">The reader to pump messages from</param>
 /// <param name="raw">The raw stream backing the reader</param>
 /// <param name="log"></param>
 public MessagePump(LDAPReader reader, NetworkStream raw, ILogger log)
 {
     _reader = reader;
     _raw    = raw;
     _head   = new MessagePumpNode();
     _tail   = null;
     _event  = new AutoResetEvent(false);
     _log    = log;
 }
예제 #4
0
        public bool MoveNext()
        {
            // No messages yet, keep reading until we find one
            if (Current != null && Current.IsTerminating)
            {
                return(false);
            }

            // Keep going until we find another message
            while (true)
            {
                if (_token.IsCancellationRequested)
                {
                    return(false);
                }

                if (_node.Message == null)
                {
                    _event.WaitOne(1000);
                }

                if (_node.Message != Current && _node.Message.MessageId == _messageId)
                {
                    Current = _node.Message;
                    return(true);
                }

                if (_node.Next != null)
                {
                    _node = _node.Next;
                }
                else
                {
                    _event.WaitOne(1000);
                }
            }
        }