在C#中解析Fix Protocol消息的最佳方法是什么

问题描述:

早上好.

我正在尝试寻找一种具有最佳性能的最佳方法来解析修复协议"消息.
然后,我给出了一些函数,如果给出了Fix Message和Fix字段,这些函数会返回一个值.它们工作得很好,但我不知道这是否是最好的方法.
见下文:

Hi, good morning.

I'm trying to find a best way(with the best performance) to parse a Fix Protocol message.
Then I made some functions that return a value if I give a Fix Message and a Fix field. They work pretty well but I don't know if is the best way to do this.
See below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FixField
{
  class Program
  {
    private const char cEql = (char)61;
    private const char cFixSep = (char)1;
    private const string cFixMsg = @"8=FIX.4.29=27735=834=2489149=ABCD52=20100415-16:01:06.98256=FIX0999991=222222226=011=00500002000000097814=017=0101069762390120=031=032=037=2010041500578358587538=100039=040=241=00500002000000097744=000026.73054=255=IBM59=060=20100415-16:01:07150=0151=00000000100010=148";

    static void Main(string[] args)
    {
      Console.WriteLine(FixField(cFixMsg,"9"));
      Console.WriteLine(FixField2(cFixMsg, "9"));
    }

    static string FixField(string strFixMsg, string strField)
    {
      int intPos;
      int intFieldLen;
      int intEnd;
      string strValue;

      intFieldLen = strField.Length + 2;

      intPos = strFixMsg.IndexOf(cFixSep + strField + cEql);

      intEnd = strFixMsg.IndexOf(cFixSep, intPos + 1);

      if (intPos == -1)
      {
        strValue = strFixMsg.Substring(2, intEnd - 2);
      }
      else
      {
        strValue = strFixMsg.Substring(intPos + intFieldLen, intEnd - intPos - intFieldLen);
      }

      return strValue;
    }

    static string FixField2(string strFixMsg, string strField)
    {
      string[] strValue = strFixMsg.Split(new char[] { cFixSep, cEql });

      int i;

      for (i = 0; strValue[i] != strField; i++)
      { }


      return strValue[i + 1];
    }

  }
}

如果您已经知道固定"格式,协议消息,那么我建议将您的消息映射到一个结构.您可以通过多种方式实现它(例如tru二进制序列化器/反序列化器,XML转换器/解串器或tru封送处理/PtrToStructure方法).

这是使用PtrToStructure方法的示例:
http://www.codeproject.com/KB/cs/gil_structs.aspx

干杯,

If you already know the format of your "fixed" protocol mesage, then I suggest map your messae to a structure. You can achive it in several ways (e.g., tru binary serializer/deserializer, XML converter/deserializer, or tru marshaling/PtrToStructure methods).

Here's an example using PtrToStructure method:
http://www.codeproject.com/KB/cs/gil_structs.aspx

Cheers,

-chris