Пример #1
0
        /// <include file='InterfaceDocumentationComments.xml' path='doc/members/member[@name="M:MethodInvocationRemoting.IRemoteReceiver.Receive"]/*'/>
        public string Receive()
        {
            CheckNotDisposed();
            string returnMessage = "";

            cancelRequest = false;

            try
            {
                while (cancelRequest == false)
                {
                    if (fileSystem.CheckFileExists(messageFilePath) == true)
                    {
                        if (fileSystem.CheckFileExists(lockFilePath) == false)
                        {
                            metricsUtilities.Begin(new MessageReceiveTime());

                            try
                            {
                                returnMessage = messageFile.ReadAll();
                                fileSystem.DeleteFile(messageFilePath);
                            }
                            catch (Exception e)
                            {
                                metricsUtilities.CancelBegin(new MessageReceiveTime());
                                throw;
                            }

                            metricsUtilities.End(new MessageReceiveTime());
                            metricsUtilities.Increment(new MessageReceived());
                            metricsUtilities.Add(new ReceivedMessageSize(returnMessage.Length));
                            loggingUtilities.LogMessageReceived(this, returnMessage);
                            break;
                        }
                    }
                    else
                    {
                        waitingForTimeout = true;
                        if (readLoopTimeout > 0)
                        {
                            System.Threading.Thread.Sleep(readLoopTimeout);
                        }
                        waitingForTimeout = false;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error receiving message.", e);
            }

            return(returnMessage);
        }
Пример #2
0
        /// <include file='InterfaceDocumentationComments.xml' path='doc/members/member[@name="M:MethodInvocationRemoting.IRemoteReceiver.Receive"]/*'/>
        public string Receive()
        {
            string returnMessage = "";

            cancelRequest = false;

            CheckNotDisposed();
            CheckConnectionOpen();

            try
            {
                IMessage receivedMessage;
                while (cancelRequest == false)
                {
                    waitingForMessage = true;
                    receivedMessage   = consumer.Receive(new TimeSpan(0, 0, 0, 0, connectLoopTimeout));
                    waitingForMessage = false;
                    if (receivedMessage != null)
                    {
                        returnMessage = (string)receivedMessage.Properties.GetString("Text");

                        metricsUtilities.Increment(new MessageReceived());
                        metricsUtilities.Add(new ReceivedMessageSize(returnMessage.Length));
                        loggingUtilities.LogMessageReceived(this, returnMessage);

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error receiving message.", e);
            }
            finally
            {
                waitingForMessage = false;
            }

            return(returnMessage);
        }
        /// <include file='InterfaceDocumentationComments.xml' path='doc/members/member[@name="M:MethodInvocationRemoting.IRemoteReceiver.Receive"]/*'/>
        public string Receive()
        {
            cancelRequest = false;
            CheckNotDisposed();
            CheckConnected();
            int    messageSequenceNumber = -1;
            string returnMessage         = "";

            while (cancelRequest == false)
            {
                // Check if there are any pending connections which would indicate the TcpRemoteSender has encountered an error and reconnected
                if (listener.Pending() == true)
                {
                    logger.Log(this, LogLevel.Warning, "New connection detected.  Attempting reconnect.");
                    AttemptConnect();
                    metricsUtilities.Increment(new TcpRemoteReceiverReconnected());
                }

                // Check if any data has been received from the TcpRemoteSender, and handle and retry if an exception occurs
                int availableData = 0;
                try
                {
                    availableData = client.Available;
                }
                catch (Exception e)
                {
                    availableData = HandleExceptionAndCheckAvailableData(e);
                }

                // If data has been received, attempt to read and parse it, and handle and retry if an exception occurs
                if (availableData != 0)
                {
                    metricsUtilities.Begin(new MessageReceiveTime());

                    MessageParseState parseState   = MessageParseState.StartOfMessage;
                    Queue <byte>      messageBytes = null; // Holds the bytes which form the body of the message received

                    try
                    {
                        messageBytes = SetupAndReadMessage(ref parseState, ref messageSequenceNumber);
                    }
                    catch (Exception e)
                    {
                        messageBytes = HandleExceptionAndRereadMessage(e, ref parseState, ref messageSequenceNumber);
                    }

                    // If the complete message has been read, break out of the current while loop
                    //   If the complete message was not read it would have been caused by a cancel request, or by a pending connection which is handled outside this block
                    if ((cancelRequest == false) && (parseState == MessageParseState.ReadCompleteMessage))
                    {
                        // If the sequence number of the message is the same as the last received message, then discard the message
                        //   This situation can be caused by the connection breaking before the sender received the last acknowledgment
                        if (messageSequenceNumber != lastMessageSequenceNumber)
                        {
                            lastMessageSequenceNumber = messageSequenceNumber;
                            returnMessage             = stringEncoding.GetString(messageBytes.ToArray());

                            metricsUtilities.End(new MessageReceiveTime());
                            metricsUtilities.Increment(new MessageReceived());
                            metricsUtilities.Add(new ReceivedMessageSize(returnMessage.Length));
                            loggingUtilities.LogMessageReceived(this, returnMessage);
                            break;
                        }
                        else
                        {
                            metricsUtilities.Increment(new TcpRemoteReceiverDuplicateSequenceNumber());
                            logger.Log(this, LogLevel.Warning, "Duplicate message with sequence number " + messageSequenceNumber + " received.  Message discarded.");
                            // Reset variables
                            messageSequenceNumber = -1;
                            returnMessage         = "";
                        }
                    }

                    metricsUtilities.End(new MessageReceiveTime());
                }

                waitingForRetry = true;
                if (receiveRetryInterval > 0)
                {
                    Thread.Sleep(receiveRetryInterval);
                }
                waitingForRetry = false;
            }

            return(returnMessage);
        }