コード例 #1
0
ファイル: FixFinalizer.cs プロジェクト: recurry/VersaFix
        /// <summary>
        /// The Finalize method calculates the values for
        /// common session layer fields and assigns those
        /// values to their respective fields.
        /// </summary>
        /// <param name="msg">
        /// The FIX message to be finalized.
        /// </param>
        public static void Finalize(FixMessage msg)
        {
            // REC: Calculate sending time and add it to the message:
            string   strTimeStamp   = DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss.fff");
            FixField fldSendingTime = msg.Header.GetField(52);

            if (fldSendingTime != null)
            {
                fldSendingTime.Content = strTimeStamp;
            }
            else
            {
                msg.Header.AddField(new FixField(52, strTimeStamp));
            }

            // REC: Calculate the body length and add it to the message:
            int      fixBodyLength = FixCalculator.GetBodyLength(msg);
            FixField fldBodyLength = msg.Header.GetField(9);

            if (fldBodyLength != null)
            {
                fldBodyLength.Content = fixBodyLength.ToString();
            }
            else
            {
                msg.Header.AddField(new FixField(9, fixBodyLength.ToString()));
            }

            // REC: Calculate the checksum and add it to the message:
            string   strChecksum = string.Format("{0:000}", FixCalculator.GetChecksum(msg) % 256);
            FixField fldChecksum = msg.Trailer.GetField(10);

            if (fldChecksum != null)
            {
                fldChecksum.Content = strChecksum;
            }
            else
            {
                msg.Trailer.AddField(new FixField(10, strChecksum));
            }
        }
コード例 #2
0
        /// <summary>
        /// The GetChecksum method calculates the checksum for
        /// an instance of a FIX message.
        /// </summary>
        /// <param name="msg">
        /// The FIX message to analyze.
        /// </param>
        /// <returns>
        /// The FIX protocol checksum of the message.
        /// </returns>
        public static int GetChecksum(FixMessage msg)
        {
            int result = 0;

            // REC: Process the header elements:
            foreach (IFixElement hdrElement in msg.Header)
            {
                result += CalculateSum(hdrElement.Tag.ToString());
                result += 0x3D;
                result += CalculateSum(hdrElement.Content);
                result += 0x01;

                FixGroup group = hdrElement as FixGroup;
                if (group != null)
                {
                    foreach (FixCollection instance in group.Instances)
                    {
                        result += CalculateSum(instance);
                    }
                }
            }

            // REC: Process the message body:
            foreach (IFixElement msgElement in msg.Content)
            {
                result += CalculateSum(msgElement.Tag.ToString());
                result += 0x3D;
                result += CalculateSum(msgElement.Content);
                result += 0x01;

                FixGroup group = msgElement as FixGroup;
                if (group != null)
                {
                    foreach (FixCollection instance in group.Instances)
                    {
                        result += CalculateSum(instance);
                    }
                }
            }

            // REC: Process the message trailer:
            foreach (IFixElement trlElement in msg.Trailer)
            {
                if (trlElement.Tag == 10)
                {
                    break;
                }

                result += CalculateSum(trlElement.Tag.ToString());
                result += 0x3D;
                result += CalculateSum(trlElement.Content);
                result += 0x01;

                FixGroup group = trlElement as FixGroup;
                if (group != null)
                {
                    foreach (FixCollection instance in group.Instances)
                    {
                        result += CalculateSum(instance);
                    }
                }
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// The GetBodyLength method calculates the appropriate
        /// value for the FIX BodyLength field of the specified
        /// message, in accordance with the FIX Protocol.
        /// </summary>
        /// <param name="msg">
        /// The message that the BodyLength value is to be
        /// calculated from.
        /// </param>
        /// <returns>
        /// The body length of the message, calculated according
        /// to the method specified in the FIX protocol.
        /// </returns>
        public static int GetBodyLength(FixMessage msg)
        {
            int result = 0;

            // REC: Calculate the combined length of all
            // elements in the header of the message:
            bool lengthFound = false;

            foreach (IFixElement element in msg.Header)
            {
                if (lengthFound == true)
                {
                    result += element.Tag.ToString().Length;
                    result += 1;
                    result += element.Content.Length;
                    result += 1;

                    FixGroup group = element as FixGroup;
                    if (group != null)
                    {
                        foreach (FixCollection instance in group.Instances)
                        {
                            result += CalculateLength(instance);
                        }
                    }
                }
                else
                {
                    if (element.Tag == 9)
                    {
                        lengthFound = true;
                    }
                }
            }

            // REC: Calculate the combined length of all
            // the body elements in the message:
            foreach (IFixElement element in msg.Content)
            {
                result += element.Tag.ToString().Length;
                result += 1;
                result += element.Content.Length;
                result += 1;

                FixGroup group = element as FixGroup;
                if (group != null)
                {
                    foreach (FixCollection instance in group.Instances)
                    {
                        result += CalculateLength(instance);
                    }
                }
            }

            // REC: Calculate the combined length of all
            // elements in the trailer of the message:
            foreach (IFixElement element in msg.Trailer)
            {
                // REC: Terminate when the checksum field
                // is encountered in the trailer:
                if (element.Tag == 10)
                {
                    break;
                }

                result += element.Tag.ToString().Length;
                result += 1;
                result += element.Content.Length;
                result += 1;

                FixGroup group = element as FixGroup;
                if (group != null)
                {
                    foreach (FixCollection instance in group.Instances)
                    {
                        result += CalculateLength(instance);
                    }
                }
            }

            return(result);
        }