Ejemplo n.º 1
0
        /// <summary>
        ///     Writes message to to the output stream.
        /// </summary>
        /// <exception cref="IOException" />
        public void Serialize(AbstractMessage message, Stream outputStream)
        {
            string name;
            if (!Names.TryGetValue(message.GetType(), out name))
            {
                throw new Exception("BitcoinSerializer doesn't currently know how to serialize " + message.GetType());
            }
            var header = new byte[4 + CommandLength + 4 + 4];

            Utils.Uint32ToByteArrayBe(_networkParameters.PacketMagic, header, 0);

            // The header array is initialized to zero so we don't have to worry about
            // NULL terminating the string here.
            for (var i = 0; i < name.Length && i < CommandLength; i++)
            {
                header[4 + i] = (byte)(name[i]);
            }

            var payload = message.BitcoinSerialize();

            Utils.Uint32ToByteArrayLe((uint)payload.Length, header, 4 + CommandLength);

            var hash = Utils.DoubleDigest(payload);
            Array.Copy(hash, 0, header, 4 + CommandLength + 4, 4);

            outputStream.Write(header);
            outputStream.Write(payload);

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("Sending {0} message: {1}", name, Utils.BytesToHexString(header) + Utils.BytesToHexString(payload));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Writes the given message out over the network using the protocol tag. For a Transaction
 /// this should be "tx" for example. It's safe to call this from multiple threads simultaneously,
 /// the actual writing will be serialized.
 /// </summary>
 /// <exception cref="IOException"/>
 public virtual void WriteMessage(AbstractMessage message)
 {
     lock (_outputStream)
     {
         _serializer.Serialize(message, _outputStream);
     }
 }