Esempio n. 1
0
 public StreamedFramingRequestContext(SingletonConnectionReader parent, Message requestMessage)
     : base(requestMessage, parent.transportSettings.CloseTimeout, parent.transportSettings.SendTimeout)
 {
     this.parent = parent;
     connection  = parent.connection;
     settings    = parent.transportSettings;
 }
Esempio n. 2
0
 public ClientSingletonConnectionReader(IConnection connection, StreamedConnectionPoolHelper connectionPoolHelper,
                                        IConnectionOrientedTransportFactorySettings settings)
     : base(connection, 0, 0, connectionPoolHelper.RemoteSecurity, settings, null)
 {
     Contract.Assert(connectionPoolHelper != null);
     _connectionPoolHelper = connectionPoolHelper;
 }
Esempio n. 3
0
        //returns false if the connection should be aborted
        bool ContinuePostValidationProcessing()
        {
            if (viaDelegate != null)
            {
                try
                {
                    viaDelegate(via);
                }
                catch (ServiceActivationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                    // return fault and close connection
                    SendFault(FramingEncodingString.ServiceActivationFailedFault);
                    return(true);
                }
            }


            transportSettings = transportSettingsCallback(via);

            if (transportSettings == null)
            {
                EndpointNotFoundException e = new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, decoder.Via));
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                // return fault and close connection
                SendFault(FramingEncodingString.EndpointNotFoundFault);
                return(false);
            }

            // we have enough information to hand off to a channel. Our job is done
            callback(this);
            return(true);
        }
Esempio n. 4
0
 protected FramingDuplexSessionChannel(ChannelManagerBase factory, IConnectionOrientedTransportFactorySettings settings,
                                       EndpointAddress remoteAddresss, Uri via, bool exposeConnectionProperty)
     : this(factory, settings, EndpointAddress.AnonymousAddress, settings.MessageVersion.Addressing.AnonymousUri(),
            remoteAddresss, via, exposeConnectionProperty)
 {
     Session = FramingConnectionDuplexSession.CreateSession(this, settings.Upgrade);
 }
Esempio n. 5
0
 protected FramingDuplexSessionChannel(ChannelManagerBase factory, IConnectionOrientedTransportFactorySettings settings,
                                       EndpointAddress remoteAddress, Uri via, bool exposeConnectionProperty)
     : this(factory, settings, s_anonymousEndpointAddress, settings.MessageVersion.Addressing == AddressingVersion.None ? null : new Uri("http://www.w3.org/2005/08/addressing/anonymous"),
            remoteAddress, via, exposeConnectionProperty)
 {
     this.Session = FramingConnectionDuplexSession.CreateSession(this, settings.Upgrade);
 }
 public StreamedFramingRequestChannel(ChannelManagerBase factory, IConnectionOrientedTransportChannelFactorySettings settings, EndpointAddress remoteAddresss, Uri via, IConnectionInitiator connectionInitiator, ConnectionPool connectionPool) : base(factory, remoteAddresss, via, settings.ManualAddressing)
 {
     this.settings = settings;
     this.connectionInitiator = connectionInitiator;
     this.connectionPool = connectionPool;
     this.messageEncoder = settings.MessageEncoderFactory.Encoder;
     this.upgrade = settings.Upgrade;
 }
 public ClientDuplexConnectionReader(ClientFramingDuplexSessionChannel channel, IConnection connection, ClientDuplexDecoder decoder, IConnectionOrientedTransportFactorySettings settings, MessageEncoder messageEncoder) : base(connection, null, 0, 0, null)
 {
     this.decoder = decoder;
     this.maxBufferSize = settings.MaxBufferSize;
     this.bufferManager = settings.BufferManager;
     this.messageEncoder = messageEncoder;
     this.channel = channel;
 }
 private FramingDuplexSessionChannel(ChannelManagerBase manager, IConnectionOrientedTransportFactorySettings settings, EndpointAddress localAddress, Uri localVia, EndpointAddress remoteAddresss, Uri via, bool exposeConnectionProperty) : base(manager, remoteAddresss, via, settings.ManualAddressing, settings.MessageVersion)
 {
     this.localAddress             = localAddress;
     this.localVia                 = localVia;
     this.exposeConnectionProperty = exposeConnectionProperty;
     this.bufferManager            = settings.BufferManager;
     this.sendLock                 = new ThreadNeutralSemaphore(1);
 }
