Example #1
0
        internal void Discover(NetPeer peer)
        {
            string content =
                "M-SEARCH * HTTP/1.1\r\n" +
                "HOST: 239.255.255.250:1900\r\n" +
                "ST:upnp:rootdevice\r\n" +
                "MAN:\"ssdp:discover\"\r\n" +
                "MX:3\r\n\r\n";

            m_discoveryResponseDeadline = NetTime.Now + 6.0; // arbitrarily chosen number, router gets 6 seconds to respond
            m_status = UPnPStatus.Discovering;

            byte[] raw = System.Text.Encoding.UTF8.GetBytes(content);

            m_peer.LogDebug("Attempting UPnP discovery");
            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);

            if (peer.Configuration.BroadcastAddress.Equals(IPAddress.Broadcast))
            {
                foreach (IPAddress address in NetUtility.GetBroadcastAddresses())
                {
                    peer.RawSend(raw, 0, raw.Length, new NetEndPoint(address, 1900));
                }
            }
            else
            {
                peer.RawSend(raw, 0, raw.Length, new NetEndPoint(peer.Configuration.BroadcastAddress, 1900));
            }

            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);
        }
Example #2
0
        /// <summary>
        /// Emit a discovery signal to all hosts on your subnet
        /// </summary>
        public void DiscoverLocalPeers(int serverPort)
        {
            NetOutgoingMessage um = CreateMessage(0);

            um.m_messageType = NetMessageType.Discovery;
            Interlocked.Increment(ref um.m_recyclingCount);

            if (m_configuration.Equals(IPAddress.Broadcast))
            {
                foreach (IPAddress address in NetUtility.GetBroadcastAddresses())
                {
                    m_unsentUnconnectedMessages.Enqueue(new NetTuple <NetEndPoint, NetOutgoingMessage>(new NetEndPoint(address, serverPort), um));
                }
            }
            else
            {
                m_unsentUnconnectedMessages.Enqueue(new NetTuple <NetEndPoint, NetOutgoingMessage>(new NetEndPoint(m_configuration.BroadcastAddress, serverPort), um));
            }
        }