コード例 #1
0
        private static void ProcessUDPPackets_ThreadFunction()
        {
            while (initialized)
            {
                UDPVitaPacket packet;
                bool          try_dequeue_result = false;

                _semNewPacket.WaitOne();
                while (try_dequeue_result = UDPCallbackQueue.TryDequeue(out packet))
                {
                    //if (UDPCallbackQueue.Count > 1)
                    //    Debug.WriteLine("*****UDPCallbackQueue.Count = " + UDPCallbackQueue.Count);

                    // ensure that the packet is at least long enough to inspect for VITA info
                    if (packet.Data.Length < 16)
                    {
                        continue;
                    }

                    VitaPacketPreamble vita_preamble = new VitaPacketPreamble(packet.Data);

                    // ensure the packet has our OUI in it -- looks like it came from us
                    if (vita_preamble.class_id.OUI != FLEX_OUI)
                    {
                        continue;
                    }

                    if (!_radioDictionaryByIP.ContainsKey(packet.Ep.Address))
                    {
                        continue;
                    }

                    Radio r = null;
                    try // catch race condition where the radio is removed before we access the dictionary
                    {
                        r = _radioDictionaryByIP[packet.Ep.Address];
                    }
                    catch (Exception)
                    {
                        // do nothing -- don't really care since the radio is going away, just don't show a UHE
                    }

                    if (r != null)
                    {
                        r.UDPDataReceivedCallback(vita_preamble, packet.Data, packet.Bytes);
                    }
                }
            }
        }
コード例 #2
0
ファイル: Discovery.cs プロジェクト: N5FPP/FlexlibMono
        private static void Receive()
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);

            while (active)
            {
                byte[] data = udp.Receive(ref ep);
                //Debug.WriteLine("UDP Received: " + buf.Length);

                // since the call above is blocking, we need to check active again here
                if (!active)
                {
                    break;
                }

                //bool vita_discovery = false;

                // ensure that the packet is at least long enough to inspect for VITA info
                if (data.Length >= 16)
                {
                    VitaPacketPreamble vita = new VitaPacketPreamble(data);

                    // ensure the packet has our OUI in it -- looks like it came from us
                    if (vita.class_id.OUI == VitaFlex.FLEX_OUI)
                    {
                        // handle discovery packets here
                        switch (vita.header.pkt_type)
                        {
                        case VitaPacketType.ExtDataWithStream:
                            switch (vita.class_id.PacketClassCode)
                            {
                            case VitaFlex.SL_VITA_DISCOVERY_CLASS:
                                ProcessVitaDiscoveryDataPacket(new VitaDiscoveryPacket(data, data.Length));
                                //vita_discovery = true;
                                break;
                            }
                            break;
                        }
                    }
                }

                /* No longer supporting older discovery protocol
                 *
                 * // skip any further processing if it was a vita discovery packet
                 * if (vita_discovery) continue;
                 *
                 * DiscoveryObject obj;
                 * if (data.Length != Marshal.SizeOf(typeof(DiscoveryObject))) continue;
                 *
                 * try
                 * {
                 *  //GCHandle pinnedPacket = GCHandle.Alloc(buf, GCHandleType.Pinned);
                 *  //obj = (DiscoveryObject)Marshal.PtrToStructure(
                 *  //    pinnedPacket.AddrOfPinnedObject(),
                 *  //    typeof(DiscoveryObject));
                 *  //pinnedPacket.Free();
                 *
                 *  obj.ip = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 0));
                 *  obj.port = ByteOrder.SwapBytes(BitConverter.ToUInt16(data, 4));
                 *  obj.radios = ByteOrder.SwapBytes(BitConverter.ToUInt16(data, 6));
                 *  obj.mask = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 8));
                 *  obj.model_len = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 12));
                 *  obj.model = Encoding.UTF8.GetString(data, 16, 32).Trim('\0');
                 *  obj.serial_len = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 48));
                 *  obj.serial = Encoding.UTF8.GetString(data, 52, 32).Trim('\0');
                 *  obj.name_len = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 84));
                 *  obj.name = Encoding.UTF8.GetString(data, 88, 32).Trim('\0');
                 *  obj.version_len = ByteOrder.SwapBytes(BitConverter.ToUInt32(data, 120));
                 *  obj.version = Encoding.UTF8.GetString(data, 124, 32).Trim('\0');
                 *
                 *  //Debug.WriteLine(DateTime.Now.ToLongTimeString() + " Model:" + obj.model + " ip:" + new IPAddress(obj.ip).ToString());
                 *  OnRadioDiscoveredEventHandler(new Radio(obj.model, obj.serial, "", new IPAddress(obj.ip), obj.version));
                 * }
                 * catch (Exception ex)
                 * {
                 *  Debug.WriteLine("EXCEPTION: " + ex.ToString());
                 * }
                 *
                 */
            }

            udp.Close();
            udp = null;
        }