public override void Randomize() { int arraylength = -1; Random rand = new Random(); int strlength; byte[] strbuf, myByte; //name strlength = rand.Next(100) + 1; strbuf = new byte[strlength]; rand.NextBytes(strbuf); //fill the whole buffer with random bytes for (int __x__ = 0; __x__ < strlength; __x__++) { if (strbuf[__x__] == 0) //replace null chars with non-null random ones { strbuf[__x__] = (byte)(rand.Next(254) + 1); } } strbuf[strlength - 1] = 0; //null terminate name = Encoding.ASCII.GetString(strbuf); //type strlength = rand.Next(100) + 1; strbuf = new byte[strlength]; rand.NextBytes(strbuf); //fill the whole buffer with random bytes for (int __x__ = 0; __x__ < strlength; __x__++) { if (strbuf[__x__] == 0) //replace null chars with non-null random ones { strbuf[__x__] = (byte)(rand.Next(254) + 1); } } strbuf[strlength - 1] = 0; //null terminate type = Encoding.ASCII.GetString(strbuf); //parameters arraylength = rand.Next(10); if (parameters == null) { parameters = new Messages.dynamic_reconfigure.ParamDescription[arraylength]; } else { Array.Resize(ref parameters, arraylength); } for (int i = 0; i < parameters.Length; i++) { //parameters[i] parameters[i] = new Messages.dynamic_reconfigure.ParamDescription(); parameters[i].Randomize(); } //parent parent = rand.Next(); //id id = rand.Next(); }
public override void Deserialize(byte[] SERIALIZEDSTUFF, ref int currentIndex) { int arraylength = -1; bool hasmetacomponents = false; object __thing; int piecesize = 0; byte[] thischunk, scratch1, scratch2; IntPtr h; //name name = ""; piecesize = BitConverter.ToInt32(SERIALIZEDSTUFF, currentIndex); currentIndex += 4; name = Encoding.ASCII.GetString(SERIALIZEDSTUFF, currentIndex, piecesize); currentIndex += piecesize; //type type = ""; piecesize = BitConverter.ToInt32(SERIALIZEDSTUFF, currentIndex); currentIndex += 4; type = Encoding.ASCII.GetString(SERIALIZEDSTUFF, currentIndex, piecesize); currentIndex += piecesize; //parameters hasmetacomponents |= true; arraylength = BitConverter.ToInt32(SERIALIZEDSTUFF, currentIndex); currentIndex += Marshal.SizeOf(typeof(System.Int32)); if (parameters == null) { parameters = new Messages.dynamic_reconfigure.ParamDescription[arraylength]; } else { Array.Resize(ref parameters, arraylength); } for (int i = 0; i < parameters.Length; i++) { //parameters[i] parameters[i] = new Messages.dynamic_reconfigure.ParamDescription(SERIALIZEDSTUFF, ref currentIndex); } //parent piecesize = Marshal.SizeOf(typeof(int)); h = IntPtr.Zero; if (SERIALIZEDSTUFF.Length - currentIndex != 0) { h = Marshal.AllocHGlobal(piecesize); Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize); } if (h == IntPtr.Zero) { throw new Exception("Alloc failed"); } parent = (int)Marshal.PtrToStructure(h, typeof(int)); Marshal.FreeHGlobal(h); currentIndex += piecesize; //id piecesize = Marshal.SizeOf(typeof(int)); h = IntPtr.Zero; if (SERIALIZEDSTUFF.Length - currentIndex != 0) { h = Marshal.AllocHGlobal(piecesize); Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize); } if (h == IntPtr.Zero) { throw new Exception("Alloc failed"); } id = (int)Marshal.PtrToStructure(h, typeof(int)); Marshal.FreeHGlobal(h); currentIndex += piecesize; }
public override byte[] Serialize(bool partofsomethingelse) { int currentIndex = 0, length = 0; bool hasmetacomponents = false; byte[] thischunk, scratch1, scratch2; List <byte[]> pieces = new List <byte[]>(); GCHandle h; //name if (name == null) { name = ""; } scratch1 = Encoding.ASCII.GetBytes((string)name); thischunk = new byte[scratch1.Length + 4]; scratch2 = BitConverter.GetBytes(scratch1.Length); Array.Copy(scratch1, 0, thischunk, 4, scratch1.Length); Array.Copy(scratch2, thischunk, 4); pieces.Add(thischunk); //type if (type == null) { type = ""; } scratch1 = Encoding.ASCII.GetBytes((string)type); thischunk = new byte[scratch1.Length + 4]; scratch2 = BitConverter.GetBytes(scratch1.Length); Array.Copy(scratch1, 0, thischunk, 4, scratch1.Length); Array.Copy(scratch2, thischunk, 4); pieces.Add(thischunk); //parameters hasmetacomponents |= true; if (parameters == null) { parameters = new Messages.dynamic_reconfigure.ParamDescription[0]; } pieces.Add(BitConverter.GetBytes(parameters.Length)); for (int i = 0; i < parameters.Length; i++) { //parameters[i] if (parameters[i] == null) { parameters[i] = new Messages.dynamic_reconfigure.ParamDescription(); } pieces.Add(parameters[i].Serialize(true)); } //parent scratch1 = new byte[Marshal.SizeOf(typeof(int))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(parent, h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); //id scratch1 = new byte[Marshal.SizeOf(typeof(int))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(id, h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); //combine every array in pieces into one array and return it int __a_b__f = pieces.Sum((__a_b__c) => __a_b__c.Length); int __a_b__e = 0; byte[] __a_b__d = new byte[__a_b__f]; foreach (var __p__ in pieces) { Array.Copy(__p__, 0, __a_b__d, __a_b__e, __p__.Length); __a_b__e += __p__.Length; } return(__a_b__d); }