protected TransportDuplexSessionChannel(
     ChannelManagerBase manager,
     ITransportFactorySettings settings,
     EndpointAddress localAddress,
     Uri localVia,
     EndpointAddress remoteAddresss,
     Uri via)
     : base(manager, remoteAddresss, via, settings.ManualAddressing, settings.MessageVersion)
 {
     this.localAddress   = localAddress;
     this.localVia       = localVia;
     this.bufferManager  = settings.BufferManager;
     this.sendLock       = new ThreadNeutralSemaphore(1);
     this.messageEncoder = settings.MessageEncoderFactory.CreateSessionEncoder();
     this.Session        = new ConnectionDuplexSession(this);
 }
Example #2
0
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            this.ThrowIfDisposedOrNotOpen();

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            if (!this.sendLock.TryEnter(timeoutHelper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(
                                                                              SR.GetString(SR.SendToViaTimedOut, Via, timeout),
                                                                              ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }

            try
            {
                // check again in case the previous send faulted while we were waiting for the lock
                this.ThrowIfDisposedOrNotOpen();
                this.ThrowIfOutputSessionClosed();

                bool success = false;
                try
                {
                    this.ApplyChannelBinding(message);

                    this.OnSendCore(message, timeoutHelper.RemainingTime());
                    success = true;
                    if (TD.MessageSentByTransportIsEnabled())
                    {
                        EventTraceActivity eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message);
                        TD.MessageSentByTransport(eventTraceActivity, this.RemoteAddress.Uri.AbsoluteUri);
                    }
                }
                finally
                {
                    if (!success)
                    {
                        this.Fault();
                    }
                }
            }
            finally
            {
                this.sendLock.Exit();
            }
        }
Example #3
0
        public Message Receive(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            if (!this.sourceLock.TryEnter(timeoutHelper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new TimeoutException(SR.GetString(SR.ReceiveTimedOut2, timeout),
                                               ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }

            try
            {
                return(source.Receive(timeoutHelper.RemainingTime()));
            }
            finally
            {
                this.sourceLock.Exit();
            }
        }
Example #4
0
 public SynchronizedMessageSource(IMessageSource source)
 {
     this.source     = source;
     this.sourceLock = new ThreadNeutralSemaphore(1);
 }
Example #5
0
        public bool WaitForMessage(TimeSpan timeout)
        {
            bool          flag;
            TimeoutHelper helper = new TimeoutHelper(timeout);

            if (!this.sourceLock.TryEnter(helper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(System.ServiceModel.SR.GetString("WaitForMessageTimedOut", new object[] { timeout }), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                flag = this.source.WaitForMessage(helper.RemainingTime());
            }
            finally
            {
                this.sourceLock.Exit();
            }
            return(flag);
        }
Example #6
0
        public Message Receive(TimeSpan timeout)
        {
            Message       message;
            TimeoutHelper helper = new TimeoutHelper(timeout);

            if (!this.sourceLock.TryEnter(helper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(System.ServiceModel.SR.GetString("ReceiveTimedOut2", new object[] { timeout }), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                message = this.source.Receive(helper.RemainingTime());
            }
            finally
            {
                this.sourceLock.Exit();
            }
            return(message);
        }
Example #7
0
 protected ReceiveContext()
 {
     this.thisLock  = new object();
     this.State     = ReceiveContextState.Received;
     this.stateLock = new ThreadNeutralSemaphore(1);
 }
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            base.ThrowIfDisposedOrNotOpen();
            TimeoutHelper helper = new TimeoutHelper(timeout);

            if (!this.sendLock.TryEnter(helper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(System.ServiceModel.SR.GetString("SendToViaTimedOut", new object[] { this.Via, timeout }), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                base.ThrowIfDisposedOrNotOpen();
                this.ThrowIfOutputSessionClosed();
                bool flag = false;
                try
                {
                    this.ApplyChannelBinding(message);
                    bool allowOutputBatching    = message.Properties.AllowOutputBatching;
                    ArraySegment <byte> segment = this.EncodeMessage(message);
                    this.Connection.Write(segment.Array, segment.Offset, segment.Count, !allowOutputBatching, helper.RemainingTime(), this.bufferManager);
                    flag = true;
                    if (TD.MessageSentByTransportIsEnabled())
                    {
                        TD.MessageSentByTransport(this.RemoteAddress.Uri.AbsoluteUri);
                    }
                }
                finally
                {
                    if (!flag)
                    {
                        base.Fault();
                    }
                }
            }
            finally
            {
                this.sendLock.Exit();
            }
        }
        private void CloseOutputSession(TimeSpan timeout)
        {
            base.ThrowIfNotOpened();
            base.ThrowIfFaulted();
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            if (!this.sendLock.TryEnter(timeoutHelper.RemainingTime()))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(System.ServiceModel.SR.GetString("CloseTimedOut", new object[] { timeout }), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                base.ThrowIfFaulted();
                if (!this.isOutputSessionClosed)
                {
                    this.isOutputSessionClosed = true;
                    bool flag = true;
                    try
                    {
                        this.Connection.Write(SessionEncoder.EndBytes, 0, SessionEncoder.EndBytes.Length, true, timeoutHelper.RemainingTime());
                        this.OnOutputSessionClosed(ref timeoutHelper);
                        flag = false;
                    }
                    finally
                    {
                        if (flag)
                        {
                            base.Fault();
                        }
                    }
                }
            }
            finally
            {
                this.sendLock.Exit();
            }
        }
 public InstanceThrottle(int maxCount)
 {
     this.throttle            = new ThreadNeutralSemaphore(maxCount);
     this.maxCount            = maxCount;
     this.warningRestoreLimit = (int)Math.Floor((double)(0.7 * maxCount));
 }