Esempio n. 9
0
 public StreamedFramingRequestChannel(ChannelManagerBase factory, IConnectionOrientedTransportChannelFactorySettings settings, EndpointAddress remoteAddresss, Uri via, IConnectionInitiator connectionInitiator, ConnectionPool connectionPool) : base(factory, remoteAddresss, via, settings.ManualAddressing)
 {
     this.settings            = settings;
     this.connectionInitiator = connectionInitiator;
     this.connectionPool      = connectionPool;
     this.messageEncoder      = settings.MessageEncoderFactory.Encoder;
     this.upgrade             = settings.Upgrade;
 }
Esempio n. 10
0
 protected SingletonConnectionReader(IConnection connection, int offset, int size, SecurityMessageProperty security, IConnectionOrientedTransportFactorySettings transportSettings, Uri via)
 {
     this.connection        = connection;
     this.offset            = offset;
     this.size              = size;
     this.security          = security;
     this.transportSettings = transportSettings;
     this.via = via;
 }
 protected SingletonConnectionReader(IConnection connection, int offset, int size, SecurityMessageProperty security, IConnectionOrientedTransportFactorySettings transportSettings, Uri via)
 {
     this.connection = connection;
     this.offset = offset;
     this.size = size;
     this.security = security;
     this.transportSettings = transportSettings;
     this.via = via;
 }
Esempio n. 12
0
 public ClientDuplexConnectionReader(ClientFramingDuplexSessionChannel channel, IConnection connection, ClientDuplexDecoder decoder,
                                     IConnectionOrientedTransportFactorySettings settings, MessageEncoder messageEncoder)
     : base(connection, null, 0, 0, null)
 {
     _decoder        = decoder;
     _maxBufferSize  = settings.MaxBufferSize;
     _bufferManager  = settings.BufferManager;
     _messageEncoder = messageEncoder;
     _channel        = channel;
 }
Esempio n. 13
0
        public static async Task WriteMessageAsync(Message message, IConnection connection, bool isRequest,
                                                   IConnectionOrientedTransportFactorySettings settings, CancellationToken token)
        {
            // TODO: Switch to using the token as this is supposed to use remaining time
            var timeoutHelper = new TimeoutHelper(TimeoutHelper.GetOriginalTimeout(token));

            byte[] endBytes = null;
            if (message != null)
            {
                MessageEncoder messageEncoder     = settings.MessageEncoderFactory.Encoder;
                byte[]         envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;

                bool writeStreamed;
                if (isRequest)
                {
                    endBytes      = SingletonEncoder.EnvelopeEndFramingEndBytes;
                    writeStreamed = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
                }
                else
                {
                    endBytes      = SingletonEncoder.EnvelopeEndBytes;
                    writeStreamed = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
                }

                if (writeStreamed)
                {
                    await connection.WriteAsync(envelopeStartBytes, 0, envelopeStartBytes.Length, false, timeoutHelper.RemainingTime());

                    Stream connectionStream   = new StreamingOutputConnectionStream(connection, settings);
                    Stream writeTimeoutStream = new TimeoutStream(connectionStream, timeoutHelper.RemainingTime());
                    messageEncoder.WriteMessage(message, writeTimeoutStream);
                }
                else
                {
                    ArraySegment <byte> messageData = messageEncoder.WriteMessage(message,
                                                                                  int.MaxValue, settings.BufferManager, envelopeStartBytes.Length + IntEncoder.MaxEncodedSize);
                    messageData = SingletonEncoder.EncodeMessageFrame(messageData);
                    Buffer.BlockCopy(envelopeStartBytes, 0, messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                                     envelopeStartBytes.Length);
                    await connection.WriteAsync(messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                                                messageData.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), settings.BufferManager);
                }
            }
            else if (isRequest) // context handles response end bytes
            {
                endBytes = SingletonEncoder.EndBytes;
            }

            if (endBytes != null)
            {
                await connection.WriteAsync(endBytes, 0, endBytes.Length,
                                            true, timeoutHelper.RemainingTime());
            }
        }
