Exemplo n.º 1
0
 private static string FieldToString(IHtspType field, string padding, bool debug)
 {
     if (false); //just a dummy
     else if (field is HtspType<Message>) {
         var sb = new StringBuilder();
         sb.AppendFormat(padding + "(msg) [{0} fields]\n", ((HtspType<Message>)field).Value.FieldCount);
         if (debug) {
             sb.Append(((HtspType<Message>)field).Value.ToString(padding + "   ", debug));
         }
         return sb.ToString();
     }
     else if (field is HtspType<long>) {
         return String.Format(padding + "(int) 0x{0}\n", ((HtspType<long>)field).Value.ToString("x"));
     }
     else if (field is HtspType<string>) {
         return String.Format(padding + "(str) \"{0}\"\n", ((HtspType<string>)field).Value);
     }
     else if (field is HtspType<byte[]>) {
         return String.Format(padding + "(bin) [{0} bytes]\n", ((HtspType<byte[]>)field).Value.Length);
     }
     else if (field is HtspListType<IHtspBaseType>) {
         var sb = new StringBuilder();
         sb.AppendFormat(padding + "(lst) [{0} items]\n", ((HtspListType<IHtspBaseType>)field).Count);
         if (debug) {
             foreach(IHtspBaseType item in ((HtspListType<IHtspBaseType>)field)) {
                 sb.AppendFormat(padding + "{0}", FieldToString(item, padding + "   ", debug));
             }
         }
         return sb.ToString();
     }
     else {
         // irgendwas werfen!
         Console.WriteLine("{0}.ToString() not supported yet!", field.GetType().ToString());
         return "";
     }
 }
Exemplo n.º 2
0
        private static void FieldToBin(IHtspType field, out byte bType, out byte[] bVal)
        {
            if (false); //just a dummy
            else if (field is HtspType<Message>) {
                bType = (byte)TypeID.MAP;
                Message val = ((HtspType<Message>)field).Value;
                bVal = val.ToBin();
            }
            else if (field is HtspType<long>) {
                bType = (byte)TypeID.S64;
                long val = ((HtspType<long>)field).Value;
                byte valLen = 0;
                long tmp = val;
                while((tmp)!=0) {
                    tmp=tmp>>8;
                    valLen++;
                }
                bVal = new byte[valLen];
                Buffer.BlockCopy(BitConverter.GetBytes(val), 0 , bVal, 0, valLen);
            }
            else if (field is HtspType<string>) {
                bType = (byte)TypeID.STR;
                string val = ((HtspType<string>)field).Value;
                bVal=Encoding.UTF8.GetBytes(val);
            }
            else if (field is HtspType<byte[]>) {
                bType = (byte)TypeID.BIN;
                bVal = ((HtspType<byte[]>)field).Value;
            }
            else if (field is HtspListType<IHtspBaseType>) {
                bType = (byte)TypeID.LIST;
                MemoryStream mstream = new MemoryStream(1460);
                foreach(IHtspType item in ((HtspListType<IHtspBaseType>)field)) {
                    byte lbType;
                    byte[] lbVal;
                    FieldToBin(item, out lbType, out lbVal);

                    mstream.WriteByte(lbType);
                    // listfields dont have a name
                    mstream.WriteByte(0);
                    mstream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(lbVal.Length)),0,4);
                    // listfields dont have a name !!!
                    //mstream.Write(bName,0,bName.Length);
                    mstream.Write(lbVal,0,lbVal.Length);
                }
                mstream.Close();
                bVal = mstream.ToArray();
            }
            else {
                // irgendwas werfen!
                bType = 0;
                bVal = new byte[0];
                Console.WriteLine("{0}.ToBin() not supported yet!", field.GetType().ToString());
            }
        }