Пример #1
0
        public byte[] GetBytes()
        {
            using (MemoryStream headerStream = new MemoryStream(), dataStream = new MemoryStream())
            {
                BinaryWriter headerBaos = new BinaryWriter(headerStream);
                BinaryWriter dataBaos   = new BinaryWriter(dataStream);

                try
                {
                    // Version
                    headerBaos.Write(ByteUtils.ShortToBytes(_version));

                    // Id
                    int len = Id.GetLengthOrZero();
                    headerBaos.Write((byte)(len & 0xff));
                    if (len > 0)
                    {
                        dataBaos.Write(Id.GetBytesOrEmptyArray());
                    }

                    // Tag
                    len = Tag.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(Tag.GetBytesOrEmptyArray());
                    }

                    // GroupId
                    len = GroupId.GetLengthOrZero();
                    headerBaos.Write((byte)(len & 0xff));
                    if (len > 0)
                    {
                        dataBaos.Write(GroupId.GetBytesOrEmptyArray());
                    }

                    // SequenceNumber
                    if (SequenceNumber == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)4);
                        dataBaos.Write(ByteUtils.IntegerToBytes(SequenceNumber));
                    }

                    // SequenceTotal
                    if (SequenceTotal == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)4);
                        dataBaos.Write(ByteUtils.IntegerToBytes(SequenceTotal));
                    }

                    // Priority
                    if (Priority == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)1);
                        dataBaos.Write(Priority);
                    }

                    //Timestamp
                    if (Timestamp == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)8);
                        dataBaos.Write(ByteUtils.LongToBytes(Timestamp));
                    }

                    // Publisher
                    len = Publisher.GetLengthOrZero();
                    headerBaos.Write((byte)(len & 0xff));
                    if (len > 0)
                    {
                        dataBaos.Write(Publisher.GetBytesOrEmptyArray());
                    }

                    // AuthId
                    len = AuthId.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(AuthId.GetBytesOrEmptyArray());
                    }

                    // AuthGroup
                    len = AuthGroup.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(AuthGroup.GetBytesOrEmptyArray());
                    }

                    // ChainPosition
                    if (ChainPosition == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)8);
                        dataBaos.Write(ByteUtils.LongToBytes(ChainPosition));
                    }

                    // Hash
                    len = Hash.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(Hash.GetBytesOrEmptyArray());
                    }

                    // PreviousHash
                    len = PreviousHash.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(PreviousHash.GetBytesOrEmptyArray());
                    }

                    // Nonce
                    len = Nonce.GetLengthOrZero();
                    headerBaos.Write(ByteUtils.ShortToBytes((short)(len & 0xffff)));
                    if (len > 0)
                    {
                        dataBaos.Write(Nonce.GetBytesOrEmptyArray());
                    }

                    // DifficultyTarget
                    if (DifficultyTarget == 0)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write((byte)4);
                        dataBaos.Write(ByteUtils.IntegerToBytes(DifficultyTarget));
                    }

                    // InfoType
                    len = InfoType.GetLengthOrZero();
                    headerBaos.Write((byte)(len & 0xff));
                    if (len > 0)
                    {
                        dataBaos.Write(InfoType.GetBytesOrEmptyArray());
                    }

                    // InfoFormat
                    len = InfoFormat.GetLengthOrZero();
                    headerBaos.Write((byte)(len & 0xff));
                    if (len > 0)
                    {
                        dataBaos.Write(InfoFormat.GetBytesOrEmptyArray());
                    }

                    // ContextData
                    if (ContextData == null)
                    {
                        headerBaos.Write((byte)0);
                    }
                    else
                    {
                        headerBaos.Write(ByteUtils.IntegerToBytes(ContextData.Length));
                        dataBaos.Write(ContextData, 0, ContextData.Length);
                    }

                    // ContentData
                    if (ContentData == null)
                    {
                        headerBaos.Write(ByteUtils.IntegerToBytes(0));
                    }
                    else
                    {
                        headerBaos.Write(ByteUtils.IntegerToBytes(ContentData.Length));
                        dataBaos.Write(ContentData, 0, ContentData.Length);
                    }

                    var headerBytes = headerStream.ToArray();
                    var dataBytes   = dataStream.ToArray();

                    var result = new byte[headerBytes.Length + dataBytes.Length];
                    Array.Copy(headerBytes, 0, result, 0, headerBytes.Length);
                    Array.Copy(dataBytes, 0, result, headerBytes.Length, dataBytes.Length);

                    return(result);
                }
                catch (Exception)
                {
                    //TODO: log
                }
                finally
                {
                    try
                    {
                        headerBaos.Dispose();
                        dataBaos.Dispose();
                    }
                    catch (Exception)
                    {
                        // TODO: log("Error when closing byte arrays streams");
                    }
                }
            }

            return(new byte[0]);
        }