Example #1
0
        internal void SendPartialPacket(PartialPacket p)
        {
            byte[] type       = { (byte)p.Type };
            byte[] hasId      = { p.HasId ? (byte)1 : (byte)0 };
            byte[] isbuffered = { 1 };
            byte[] length     = BitConverter.GetBytes(p.Data.Length);

            byte[] posBytes         = BitConverter.GetBytes(p.Position);
            byte[] isEndBytes       = p.IsEnd ? new byte[] { 1 } : new byte[] { 0 };
            byte[] totalLengthBytes = BitConverter.GetBytes(p.TotalDataSize);

            byte[] arrData = p.Data;

            byte[] b;
            if (p.HasId)
            {
                b = ByteArrayHelpers.Combine(type, hasId, p.Id, isbuffered, length, posBytes, isEndBytes, totalLengthBytes, arrData);
            }
            else
            {
                b = ByteArrayHelpers.Combine(type, hasId, isbuffered, length, posBytes, isEndBytes, totalLengthBytes, arrData);
            }

            _stream.Write(b, 0, b.Length);
        }
Example #2
0
        public Hash GetSummarizedStateHash()
        {
            if (InlineTraces.Count == 0)
            {
                return(StateHash ?? Hash.Default);
            }

            var hashes = new List <Hash>()
            {
                StateHash
            };

            hashes.AddRange(InlineTraces.Select(x => x.GetSummarizedStateHash()));
            return(Hash.FromRawBytes(ByteArrayHelpers.Combine(hashes.Select(x => x.DumpByteArray()).ToArray())));
        }
        public void Bytes_Combine_And_SubArray()
        {
            var byteArray1 = Hash.Generate().DumpByteArray();
            var byteArray2 = Hash.Generate().DumpByteArray();
            var bytes      = ByteArrayHelpers.Combine(byteArray1, byteArray2);

            bytes.Length.ShouldBe(byteArray1.Length + byteArray2.Length);

            var bytes1 = ByteArrayHelpers.ConcatArrays(byteArray1, byteArray2, bytes);

            bytes1.Length.ShouldBe(byteArray1.Length + byteArray2.Length + bytes.Length);

            var subArray1 = ByteArrayHelpers.SubArray(bytes, 0, byteArray1.Length);
            var subArray2 = ByteArrayHelpers.SubArray(bytes, byteArray1.Length, byteArray2.Length);

            subArray1.ShouldBe(byteArray1);
            subArray2.ShouldBe(byteArray2);
        }
Example #4
0
        public static async Task CommitChangesAsync(this TransactionTrace trace, IStateStore stateStore)
        {
            if (trace.ExecutionStatus != ExecutionStatus.ExecutedButNotCommitted)
            {
                throw new InvalidOperationException(
                          $"Attempting to commit a trace with a wrong status {trace.ExecutionStatus}.");
            }

            if (trace.StateChanges.Count > 0)
            {
                await stateStore.PipelineSetDataAsync(trace.StateChanges.ToDictionary(x => x.StatePath, x => x.StateValue.CurrentValue.ToByteArray()));
            }

            trace.StateHash       = Hash.FromRawBytes(ByteArrayHelpers.Combine(trace.StateChanges.Select(x => x.StatePath.GetHash()).OrderBy(x => x).Select(x => x.Value.ToByteArray()).ToArray()));
            trace.ExecutionStatus = ExecutionStatus.ExecutedAndCommitted;
            foreach (var trc in trace.InlineTraces)
            {
                await trc.CommitChangesAsync(stateStore);
            }
        }
Example #5
0
        internal void SendPacketFromMessage(Message p)
        {
            byte[] type       = { (byte)p.Type };
            byte[] hasId      = { p.HasId ? (byte)1 : (byte)0 };
            byte[] isbuffered = { 0 };
            byte[] length     = BitConverter.GetBytes(p.Length);
            byte[] arrData    = p.Payload;

            byte[] b;

            if (p.HasId)
            {
                b = ByteArrayHelpers.Combine(type, hasId, p.Id, isbuffered, length, arrData);
            }
            else
            {
                b = ByteArrayHelpers.Combine(type, hasId, isbuffered, length, arrData);
            }

            _stream.Write(b, 0, b.Length);
        }
Example #6
0
        /// <summary>
        /// Reads the bytes from the stream.
        /// </summary>
        private async Task Read()
        {
            try
            {
                while (true)
                {
                    // Read type
                    int type = await ReadByte();

                    // Read if the message is associated with an id
                    bool hasId = await ReadBoolean();

                    byte[] id = null;
                    if (hasId)
                    {
                        // The Id is a 128-bit guid
                        id = await ReadBytesAsync(IdLength);
                    }

                    // Is this a partial reception ?
                    bool isBuffered = await ReadBoolean();

                    // Read the size of the data
                    int length = await ReadInt();

                    if (isBuffered)
                    {
                        // If it's a partial packet read the packet info
                        PartialPacket partialPacket = await ReadPartialPacket(length);

                        // todo property control

                        if (!partialPacket.IsEnd)
                        {
                            _partialPacketBuffer.Add(partialPacket);

                            if (_partialPacketBuffer.Count == 0)
                            {
                                _logger.Trace($"Received first packet: {partialPacket.Type}, total size: {partialPacket.TotalDataSize}.");
                            }
                        }
                        else
                        {
                            // This is the last packet
                            // Concat all data

                            _partialPacketBuffer.Add(partialPacket);

                            byte[] allData =
                                ByteArrayHelpers.Combine(_partialPacketBuffer.Select(pp => pp.Data).ToArray());

                            _logger.Trace($"Received last packet: {_partialPacketBuffer.Count}, total length: {allData.Length}.");

                            // Clear the buffer for the next partial to receive
                            _partialPacketBuffer.Clear();

                            Message message;
                            if (hasId)
                            {
                                message = new Message {
                                    Type = type, HasId = true, Id = id, Length = allData.Length, Payload = allData
                                };
                            }
                            else
                            {
                                message = new Message {
                                    Type = type, HasId = false, Length = allData.Length, Payload = allData
                                };
                            }

                            FireMessageReceivedEvent(message);
                        }
                    }
                    else
                    {
                        // If it's not a partial packet the next "length" bytes should be
                        // the entire data

                        byte[] packetData = await ReadBytesAsync(length);

                        Message message;
                        if (hasId)
                        {
                            message = new Message {
                                Type = type, HasId = true, Id = id, Length = length, Payload = packetData
                            };
                        }
                        else
                        {
                            message = new Message {
                                Type = type, HasId = false, Length = length, Payload = packetData
                            };
                        }

                        FireMessageReceivedEvent(message);
                    }
                }
            }
            catch (PeerDisconnectedException)
            {
                StreamClosed?.Invoke(this, EventArgs.Empty);
                Close();
            }
            catch (Exception e)
            {
                if (!IsConnected && e is IOException)
                {
                    // If the stream fails while the connection is logically closed (call to Close())
                    // we simply return - the StreamClosed event will no be closed.
                    return;
                }

                Close();
                StreamClosed?.Invoke(this, EventArgs.Empty);
            }
        }