예제 #1
0
        static async Task ReadPipeAsync(PipeReader reader, CsvWriter csvWriter, Mapper mapper)
        {
            while (true)
            {
                var result = await reader.ReadAsync();

                ReadOnlySequence <byte> buffer = result.Buffer;

                while (TryReadCarUpdateDatagram(ref buffer, out var completeDatagram))
                {
                    var carData = StructSerializer
                                  .Deserialize <CarUpdateDatagram>(completeDatagram.ToArray(), 0);

                    var carModel = mapper.Map <CarStateModel>(carData);
                    csvWriter.NextRecord();
                    csvWriter.WriteRecord(carModel);
                }

                reader.AdvanceTo(buffer.Start, buffer.End);

                if (result.IsCompleted)
                {
                    break;
                }
            }

            await reader.CompleteAsync();
        }
예제 #2
0
        protected override DataChunk Read(IFileReader dataReader)
        {
            long            offset;
            GzipChunkPrefix prefix;
            DataChunk       dataChunk = null;

            lock (_locker)
            {
                if (_readOffset >= dataReader.Size)
                {
                    return(null);
                }

                var prefixSize  = StructSerializer.GetSize <GzipChunkPrefix>();
                var prefixBytes = dataReader.ReadChunk(_readOffset, prefixSize);
                prefix      = StructSerializer.Deserialize <GzipChunkPrefix>(prefixBytes);
                offset      = _readOffset + prefixSize;
                _readOffset = offset + prefix.GzipDataSize;
            }


            var decompressedData = new byte[prefix.ChunkSize];
            var data             = dataReader.ReadChunk(offset, prefix.GzipDataSize);

            using (MemoryStream memoryStream = new MemoryStream(data))
            {
                using (GZipStream decompressionStream = new GZipStream(memoryStream, CompressionMode.Decompress, leaveOpen: true))
                {
                    var decompressedDataSize = decompressionStream.Read(decompressedData, 0, prefix.ChunkSize);

                    if (decompressedDataSize != prefix.ChunkSize)
                    {
                        Console.WriteLine("The specified compressed file is malformed.");
                    }
                }

                dataChunk = new DataChunk(prefix.Offset, decompressedData);
            }

            return(dataChunk);
        }
예제 #3
0
 /// <summary>
 /// Convert the bytes to a structure in host-endian format (little-endian on PCs).
 /// To use with big-endian data, reverse all of the data bytes and create a struct that is in the reverse order of the data.
 /// </summary>
 /// <typeparam name="T">The structure's type</typeparam>
 /// <param name="source">The buffer.</param>
 /// <returns></returns>
 public static T ToStruct <T>([NotNull] this byte[] source) where T : struct => StructSerializer.Deserialize <T>(source.GetOrThrowIfNull(nameof(source)));
예제 #4
0
        static async Task Main(string[] args)
        {
            // udp server configuration
            var serverIpAddress = IPAddress.Parse("192.168.1.53");
            var serverPort      = 9996;
            var serverEndpoint  = new IPEndPoint(serverIpAddress, serverPort);

            // automapper initialization for mapping interop Datagrams => Models
            // NTS: Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
            MapperConfiguration config = new MapperConfiguration(
                cfg =>
            {
                cfg.CreateMap <HandshakeResponseDatagram, HandshakeResponseModel>();
                cfg.CreateMap <CarUpdateDatagram, CarStateModel>();
            }
                );

            var mapper = new Mapper(config);

            var handshakeRequest = new TelemetryServerConfigurationDatagram()
            {
                identifier  = PlatformType.Web,
                version     = 1,
                operationId = OperationType.Handshake
            };

            // serialize request
            var requestByteArray = StructSerializer.Serialize(handshakeRequest);

            // send request and block for response
            using var client = new UdpClient();
            client.Connect(serverEndpoint);
            client.Send(requestByteArray, Marshal.SizeOf(typeof(TelemetryServerConfigurationDatagram)));
            var responseByteArray = client.Receive(ref serverEndpoint);

            // deserialize and map to model
            var handshakeResponse = StructSerializer.Deserialize <HandshakeResponseDatagram>(responseByteArray, 0);
            var handshakeOutput   = mapper.Map <HandshakeResponseModel>(handshakeResponse);

            // print result of handshake
            Console.WriteLine(handshakeOutput);

            var updateRequest = new TelemetryServerConfigurationDatagram()
            {
                identifier  = PlatformType.Web,
                operationId = OperationType.SubscriberUpdate,
                version     = 1
            };

            requestByteArray = StructSerializer.Serialize(updateRequest);
            await client.SendAsync(requestByteArray, Marshal.SizeOf(typeof(TelemetryServerConfigurationDatagram)));

            var path = $"telemetry/" +
                       $"{handshakeOutput.DriverName}/" +
                       $"{handshakeOutput.TrackName}/" +
                       $"{handshakeOutput.CarName}/";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using var streamWriter = new StreamWriter($"{path}/{DateTime.Now.ToFileTime()}.csv");
            using var csvWriter    = new CsvWriter(streamWriter, CultureInfo.InvariantCulture);
            csvWriter.WriteHeader(typeof(CarStateModel));

            Console.CancelKeyPress += (sender, e) =>
            {
                csvWriter.Flush();
                streamWriter.Close();
                csvWriter.Dispose();
                streamWriter.Dispose();

                var disconnect = new TelemetryServerConfigurationDatagram()
                {
                    identifier  = PlatformType.Web,
                    operationId = OperationType.Dismiss,
                    version     = 1
                };

                requestByteArray = StructSerializer.Serialize(disconnect);
                client.Send(requestByteArray, Marshal.SizeOf(typeof(TelemetryServerConfigurationDatagram)));
                Console.WriteLine("Exiting Gracefully...");
                Environment.Exit(0);
            };

            await ProcessSubscriberUpdates(client, csvWriter, mapper);
        }