Esempio n. 14
0
        protected SingletonConnectionReader(IConnection connection, int offset, int size, SecurityMessageProperty security,
            IConnectionOrientedTransportFactorySettings transportSettings, Uri via)
        {
            Contract.Assert(connection != null);

            _connection = connection;
            _offset = offset;
            _size = size;
            _security = security;
            _transportSettings = transportSettings;
            _via = via;
        }
Esempio n. 15
0
        protected SingletonConnectionReader(IConnection connection, int offset, int size, SecurityMessageProperty security,
                                            IConnectionOrientedTransportFactorySettings transportSettings, Uri via)
        {
            Contract.Assert(connection != null);

            _connection        = connection;
            _offset            = offset;
            _size              = size;
            _security          = security;
            _transportSettings = transportSettings;
            _via = via;
        }
Esempio n. 16
0
        public static void WriteMessage(Message message, IConnection connection, bool isRequest,
                                        IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper)
        {
            byte[] endBytes = null;
            if (message != null)
            {
                MessageEncoder messageEncoder     = settings.MessageEncoderFactory.Encoder;
                byte[]         envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;

                bool writeStreamed;
                if (isRequest)
                {
                    endBytes      = SingletonEncoder.EnvelopeEndFramingEndBytes;
                    writeStreamed = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
                }
                else
                {
                    endBytes      = SingletonEncoder.EnvelopeEndBytes;
                    writeStreamed = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
                }

                if (writeStreamed)
                {
                    connection.Write(envelopeStartBytes, 0, envelopeStartBytes.Length, false, timeoutHelper.RemainingTime());
                    Stream connectionStream   = new StreamingOutputConnectionStream(connection, settings);
                    Stream writeTimeoutStream = new TimeoutStream(connectionStream, timeoutHelper.RemainingTime());
                    messageEncoder.WriteMessage(message, writeTimeoutStream);
                }
                else
                {
                    ArraySegment <byte> messageData = messageEncoder.WriteMessage(message,
                                                                                  int.MaxValue, settings.BufferManager, envelopeStartBytes.Length + IntEncoder.MaxEncodedSize);
                    messageData = SingletonEncoder.EncodeMessageFrame(messageData);
                    Buffer.BlockCopy(envelopeStartBytes, 0, messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                                     envelopeStartBytes.Length);
                    connection.Write(messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                                     messageData.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), settings.BufferManager);
                }
            }
            else if (isRequest) // context handles response end bytes
            {
                endBytes = SingletonEncoder.EndBytes;
            }

            if (endBytes != null)
            {
                connection.Write(endBytes, 0, endBytes.Length,
                                 true, timeoutHelper.RemainingTime());
            }
        }
 public static void WriteMessage(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper)
 {
     byte[] envelopeEndFramingEndBytes = null;
     if (message != null)
     {
         bool flag;
         MessageEncoder encoder = settings.MessageEncoderFactory.Encoder;
         byte[] envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;
         if (isRequest)
         {
             envelopeEndFramingEndBytes = SingletonEncoder.EnvelopeEndFramingEndBytes;
             flag = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
         }
         else
         {
             envelopeEndFramingEndBytes = SingletonEncoder.EnvelopeEndBytes;
             flag = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
         }
         if (flag)
         {
             connection.Write(envelopeStartBytes, 0, envelopeStartBytes.Length, false, timeoutHelper.RemainingTime());
             Stream stream = new StreamingOutputConnectionStream(connection, settings);
             Stream stream2 = new TimeoutStream(stream, ref timeoutHelper);
             encoder.WriteMessage(message, stream2);
         }
         else
         {
             ArraySegment<byte> segment = SingletonEncoder.EncodeMessageFrame(encoder.WriteMessage(message, 0x7fffffff, settings.BufferManager, envelopeStartBytes.Length + 5));
             Buffer.BlockCopy(envelopeStartBytes, 0, segment.Array, segment.Offset - envelopeStartBytes.Length, envelopeStartBytes.Length);
             connection.Write(segment.Array, segment.Offset - envelopeStartBytes.Length, segment.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), settings.BufferManager);
         }
     }
     else if (isRequest)
     {
         envelopeEndFramingEndBytes = SingletonEncoder.EndBytes;
     }
     if (envelopeEndFramingEndBytes != null)
     {
         connection.Write(envelopeEndFramingEndBytes, 0, envelopeEndFramingEndBytes.Length, true, timeoutHelper.RemainingTime());
     }
 }
