Пример #1
0
        private static Xbox CreateXbox(NapPacket packet, EndPoint remoteEndPoint)
        {
            //Check
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }
            if (remoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(remoteEndPoint));
            }
            if (packet.Type != NapType.Reply)
            {
                throw new ArgumentException("Invalid packet.", nameof(packet));
            }

            //Create
            Xbox xbox = new Xbox()
            {
                Name = packet.Name, RemoteEndPoint = (IPEndPoint)remoteEndPoint
            };

            //Return
            return(xbox);
        }
Пример #2
0
        /// <summary>
        /// Attempts to discover a debug Xbox at a specified end point within a specified time limit.
        /// </summary>
        /// <param name="remoteAddress">The remote end point of the debug Xbox console.</param>
        /// <param name="timeout">The amount of time in milliseconds to wait for responses before returning the results.</param>
        /// <returns>The </returns>
        public static Xbox Discover(IPAddress remoteAddress, int timeout = 1000)
        {
            //Check
            if (remoteAddress == null)
            {
                throw new ArgumentNullException(nameof(remoteAddress));
            }
            if (timeout < 1)
            {
                throw new ArgumentException(nameof(timeout));
            }

            //Prepare
            Xbox      xbox        = null;
            DateTime  requestTime = new DateTime();
            NapPacket response    = new NapPacket();

            byte[]   responsePacket  = new byte[NapPacket.MaxLength];
            byte[]   discoveryPacket = NapPacket.CreateDiscoveryPacket().GetPacket();
            EndPoint remoteEndPoint  = new IPEndPoint(remoteAddress, NapPort);
            Socket   socket          = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                EnableBroadcast = true
            };

            socket.Bind(localEndPoint);

            //Send
            socket.SendTo(discoveryPacket, remoteEndPoint);

            //Loop
            requestTime = DateTime.Now;
            do
            {
                //Sleep
                Thread.Sleep(1);
                while (socket.Available > 0)
                {
                    responsePacket = new byte[socket.Available];
                    socket.ReceiveFrom(responsePacket, ref remoteEndPoint);
                    response.SetPacket(responsePacket);
                    if (response.Type == NapType.Reply)
                    {
                        //Close
                        socket.Close();
                        return(CreateXbox(response, remoteEndPoint));
                    }
                }
            }while ((DateTime.Now - requestTime).TotalMilliseconds < timeout);

            //Close
            socket.Close();
            return(xbox);
        }
Пример #3
0
        /// <summary>
        /// Attempts to discover debug Xbox consoles within a specified time limit.
        /// </summary>
        /// <param name="timeout">The amount of time in milliseconds to wait for responses before returning the results.</param>
        /// <returns>An array of discovered <see cref="Xbox"/> elements.</returns>
        public static Xbox[] Discover(int timeout = 1000)
        {
            DateTime  requestTime = new DateTime();
            NapPacket response    = new NapPacket();

            byte[]        responsePacket  = new byte[NapPacket.MaxLength];
            byte[]        discoveryPacket = NapPacket.CreateDiscoveryPacket().GetPacket();
            EndPoint      remoteEndPoint  = localEndPoint;
            List <Socket> sockets         = new List <Socket>();
            List <Xbox>   xboxes          = new List <Xbox>();

            foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation ua in networkInterface.GetIPProperties().UnicastAddresses)
                {
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                    {
                        EnableBroadcast = true
                    };

                    try
                    {
                        socket.Bind(new IPEndPoint(ua.Address, 0));
                        socket.SendTo(discoveryPacket, discoveryEndPoint);
                        sockets.Add(socket);
                    }
                    catch { }
                }
            }

            requestTime = DateTime.Now;
            do
            {
                Thread.Sleep(1);
                foreach (Socket socket in sockets)
                {
                    while (socket.Available > 0)
                    {
                        responsePacket = new byte[socket.Available];
                        socket.ReceiveFrom(responsePacket, ref remoteEndPoint);
                        response.SetPacket(responsePacket);
                        if (response.Type == NapType.Reply)
                        {
                            xboxes.Add(CreateXbox(response, remoteEndPoint));
                        }
                    }
                }
            }while ((DateTime.Now - requestTime).TotalMilliseconds < timeout);

            sockets.ForEach(s => s.Close());
            sockets.Clear();
            return(xboxes.ToArray());
        }
Пример #4
0
        /// <summary>
        /// Attempts to discover debug Xbox consoles with the specified name within a specified time limit.
        /// </summary>
        /// <param name="name">The name of the debug Xbox console or domain name.</param>
        /// <param name="timeout">The amount of time in milliseconds to wait for responses before returning the results.</param>
        /// <returns>An array of discovered <see cref="Xbox"/> elements.</returns>
        public static Xbox[] Discover(string name, int timeout = 1000)
        {
            //Check
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (name == string.Empty)
            {
                throw new ArgumentException(nameof(name));
            }
            if (timeout < 1)
            {
                throw new ArgumentException(nameof(timeout));
            }

            //Prepare
            DateTime    requestTime = new DateTime();
            List <Xbox> xboxes      = new List <Xbox>();
            NapPacket   response    = new NapPacket();

            byte[]        responsePacket  = new byte[NapPacket.MaxLength];
            byte[]        discoveryPacket = NapPacket.CreateLookupPacket(name).GetPacket();
            EndPoint      remoteEndPoint  = discoveryEndPoint;
            List <Socket> sockets         = new List <Socket>();

            //Prepare
            foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation ua in networkInterface.GetIPProperties().UnicastAddresses)
                {
                    //Create Socket
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                    {
                        EnableBroadcast = true
                    };

                    //Attempt to bind and send
                    try
                    {
                        socket.Bind(new IPEndPoint(ua.Address, 0));
                        socket.SendTo(discoveryPacket, discoveryEndPoint);

                        //Add socket to list
                        sockets.Add(socket);
                    }
                    catch { }
                }
            }

            //Loop
            requestTime = DateTime.Now;
            do
            {
                //Sleep
                Thread.Sleep(1);
                foreach (Socket socket in sockets)
                {
                    while (socket.Available > 0)
                    {
                        responsePacket = new byte[socket.Available];
                        socket.ReceiveFrom(responsePacket, ref remoteEndPoint);
                        response.SetPacket(responsePacket);
                        if (response.Type == NapType.Reply)
                        {
                            xboxes.Add(CreateXbox(response, remoteEndPoint));
                        }
                    }
                }
            }while ((DateTime.Now - requestTime).TotalMilliseconds < timeout);

            //Close
            sockets.ForEach(s => s.Close());
            sockets.Clear();

            //Return
            return(xboxes.ToArray());
        }