public void TestCase() { Message msg1 = new Message("blah"); msg1.SetBinField("t_bin", new byte[]{1,2,3,4}); msg1.SetIntField("t_int", 0xAB); msg1.SetStringField("t_string", "hallo"); Message msg3 = new Message("blub"); msg3.SetStringField("a", "bbbbbbbbbbbbbbbbbbbbbb"); msg3.SetIntField("blahahah", 1); msg1.SetMessageField("t_message", msg3); var list = new HtspListType<IHtspBaseType>(); list.Add(new HtspType<long>(0x99)); list.Add(new HtspType<long>(0x77)); msg1.SetListField("t_list", list); string sMsg1 = msg1.ToString(true); Message msg2 = new Message(msg1.ToBin()); string sMsg2 = msg2.ToString(true); Console.WriteLine(sMsg1); Console.WriteLine(BitConverter.ToString(msg1.ToBin())); Console.WriteLine(sMsg2); Console.WriteLine(BitConverter.ToString(msg2.ToBin())); StringAssert.AreEqualIgnoringCase(sMsg1, sMsg2); }
private static IHtspType ParseField(TypeID fType, byte[] bin, int offset, int valLen) { switch(fType) { case TypeID.MAP: { byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); Message msg = new Message(bVal, 0, valLen); return new HtspType<Message>(msg); } case TypeID.S64: { return new HtspType<long>(ParseIntField(bin, offset, valLen)); } case TypeID.STR: { return new HtspType<string>(Encoding.UTF8.GetString(bin,offset,valLen)); } case TypeID.BIN: { byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); return new HtspType<byte[]>(bVal); } case TypeID.LIST: { HtspListType<IHtspBaseType> list = new HtspListType<IHtspBaseType>(16); int lOffset = 0; while (lOffset < valLen) { TypeID lfType = (TypeID)bin[offset + lOffset++]; int lNameLen = bin[offset + lOffset++]; int lValLen = ParseValueLength(bin, offset + lOffset); lOffset += 4; // listfields dont have a name! we should probaly check that. //string name = Encoding.UTF8.GetString(bin,offset + lOffset,nameLen); lOffset += lNameLen; list.Add((IHtspBaseType)ParseField(lfType, bin, offset + lOffset, lValLen)); lOffset += lValLen; } return list; } default: { Console.WriteLine("MessageType ({0}) not implented yet!", fType); /* byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); Console.WriteLine(BitConverter.ToString(bVal)); */ return null; } } }
public void SetListField(string name, HtspListType<IHtspBaseType> val) { if (fields.ContainsKey(name)) { // implement own exception types! throw new Exception("field does alread exist!"); } // TODO: check for possible cycle! /* else if () { // implement own exception types! // this gets really ugly, because of all the recursive function calls throw new Exception("Message or List must not cotain itself!"); } */ else { fields[name] = (val); } }