//------------------------------------------------------------------------------
        //
        // Method: CompressString
        //
        //------------------------------------------------------------------------------
        /// <summary>
        /// Compresses a string.
        /// </summary>
        /// <param name="inputString">The string to compress.</param>
        /// <returns>The compressed string.</returns>
        private string CompressString(string inputString)
        {
            metricsUtilities.Begin(new StringCompressTime());

            // Note that no try/catch block is included in this method.  According to the .NET documentation, most methods here will only cause exceptions in cases like null parameters being passed (e.g. in the case if UTF8Encoding.GetBytes() and Convert.ToBase64String()), which would be very unlikely to occur if these classes are used in the intended context.

            byte[] inputStringBytes = stringEncoding.GetBytes(inputString);
            byte[] compressedByteArray;

            // Use the GZipStream class to compress the bytes of the string
            using (MemoryStream compressedStringStream = new MemoryStream())
                using (GZipStream compressor = new GZipStream(compressedStringStream, CompressionMode.Compress))
                {
                    compressor.Write(inputStringBytes, 0, inputStringBytes.Length);
                    compressor.Close();
                    compressedByteArray = compressedStringStream.ToArray();
                    compressedStringStream.Close();
                }

            // Convert the compressed bytes to a base 64 string
            string returnString = Convert.ToBase64String(compressedByteArray);

            metricsUtilities.End(new StringCompressTime());
            metricsUtilities.Increment(new StringCompressed());
            metricsUtilities.Add(new CompressedStringSize(returnString.Length));
            loggingUtilities.LogCompressedString(this, returnString);

            return(returnString);
        }
示例#2
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);
        }
示例#3
0
        /// <include file='InterfaceDocumentationComments.xml' path='doc/members/member[@name="M:MethodInvocationRemoting.IMethodInvocationSerializer.Serialize(MethodInvocationRemoting.IMethodInvocation)"]/*'/>
        public string Serialize(IMethodInvocation inputMethodInvocation)
        {
            metricsUtilities.Begin(new MethodInvocationSerializeTime());

            string returnString = "";

            try
            {
                returnString = SerializeObject(inputMethodInvocation);
            }
            catch (Exception e)
            {
                metricsUtilities.CancelBegin(new MethodInvocationSerializeTime());
                throw new SerializationException("Failed to serialize method invocation '" + inputMethodInvocation.Name + "'.", inputMethodInvocation, e);
            }

            metricsUtilities.End(new MethodInvocationSerializeTime());
            metricsUtilities.Increment(new MethodInvocationSerialized());
            metricsUtilities.Add(new SerializedMethodInvocationSize(returnString.Length));
            loggingUtilities.LogSerializedItem(this, returnString, "method invocation");

            return(returnString);
        }
示例#4
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);
        }