Esempio n. 18
0
 private FramingDuplexSessionChannel(ChannelManagerBase manager, IConnectionOrientedTransportFactorySettings settings,
                                     EndpointAddress localAddress, Uri localVia, EndpointAddress remoteAddress, Uri via, bool exposeConnectionProperty)
     : base(manager, settings, localAddress, localVia, remoteAddress, via)
 {
     _exposeConnectionProperty = exposeConnectionProperty;
 }
Esempio n. 19
0
        async void ContinueReadingAsync()
        {
            bool success = false;

            try
            {
                for (;;)
                {
                    if (size == 0)
                    {
                        offset = 0;
                        size   = await Connection.ReadAsync(0, connectionBuffer.Length, GetRemainingTimeout());

                        if (size == 0)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
                        }
                    }

                    int bytesDecoded = decoder.Decode(connectionBuffer, offset, size);
                    if (bytesDecoded > 0)
                    {
                        offset += bytesDecoded;
                        size   -= bytesDecoded;
                    }

                    if (decoder.CurrentState == ServerSessionDecoder.State.PreUpgradeStart)
                    {
                        via = decoder.Via;
                        if (await Connection.ValidateAsync(via))
                        {
                            settings = transportSettingsCallback(via);

                            if (settings == null)
                            {
                                EndpointNotFoundException e = new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, decoder.Via));
                                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);

                                SendFault(FramingEncodingString.EndpointNotFoundFault);
                                // This goes through the failure path (Abort) even though it doesn't throw.
                                return;
                            }

                            // we have enough information to hand off to a channel. Our job is done
                            callback(this);
                        }

                        break; //exit loop, set success=true;
                    }
                }
                success = true;
            }
            catch (CommunicationException exception)
            {
                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
            }
            catch (TimeoutException exception)
            {
                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                if (!ExceptionHandlerHelper.HandleTransportExceptionHelper(e))
                {
                    throw;
                }
                // containment -- all exceptions abort the reader, no additional containment action necessary
            }
            finally
            {
                if (!success)
                {
                    Abort();
                }
            }
        }
 public WriteMessageAsyncResult(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state) : base(callback, state)
 {
     this.connection = connection;
     this.encoder = settings.MessageEncoderFactory.Encoder;
     this.bufferManager = settings.BufferManager;
     this.timeoutHelper = timeoutHelper;
     this.message = message;
     this.settings = settings;
     bool flag = true;
     bool flag2 = false;
     if (message == null)
     {
         if (isRequest)
         {
             this.endBytes = SingletonEncoder.EndBytes;
         }
         flag2 = this.WriteEndBytes();
     }
     else
     {
         try
         {
             bool flag3;
             byte[] envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;
             if (isRequest)
             {
                 this.endBytes = SingletonEncoder.EnvelopeEndFramingEndBytes;
                 flag3 = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
             }
             else
             {
                 this.endBytes = SingletonEncoder.EnvelopeEndBytes;
                 flag3 = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
             }
             if (flag3)
             {
                 if (onWriteStartBytes == null)
                 {
                     onWriteStartBytes = Fx.ThunkCallback(new AsyncCallback(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteStartBytes));
                 }
                 IAsyncResult result = connection.BeginWrite(envelopeStartBytes, 0, envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), onWriteStartBytes, this);
                 if (result.CompletedSynchronously)
                 {
                     if (onWriteStartBytesScheduled == null)
                     {
                         onWriteStartBytesScheduled = new Action<object>(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteStartBytesScheduled);
                     }
                     ActionItem.Schedule(onWriteStartBytesScheduled, result);
                 }
             }
             else
             {
                 ArraySegment<byte> segment = SingletonEncoder.EncodeMessageFrame(settings.MessageEncoderFactory.Encoder.WriteMessage(message, 0x7fffffff, this.bufferManager, envelopeStartBytes.Length + 5));
                 this.bufferToFree = segment.Array;
                 Buffer.BlockCopy(envelopeStartBytes, 0, segment.Array, segment.Offset - envelopeStartBytes.Length, envelopeStartBytes.Length);
                 if (onWriteBufferedMessage == null)
                 {
                     onWriteBufferedMessage = Fx.ThunkCallback(new AsyncCallback(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteBufferedMessage));
                 }
                 IAsyncResult result2 = connection.BeginWrite(segment.Array, segment.Offset - envelopeStartBytes.Length, segment.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), onWriteBufferedMessage, this);
                 if (result2.CompletedSynchronously)
                 {
                     flag2 = this.HandleWriteBufferedMessage(result2);
                 }
             }
             flag = false;
         }
         finally
         {
             if (flag)
             {
                 this.Cleanup();
             }
         }
     }
     if (flag2)
     {
         base.Complete(true);
     }
 }
 public static IAsyncResult BeginWriteMessage(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state)
 {
     return(new WriteMessageAsyncResult(message, connection, isRequest, settings, ref timeoutHelper, callback, state));
 }
 public static void WriteMessage(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper)
 {
     byte[] envelopeEndFramingEndBytes = null;
     if (message != null)
     {
         bool           flag;
         MessageEncoder encoder            = settings.MessageEncoderFactory.Encoder;
         byte[]         envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;
         if (isRequest)
         {
             envelopeEndFramingEndBytes = SingletonEncoder.EnvelopeEndFramingEndBytes;
             flag = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
         }
         else
         {
             envelopeEndFramingEndBytes = SingletonEncoder.EnvelopeEndBytes;
             flag = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
         }
         if (flag)
         {
             connection.Write(envelopeStartBytes, 0, envelopeStartBytes.Length, false, timeoutHelper.RemainingTime());
             Stream stream  = new StreamingOutputConnectionStream(connection, settings);
             Stream stream2 = new TimeoutStream(stream, ref timeoutHelper);
             encoder.WriteMessage(message, stream2);
         }
         else
         {
             ArraySegment <byte> segment = SingletonEncoder.EncodeMessageFrame(encoder.WriteMessage(message, 0x7fffffff, settings.BufferManager, envelopeStartBytes.Length + 5));
             Buffer.BlockCopy(envelopeStartBytes, 0, segment.Array, segment.Offset - envelopeStartBytes.Length, envelopeStartBytes.Length);
             connection.Write(segment.Array, segment.Offset - envelopeStartBytes.Length, segment.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), settings.BufferManager);
         }
     }
     else if (isRequest)
     {
         envelopeEndFramingEndBytes = SingletonEncoder.EndBytes;
     }
     if (envelopeEndFramingEndBytes != null)
     {
         connection.Write(envelopeEndFramingEndBytes, 0, envelopeEndFramingEndBytes.Length, true, timeoutHelper.RemainingTime());
     }
 }
            public WriteMessageAsyncResult(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state) : base(callback, state)
            {
                this.connection    = connection;
                this.encoder       = settings.MessageEncoderFactory.Encoder;
                this.bufferManager = settings.BufferManager;
                this.timeoutHelper = timeoutHelper;
                this.message       = message;
                this.settings      = settings;
                bool flag  = true;
                bool flag2 = false;

                if (message == null)
                {
                    if (isRequest)
                    {
                        this.endBytes = SingletonEncoder.EndBytes;
                    }
                    flag2 = this.WriteEndBytes();
                }
                else
                {
                    try
                    {
                        bool   flag3;
                        byte[] envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;
                        if (isRequest)
                        {
                            this.endBytes = SingletonEncoder.EnvelopeEndFramingEndBytes;
                            flag3         = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
                        }
                        else
                        {
                            this.endBytes = SingletonEncoder.EnvelopeEndBytes;
                            flag3         = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
                        }
                        if (flag3)
                        {
                            if (onWriteStartBytes == null)
                            {
                                onWriteStartBytes = Fx.ThunkCallback(new AsyncCallback(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteStartBytes));
                            }
                            IAsyncResult result = connection.BeginWrite(envelopeStartBytes, 0, envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), onWriteStartBytes, this);
                            if (result.CompletedSynchronously)
                            {
                                if (onWriteStartBytesScheduled == null)
                                {
                                    onWriteStartBytesScheduled = new Action <object>(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteStartBytesScheduled);
                                }
                                ActionItem.Schedule(onWriteStartBytesScheduled, result);
                            }
                        }
                        else
                        {
                            ArraySegment <byte> segment = SingletonEncoder.EncodeMessageFrame(settings.MessageEncoderFactory.Encoder.WriteMessage(message, 0x7fffffff, this.bufferManager, envelopeStartBytes.Length + 5));
                            this.bufferToFree = segment.Array;
                            Buffer.BlockCopy(envelopeStartBytes, 0, segment.Array, segment.Offset - envelopeStartBytes.Length, envelopeStartBytes.Length);
                            if (onWriteBufferedMessage == null)
                            {
                                onWriteBufferedMessage = Fx.ThunkCallback(new AsyncCallback(StreamingConnectionHelper.WriteMessageAsyncResult.OnWriteBufferedMessage));
                            }
                            IAsyncResult result2 = connection.BeginWrite(segment.Array, segment.Offset - envelopeStartBytes.Length, segment.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime(), onWriteBufferedMessage, this);
                            if (result2.CompletedSynchronously)
                            {
                                flag2 = this.HandleWriteBufferedMessage(result2);
                            }
                        }
                        flag = false;
                    }
                    finally
                    {
                        if (flag)
                        {
                            this.Cleanup();
                        }
                    }
                }
                if (flag2)
                {
                    base.Complete(true);
                }
            }
 private void ReadAndDispatch()
 {
     bool flag = false;
     try
     {
         while (((this.size > 0) || !this.isReadPending) && !base.IsClosed)
         {
             if (this.size == 0)
             {
                 this.isReadPending = true;
                 if (this.onAsyncReadComplete == null)
                 {
                     this.onAsyncReadComplete = new WaitCallback(this.OnAsyncReadComplete);
                 }
                 if (base.Connection.BeginRead(0, this.connectionBuffer.Length, this.GetRemainingTimeout(), this.onAsyncReadComplete, null) == AsyncReadResult.Queued)
                 {
                     break;
                 }
                 this.HandleReadComplete();
             }
             int num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
             if (num > 0)
             {
                 this.offset += num;
                 this.size -= num;
             }
             if (this.decoder.CurrentState == ServerSingletonDecoder.State.PreUpgradeStart)
             {
                 this.via = this.decoder.Via;
                 if (!base.Connection.Validate(this.via))
                 {
                     return;
                 }
                 if (this.viaDelegate != null)
                 {
                     try
                     {
                         this.viaDelegate(this.via);
                     }
                     catch (ServiceActivationException exception)
                     {
                         if (DiagnosticUtility.ShouldTraceInformation)
                         {
                             DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                         }
                         this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ServiceActivationFailed");
                         break;
                     }
                 }
                 this.transportSettings = this.transportSettingsCallback(this.via);
                 if (this.transportSettings == null)
                 {
                     EndpointNotFoundException exception2 = new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { this.decoder.Via }));
                     if (DiagnosticUtility.ShouldTraceInformation)
                     {
                         DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
                     }
                     this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/EndpointNotFound");
                     return;
                 }
                 this.callback(this);
                 break;
             }
         }
         flag = true;
     }
     catch (CommunicationException exception3)
     {
         if (DiagnosticUtility.ShouldTraceInformation)
         {
             DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information);
         }
     }
     catch (TimeoutException exception4)
     {
         if (DiagnosticUtility.ShouldTraceInformation)
         {
             DiagnosticUtility.ExceptionUtility.TraceHandledException(exception4, TraceEventType.Information);
         }
     }
     catch (Exception exception5)
     {
         if (Fx.IsFatal(exception5))
         {
             throw;
         }
         if (!System.ServiceModel.Dispatcher.ExceptionHandler.HandleTransportExceptionHelper(exception5))
         {
             throw;
         }
     }
     finally
     {
         if (!flag)
         {
             base.Abort();
         }
     }
 }
