Beispiel #1
0
        /// <summary>
        /// Fetches a UPnP device from the network
        /// </summary>
        /// <returns></returns>
        private static async Task <Open.Nat.NatDevice> GetUPnPDevice()
        {
            var discoverer = new Open.Nat.NatDiscoverer();

            return(await discoverer.DiscoverDeviceAsync(Open.Nat.PortMapper.Upnp, CancellationTokenSource));
        }
Beispiel #2
0
        public P2PClient(string pubNubPubKey, string pubNubSubKey)
        {
            connectedPeers      = new Dictionary <string, P2PPeer>();
            uniqueIdsPubNubSeen = new HashSet <string>();
            localIp             = GetLocalIPAddress();
            externalIp          = GetExternalIp();

            /*
             * var discoverer = new NatDiscoverer();
             * var device = discoverer.DiscoverDeviceAsync().Result;
             *
             * var ip = device.GetExternalIPAsync().Result;
             * Debug.Log("The external IP Address is: " + ip);
             */

            var discoverer = new Open.Nat.NatDiscoverer();
            var device     = discoverer.DiscoverDeviceAsync().Result;

            IPAddress localAddr   = IPAddress.Parse(localIp);
            int       workingPort = -1;

            for (int i = 0; i < ports.Length; i++)
            {
                try
                {
                    // Test tcp with  nc -vz externalip 5293 in linux
                    //tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    // Test udp with  nc -vz -u externalip 5293 in linux
                    Socket tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    tempServer.Bind(new IPEndPoint(localAddr, ports[i]));
                    tempServer.Close();
                    workingPort = ports[i];
                    break;
                }
                catch
                {
                }
            }


            if (workingPort == -1)
            {
                throw new Exception("Failed to connect to a port");
            }

            localPort    = workingPort;
            externalPort = workingPort;

            // Mapping ports


            device.CreatePortMapAsync(new Open.Nat.Mapping(Open.Nat.Protocol.Udp, localPort, externalPort));


            Socket    tempServera;
            IPAddress localAddra = IPAddress.Parse(localIp);

            try
            {
                // Test tcp with  nc -vz externalip 5293 in linux
                //tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                // Test udp with  nc -vz -u externalip 5293 in linux
                tempServera = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                tempServera.Bind(new IPEndPoint(localAddra, localPort));
                server = tempServera;
                if (stopped)
                {
                    server.Close();
                    server = null;
                }
            }
            catch
            {
                server = null;
            }

            if (server == null)
            {
                throw new Exception("failed: " + localPort + " " + externalPort);
            }
            udpClient = new UdpClient(localPort);


            pubnub = new Pubnub(pubNubPubKey, pubNubSubKey);
            pubnub.Subscribe <string>(
                channelName,
                OnPubNubMessage,
                OnPubNubConnect,
                OnPubNubError);
            Debug.Log("Initialized P2P Client");
        }