Пример #1
0
        private void ReceiveTokens(params ITokenHandler[] handlers)
        {
            Logger.Instance?.WriteLine();
            Logger.Instance?.WriteLine("---------- Receive Tokens ----------");
            try
            {
#if ENABLE_ARRAY_POOL
                using (var tokenStream = new TokenReceiveStream(_networkStream, _environment, _arrayPool))
#else
                using (var tokenStream = new TokenReceiveStream(_networkStream, _environment))
#endif
                {
                    foreach (var receivedToken in _reader.Read(tokenStream, _environment))
                    {
                        foreach (var handler in handlers)
                        {
                            if (handler != null && handler.CanHandle(receivedToken.Type))
                            {
                                handler.Handle(receivedToken);
                            }
                        }
                    }
                }
            }
            finally
            {
                LastActive = DateTime.UtcNow;
            }
        }
Пример #2
0
        public IEnumerable <IToken> Read(TokenReceiveStream stream, DbEnvironment env)
        {
            IFormatToken previousFormatToken = null;

            while (stream.DataAvailable)
            {
                var rawTokenType = (byte)stream.ReadByte();
                var tokenType    = (TokenType)rawTokenType;

                if (Readers.ContainsKey(tokenType))
                {
                    var t = Readers[tokenType](stream, env, previousFormatToken);

                    if (t is IFormatToken token)
                    {
                        Logger.Instance?.WriteLine($"**Set new format token**");
                        previousFormatToken = token;
                    }

                    yield return(t);
                }
                else
                {
                    Logger.Instance?.WriteLine($"!!! Hit unknown token type {tokenType} !!!");
                    var t = new CatchAllToken(rawTokenType);
                    t.Read(stream, env, previousFormatToken);
                    yield return(t);
                }

                if (stream.IsCancelled)
                {
                    Logger.Instance?.WriteLine($"{nameof(TokenReceiveStream)} - received cancel status flag");

                    yield return
                        (new DoneToken
                    {
                        Count = 0,
                        Status = DoneToken.DoneStatus.TDS_DONE_ATTN
                    });
                }
            }
        }
Пример #3
0
        private void InternalExecuteQueryAsync(AseCommand command, AseTransaction transaction, TaskCompletionSource <DbDataReader> readerSource, CommandBehavior behavior)
        {
            AssertExecutionStart();
#if ENABLE_SYSTEM_DATA_COMMON_EXTENSIONS
            var dataReader = new AseDataReader(command, behavior, EventNotifier);
#else
            var dataReader = new AseDataReader(behavior, EventNotifier);
#endif
            try
            {
                SendPacket(new NormalPacket(BuildCommandTokens(command, behavior)));

                var envChangeTokenHandler         = new EnvChangeTokenHandler(_environment, _parameters.Charset);
                var doneHandler                   = new DoneTokenHandler();
                var dataReaderHandler             = new StreamingDataReaderTokenHandler(readerSource, dataReader, EventNotifier);
                var responseParameterTokenHandler = new ResponseParameterTokenHandler(command.AseParameters);

                Logger.Instance?.WriteLine();
                Logger.Instance?.WriteLine("---------- Receive Tokens ----------");
                try
                {
#if ENABLE_ARRAY_POOL
                    using (var tokenStream = new TokenReceiveStream(_networkStream, _environment, _arrayPool))
#else
                    using (var tokenStream = new TokenReceiveStream(_networkStream, _environment))
#endif
                    {
                        foreach (var receivedToken in _reader.Read(tokenStream, _environment))
                        {
                            if (envChangeTokenHandler.CanHandle(receivedToken.Type))
                            {
                                envChangeTokenHandler.Handle(receivedToken);
                            }

                            if (doneHandler.CanHandle(receivedToken.Type))
                            {
                                doneHandler.Handle(receivedToken);
                            }

                            if (dataReaderHandler.CanHandle(receivedToken.Type))
                            {
                                dataReaderHandler.Handle(receivedToken);
                            }

                            if (responseParameterTokenHandler.CanHandle(receivedToken.Type))
                            {
                                responseParameterTokenHandler.Handle(receivedToken);
                            }
                        }
                    }
                }
                finally
                {
                    LastActive = DateTime.UtcNow;
                }

                // This tells the data reader to stop waiting for more results.
                dataReader.CompleteAdding();

                AssertExecutionCompletion(doneHandler);

                if (transaction != null && doneHandler.TransactionState == TranState.TDS_TRAN_ABORT)
                {
                    transaction.MarkAborted();
                }

                dataReaderHandler.AssertNoErrors();

                if (doneHandler.Canceled)
                {
                    readerSource.TrySetCanceled(); // If we have already begun returning data, then this will get lost.
                }
                else
                {
                    readerSource.TrySetResult(dataReader); // Catchall - covers cases where no data is returned by the server.
                }
            }
            catch (Exception ex)
            {
                // If we have already begun returning data, then this will get lost.
                if (!readerSource.TrySetException(ex))
                {
                    throw;
                }
            }
        }