public byte[] Serialize() { PortableStorage Storage = new PortableStorage(); Storage.AddEntry("current_height", CurrentHeight); Storage.AddEntry("top_id", TopId); return(Storage.Serialize(false)); }
public byte[] Serialize() { PortableStorage Storage = new PortableStorage(); Storage.AddEntry("ip", IpAddress); Storage.AddEntry("port", Port); return(Storage.Serialize(false)); }
public byte[] Serialize() { PortableStorage Storage = new PortableStorage(); Storage.AddEntry("address", Address.Serialize()); Storage.AddEntry("id", Id); Storage.AddEntry("last_seen", LastSeen); return(Storage.Serialize(false)); }
public byte[] Serialize() { PortableStorage Storage = new PortableStorage(); Storage.AddEntry("network_id", NetworkId); Storage.AddEntry("version", Version); Storage.AddEntry("peer_id", PeerId); Storage.AddEntry("local_time", LocalTime); Storage.AddEntry("my_port", Port); return(Storage.Serialize(false)); }
public static CoreSyncData Deserialize(object Value) { if (Value.GetType() != typeof(PortableStorage)) { throw new Exception("Invalid deserialization object"); } PortableStorage Storage = (PortableStorage)Value; return(new CoreSyncData { CurrentHeight = (uint)Storage.GetEntry("current_height"), TopId = (string)Storage.GetEntry("top_id") }); }
public static NodeData Deserialize(object Value) { if (Value.GetType() != typeof(PortableStorage)) { throw new Exception("Invalid deserialization object"); } PortableStorage Storage = (PortableStorage)Value; return(new NodeData { NetworkId = (string)Storage.GetEntry("network_id"), Version = (byte)Storage.GetEntry("version"), PeerId = (ulong)Storage.GetEntry("peer_id"), LocalTime = (ulong)Storage.GetEntry("local_time"), Port = (uint)Storage.GetEntry("my_port") }); }
// Deserializes an entry from a byte array and adds it to storage private byte[] DeserializeEntry(byte[] Buffer) { // Get entry name length int NameLength = Encoding.ByteArrayToInteger <byte>(Buffer, 0, 1);//DeserializeVarInt<int>(Buffer, 0, out int Offset); int Offset = 1; if (NameLength < 1 || NameLength > MAX_STRING_LENGTH) { throw new Exception("Name size exceeds allowed string bounds"); } // Get entry name string Name = Encoding.ByteArrayToString(Buffer, Offset, NameLength); Offset += NameLength; Buffer = Encoding.SplitByteArray(Buffer, Offset, Buffer.Length - Offset); // Get object type SerializationType Type = GetType(Buffer); if ((int)Type < 1 || (int)Type > 14) { throw new Exception("Invalid serialization type caught: " + Encoding.ByteArrayToHexString(Encoding.SplitByteArray(Buffer, 1, Buffer.Length - 1))); } Buffer = Encoding.SplitByteArray(Buffer, 1, Buffer.Length - 1); // Create an entry object var Output = new object(); // Object is a string if (Type == SerializationType.STRING) { // Deserialize string length int Length = DeserializeVarInt <int>(Buffer, 0, out Offset); // Deserialize string Output = Encoding.ByteArrayToString(Encoding.SplitByteArray(Buffer, Offset, Length)); // Resize buffer Offset += (Output as string).Length; if (Buffer.Length - Offset > 0) { Buffer = Encoding.SplitByteArray(Buffer, Offset, Buffer.Length - Offset); } } // Object is an integer else if ((int)Type >= 1 && (int)Type <= 9) { // Create a generic method wrapper to access deserialization MethodInfo MethodInfo = typeof(Encoding).GetMethod("ByteArrayToInteger", new[] { typeof(byte[]), typeof(int) }); Type[] Args = new Type[] { ConvertSerializationType(Type) }; MethodInfo Method = MethodInfo.MakeGenericMethod(Args); // Deserialize integer Output = Method.Invoke(null, new object[] { Buffer, 0 }); // Resize buffer Offset = Encoding.GetSizeOfObject(Output); if (Buffer.Length - Offset > 0) { Buffer = Encoding.SplitByteArray(Buffer, Offset, Buffer.Length - Offset); } } // Object is an object else if (Type == SerializationType.OBJECT) { // Create a new storage object PortableStorage Storage = new PortableStorage(); // Deserialize entry Buffer = Storage.Deserialize(Buffer, false); Output = Storage; } // Add to entries Entries.Add(Name, Output); // Return buffer output return(Buffer); }