Пример #1
0
        public MessageBlock[] GetMessageBlocks(byte[] data)
        {
            var messageId = Guid.NewGuid();

            if (data.Length <= MaxSize)
            {
                return(new MessageBlock[]
                {
                    new MessageBlock()
                    {
                        Blocks = 1,
                        BlockSize = data.Length,
                        Index = 0,
                        MessageId = messageId,
                        Data = data
                    }
                });
            }
            else
            {
                var blocks = data.Length / MaxSize + 1;
                var result = new MessageBlock[blocks];
                int i;
                for (i = 0; i < blocks - 1; i++)
                {
                    var blockData = new byte[MaxSize];
                    Array.Copy(data, MaxSize * i, blockData, 0, MaxSize);
                    var block = new MessageBlock()
                    {
                        Blocks    = blocks,
                        BlockSize = MaxSize,
                        Data      = blockData,
                        Index     = i,
                        MessageId = messageId
                    };
                    result[i] = block;
                }
                var lastBlockSize = data.Length % MaxSize;
                var lastBlockData = new byte[MaxSize];
                Array.Copy(data, MaxSize * i, lastBlockData, 0, lastBlockSize);
                var lastBlock = new MessageBlock()
                {
                    Blocks    = blocks,
                    BlockSize = lastBlockSize,
                    Data      = lastBlockData,
                    Index     = i,
                    MessageId = messageId
                };
                result[i] = lastBlock;
                return(result);
            }
        }
Пример #2
0
        public static byte[] ToBytes(this MessageBlock block)
        {
            int size = Marshal.SizeOf(block);

            byte[] bytes = new byte[size];
            IntPtr ptr   = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(block, ptr, true);
            Marshal.Copy(ptr, bytes, 0, size);
            Marshal.FreeHGlobal(ptr);

            return(bytes);
        }
Пример #3
0
        public static MessageBlock FromBytes(this byte[] bytes)
        {
            MessageBlock block = new MessageBlock();

            int    size = Marshal.SizeOf(block);
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.Copy(bytes, 0, ptr, size);

            block = (MessageBlock)Marshal.PtrToStructure(ptr, block.GetType());
            Marshal.FreeHGlobal(ptr);

            return(block);
        }
Пример #4
0
        public bool TryGetMessageFromBlocks(MessageBlock block, out byte[] data)
        {
            if (block.Blocks == 1)
            {
                data = new byte[block.BlockSize];
                Array.Copy(block.Data, 0, data, 0, block.BlockSize);
                return(true);
            }

            List <MessageBlock> list;

            if (!messageBlocksDictionary.TryGetValue(block.MessageId, out list))
            {
                list = new List <MessageBlock>();
                messageBlocksDictionary[block.MessageId] = list;
            }
            messageBlocksDictionary[block.MessageId].Add(block);

            if (messageBlocksDictionary[block.MessageId].Count != block.Blocks)
            {
                data = null;
                return(false);
            }

            list = messageBlocksDictionary[block.MessageId];
            list.Sort((x, y) => x.Index.CompareTo(y.Index));
            var blockCount = list.Count;
            var size       = (blockCount - 1) * MaxSize + list[blockCount - 1].BlockSize;

            data = new byte[size];
            int i;

            for (i = 0; i < blockCount - 1; i++)
            {
                Array.Copy(list[i].Data, 0, data, i * MaxSize, MaxSize);
            }
            Array.Copy(list[i].Data, 0, data, i * MaxSize, list[i].BlockSize);
            return(true);
        }