Exemplo n.º 1
0
 public static byte[] SerializeToBytes(ListMessage instance)
 {
     using (MemoryStream ms = new MemoryStream()) {
         Serialize(ms, instance);
         return(ms.ToArray());
     }
 }
Exemplo n.º 2
0
        public static ListMessage Deserialize(Stream stream)
        {
            ListMessage instance = new ListMessage();

            Deserialize(stream, instance);
            return(instance);
        }
Exemplo n.º 3
0
        public static Message FromChunk(Chunk chunk, KeyStorage keyStorage)
        {
            if (chunk == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(chunk.Data)) {
                byte[] whisper = new byte[7];
                if (ms.Read(whisper, 0, whisper.Length) != whisper.Length)
                {
                    throw new InvalidDataException("Header not right length");
                }
                if (Encoding.ASCII.GetString(whisper) != "Whisper")
                {
                    throw new InvalidDataException("Missing header");
                }

                MessageHeader header = MessageHeader.Deserialize(ProtocolParser.ReadBytes(ms));

                byte[]  messageBytes = ProtocolParser.ReadBytes(ms);
                Message message;
                switch (header.MessageId)
                {
                case 1:
                    message = TreeMessage.Deserialize(messageBytes);
                    break;

                case 2:
                    message = RouteMessage.Deserialize(messageBytes);
                    break;

                case 3:
                    message = ListMessage.Deserialize(messageBytes);
                    break;

                default:
                    throw new NotImplementedException();
                }

                //Verify signature
                if (header.Signature != null)
                {
                    foreach (PublicKey key in keyStorage.PublicKeys)
                    {
                        if (key.Verify(messageBytes, header.Signature))
                        {
                            message.Signature = key;
                            break;
                        }
                    }
                }

                return(message);
            }
        }
Exemplo n.º 4
0
 public static void Serialize(Stream stream, ListMessage instance)
 {
     if (instance.List != null)
     {
         foreach (byte[] i1 in instance.List)
         {
             ProtocolParser.WriteKey(stream, new ProtocolBuffers.Key(1, Wire.LengthDelimited));
             ProtocolParser.WriteBytes(stream, i1);
         }
     }
 }
Exemplo n.º 5
0
 public static byte[] SerializeMessage(Message m)
 {
     if (m is TreeMessage)
     {
         return(TreeMessage.SerializeToBytes((TreeMessage)m));
     }
     if (m is RouteMessage)
     {
         return(RouteMessage.SerializeToBytes((RouteMessage)m));
     }
     if (m is ListMessage)
     {
         return(ListMessage.SerializeToBytes((ListMessage)m));
     }
     throw new NotImplementedException();
 }