Beispiel #1
0
        internal void Resolve2()
        {
            Logger.Log.Info("Start P2P resolver...");
            // create a resolver object to resolve a peername
            resolver = new PeerNameResolver();
            string endpointUrl = null;

            // resolve the PeerName - this is a network operation and will block until the resolve completes
            //PeerNameRecordCollection results = resolver.Resolve(peerName, 100); // Max 100 records
            PeerNameRecordCollection results = resolver.Resolve(peerName, Cloud.Available, 100); // Max 100 records

            foreach (PeerNameRecord record in results)
            {
                foreach (IPEndPoint ep in record.EndPointCollection)
                {
                    if (ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        endpointUrl = string.Format(endpointuriformat4, ep.Address, ep.Port);
                    }
                    else
                    if (ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    //if ((ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) && !ep.Address.IsIPv6LinkLocal)
                    {
                        endpointUrl = string.Format(endpointuriformat6, ep.Address, ep.Port);
                    }

                    try
                    {
                        NetTcpBinding binding      = Helper.GetStreamBinding();
                        IP2PService   serviceProxy = ChannelFactory <IP2PService> .CreateChannel(
                            binding, new EndpointAddress(endpointUrl));

                        PeerEntry peer = new PeerEntry();
                        peer.PeerName      = record.PeerName;
                        peer.ServiceProxy  = serviceProxy;
                        peer.DisplayString = serviceProxy.GetName();
                        if (record.Comment != null)
                        {
                            peer.Comment = record.Comment;
                        }

                        if (record.Data != null)
                        {
                            peer.Data = System.Text.Encoding.ASCII.GetString(record.Data);
                        }
                        Logger.Log.Info(string.Format("SUCCESS Connect:{0} \t\t {1} ", endpointUrl, peer.Comment));

                        Vault.Peers.Add(peer);
                    }
                    catch (EndpointNotFoundException e)
                    {
                        Logger.Log.Debug(e.Message);
                    }
                }
            }
        }
Beispiel #2
0
        internal void Init()
        {
            // @need refactor
            string port = ConfigurationManager.AppSettings["port"];

            Port = Convert.ToInt32(port);
            string username = ConfigurationManager.AppSettings["username"];

            Username = username;

            string machineName = Environment.MachineName;
            string serviceUrl  = null;

            //  Getting URL-adress of service with IPv4
            //  and port from config.
            foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                //if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && !address.IsIPv6LinkLocal)
                {
                    serviceUrl = string.Format(endpointuriformat6, address, port);
                    break;
                }
            }

            // Check for null
            if (serviceUrl == null)
            {
                Logger.Log.Info("Not defined Endpoint address for WCF.", "Networking Error");
                MessageBox.Show("Not defined Endpoint address for WCF.", "Networking Error", MessageBoxButtons.OK);
            }

            // Регистрация и запуск службы WCF
            localService = new P2PService(mainform, username); // @TODO Need refactor

            host = new ServiceHost(localService, new Uri(serviceUrl));
            NetTcpBinding binding = Helper.GetStreamBinding();

            //NetTcpBinding binding = new NetTcpBinding();
            //binding.Security.Mode = SecurityMode.None;

            ///////
            ///////
            //// Stream add
            //////
            //////
            //binding.TransferMode = TransferMode.Streamed;
            //binding.MaxReceivedMessageSize = 20134217728; // 20 GB
            //binding.MaxBufferPoolSize = 1024 * 1024; // 1 MB
            //binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
            //binding.SendTimeout = new TimeSpan(1, 0, 0);
            //////
            //////
            //////
            //////

            host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl);
            try
            {
                host.Open();
            }
            catch (AddressAlreadyInUseException)
            {
                Logger.Log.Info("Can`t start listening, port is busy.", "WCF Error");
                MessageBox.Show("Can`t start listening, port is busy.", "WCF Error", MessageBoxButtons.OK);
                Application.Exit();
            }

            // Creates a secure (not spoofable) PeerName
            peerName = new PeerName(Application.ProductName, PeerNameType.Secured);
            //peerName = new PeerName(Application.ProductName + guid, PeerNameType.Unsecured);
            //peerName = new PeerName(Application.ProductName, PeerNameType.Unsecured);
            //peerName = new PeerName( Application.ProductName, PeerNameType.Secured);
            //peerName = new PeerName(Application.ProductName + "1234567890", PeerNameType.Secured);
            pnReg          = new PeerNameRegistration();
            pnReg.PeerName = peerName;
            pnReg.Cloud    = Cloud.Available;

            //pnReg.Port = (int) Config.Port;
            pnReg.Port = Port;

            // OPTIONAL

            // The properties set below are optional.
            // You can register a PeerName without setting these properties
            // but this properties can help identify PC
            //pnReg.Comment = "up to 39 unicode char comment";
            pnReg.Comment = username.Substring(0, Math.Min(39, username.Length));
            pnReg.Data    = Encoding.UTF8.GetBytes("A data blob associated with the name"); // @TODO Add binary data for Peer2Peer Netwwork

            // OPTIONAL
            // The properties below are also optional, but will not be set (ie. are commented out) for this example

            //pnReg.IPEndPointCollection = // a list of all {IPv4/v6 address, port} pairs to associate with the peername
            //pnReg.Cloud = //the scope in which the name should be registered (local subnet, internet, etc)
            pnReg.Cloud = Cloud.Global; // resolve in Global Network -
            //pnReg.Cloud = Cloud.Available; // resolve in Global Network -
        }
