예제 #1
0
        public async Task HandleStream(ProtocolInput protocolInput)
        {
            using StreamReader streamReader = new StreamReader(protocolInput.NetworkStream);

            while (!protocolInput.StoppingToken.IsCancellationRequested)
            {
                string data = await streamReader.ReadLineAsync();

                Location <MeitrackData> location = meitrackLocationParser.Parse(data);

                if (location != null)
                {
                    await locationService.Add(location);
                }
            }

            streamReader.Close();
        }
예제 #2
0
        public async Task HandleStream(ProtocolInput protocolInput)
        {
            using BinaryReader binaryReader = new BinaryReader(protocolInput.NetworkStream);
            await using (BinaryWriter binaryWriter = new BinaryWriter(protocolInput.NetworkStream))
            {
                byte[] data = new byte[2048];
                bool   firstDataReceived = true;

                string imei = string.Empty;

                while (binaryReader.Read() != -1 || !protocolInput.StoppingToken.IsCancellationRequested)
                {
                    binaryReader.Read(data, 0, data.Length);

                    if (firstDataReceived)
                    {
                        for (int i = 1; i <= 15; i++)
                        {
                            imei += (char)data[i];
                        }

                        binaryWriter.Write(01);
                        firstDataReceived = false;
                    }
                    else
                    {
                        List <LocationHolder> locations = teltonikaLocationParser.Convert(data, imei).ToList();

                        if (locations.Any())
                        {
                            await locationService.AddRange(locations.Select(x => x.Location).ToList());

                            string reply = "000000" + Utility.ConvertToHex(locations.Count);

                            binaryWriter.Write(Utility.HexStringToByteArray(reply));
                        }
                    }
                }

                binaryWriter.Close();
            }

            binaryReader.Close();
        }
예제 #3
0
        private async Task HandleClient(TcpClient tcpClient, IProtocol protocol, CancellationToken stoppingToken)
        {
            Connection connection = null;

            try
            {
                connection = await connectionService.NewConnection((IPEndPoint)tcpClient.Client.RemoteEndPoint);

                await using (NetworkStream networkStream = tcpClient.GetStream())
                {
                    ProtocolInput protocolInput = new ProtocolInput
                    {
                        NetworkStream = networkStream,
                        StoppingToken = stoppingToken
                    };

                    await protocol.HandleStream(protocolInput);

                    networkStream.Close();
                }

                tcpClient.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    await connectionService.MarkConnectionAsClosed(connection);
                }
            }
        }