Beispiel #1
0
        //------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Functions
        //------------------------------------------------------------------------------------------------------------------------
        public void Initialize(bool EnableDiscovery, bool EnableBroadcasting)
        {
            lock (this)
            {
                try
                {
                    //setup socket
                    if (EnableDiscovery || EnableBroadcasting)
                    {
#if NETFX
                        _sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        _sock.EnableBroadcast     = true;
                        _sock.ExclusiveAddressUse = false;
                        _sock.DontFragment        = true;
                        _sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                        if (EnableDiscovery)
                        {
                            try { _sock.Bind(new IPEndPoint(IPAddress.Any, BroadcastPort)); } catch (Exception ex2) { DebugEx.Assert(ex2, "Discovery Bind failed"); }
                        }
#elif UNIVERSAL
                        _sock = new DatagramSocket();
                        _sock.Control.DontFragment = true;
                        if (EnableDiscovery)
                        {
                            _sock.MessageReceived += _sock_MessageReceived;
                            try { _sock.BindServiceNameAsync(BroadcastPort.ToStringInvariant()).AsTask().Wait(); } catch (Exception ex2) { DebugEx.Assert(ex2, "Discovery Bind failed"); }
                        }
#endif
                    }

                    //start tasks
                    IsRunning = true;
#if NETFX
                    if (EnableDiscovery)
                    {
                        DiscoveryTask              = new Thread(DiscoveryTaskEntryPoint);
                        DiscoveryTask.Name         = "YPC LANDiscoverer discovery heartbeat";
                        DiscoveryTask.IsBackground = true;
                        DiscoveryTask.Start();
                    }
#endif

                    if (EnableBroadcasting)
                    {
#if NETFX
                        BroadcastTask              = new Thread(BroadcastTaskEntryPoint);
                        BroadcastTask.Name         = "YPC LANDiscoverer broadcast heartbeat";
                        BroadcastTask.IsBackground = true;
                        BroadcastTask.Start();
#elif UNIVERSAL
                        BroadcastTask = BroadcastTaskEntryPoint();
#endif
                    }
                }
                catch (Exception ex)
                {
                    Deinitialize();
                    DebugEx.Assert(ex);
                }
            }
        }