Example #1
0
        protected void SetOutputChannel(UdpOutputChannel udpOutputChannel)
        {
            Fx.Assert(this.UdpOutputChannel == null, "this.UdpOutputChannel must be null");
            Fx.Assert(udpOutputChannel != null, "udpOutputChannel can't be null, since SetOutputChannel should be called only once");

            this.UdpOutputChannel = udpOutputChannel;
        }
Example #2
0
            public CloseAsyncResult(UdpOutputChannel channel, TimeSpan timeout, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.channel       = channel;
                this.timeoutHelper = new TimeoutHelper(timeout);

                if (this.BeginCleanup())
                {
                    this.Complete(true);
                }
            }
Example #3
0
            private bool ContinueRetransmission(RetransmitState state)
            {
                this.ThrowIfTimedOut();

                while (true)
                {
                    switch (state)
                    {
                    case RetransmitState.TransmitCompleted:
                        if (!this.retransmitIterator.MoveNext())
                        {
                            // We are done retransmitting
                            return(true);
                        }

                        if (this.retransmitIterator.CurrentDelay > 0)
                        {
                            this.retransmitTimer.Set(this.retransmitIterator.CurrentDelay);
                            return(false);
                        }

                        state = RetransmitState.WaitCompleted;
                        break;

                    case RetransmitState.WaitCompleted:
                        if (this.IsCanceled)
                        {
                            this.channel.ThrowIfAborted();
                            return(true);
                        }

                        // since we only invoke the encoder once just before the initial send of the message
                        // we need to handle logging the message in the retransmission case
                        if (MessageLogger.LogMessagesAtTransportLevel)
                        {
                            UdpOutputChannel.LogMessage(ref this.message, this.messageData);
                        }

                        // !completedSync
                        if (!this.BeginTransmitMessage())
                        {
                            return(false);
                        }

                        state = RetransmitState.TransmitCompleted;
                        break;

                    default:
                        Fx.Assert("Unknown RetransmitState value encountered");
                        return(true);
                    }
                }
            }
Example #4
0
        public UdpRequestContext(UdpOutputChannel outputChannel, Message requestMessage)
            : base(requestMessage, outputChannel.InternalCloseTimeout, outputChannel.InternalSendTimeout)
        {
            Fx.Assert(outputChannel != null, "replyChannel can't be null");
            this.outputChannel = outputChannel;

            if (!NetworkInterfaceMessageProperty.TryGet(requestMessage, out this.networkInterfaceMessageProperty))
            {
                Fx.Assert("requestMessage must always contain NetworkInterfaceMessageProperty");
            }

            RemoteEndpointMessageProperty remoteEndpointMessageProperty;

            if (!requestMessage.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out remoteEndpointMessageProperty))
            {
                Fx.Assert("requestMessage must always contain RemoteEndpointMessageProperty");
            }

            UriBuilder uriBuilder = new UriBuilder(UdpConstants.Scheme, remoteEndpointMessageProperty.Address, remoteEndpointMessageProperty.Port);

            this.via = uriBuilder.Uri;
        }
Example #5
0
            public SendAsyncResult(UdpOutputChannel channel, Message message, TimeSpan timeout, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.timeoutHelper = new TimeoutHelper(timeout);

                this.channel = channel;
                this.message = message;
                bool throwing = true;
                bool completedSynchronously = false;

                try
                {
                    this.Initialize(message);

                    completedSynchronously = this.BeginTransmitMessage();

                    if (completedSynchronously && this.retransmissionEnabled)
                    {
                        // initial send completed sync, now we need to start the retransmission process...
                        completedSynchronously = this.BeginRetransmission();
                    }

                    throwing = false;
                }
                finally
                {
                    if (throwing)
                    {
                        this.Cleanup();
                    }
                }

                if (completedSynchronously)
                {
                    this.CompleteAndCleanup(true, null);
                }
            }
Example #6
0
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            if (message is NullMessage)
            {
                return;
            }

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            IPEndPoint remoteEndPoint;

            UdpSocket[] sendSockets;
            Exception   exceptionToBeThrown;

            sendSockets = this.GetSendSockets(message, out remoteEndPoint, out exceptionToBeThrown);

            if (exceptionToBeThrown != null)
            {
                throw FxTrace.Exception.AsError(exceptionToBeThrown);
            }

            if (timeoutHelper.RemainingTime() <= TimeSpan.Zero)
            {
                throw FxTrace.Exception.AsError(new TimeoutException(SR.SendTimedOut(remoteEndPoint, timeout)));
            }

            bool returnBuffer = false;
            ArraySegment <byte> messageData = default(ArraySegment <byte>);

            bool sendingMulticast = UdpUtility.IsMulticastAddress(remoteEndPoint.Address);
            SynchronousRetransmissionHelper retransmitHelper = null;
            RetransmitIterator retransmitIterator            = null;

            bool shouldRetransmit = this.ShouldRetransmitMessage(sendingMulticast);

            try
            {
                if (shouldRetransmit)
                {
                    retransmitIterator = this.CreateRetransmitIterator(sendingMulticast);
                    retransmitHelper   = new SynchronousRetransmissionHelper(sendingMulticast);
                    this.RetransmitStarting(message.Headers.MessageId, retransmitHelper);
                }

                messageData  = this.EncodeMessage(message);
                returnBuffer = true;

                this.TransmitMessage(messageData, sendSockets, remoteEndPoint, timeoutHelper);

                if (shouldRetransmit)
                {
                    while (retransmitIterator.MoveNext())
                    {
                        // wait for currentDelay time, then retransmit
                        if (retransmitIterator.CurrentDelay > 0)
                        {
                            retransmitHelper.Wait(retransmitIterator.CurrentDelay);
                        }

                        if (retransmitHelper.IsCanceled)
                        {
                            ThrowIfAborted();
                            return;
                        }

                        // since we only invoke the encoder once just before the initial send of the message
                        // we need to handle logging the message in the retransmission case
                        if (MessageLogger.LogMessagesAtTransportLevel)
                        {
                            UdpOutputChannel.LogMessage(ref message, messageData);
                        }

                        this.TransmitMessage(messageData, sendSockets, remoteEndPoint, timeoutHelper);
                    }
                }
            }
            finally
            {
                if (returnBuffer)
                {
                    this.BufferManager.ReturnBuffer(messageData.Array);
                }

                if (shouldRetransmit)
                {
                    this.RetransmitStopping(message.Headers.MessageId);

                    if (retransmitHelper != null)
                    {
                        retransmitHelper.Dispose();
                    }
                }
            }
        }