Esempio n. 25
0
        public async static Task WriteMessageAsync(Message message, IConnection connection, bool isRequest,
            IConnectionOrientedTransportFactorySettings settings, TimeoutHelper timeoutHelper)
        {
            byte[] endBytes = null;
            if (message != null)
            {
                MessageEncoder messageEncoder = settings.MessageEncoderFactory.Encoder;
                byte[] envelopeStartBytes = SingletonEncoder.EnvelopeStartBytes;

                bool writeStreamed;
                if (isRequest)
                {
                    endBytes = SingletonEncoder.EnvelopeEndFramingEndBytes;
                    writeStreamed = TransferModeHelper.IsRequestStreamed(settings.TransferMode);
                }
                else
                {
                    endBytes = SingletonEncoder.EnvelopeEndBytes;
                    writeStreamed = TransferModeHelper.IsResponseStreamed(settings.TransferMode);
                }

                if (writeStreamed)
                {
                    await connection.WriteAsync(envelopeStartBytes, 0, envelopeStartBytes.Length, false, timeoutHelper.RemainingTime());
                    Stream connectionStream = new StreamingOutputConnectionStream(connection, settings);
                    Stream writeTimeoutStream = new TimeoutStream(connectionStream, timeoutHelper.RemainingTime());
                    await messageEncoder.WriteMessageAsync(message, writeTimeoutStream);
                }
                else
                {
                    ArraySegment<byte> messageData = await messageEncoder.WriteMessageAsync(message,
                        int.MaxValue, settings.BufferManager, envelopeStartBytes.Length + IntEncoder.MaxEncodedSize);
                    messageData = SingletonEncoder.EncodeMessageFrame(messageData);
                    Buffer.BlockCopy(envelopeStartBytes, 0, messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                        envelopeStartBytes.Length);
                    await connection.WriteAsync(messageData.Array, messageData.Offset - envelopeStartBytes.Length,
                        messageData.Count + envelopeStartBytes.Length, true, timeoutHelper.RemainingTime());
                }
            }
            else if (isRequest) // context handles response end bytes
            {
                endBytes = SingletonEncoder.EndBytes;
            }

            if (endBytes != null && endBytes.Length > 0)
            {
                await connection.WriteAsync(endBytes, 0, endBytes.Length,
                    true, timeoutHelper.RemainingTime());
            }
        }
 public static IAsyncResult BeginWriteMessage(Message message, IConnection connection, bool isRequest, IConnectionOrientedTransportFactorySettings settings, ref TimeoutHelper timeoutHelper, AsyncCallback callback, object state)
 {
     return new WriteMessageAsyncResult(message, connection, isRequest, settings, ref timeoutHelper, callback, state);
 }
