private void SendQuery()
        {
            foreach (var queryInfo in _pendingQueries.GetConsumingEnumerable())
            {
                byte streamId = AcquireNextStreamId();

                if (_isClosed == 1)
                {
                    // abort queries.
                    var ex = new OperationCanceledException();
                    queryInfo.NotifyError(ex);
                    _instrumentation.ClientTrace(queryInfo.Token, EventType.Cancellation);
                    throw ex;
                }

                try
                {
                    InstrumentationToken token = queryInfo.Token;
                    bool tracing = 0 != (token.ExecutionFlags & ExecutionFlags.ServerTracing);
                    using (var bufferingFrameWriter = new BufferingFrameWriter(tracing))
                    {
                        queryInfo.Write(bufferingFrameWriter);

                        _logger.Debug("Starting writing frame for stream {0}@{1}", streamId, Endpoint);
                        _instrumentation.ClientTrace(token, EventType.BeginWrite);

                        _queryInfos[streamId] = queryInfo;
                        bufferingFrameWriter.SendFrame(streamId, _socket);

                        _logger.Debug("Done writing frame for stream {0}@{1}", streamId, Endpoint);
                        _instrumentation.ClientTrace(token, EventType.EndWrite);
                    }
                }
                catch (Exception ex)
                {
                    queryInfo.NotifyError(ex);

                    if (IsStreamInBadState(ex))
                    {
                        throw;
                    }
                }
            }
        }
        private void WriteNextFrame(Action<IFrameWriter> writer, byte streamId)
        {
            try
            {
                InstrumentationToken token = _queryInfos[streamId].InstrumentationToken;
                _instrumentation.ClientTrace(token, EventType.BeginWrite);

                // acquire the global lock to write the request
                _logger.Debug("Starting writing frame for stream {0}@{1}", streamId, Endpoint);
                bool tracing = 0 != (token.ExecutionFlags & ExecutionFlags.ServerTracing);
                using (BufferingFrameWriter bufferingFrameWriter = new BufferingFrameWriter(streamId, tracing))
                {
                    writer(bufferingFrameWriter);

                    lock (_globalLock)
                    {
                        bufferingFrameWriter.SendFrame(_socket);
                    }
                }

                _logger.Debug("Done writing frame for stream {0}@{1}", streamId, Endpoint);

                _instrumentation.ClientTrace(token, EventType.EndWrite);
            }
            catch (Exception ex)
            {
                _queryInfos[streamId].Exception = ex;
                _queryInfos[streamId].ReadTask.RunSynchronously();
            }
        }
Exemple #3
0
        private void SendQuery()
        {
            while (true)
            {
                QueryInfo queryInfo;
                lock (_lock)
                {
                    while (!_isClosed && 0 == _pendingQueries.Count)
                    {
                        Monitor.Wait(_lock);
                    }
                    if (_isClosed)
                    {
                        Monitor.Pulse(_lock);
                        return;
                    }

                    queryInfo = _pendingQueries.Dequeue();
                }

                try
                {
                    // acquire the global lock to write the request
                    InstrumentationToken token = queryInfo.Token;
                    bool tracing = 0 != (token.ExecutionFlags & ExecutionFlags.ServerTracing);
                    using (BufferingFrameWriter bufferingFrameWriter = new BufferingFrameWriter(tracing))
                    {
                        queryInfo.Write(bufferingFrameWriter);

                        ushort streamId;
                        lock (_lock)
                        {
                            while (!_isClosed && 0 == _availableStreamIds.Count)
                            {
                                Monitor.Wait(_lock);
                            }
                            if (_isClosed)
                            {
                                queryInfo.NotifyError(new OperationCanceledException());
                                _instrumentation.ClientTrace(token, EventType.Cancellation);
                                Monitor.Pulse(_lock);
                                return;
                            }

                            streamId = _availableStreamIds.Pop();
                        }

                        _logger.Debug("Starting writing frame for stream {0}@{1}", streamId, Endpoint);
                        _instrumentation.ClientTrace(token, EventType.BeginWrite);

                        _queryInfos[streamId] = queryInfo;
                        bufferingFrameWriter.SendFrame(streamId, _socket);

                        _logger.Debug("Done writing frame for stream {0}@{1}", streamId, Endpoint);
                        _instrumentation.ClientTrace(token, EventType.EndWrite);
                    }
                }
                catch (Exception ex)
                {
                    queryInfo.NotifyError(ex);

                    if (IsStreamInBadState(ex))
                    {
                        throw;
                    }
                }
            }
        }