Exemplo n.º 1
0
        ConnectionModeReader SetupModeReader(IConnection connection, bool isCached)
        {
            ConnectionModeReader modeReader;

            if (isCached)
            {
                if (onCachedConnectionModeKnown == null)
                {
                    onCachedConnectionModeKnown = new ConnectionModeCallback(OnCachedConnectionModeKnown);
                }

                modeReader = new ConnectionModeReader(connection, onCachedConnectionModeKnown, onConnectionClosed);
            }
            else
            {
                if (onConnectionModeKnown == null)
                {
                    onConnectionModeKnown = new ConnectionModeCallback(OnConnectionModeKnown);
                }

                modeReader = new ConnectionModeReader(connection, onConnectionModeKnown, onConnectionClosed);
            }

            lock (ThisLock)
            {
                if (isDisposed)
                {
                    modeReader.Dispose();
                    return(null);
                }

                connectionReaders.Add(modeReader);
                return(modeReader);
            }
        }
Exemplo n.º 2
0
        static void ReadCallback(object state)
        {
            ConnectionModeReader reader   = (ConnectionModeReader)state;
            bool      completeSelf        = false;
            Exception completionException = null;

            try
            {
                if (reader.GetReadResult())
                {
                    completeSelf = reader.ContinueReading();
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                completeSelf        = true;
                completionException = e;
            }

            if (completeSelf)
            {
                reader.Complete(completionException);
            }
        }
Exemplo n.º 3
0
        public void ReuseConnection(IConnection connection, TimeSpan closeTimeout)
        {
            connection.ExceptionEventType = TraceEventType.Information;
            ConnectionModeReader modeReader = SetupModeReader(connection, true);

            if (modeReader != null)
            {
                if (reuseConnectionCallback == null)
                {
                    reuseConnectionCallback = new Action <object>(ReuseConnectionCallback);
                }

                ActionItem.Schedule(reuseConnectionCallback, new ReuseConnectionState(modeReader, closeTimeout));
            }
        }
Exemplo n.º 4
0
        void OnConnectionAvailable(IConnection connection, Action connectionDequeuedCallback)
        {
            ConnectionModeReader modeReader = SetupModeReader(connection, false);

            if (modeReader != null)
            {
                // StartReading() will never throw non-fatal exceptions;
                // it propagates all exceptions into the onConnectionModeKnown callback,
                // which is where we need our robust handling
                modeReader.StartReading(channelInitializationTimeout, connectionDequeuedCallback);
            }
            else
            {
                connectionDequeuedCallback();
            }
        }
Exemplo n.º 5
0
 public ReuseConnectionState(ConnectionModeReader modeReader, TimeSpan closeTimeout)
 {
     this.modeReader   = modeReader;
     this.closeTimeout = closeTimeout;
 }
Exemplo n.º 6
0
        void OnConnectionModeKnownCore(ConnectionModeReader modeReader, bool isCached)
        {
            lock (ThisLock)
            {
                if (isDisposed)
                {
                    return;
                }

                connectionReaders.Remove(modeReader);
            }

            bool closeReader = true;

            try
            {
                FramingMode framingMode;
                try
                {
                    framingMode = modeReader.GetConnectionMode();
                }
                catch (CommunicationException exception)
                {
                    TraceEventType eventType = modeReader.Connection.ExceptionEventType;
                    DiagnosticUtility.TraceHandledException(exception, eventType);
                    return;
                }
                catch (TimeoutException exception)
                {
                    if (!isCached)
                    {
                        exception = new TimeoutException(SR.Format(SR.ChannelInitializationTimeout, channelInitializationTimeout), exception);
                        ErrorBehaviorHelper.ThrowAndCatch(exception);
                    }

                    TraceEventType eventType = modeReader.Connection.ExceptionEventType;
                    DiagnosticUtility.TraceHandledException(exception, eventType);
                    return;
                }

                switch (framingMode)
                {
                case FramingMode.Duplex:
                    OnDuplexConnection(modeReader.Connection, modeReader.ConnectionDequeuedCallback,
                                       modeReader.StreamPosition, modeReader.BufferOffset, modeReader.BufferSize,
                                       modeReader.GetRemainingTimeout());
                    break;

                case FramingMode.Singleton:
                    OnSingletonConnection(modeReader.Connection, modeReader.ConnectionDequeuedCallback,
                                          modeReader.StreamPosition, modeReader.BufferOffset, modeReader.BufferSize,
                                          modeReader.GetRemainingTimeout());
                    break;

                default:
                {
                    Exception inner = new InvalidDataException(SR.Format(
                                                                   SR.FramingModeNotSupported, framingMode));
                    Exception exception = new ProtocolException(inner.Message, inner);
                    FramingEncodingString.AddFaultString(exception, FramingEncodingString.UnsupportedModeFault);
                    ErrorBehaviorHelper.ThrowAndCatch(exception);
                    return;
                }
                }

                closeReader = false;
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                if (!ExceptionHandlerHelper.HandleTransportExceptionHelper(e))
                {
                    throw;
                }

                // containment -- the reader is aborted, no need for additional containment
            }
            finally
            {
                if (closeReader)
                {
                    modeReader.Dispose();
                }
            }
        }
Exemplo n.º 7
0
 void OnConnectionModeKnown(ConnectionModeReader modeReader)
 {
     OnConnectionModeKnownCore(modeReader, false);
 }
Exemplo n.º 8
0
 void OnCachedConnectionModeKnown(ConnectionModeReader modeReader)
 {
     OnConnectionModeKnownCore(modeReader, true);
 }