Esempio n. 27
0
 public ClientSingletonConnectionReader(IConnection connection, StreamedConnectionPoolHelper connectionPoolHelper,
     IConnectionOrientedTransportFactorySettings settings)
     : base(connection, 0, 0, connectionPoolHelper.RemoteSecurity, settings, null)
 {
     Contract.Assert(connectionPoolHelper != null);
     _connectionPoolHelper = connectionPoolHelper;
 }
        private void ReadAndDispatch()
        {
            bool flag = false;

            try
            {
                while (((this.size > 0) || !this.isReadPending) && !base.IsClosed)
                {
                    if (this.size == 0)
                    {
                        this.isReadPending = true;
                        if (this.onAsyncReadComplete == null)
                        {
                            this.onAsyncReadComplete = new WaitCallback(this.OnAsyncReadComplete);
                        }
                        if (base.Connection.BeginRead(0, this.connectionBuffer.Length, this.GetRemainingTimeout(), this.onAsyncReadComplete, null) == AsyncReadResult.Queued)
                        {
                            break;
                        }
                        this.HandleReadComplete();
                    }
                    int num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
                    if (num > 0)
                    {
                        this.offset += num;
                        this.size   -= num;
                    }
                    if (this.decoder.CurrentState == ServerSingletonDecoder.State.PreUpgradeStart)
                    {
                        this.via = this.decoder.Via;
                        if (!base.Connection.Validate(this.via))
                        {
                            return;
                        }
                        if (this.viaDelegate != null)
                        {
                            try
                            {
                                this.viaDelegate(this.via);
                            }
                            catch (ServiceActivationException exception)
                            {
                                if (DiagnosticUtility.ShouldTraceInformation)
                                {
                                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                                }
                                this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ServiceActivationFailed");
                                break;
                            }
                        }
                        this.transportSettings = this.transportSettingsCallback(this.via);
                        if (this.transportSettings == null)
                        {
                            EndpointNotFoundException exception2 = new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { this.decoder.Via }));
                            if (DiagnosticUtility.ShouldTraceInformation)
                            {
                                DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
                            }
                            this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/EndpointNotFound");
                            return;
                        }
                        this.callback(this);
                        break;
                    }
                }
                flag = true;
            }
            catch (CommunicationException exception3)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information);
                }
            }
            catch (TimeoutException exception4)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception4, TraceEventType.Information);
                }
            }
            catch (Exception exception5)
            {
                if (Fx.IsFatal(exception5))
                {
                    throw;
                }
                if (!System.ServiceModel.Dispatcher.ExceptionHandler.HandleTransportExceptionHelper(exception5))
                {
                    throw;
                }
            }
            finally
            {
                if (!flag)
                {
                    base.Abort();
                }
            }
        }
 public ClientSingletonConnectionReader(IConnection connection, StreamedFramingRequestChannel.StreamedConnectionPoolHelper connectionPoolHelper, IConnectionOrientedTransportFactorySettings settings) : base(connection, 0, 0, connectionPoolHelper.RemoteSecurity, settings, null)
 {
     this.connectionPoolHelper = connectionPoolHelper;
 }
 public StreamedFramingRequestContext(SingletonConnectionReader parent, Message requestMessage) : base(requestMessage, parent.transportSettings.CloseTimeout, parent.transportSettings.SendTimeout)
 {
     this.parent = parent;
     this.connection = parent.connection;
     this.settings = parent.transportSettings;
 }
Esempio n. 31
0
 public ClientSingletonConnectionReader(IConnection connection, StreamedFramingRequestChannel.StreamedConnectionPoolHelper connectionPoolHelper, IConnectionOrientedTransportFactorySettings settings) : base(connection, 0, 0, connectionPoolHelper.RemoteSecurity, settings, null)
 {
     this.connectionPoolHelper = connectionPoolHelper;
 }