Пример #1
0
 public InfoChangedEventArgs(DroneInfo info)
 {
     this.Info = info;
 }
Пример #2
0
        private void HandlePacket(byte[] packet)
        {
            // jedes Drohnen Paket ist mindestens HeaderSize Bytes lang und fangen mit "FLY" an
            using (MemoryStream stream = new MemoryStream(packet))
            {
                PacketBuffer buffer = new PacketBuffer(stream);

                if (packet.Length < HeaderSize || buffer.ReadByte() != 'F' || buffer.ReadByte() != 'L' || buffer.ReadByte() != 'Y')
                {
                    return;
                }

                int revision = buffer.ReadInt();

                bool       isGuaranteed = buffer.ReadByte() > 0;
                PacketType type         = (PacketType)buffer.ReadByte();

                if (Config.VerbosePacketReceive &&
                    type != PacketType.Ack &&
                    (type != PacketType.Ping || Config.LogPingPacket))
                {
                    Log.Verbose("[{0}] Received: [{1}] {2}, size: {3} bytes", Address.ToString(), revision, type, packet.Length);
                }

                switch (type)
                {
                case PacketType.Ping:
                    if (packet.Length < HeaderSize + sizeof(long))
                    {
                        throw new InvalidDataException("Packet is not long enough.");
                    }

                    bool wasNotConnected = !CheckConnection();

                    lastPing = Environment.TickCount;

                    long time = buffer.ReadLong();     // time ist der Wert von stopwatch zum Zeitpunkt des Absenden des Pakets
                    Ping = (int)(stopwatch.ElapsedMilliseconds - time);

                    if (wasNotConnected)
                    {
                        OnConnected?.Invoke(this, EventArgs.Empty);
                    }

                    RemovePacketToAcknowlegde(revision);
                    break;

                case PacketType.Ack:
                    IPacket acknowlegdedPacket;
                    if (!packetsToAcknowledge.TryGetValue(revision, out acknowlegdedPacket))
                    {
                        if (Config.VerbosePacketReceive)
                        {
                            Log.Verbose("[{0}] Unknown acknowlegde: [{1}]", Address.ToString(), revision);
                        }
                        break;
                    }

                    if (Config.VerbosePacketReceive)
                    {
                        Log.Verbose("[{0}] Acknowlegde: [{1}] {2}", Address.ToString(), revision, acknowlegdedPacket.Type);
                    }

                    RemovePacketToAcknowlegde(revision);
                    break;

                case PacketType.Info:
                    Info     = new DroneInfo(buffer);
                    Settings = DroneSettings.Read(buffer);

                    RemovePacketToAcknowlegde(revision);
                    break;

                default:
                    throw new InvalidDataException("Invalid packet type to get sent by cluster.");
                }
            }
        }
Пример #3
0
 public InfoChangedEventArgs(Drone drone)
 {
     this.Info = drone.Info;
 }
Пример #4
0
        private void HandlePacket(byte[] packet)
        {
            // jedes Drohnen Paket ist mindestens HeaderSize Bytes lang und fangen mit "FLY" an
            using (MemoryStream stream = new MemoryStream(packet))
            {
                PacketBuffer buffer = new PacketBuffer(stream);

                if (packet.Length < HeaderSize || buffer.ReadByte() != 'F' || buffer.ReadByte() != 'L' || buffer.ReadByte() != 'Y')
                {
                    return;
                }

                int revision = buffer.ReadInt();

                bool       isGuaranteed = buffer.ReadByte() > 0;
                PacketType type         = (PacketType)buffer.ReadByte();

                if (Config.VerbosePacketReceive &&
                    type != PacketType.Ack &&
                    (Config.LogPingPacket || type != PacketType.Ping) &&
                    (Config.LogNoisyPackets || !type.IsNosiy()))
                {
                    Log.Verbose("[{0}] Received:    [{1}] {2}, size: {3} bytes", Address.ToString(), revision, type, packet.Length);
                }

                switch (type)
                {
                case PacketType.Ping:
                    bool wasNotConnected = !CheckConnection();
                    lastPing = Environment.TickCount;

                    long time = 0;
                    if (packet.Length >= HeaderSize + sizeof(long))
                    {
                        time = buffer.ReadLong();     // time ist der Wert von stopwatch zum Zeitpunkt des Absenden des Pakets
                    }
                    else
                    {
                        Log.Error("Invalid ping packet received with length: {0}", packet.Length);
                    }

                    int ping = (int)(stopwatch.ElapsedMilliseconds - time);
                    if (ping < 0)
                    {
                        Log.Warning("Invalid ping value received: {0}", ping);
                        ping = 0;
                    }
                    Ping = ping;

                    if (wasNotConnected)
                    {
                        OnConnected?.Invoke(this, EventArgs.Empty);
                    }

                    RemovePacketToAcknowledge(revision);
                    break;

                case PacketType.Ack:
                    IPacket acknowlegdedPacket;
                    if (!packetsToAcknowledge.TryGetValue(revision, out acknowlegdedPacket))
                    {
                        if (Config.VerbosePacketReceive)
                        {
                            Log.Verbose("[{0}] Unknown acknowledge: [{1}]", Address.ToString(), revision);
                        }
                        break;
                    }

                    if (Config.VerbosePacketReceive)
                    {
                        Log.Verbose("[{0}] Acknowledge: [{1}] {2}", Address.ToString(), revision, acknowlegdedPacket.Type);
                    }

                    RemovePacketToAcknowledge(revision);
                    break;

                case PacketType.Info:
                    Info     = new DroneInfo(buffer);
                    Settings = DroneSettings.Read(buffer);

                    if (firstInfo)
                    {
                        Log.Info("Received drone info for first time...");
                        Log.WriteProperties(LogLevel.Info, Info);

                        firstInfo = false;
                    }

                    RemovePacketToAcknowledge(revision);
                    break;

                default:
                    throw new InvalidDataException("Invalid packet type received.");
                }
            }
        }