public bool Dispatch(StreamBuffer buffer)
        {
            var responses = new List <Data>();

            lock (_listResponseAction)
            {
                foreach (var data in _listResponseAction)
                {
                    responses.Add(data);
                }
            }

            foreach (var data in responses)
            {
                if (data.Predicate(buffer) == true)
                {
                    lock (_listResponseAction)
                        _listResponseAction.Remove(data);

                    var result = new IOEventResult(_session, IOEventType.Read,
                                                   buffer.Buffer, 0, buffer.WrittenBytes, AegisResult.Ok);
                    data.Dispatcher(result);
                    return(true);
                }
            }

            return(false);
        }
 private void NetworkConnected(IOEventResult result)
 {
     if (_tcsConnect != null)
     {
         _tcsConnect.SetResult(result.Result == AegisResult.Ok);
         _tcsConnect = null;
     }
 }
        private void NetworkClosed(IOEventResult result)
        {
            lock (_listTCS)
            {
                foreach (TCSData data in _listTCS)
                {
                    data.tcs.SetCanceled();
                }

                _listTCS.Clear();
            }


            if (_tcsConnect != null)
            {
                _tcsConnect.SetException(new AegisException("Connection closed when trying ConnectAndWait()"));
                _tcsConnect = null;
            }
        }