コード例 #1
0
        protected override void Loop(CancellationToken token)
        {
            using (var tcpClient = new TcpClient(_networkConfiguration.DroneHostname, ControlPort))
            using (NetworkStream stream = tcpClient.GetStream())
            {
                var buffer = new byte[NetworkBufferSize];
                Stopwatch swConfigTimeout = Stopwatch.StartNew();
                while (token.IsCancellationRequested == false && swConfigTimeout.ElapsedMilliseconds < ConfigTimeout)
                {
                    int offset = 0;
                    if (tcpClient.Available > 0)
                    {
                        offset += stream.Read(buffer, offset, buffer.Length);

                        // config eof check
                        if (offset > 0 && buffer[offset - 1] == 0x00)
                        {
                            var data = new byte[offset];
                            Array.Copy(buffer, data, offset);
                            var packet = new ConfigurationPacket
                                {
                                    Timestamp = DateTime.UtcNow.Ticks,
                                    Data = data
                                };
                            _configurationAcquired(packet);

                            return;
                        }
                    }
                    Thread.Sleep(10);
                }
            }
        }
コード例 #2
0
 public static bool TryUpdate(DroneConfiguration configuration, ConfigurationPacket packet)
 {
     bool updated = false;
     using (var ms = new MemoryStream(packet.Data))
     using (var sr = new StreamReader(ms))
     {
         string line;
         while ((line = sr.ReadLine()) != null)
         {
             Match match = _reKeyValue.Match(line);
             if (match.Success)
             {
                 string key = match.Groups["key"].Value;
                 IConfigurationItem item;
                 if (configuration.Items.TryGetValue(key, out item))
                 {
                     string value = match.Groups["value"].Value;
                     if (item.TryUpdate(value))
                     {
                         updated = true;
                     }
                 }
                 else
                 {
                     Trace.TraceWarning("Configuration key {0} is not supported by parser. Original line: {1}", key, line);
                 }
             }
         }
     }
     return updated;
 }