public virtual List <byte> Encode(XElement xParentElem) { packet = new List <byte>(0); PDU pdu = this; XElement xElem = (XElement)xParentElem.Elements().First(x => ((XElement)x).Name.ToString().StartsWith("PDU") == true); pdu = (PDU)System.Activator.CreateInstance(Type.GetType(this.GetType().Namespace.ToString() + "." + xElem.Name.ToString())); //If not the base class if (pdu.GetType().IsSubclassOf(this.GetType())) { packet.AddRange(pdu.Encode(xElem)); } // Working here return(packet); }
public List <byte> Encode(XElement xParentElem) { packet = new List <byte>(); XElement xElem = (XElement)xParentElem.Element("NetworkCtl"); packet.Add(0x01); // Version packet.Add(0x00); // default values var value = xElem.Element("MessageType").Value; messageType = (MTYPE)System.Enum.Parse(typeof(MTYPE), value, true); packet[1] = EncodeMType(messageType, packet[1]); value = xElem.Element("ExpectingReply").Value; expectingReply = (CONFIRM)System.Enum.Parse(typeof(CONFIRM), value, true); if (xElem.Element("Destination") != null) { packet[1] = EncodeDestonation(1, packet[1]); AddrUnit a = new AddrUnit(AddrUnit.ADDR_TYPE.DESTONATION); packet.AddRange(a.Encode(xElem.Element("Destination"))); } if (xElem.Element("Source") != null) { packet[1] = EncodeSource(1, packet[1]); AddrUnit a = new AddrUnit(AddrUnit.ADDR_TYPE.SOURCE); packet.AddRange(a.Encode(xElem.Element("Destination"))); } if (xElem.Element("Destination") != null) { byte hops = 0; if (xElem.Element("Destination").Element("HopCount") != null) { value = xElem.Element("Destination").Element("HopCount").Value; hops = Convert.ToByte(value, 16); } packet.Add(hops); } if (messageType == MTYPE.APPICATION) { packet.AddRange(pdu.Encode(xParentElem)); } else { nsdu = nsdu.Encode(xParentElem); } return(packet); }