context: using System;
  using System.Runtime.InteropServices;
  using iTrading.Core.Data;
  using iTrading.Core.Kernel;
  using iTrading.Core.Data;
  
  namespace iTrading.Core.Data
  {
      /// <summary>For internal use only.
      /// </summary>
      [ComVisible(false)]
      public class Bytes
      {
          private char[] charBuf;
          private iTrading.Core.Kernel.Connection connection;
          private byte[] inBuf;
          private int inIdx;
          private byte[] outBuf;
          private int outIdx;
          private const int ByteSize = 1024;
          /// <summary>
          /// </summary>
          public Bytes()
          {
              this.charBuf = new char[ByteSize];
              this.connection = null;
              this.inIdx = 0;
              this.outBuf = new byte[ByteSize];
              this.outIdx = 0;
          }
  
          /// <summary>
          /// </summary>
          /// <param name="connection"></param>
          public Bytes(iTrading.Core.Kernel.Connection connection)
          {
              this.charBuf = new char[ByteSize];
              this.connection = null;
              this.inIdx = 0;
              this.outBuf = new byte[ByteSize];
              this.outIdx = 0;
              this.connection = connection;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Account" />.
          /// </summary>
          /// <returns></returns>
          public Account ReadAccount()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadAccount: connection not set");
              }
              string str = this.ReadString();
              foreach (Account account in this.connection.Accounts)
              {
                  if (account.Name == str)
                  {
                      return account;
                  }
              }
              throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadAccount: unknown account '" + str + "'");
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.AccountItemType" />.
          /// </summary>
          /// <returns></returns>
          public AccountItemType ReadAccountItemType()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadAccountItemType: connection not set");
              }
              int num = this.ReadInt32();
              AccountItemType type = this.connection.AccountItemTypes[(AccountItemTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadAccountItemType: unknown account item type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.ActionType" />.
          /// </summary>
          /// <returns></returns>
          public ActionType ReadActionType()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadActionType: connection not set");
              }
              int num = this.ReadInt32();
              ActionType type = this.connection.ActionTypes[(ActionTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadActionType: unknown action type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read bool value.
          /// </summary>
          /// <returns></returns>
          public bool ReadBoolean()
          {
              if ((this.inBuf == null) || ((this.inIdx + 1) > this.inBuf.Length))
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadBoolean: unexpected end of byte stream");
              }
              bool flag = BitConverter.ToBoolean(this.inBuf, this.inIdx);
              this.inIdx++;
              return flag;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.ConnectionStatus" />.
          /// </summary>
          /// <returns></returns>
          public ConnectionStatus ReadConnectionStatus()
          {
              int num = this.ReadInt32();
              ConnectionStatus status = ConnectionStatus.All[(ConnectionStatusId) num];
              if (status == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadConnectionStatus: unknown connection status '" + num + "'");
              }
              return status;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Currency" />.
          /// </summary>
          /// <returns></returns>
          public iTrading.Core.Kernel.Currency ReadCurrency()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadCurrency: connection not set");
              }
              int num = this.ReadInt32();
              iTrading.Core.Kernel.Currency currency = this.connection.Currencies[(CurrencyId) num];
              if (currency == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadCurrency: unknown currency '" + num + "'");
              }
              return currency;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.CurrencyId" />.
          /// </summary>
          /// <returns></returns>
          public CurrencyId ReadCurrencyId()
          {
              return (CurrencyId) this.ReadInt32();
          }
  
          /// <summary>
          /// Read DateTime.
          /// </summary>
          /// <returns></returns>
          public DateTime ReadDateTime()
          {
              if ((this.inBuf == null) || ((this.inIdx + 8) > this.inBuf.Length))
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadDateTime: unexpected end of byte stream");
              }
              DateTime time = new DateTime(BitConverter.ToInt64(this.inBuf, this.inIdx));
              this.inIdx += 8;
              return time;
          }
  
          /// <summary>
          /// Read 8 byte floating point value.
          /// </summary>
          /// <returns></returns>
          public double ReadDouble()
          {
              if ((this.inBuf == null) || ((this.inIdx + 8) > this.inBuf.Length))
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadDouble: unexpected end of byte stream");
              }
              double num = BitConverter.ToDouble(this.inBuf, this.inIdx);
              this.inIdx += 8;
              return num;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Exchange" />.
          /// </summary>
          /// <returns></returns>
          public Exchange ReadExchange()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadExchange: connection not set");
              }
              int num = this.ReadInt32();
              Exchange exchange = this.connection.Exchanges[(ExchangeId) num];
              if (exchange == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadExchange: unknown exchange '" + num + "'");
              }
              return exchange;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.FeatureType" />.
          /// </summary>
          /// <returns></returns>
          public FeatureType ReadFeatureType()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadFeatureType: connection not set");
              }
              int num = this.ReadInt32();
              FeatureType type = this.connection.FeatureTypes[(FeatureTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadFeatureType: unknown feature type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read 4 byte integer.
          /// </summary>
          /// <returns></returns>
          public int ReadInt32()
          {
              if ((this.inBuf == null) || ((this.inIdx + 4) > this.inBuf.Length))
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadInt32: unexpected end of byte stream");
              }
              int num = BitConverter.ToInt32(this.inBuf, this.inIdx);
              this.inIdx += 4;
              return num;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.LicenseType" />.
          /// </summary>
          /// <returns></returns>
          public LicenseType ReadLicenseType()
          {
              int num = this.ReadInt32();
              LicenseType type = LicenseType.All[(LicenseTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadLicenseType: unknown license type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.MarketDataType" />.
          /// </summary>
          /// <returns></returns>
          public MarketDataType ReadMarketData()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadMarketData: connection not set");
              }
              int num = this.ReadInt32();
              MarketDataType type = this.connection.MarketDataTypes[(MarketDataTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadMarketData: unknown market data type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.MarketPosition" />.
          /// </summary>
          /// <returns></returns>
          public MarketPosition ReadMarketPosition()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadMarketPosition: connection not set");
              }
              int num = this.ReadInt32();
              MarketPosition position = this.connection.MarketPositions[(MarketPositionId) num];
              if (position == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadMarketPosition: unknown market position '" + num + "'");
              }
              return position;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.ModeType" />.
          /// </summary>
          /// <returns></returns>
          public ModeType ReadModeType()
          {
              int num = this.ReadInt32();
              ModeType type = ModeType.All[(ModeTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadModeType: unknown mode type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.NewsItemType" />.
          /// </summary>
          /// <returns></returns>
          public NewsItemType ReadNewsItemType()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadNewsItemType: connection not set");
              }
              int num = this.ReadInt32();
              NewsItemType type = this.connection.NewsItemTypes[(NewsItemTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadNewsItemType: unknown news type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Order" />.
          /// </summary>
          /// <returns></returns>
          public Order ReadOrder()
          {
              int id = this.ReadInt32();
              Order order = this.ToOrder(id);
              if (order == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadOrder: unknown order '" + id + "'");
              }
              return order;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.OrderState" />.
          /// </summary>
          /// <returns></returns>
          public OrderState ReadOrderState()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadOrderState: connection not set");
              }
              int num = this.ReadInt32();
              OrderState state = this.connection.OrderStates[(OrderStateId) num];
              if (state == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadOrderState: unknown order state '" + num + "'");
              }
              return state;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.OrderType" />.
          /// </summary>
          /// <returns></returns>
          public OrderType ReadOrderType()
          {
              if (this.connection == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadOrderType: connection not set");
              }
              int num = this.ReadInt32();
              OrderType type = this.connection.OrderTypes[(OrderTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadOrderType: unknown order type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.ProviderType" />.
          /// </summary>
          /// <returns></returns>
          public ProviderType ReadProvider()
          {
              int num = this.ReadInt32();
              ProviderType type = ProviderType.All[(ProviderTypeId) num];
              if (type == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadProvider: unknown provider type '" + num + "'");
              }
              return type;
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Request" />.
          /// </summary>
          /// <returns></returns>
          public Request ReadRequest()
          {
              int id = this.ReadInt32();
              return this.ToRequest(id);
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Right" />.
          /// </summary>
          /// <returns></returns>
          public Right ReadRight()
          {
              int num = this.ReadInt32();
              Right right = Right.All[(RightId) num];
              if (right == null)
              {
                  throw new TMException(ErrorCode.Panic, "Cbi.Bytes.ReadRight: unknown right '" + num + "'");
              }
              return right;
          }
  
          /// <summary>
          /// Deserialize a <see cref="T:iTrading.Core.Kernel.ITradingSerializable" /> object.
          /// </summary>
          /// <returns></returns>
          public ITradingSerializable ReadSerializable()
          {
              int num = this.ReadInt32();
              if (num < 0)
              {
                  return null;
              }
              int version = this.ReadInt32();
              switch (((ClassId) num))
              {
                  case ClassId.AccountEventArgs:
                      return new AccountEventArgs(this, version);
  
                  case ClassId.AccountItemTypeEventArgs:
                      return new AccountItemTypeEventArgs(this, version);
  
                  case ClassId.AccountUpdateEventArgs:
                      return new AccountUpdateEventArgs(this, version);
  
                  case ClassId.ActionTypeEventArgs:
                      return new ActionTypeEventArgs(this, version);
  
                  case ClassId.Connection:
                      return new iTrading.Core.Kernel.Connection(this, version);
  
                  case ClassId.ConnectionStatusEventArgs:
                      return new ConnectionStatusEventArgs(this, version);
  
                  case ClassId.CurrencyEventArgs:
                      return new CurrencyEventArgs(this, version);
  
                  case ClassId.ErrorEventArgs:
                      return new ITradingErrorEventArgs(this, version);
  
                  case ClassId.ExchangeEventArgs:
                      return new ExchangeEventArgs(this, version);
  
                  case ClassId.Execution:
                      return new Execution(this, version);
  
                  case ClassId.ExecutionUpdateEventArgs:
                      return new ExecutionUpdateEventArgs(this, version);
  
                  case ClassId.FeatureTypeEventArgs:
                      return new FeatureTypeEventArgs(this, version);
  
                  case ClassId.IBOptions:
                      return new IBOptions(this, version);
  
                  case ClassId.MarketData:
                      return new MarketData(this, version);
  
                  case ClassId.MarketDataEventArgs:
                      return new MarketDataEventArgs(this, version);
  
                  case ClassId.MarketDataTypeEventArgs:
                      return new MarketDataTypeEventArgs(this, version);
  
                  case ClassId.MarketDepth:
                      return new MarketDepth(this, version);
  
                  case ClassId.MarketDepthEventArgs:
                      return new MarketDepthEventArgs(this, version);
  
                  case ClassId.MarketPositionEventArgs:
                      return new MarketPositionEventArgs(this, version);
  
                  case ClassId.MbtOptions:
                      return new MbtOptions(this, version);
  
                  case ClassId.NewsEventArgs:
                      return new NewsEventArgs(this, version);
  
                  case ClassId.NewsItemTypeEventArgs:
                      return new NewsItemTypeEventArgs(this, version);
  
                  case ClassId.OptionsBase:
                      return new OptionsBase(this, version);
  
                  case ClassId.Order:
                      return new Order(this, version);
  
                  case ClassId.OrderStateEventArgs:
                      return new OrderStateEventArgs(this, version);
  
                  case ClassId.OrderStatusEventArgs:
                      return new OrderStatusEventArgs(this, version);
  
                  case ClassId.OrderTypeEventArgs:
                      return new OrderTypeEventArgs(this, version);
  
                  case ClassId.PatsOptions:
                      return new PatsOptions(this, version);
  
                  case ClassId.PositionUpdateEventArgs:
                      return new PositionUpdateEventArgs(this, version);
  
                  case ClassId.Symbol:
                      return new Symbol(this, version);
  
                  case ClassId.SymbolEventArgs:
                      return new SymbolEventArgs(this, version);
  
                  case ClassId.SymbolTypeEventArgs:
                      return new SymbolTypeEventArgs(this, version);
  
                  case ClassId.TimeInForceEventArgs:
                      return new TimeInForceEventArgs(this, version);
  
                  case ClassId.TrackOptions:
                      return new TrackOptions(this, version);
  
                  case ClassId.SimulationAccountOptions:
                      return new SimulationAccountOptions(this, version);
  
                  case ClassId.SimulationSymbolOptions:
                      return new SimulationSymbolOptions(this, version);
  
                  case ClassId.DataFileHeader:
                      return new DataFileHeader(this, version);
  
                  case ClassId.TimerEventArgs:
                      return new TimerEventArgs(this, version);
  
                  case ClassId.DtnOptions:
                      return new DtnOptions(this, version);
  
                  case ClassId.ESignalOptions:
                      return new ESignalOptions(this, version);
  
                  case ClassId.BarUpdateEventArgs:
                      return new BarUpdateEventArgs(this, version);
  
                  case ClassId.YahooOptions:
                      return new YahooOptions(this, version);
  
                  case ClassId.CTOptions:
                      return new CTOptions(this, version);
              }
              throw new TMException(ErrorCode.Panic, "unknown type id '" + num + "' to deserialize");
          }
  
          /// <summary>
          /// Read string.
          /// </summary>
          /// <returns></returns>
          public string ReadString()
          {
              int length = 0;
              while (true)
              {
                  char ch = BitConverter.ToChar(this.inBuf, this.inIdx + (2 * length));
                  if (ch == '\0')
                  {
                      this.inIdx += 2 * (length + 1);
                      return new string(this.charBuf, 0, length);
                  }
                  if (length >= this.charBuf.Length)
                  {
                      char[] array = new char[2 * this.charBuf.Length];
                      this.charBuf.CopyTo(array, 0);
                      this.charBuf = array;
                  }
                  this.charBuf[length] = ch;
                  length++;
              }
          }
  
          /// <summary>
          /// Read <see cref="T:iTrading.Core.Kernel.Symbol" />.
          /// </summary>
          /// <returns></returns>
          public Symbol ReadSymbol()
          {
              int id = this.ReadInt32();
  
gt:             Symbol symbol = this.ToSymbol(id);