Beispiel #3
0
        internal void Resolve2_old()
        {
            Logger.Log.Info("Start P2P resolver...");
            // create a resolver object to resolve a peername
            resolver = new PeerNameResolver();

            // resolve the PeerName - this is a network operation and will block until the resolve completes
            PeerNameRecordCollection results = resolver.Resolve(peerName, 100); // Max 100 records

            foreach (PeerNameRecord record in results)
            {
                foreach (IPEndPoint ep in record.EndPointCollection)
                {
                    //if (ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    if (ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && !ep.Address.IsIPv6LinkLocal)
                    {
                        try
                        {
                            string        endpointUrl = string.Format(endpointuriformat, ep.Address, ep.Port);
                            NetTcpBinding binding     = Helper.GetStreamBinding();

                            //NetTcpBinding binding = new NetTcpBinding();
                            ////binding.Security.Mode = SecurityMode.None;
                            //binding.Security.Mode = SecurityMode.None;
                            ////EndpointAddress epa = new EndpointAddress()
                            ///////
                            ///////
                            //// Stream add
                            //////
                            //////
                            //binding.TransferMode = TransferMode.Streamed;
                            //binding.MaxReceivedMessageSize = 20134217728; // 20 GB
                            //binding.MaxBufferPoolSize = 1024 * 1024; // 1 MB
                            //////
                            //////
                            //////
                            //////
                            IP2PService serviceProxy = ChannelFactory <IP2PService> .CreateChannel(
                                binding, new EndpointAddress(endpointUrl));

                            //IP2PService serviceProxy = ChannelFactory<IP2PService>.CreateChannel(
                            //    binding, new EndpointAddress(ep.Address.ToString() + ":" + Port.ToString()));

                            //PeerEntry entry = new PeerEntry
                            //{
                            //    PeerName = record.PeerName,
                            //    ServiceProxy = serviceProxy,
                            //    DisplayString = serviceProxy.GetName(),
                            //};

                            PeerEntry peer = new PeerEntry();
                            peer.PeerName      = record.PeerName;
                            peer.ServiceProxy  = serviceProxy;
                            peer.DisplayString = serviceProxy.GetName();
                            if (record.Comment != null)
                            {
                                peer.Comment = record.Comment;
                            }

                            if (record.Data != null)
                            {
                                peer.Data = System.Text.Encoding.ASCII.GetString(record.Data);
                            }


                            //Vault.Peers.Add(peer);
                            Logger.Log.Info(endpointUrl);
                            Logger.Log.Info(peer.PeerName);
                            Logger.Log.Info(peer.Comment);
                            Logger.Log.Info("\t Endpoint:{0}", ep);

                            Vault.Peers.Add(peer);
                        }
                        catch (EndpointNotFoundException e)
                        {
                            //Vault.Peers.Add(
                            //   new PeerEntry
                            //   {
                            //       PeerName = peer.PeerName,
                            //       DisplayString = "Unknown Peer",
                            //   });
                            Logger.Log.Info(e.Message);
                        }
                    }
                }
            }
        }