Beispiel #1
0
        private void BeginReading()
        {
            try
            {
                if (!(_bufferingMode is FrameBuffering))
                {
                    lock (_readerSocketStreamBusy)
                    {
                        while (_readerSocketStreamBusy.Value)
                        {
                            Monitor.Wait(_readerSocketStreamBusy);
                        }
                        _readerSocketStreamBusy.Value = true;
                    }
                }

                lock (_readerSocketStream)
                {
                    var rh = _readerSocketStream.BeginRead(_buffer[_bufNo], 0, _buffer[_bufNo].Length, new AsyncCallback((ar) =>
                    {
                        try
                        {
                            int bytesReadCount;
                            lock (_readerSocketStream)
                                bytesReadCount = _readerSocketStream.EndRead(ar);

                            if (bytesReadCount == 0)
                            {
                                if (_alreadyDisposed)
                                {
                                    CompleteReader();
                                    return;
                                }

                                throw new CassandraConnectionIOException();
                            }
                            else
                            {
                                foreach (var frame in _bufferingMode.Process(_buffer[_bufNo], bytesReadCount, _readerSocketStream, _compressor))
                                {
                                    Action <ResponseFrame> act = null;
                                    lock (_frameGuardier)
                                    {
                                        if (frame.FrameHeader.StreamId == 0xFF)
                                        {
                                            act = _frameEventCallback.Value;
                                        }
                                        else if (frame.FrameHeader.StreamId <= sbyte.MaxValue)
                                        {
                                            if (_frameReadTimers[frame.FrameHeader.StreamId] != null)
                                            {
                                                _frameReadTimers[frame.FrameHeader.StreamId].Change(Timeout.Infinite,
                                                                                                    Timeout.Infinite);
                                            }
                                            act = _frameReadCallback[frame.FrameHeader.StreamId];
                                            _frameReadCallback[frame.FrameHeader.StreamId] = null;
                                        }
                                    }

                                    if (act == null)
                                    {
                                        throw new InvalidOperationException("Protocol error! Unmached response. Terminating all requests now...");
                                    }

                                    act.BeginInvoke(frame, (tar) =>
                                    {
                                        try
                                        {
                                            (tar.AsyncState as Action <ResponseFrame>).EndInvoke(tar);
                                        }
                                        catch (Exception ex)
                                        {
                                            SetupSocketException(ex);
                                        }
                                        finally
                                        {
                                            if (!(_bufferingMode is FrameBuffering))
                                            {
                                                if (IsHealthy)
                                                {
                                                    BeginReading();
                                                }
                                            }
                                        }
                                    }, act);
                                }
                                _bufNo = 1 - _bufNo;
                            }
                        }
                        catch (Exception ex)
                        {
                            SetupSocketException(ex);
                        }
                        finally
                        {
                            if (_bufferingMode is FrameBuffering)
                            {
                                if (IsHealthy)
                                {
                                    BeginReading();
                                }
                            }
                            else
                            {
                                lock (_readerSocketStreamBusy)
                                {
                                    _readerSocketStreamBusy.Value = false;
                                    Monitor.PulseAll(_readerSocketStreamBusy);
                                }
                            }
                        }
                    }), null);
                }
            }
            catch (IOException e)
            {
                if (!SetupSocketException(e))
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        private void BeginReading()
        {
            try
            {
                if (!(_bufferingMode is FrameBuffering))
                {
                    _readerSocketStreamBusy.WaitOne();
                }

                var rh = _socketStream.BeginRead(_buffer[_bufNo], 0, _buffer[_bufNo].Length, new AsyncCallback((ar) =>
                {
                    try
                    {
                        // when already disposed, _socketStream.EndRead() throws exception
                        // when accessing disposed instance _socket internally.
                        // so need to check disposed first
                        if (_alreadyDisposed.IsTaken())
                        {
                            ForceComplete();
                            return;
                        }

                        int bytesReadCount = _socketStream.EndRead(ar);
                        if (bytesReadCount == 0)
                        {
                            if (_alreadyDisposed.IsTaken())
                            {
                                ForceComplete();
                                return;
                            }

                            throw new CassandraConnectionIOException();
                        }
                        else
                        {
                            foreach (var frame in _bufferingMode.Process(_buffer[_bufNo], bytesReadCount, _socketStream, _compressor))
                            {
                                Action <ResponseFrame> act = null;
                                if (frame.FrameHeader.StreamId == 0xFF)
                                {
                                    act = _frameEventCallback.Value;
                                }
                                else if (frame.FrameHeader.StreamId <= sbyte.MaxValue)
                                {
                                    if (_frameReadTimers[frame.FrameHeader.StreamId] != null)
                                    {
                                        _frameReadTimers[frame.FrameHeader.StreamId].Change(Timeout.Infinite,
                                                                                            Timeout.Infinite);
                                    }
                                    act = _frameReadCallback[frame.FrameHeader.StreamId];
                                    _frameReadCallback[frame.FrameHeader.StreamId] = null;
                                }

                                if (act == null)
                                {
                                    throw new InvalidOperationException("Protocol error! Unmached response. Terminating all requests now...");
                                }

                                act.BeginInvoke(frame, (tar) =>
                                {
                                    try
                                    {
                                        (tar.AsyncState as Action <ResponseFrame>).EndInvoke(tar);
                                    }
                                    catch (Exception ex)
                                    {
                                        SetupSocketException(ex);
                                    }
                                    finally
                                    {
                                        if (!(_bufferingMode is FrameBuffering))
                                        {
                                            if (IsHealthy)
                                            {
                                                BeginReading();
                                            }
                                        }
                                    }
                                }, act);
                            }
                            _bufNo = 1 - _bufNo;
                        }
                    }
                    catch (Exception ex)
                    {
                        SetupSocketException(ex);
                    }
                    finally
                    {
                        if (_bufferingMode is FrameBuffering)
                        {
                            if (IsHealthy)
                            {
                                BeginReading();
                            }
                        }
                        else
                        {
                            _readerSocketStreamBusy.Set();
                        }
                    }
                }), null);
            }
            catch (IOException e)
            {
                if (!SetupSocketException(e))
                {
                    throw;
                }
            }
        }