Beispiel #1
0
        private static ItemPayload ToPayload(DataFileRecord record)
        {
            switch (record.Type)
            {
            case DataFileRecord.CommandV1:
                return(new CommandPayload
                {
                    WhenExecuted = record.WhenExecuted,
                    Command = record.Command
                });

            case DataFileRecord.CommandV2:
                return(new CommandPayload
                {
                    MachineName = record.MachineName,
                    Pid = record.Pid,
                    WhenExecuted = record.WhenExecuted,
                    Command = record.Command,
                    Output = record.Output
                });


            case DataFileRecord.Raw:
                return(new RawPayload(record.Buffer));

            default:
                throw new InvalidOperationException($"Unexpected data file record type: 0x{record.Type:X2}");
            }
        }
Beispiel #2
0
        private long WriteRecord(DataFileRecord record, ref string stateTag, ref int binarySize)
        {
            var dataSize = record.DataSize;

            if (dataSize < 1 || dataSize > ushort.MaxValue)
            {
                throw new InvalidOperationException($"Invalid record data size: {dataSize}");
            }

            long position;
            var  size = (ushort)dataSize;

            using (var writer = GetWriterForAppend())
            {
                if (stateTag != null && StateTag != stateTag)
                {
                    return(-1L);
                }

                var stream = writer.BaseStream;
                position = stream.Position;

                WriteOpenMark(writer, size);
                var dataOffset = stream.Position;

                WriteRecordData(writer, record);
                var writtenSize = stream.Position - dataOffset;
                if (writtenSize != size)
                {
                    throw new InvalidOperationException($"Expected {size} bytes, but {writtenSize} were written.");
                }

                WriteZeros(writer, Align(writtenSize) - writtenSize);
                WriteCloseMark(writer, size);

                binarySize = (int)(stream.Position - position);
            }

            // MINOR: Slight change of concurrent modification between file closure and this read.
            stateTag = StateTag;

            return(position);
        }
Beispiel #3
0
        private DataFileRecord ReadRecord(BinaryReader reader, ref int binarySize)
        {
            var stream = reader.BaseStream;
            var offset = stream.Position;

            var size = ReadOpenMark(reader);

            size = Align(size);

            var buffer = new byte[size];
            var index  = 0;

            while (size > 0)
            {
                var read = reader.Read(buffer, index, size);
                if (read < 1 || read > size)
                {
                    throw new IOException($"Reader method received {size}, returned {read}.");
                }
                size  -= (ushort)read;
                index += read;
            }

            ReadCloseMark(reader);
            binarySize = (int)(stream.Position - offset);

            stream = new MemoryStream(buffer);
            var recordReader = new BinaryReader(stream, Encoding.UTF8);
            var result       = new DataFileRecord();

            try
            {
                result.ReadFrom(recordReader);
            }
            catch (Exception ex)
            {
                _trace.TraceError("Error reading record at {0}: {1}", offset, ex);
                result.SetRawData(buffer);
            }

            return(result);
        }
Beispiel #4
0
 private void WriteRecordData(BinaryWriter writer, DataFileRecord record)
 {
     AssertAligned(writer);
     record.WriteTo(writer);
 }