Exemplo n.º 1
0
        //Will return 0, 1, or 2 batches
        public SafeOrderedReturn Ordered(SafeEnvelope msg, int index)
        {
            try
            {
                SafeBatch[] batches        = new SafeBatch[2];
                bool        ispendingbatch = true;
                ulong       MsgSizeBytes   = (ulong)msg.Payload.Length; //Length of byte array = size in bytes

                //Message is too big and thus will overflow, send pending and this msg in its own batch
                if (MsgSizeBytes > MaxPendingSizeBytes)
                {
                    if (PendingBatch.Messages.Count() > 0)
                    {
                        batches[0] = Cut(); //Add pending batch to return
                    }
                    SafeBatch newbatch = new SafeBatch();
                    newbatch.Messages    = new SafeEnvelope[1];
                    newbatch.Messages[0] = msg;
                    batches[1]           = newbatch;
                    ispendingbatch       = false;
                }
                else
                {
                    //Message will cause overflow, cut pending batch
                    if (PendingBatchSizeBytes + MsgSizeBytes > MaxPendingSizeBytes)
                    {
                        batches[0] = Cut(); //Add pending batch to return
                    }

                    while (index > MsgNum || pendinglocked)
                    {
                    }                                       //Wait for it to be this msg's turn and not currently cutting batch

                    AddToPending(msg, MsgSizeBytes, index); //Add message to pending batch

                    //Pending batch has reached max count, must cut
                    //If the last if statement was true, this one will not be true
                    if (PendingBatch.MsgCount >= MaxPendingMsgCount)
                    {
                        pendinglocked  = true;
                        batches[1]     = Cut(); //Add pending batch to return
                        ispendingbatch = false;
                    }

                    Interlocked.Add(ref MsgNum, 1); //Increment msgnum
                }
                SafeOrderedReturn toreturn = new SafeOrderedReturn();
                toreturn.Batches        = batches;
                toreturn.IsPendingBatch = ispendingbatch;
                return(toreturn);
            }
            catch
            {
                int a = 1;
                return(new SafeOrderedReturn());
            }
        }
Exemplo n.º 2
0
        //Create block out of batch
        unsafe public Block CreateNextBlock(Batch newbatch)
        {
            byte[]    previousblockhash = BlockHeaderHash(PreviousBlockHeader);
            BlockData blockdata         = new BlockData();

            byte[][] data = new byte[newbatch.MsgCount][];
            //Convert each message to byte format to add to data
            Parallel.For(0, newbatch.MsgCount, i =>
            {
                SafeEnvelope safe   = new SafeEnvelope();
                List <byte> pllist  = new List <byte>();
                List <byte> siglist = new List <byte>();
                int j = 0;
                while (true)
                {
                    if (newbatch.Messages[i].Payload[j] == 0)
                    {
                        break;
                    }
                    else
                    {
                        pllist.Add(newbatch.Messages[i].Payload[j]);
                    }
                    j++;
                }
                j = 0;
                while (true)
                {
                    if (newbatch.Messages[i].Signature[j] == 0)
                    {
                        break;
                    }
                    else
                    {
                        siglist.Add(newbatch.Messages[i].Signature[j]);
                    }
                    j++;
                }
                safe.Payload   = pllist.ToArray();
                safe.Signature = siglist.ToArray();
                data[i]        = Marshal(safe);
            });
            blockdata.Data = data;
            Block newblock = NewBlock(PreviousBlockHeader.Number + 1, previousblockhash);

            newblock.Header.DataHash = BlockDataHash(blockdata);
            newblock.Data            = blockdata;
            PreviousBlockHeader      = newblock.Header;
            return(newblock);
        }
Exemplo n.º 3
0
 //Add message to pending batch
 private void AddToPending(SafeEnvelope msg, ulong size, int index)
 {
     PendingBatch.Messages[PendingBatch.MsgCount] = msg;
     Interlocked.Increment(ref PendingBatch.MsgCount);
     PendingBatchSizeBytes += size;
 }