public override void Randomize() { int arraylength = -1; Random rand = new Random(); int strlength; byte[] strbuf, myByte; //header header = new Header(); header.Randomize(); //status status = new Messages.sensor_msgs.NavSatStatus(); status.Randomize(); //latitude latitude = (rand.Next() + rand.NextDouble()); //longitude longitude = (rand.Next() + rand.NextDouble()); //altitude altitude = (rand.Next() + rand.NextDouble()); //position_covariance if (position_covariance == null) { position_covariance = new double[9]; } else { Array.Resize(ref position_covariance, 9); } for (int i = 0; i < position_covariance.Length; i++) { //position_covariance[i] position_covariance[i] = (rand.Next() + rand.NextDouble()); } //position_covariance_type myByte = new byte[1]; rand.NextBytes(myByte); position_covariance_type = myByte[0]; }
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; //header if (header == null) { header = new Header(); } pieces.Add(header.Serialize(true)); //status if (status == null) { status = new Messages.sensor_msgs.NavSatStatus(); } pieces.Add(status.Serialize(true)); //latitude scratch1 = new byte[Marshal.SizeOf(typeof(double))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(latitude, h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); //longitude scratch1 = new byte[Marshal.SizeOf(typeof(double))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(longitude, h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); //altitude scratch1 = new byte[Marshal.SizeOf(typeof(double))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(altitude, h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); //position_covariance hasmetacomponents |= false; if (position_covariance == null) { position_covariance = new double[0]; } for (int i = 0; i < position_covariance.Length; i++) { //position_covariance[i] scratch1 = new byte[Marshal.SizeOf(typeof(double))]; h = GCHandle.Alloc(scratch1, GCHandleType.Pinned); Marshal.StructureToPtr(position_covariance[i], h.AddrOfPinnedObject(), false); h.Free(); pieces.Add(scratch1); } //position_covariance_type pieces.Add(new[] { (byte)position_covariance_type }); //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); }
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; //header header = new Header(SERIALIZEDSTUFF, ref currentIndex); //status status = new Messages.sensor_msgs.NavSatStatus(SERIALIZEDSTUFF, ref currentIndex); //latitude piecesize = Marshal.SizeOf(typeof(double)); 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"); } latitude = (double)Marshal.PtrToStructure(h, typeof(double)); Marshal.FreeHGlobal(h); currentIndex += piecesize; //longitude piecesize = Marshal.SizeOf(typeof(double)); 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"); } longitude = (double)Marshal.PtrToStructure(h, typeof(double)); Marshal.FreeHGlobal(h); currentIndex += piecesize; //altitude piecesize = Marshal.SizeOf(typeof(double)); 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"); } altitude = (double)Marshal.PtrToStructure(h, typeof(double)); Marshal.FreeHGlobal(h); currentIndex += piecesize; //position_covariance hasmetacomponents |= false; if (position_covariance == null) { position_covariance = new double[9]; } else { Array.Resize(ref position_covariance, 9); } for (int i = 0; i < position_covariance.Length; i++) { //position_covariance[i] piecesize = Marshal.SizeOf(typeof(double)); 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"); } position_covariance[i] = (double)Marshal.PtrToStructure(h, typeof(double)); Marshal.FreeHGlobal(h); currentIndex += piecesize; } //position_covariance_type position_covariance_type = SERIALIZEDSTUFF[currentIndex++]; }