Beispiel #1
0
        public List <Peer> ResolveByPeerName(string peerName)
        {
            try
            {
                if (string.IsNullOrEmpty(peerName))
                {
                    throw new ArgumentException("Cannot have a null or empty peer name.");
                }

                PeerNameResolver         resolver     = new PeerNameResolver();
                PeerNameRecordCollection resolvedName = resolver.Resolve(new PeerName(peerName, PeerNameType.Unsecured),
                                                                         Cloud.AllLinkLocal);

                List <Peer> foundPeers = new List <Peer>();
                foreach (PeerNameRecord foundItem in resolvedName)
                {
                    Peer peer = new Peer()
                    {
                        PeerName     = foundItem.PeerName.Classifier,
                        PeerHostName = foundItem.PeerName.PeerHostName,
                        Comments     = foundItem.Comment
                    };

                    foundPeers.Add(peer);
                }

                return(foundPeers);
            }
            catch (PeerToPeerException px)
            {
                throw new Exception(px.InnerException.Message);
            }
        }
Beispiel #2
0
        public void StartResolving()
        {
            Thread th = new Thread(() =>
            {
                while (true)
                {
                    List <IPEndPoint> res = new List <IPEndPoint>();

                    PeerNameResolver resolver        = new PeerNameResolver();
                    PeerNameRecordCollection results = resolver.Resolve(peerName, Cloud.AllLinkLocal);

                    foreach (var peer in results)
                    {
                        foreach (var item in peer.EndPointCollection)
                        {
                            if (item.AddressFamily == AddressFamily.InterNetwork)
                            {
                                res.Add(item);
                                break;
                            }
                        }
                    }

                    lock (resolverLock)
                    {
                        peers = res;
                    }

                    Thread.Sleep(10000);
                }
            });

            th.Start();
        }
Beispiel #3
0
        private void btnResolve_Click(object sender, RoutedEventArgs e)
        {
            if (txtPeerName.Text.Length > 0)
            {
                // 建立PeerName物件
                PeerName peerName = new PeerName(txtPeerName.Text);

                // 建立PeerNameResolver物件
                PeerNameResolver resolver = new PeerNameResolver();

                // 將PNRP Peer Name解析為PNRP Peer Name記錄物件
                PeerNameRecordCollection pmrcs = resolver.Resolve(peerName);

                if (pmrcs.Count > 0)
                {
                    lstPeerNameRecord.Items.Clear();
                }
                else
                {
                    MessageBox.Show("No Peer Name Record Found", "Peer Name Resolver", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                foreach (PeerNameRecord pmrc in pmrcs)
                {
                    foreach (IPEndPoint endpoint in pmrc.EndPointCollection)
                    {
                        lstPeerNameRecord.Items.Add(endpoint);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please enter Peer Name.", "Peer Name Resolver", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #4
0
        public string[] getIPAndPort(string classifier, string IPVersion)
        {
            string[] result = { "", "" };

            PeerNameRecordCollection records = getPeerNameRecords(classifier);

            foreach (PeerNameRecord record in records)
            {
                foreach (var endpoint in record.EndPointCollection)
                {
                    String address = endpoint.Address.ToString();
                    if (IPVersion == IP_v6)
                    {
                        if ((address.IndexOfAny(new char[] { ':' }) >= 0))
                        {
                            //It's IPv6
                            result[0] = address;
                            result[1] = endpoint.Port.ToString();
                        }
                    }
                    else
                    {
                        if ((address.IndexOfAny(new char[] { ':' }) < 0))
                        {
                            //It's IPv4
                            result[0] = address;
                            result[1] = endpoint.Port.ToString();
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Console.WriteLine("Usage: PeerNameResolver.exe <PeerNameToResolve>");
                    return;
                }

                // create a resolver object to resolve a Peer Name that was previously published
                PeerNameResolver resolver = new PeerNameResolver();
                // The Peer Name to resolve must be passed as the first command line argument to the application
                PeerName peerName = new PeerName(args[0]);
                // resolve the Peer Name - this is a network operation and will block until the resolve completes
                PeerNameRecordCollection results = resolver.Resolve(peerName);

                // Display the data returned by the resolve operation
                Console.WriteLine("Resolve operation complete.\n", peerName);
                Console.WriteLine("Results for PeerName: {0}", peerName);
                Console.WriteLine();

                int count = 1;
                foreach (PeerNameRecord record in results)
                {
                    Console.WriteLine("Record #{0} results...", count);

                    Console.Write("Comment:");
                    if (record.Comment != null)
                    {
                        Console.Write(record.Comment);
                    }
                    Console.WriteLine();

                    Console.Write("Data:");
                    if (record.Data != null)
                    {
                        //Assumes the data blob associated with the PeerName is made up of ASCII characters
                        Console.Write(System.Text.Encoding.ASCII.GetString(record.Data));
                    }
                    Console.WriteLine();

                    Console.WriteLine("Endpoints:");
                    foreach (IPEndPoint endpoint in record.EndPointCollection)
                    {
                        Console.WriteLine("\t Endpoint:{0}", endpoint);
                        Console.WriteLine();
                    }

                    count++;
                }

                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured while attempting to resolve the PeerName: {0}", e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
 public ResolveCompletedEventArgs(
                                         PeerNameRecordCollection peerNameRecordCollection,
                                         Exception error,
                                         bool canceled,
                                         object userToken)
     : base(error, canceled, userToken)
 {
     m_PeerNameRecordCollection = peerNameRecordCollection;
 }
Beispiel #7
0
 static void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
 {
     if (!e.Cancelled && e.Error == null && e.PeerNameRecordCollection != null)
     {
         records = e.PeerNameRecordCollection;
         DisplayResults();
         isRunning = false;
     }
 }
Beispiel #8
0
 void peerNameResolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
 {
     if (e.Cancelled || e.Error != null || e.PeerNameRecordCollection == null)
     {
         return;
     }
     Records = e.PeerNameRecordCollection;
     ResolutionCompleted(this, e);
 }
Beispiel #9
0
 static void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
 {
     if (!e.Cancelled && e.Error == null && e.PeerNameRecordCollection != null)
     {
         records = e.PeerNameRecordCollection;
         DisplayResults();
         isRunning = false;
     }
 }
Beispiel #10
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 #11
0
        /// <summary>
        /// 解析对等名称
        /// </summary>
        /// <param name="myPeerName"></param>
        public static PeerNameRecordCollection ResolverPeer(String myPeerName)
        {
            Thread.Sleep(500);
            strPeerInfos.Clear();
            // 建立PeerName实例
            PeerName peerName = new PeerName("0." + myPeerName);
            // 建立PeerNameResolver实例
            PeerNameResolver resolver = new PeerNameResolver();
            // 对PNRP Peer Name进行解析
            PeerNameRecordCollection pmrcs = resolver.Resolve(peerName);

            return(pmrcs);
        }
        public void PeerNameRegistry_ResolvePeerName()
        {
            PeerNameRecordCollection records = null;

            sut.ResolutionCompleted += (sender, args) =>
            {
                records = args.PeerNameRecordCollection;
            };
            var peerName     = sut.CreatePeerName("Test", false);
            var registration = sut.RegisterPeer(peerName, 9714);
            var peer         = sut.ResolvePeerName(peerName);

            Assert.That(records, Is.Not.Null);
        }
        /// <summary>
        /// 解析对等名称
        /// </summary>
        /// <param name="myPeerName"></param>
        public static void ResolverPeer(String myPeerName)
        {
            // 建立PeerName实例
            PeerName peerName = new PeerName("0." + myPeerName);
            // 建立PeerNameResolver实例
            PeerNameResolver resolver = new PeerNameResolver();
            // 对PNRP Peer Name进行解析
            PeerNameRecordCollection pmrcs = resolver.Resolve(peerName);

            foreach (PeerNameRecord pmrc in pmrcs)
            {
                foreach (IPEndPoint endpoint in pmrc.EndPointCollection)
                {
                    Console.WriteLine(endpoint);
                }
            }
        }
Beispiel #14
0
        public PeerNameRecordCollection getPeerNameRecords(string classifier)
        {
            PeerNameRecordCollection peers    = null;
            PeerNameResolver         resolver = new PeerNameResolver();

            Console.WriteLine("Please wait, Resolving...");

            try
            {
                peers = resolver.Resolve(new PeerName(classifier, PeerNameType.Unsecured));
            }
            catch (PeerToPeerException ex)
            {
                Console.WriteLine("PeerToPeer Exception: {0}", ex.Message);
            }

            return(peers);
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            // create a resolver object to resolve a peername
            PeerNameResolver resolver = new PeerNameResolver();
            // the peername to resolve must be passed as the first command line argument to the application
            PeerName peerName = new PeerName("3df33a18134282a04f56b887633e5a6640b7682e.RowansWebServer");
            // resolve the PeerName - this is a network operation and will block until the resolve completes
            PeerNameRecordCollection results = resolver.Resolve(peerName);

            // Display the data returned by the resolve operation
            Console.WriteLine("Results for PeerName: {0}", peerName);
            Console.WriteLine();
            int count = 1;

            foreach (PeerNameRecord record in results)
            {
                Console.WriteLine("Record #{0} results...", count);

                Console.Write("Comment:");
                if (record.Comment != null)
                {
                    Console.Write(record.Comment);
                }
                Console.WriteLine();

                Console.Write("Data:");
                if (record.Data != null)
                {
                    Console.Write(System.Text.Encoding.ASCII.GetString(record.Data));
                }
                Console.WriteLine();

                Console.WriteLine("Endpoints:");
                foreach (IPEndPoint endpoint in record.EndPointCollection)
                {
                    Console.WriteLine("\t Endpoint:{0}", endpoint);
                    Console.WriteLine();
                }

                count++;
            }

            Console.ReadKey();
        }
Beispiel #16
0
        /// <summary>
        /// 解析名称
        /// </summary>
        private void Resolve()
        {
            while (!isExit)
            {
                PeerNameRecordCollection recColl = MyPNRP.ResolverPeer(strpeerName);
                resolvedListViewItem.Clear();

                foreach (PeerNameRecord record in recColl)
                {
                    foreach (IPEndPoint endP in record.EndPointCollection)
                    {
                        if (endP.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            ListViewItem item1 = new ListViewItem(endP.ToString());

                            resolvedListViewItem.Add(item1);
                        }
                    }
                }

                //检查是否有其他终端退出,如已经退出则从列表中删除该项
                if (resolvedListViewItem.Count == 0)
                {
                    ClearItems();
                }
                else
                {
                    RemoveItem();
                }

                foreach (PeerNameRecord record in recColl)
                {
                    foreach (IPEndPoint endP in record.EndPointCollection)
                    {
                        if (endP.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            ListViewItem item1 = new ListViewItem(endP.ToString());
                            AppendItem(item1);
                        }
                    }
                }
            }
        }
Beispiel #17
0
        internal void Resolve()
        {
            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);

            foreach (PeerNameRecord record in results)
            {
                PeerEntry peer = new PeerEntry();
                if (record.Comment != null)
                {
                    peer.Comment = record.Comment;
                }

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

                //peer.Endpoints = record.EndPointCollection;
                //foreach (IPEndPoint endpoint in record.EndPointCollection)
                //{
                //    Console.WriteLine("\t Endpoint:{0}", endpoint);
                //    Console.WriteLine();
                //}
                Vault.Peers.Add(peer);

                Logger.Log.Info("Peers Resolver");
                Logger.Log.Info(peer.PeerName);
                Logger.Log.Info(peer.Comment);
                foreach (IPEndPoint endpoint in record.EndPointCollection)
                {
                    Logger.Log.Info("\t Endpoint:{0}", endpoint);
                }
                Logger.Log.Info("End P2P resolver...");
            }
        }
Beispiel #18
0
        void Init()
        {
            StringBuilder            str      = new StringBuilder();
            PeerName                 myPeer   = new PeerName("MySecurePeer", PeerNameType.Secured);
            PeerNameResolver         resolver = new PeerNameResolver();
            PeerName                 peerName = new PeerName(classifier, PeerNameType.Secured);
            PeerNameRecordCollection results  = resolver.Resolve(myPeer);

            str.AppendLine(string.Format("{0} Peers Found:", results.Count.ToString()));
            int i = 1;

            foreach (PeerNameRecord peer in results)
            {
                str.AppendLine(string.Format("{0} Peer:{1}", i++, peer.PeerName.ToString()));

                foreach (IPEndPoint ip in peer.EndPointCollection)
                {
                    str.AppendLine(string.Format("\t Endpoint: {0}, port {1}", ip.Address.ToString(), ip.Port.ToString()));
                }
            }
            textBox1.Text = str.ToString();
        }
Beispiel #19
0
        private void Search()
        {
            if (textBox_Name2.Text == "")
            {
                return;
            }

            // 初始化要搜索的资源名
            PeerName searchSeed = new PeerName("0." + textBox_Name2.Text);
            // PeerNameResolver类是将节点名解析为PeerNameRecord的值(即将通过资源名来查找资源名所在的地址,包括IP地址和端口号)
            // PeerNameRecord用来定于云中的各个节点
            PeerNameResolver myresolver = new PeerNameResolver();

            // PeerNameRecordCollection表示PeerNameRecord元素的容器
            // Resolve方法是同步的完成解析
            // 使用同步方法可能会出现界面“假死”现象
            // 解决界面假死现象可以采用多线程或异步的方式
            // 关于多线程的知识可以参考本人博客中多线程系列我前面UDP编程中有所使用
            // 在这里就不列出多线程的使用了,朋友可以自己实现,如果有问题可以留言给我一起讨论
            PeerNameRecordCollection recordCollection = myresolver.Resolve(searchSeed);

            if (recordCollection.Count == 0)
            {
                return;
            }
            PeerNameRecord record = recordCollection[0];
            ListViewItem   item   = new ListViewItem();
            string         t      = record.Comment.ToString();

            if (this.lastInfoTime != t)
            {
                this.lastInfoTime = t;
                item.SubItems.Add(record.Comment.ToString());
                item.SubItems.Add(record.PeerName.ToString().Substring(2));
                item.SubItems.Add(Encoding.UTF8.GetString(record.Data));
                listView1.Items.Add(item);
            }
        }
        /// <summary>
        /// 解析名称
        /// </summary>
        private void ResoveName(String strPeerName)
        {
            listView1.Items.Clear();
            PeerName                 myPeer  = new PeerName(strPeerName);
            PeerNameResolver         myRes   = new PeerNameResolver();
            PeerNameRecordCollection recColl = myRes.Resolve(myPeer);

            foreach (PeerNameRecord record in recColl)
            {
                foreach (IPEndPoint endP in record.EndPointCollection)
                {
                    if (endP.AddressFamily.Equals(AddressFamily.InterNetwork))
                    {
                        ListViewItem item1 = new ListViewItem();
                        item1.SubItems.Add(record.PeerName.ToString());
                        item1.SubItems.Add(endP.ToString());
                        item1.SubItems.Add(Encoding.UTF8.GetString(record.Data));
                        item1.SubItems.Add(record.PeerName.PeerHostName);
                        listView1.Items.Add(item1);
                    }
                }
            }
        }
        // 搜索资源
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbxSeed.Text == "")
            {
                MessageBox.Show("请先输入要寻找的种子资源名", "提示");
                return;
            }

            lstViewOnlinePeer.Items.Clear();
            // 初始化要搜索的资源名
            PeerName searchSeed = new PeerName("0." + tbxSeed.Text);
            // PeerNameResolver类是将节点名解析为PeerNameRecord的值(即将通过资源名来查找资源名所在的地址,包括IP地址和端口号)
            // PeerNameRecord用来定于云中的各个节点
            PeerNameResolver myresolver = new PeerNameResolver();

            // PeerNameRecordCollection表示PeerNameRecord元素的容器
            // Resolve方法是同步的完成解析
            // 使用同步方法可能会出现界面“假死”现象
            // 解决界面假死现象可以采用多线程或异步的方式
            // 关于多线程的知识可以参考本人博客中多线程系列我前面UDP编程中有所使用
            // 在这里就不列出多线程的使用了,朋友可以自己实现,如果有问题可以留言给我一起讨论
            PeerNameRecordCollection recordCollection = myresolver.Resolve(searchSeed);

            foreach (PeerNameRecord record in recordCollection)
            {
                foreach (IPEndPoint endpoint in record.EndPointCollection)
                {
                    if (endpoint.AddressFamily.Equals(AddressFamily.InterNetwork))
                    {
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(endpoint.ToString());
                        item.SubItems.Add(Encoding.UTF8.GetString(record.Data));
                        lstViewOnlinePeer.Items.Add(item);
                    }
                }
            }
        }
Beispiel #22
0
 void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
 {
     Debug.WriteLine("Completed Count: " + e.PeerNameRecordCollection.Count.ToString());
     this.Records = e.PeerNameRecordCollection;
     this.IsReady = true;
 }
Beispiel #23
0
 void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
 {
     Debug.WriteLine("Completed Count: " + e.PeerNameRecordCollection.Count.ToString());
     this.Records = e.PeerNameRecordCollection;
     this.IsReady = true;
 }
Beispiel #24
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);
                        }
                    }
                }
            }
        }
Beispiel #25
0
        static void Main(string[] args)
        {
            PeerNameResolver resolver = new PeerNameResolver();
            PeerName         peerName = new PeerName("0.peerchat");

            PeerNameRecordCollection result = resolver.Resolve(peerName);

            PeerNameRecord record;

            for (int i = 0; i < result.Count; i++)
            {
                record = result[i];
                Console.WriteLine("record #{0}", i);
                if (!string.IsNullOrEmpty(record.PeerName.PeerHostName))
                {
                    Console.WriteLine("Peer name is :{0}", record.PeerName.PeerHostName);
                }

                if (record.Comment != null)
                {
                    Console.WriteLine(record.Comment);
                }

                Console.Write("Data: ");
                if (record.Data != null)
                {
                    Console.WriteLine(Encoding.ASCII.GetString(record.Data));
                }
                else
                {
                    Console.WriteLine();
                }
                Console.WriteLine("Endpoints:");
                foreach (IPEndPoint endPoint in record.EndPointCollection)
                {
                    Console.WriteLine("Endpoint:{0}", endPoint);
                }

                // Uri uri = new Uri(string.Format("net.peer://{0}/GetName/", record.PeerName));

                // ChannelFactory<IPeerNetwork> myChanelFactory = new ChannelFactory<IPeerNetwork>(
                //new NetPeerTcpBinding(), new EndpointAddress("net.peer://peerchat/GetName"));

                // IPeerNetwork peer = myChanelFactory.CreateChannel();

                // peer.GetName();



                //Console.WriteLine();
            }

            //ChannelFactory<IPeerNetwork> myChanelFactory = new ChannelFactory<IPeerNetwork>("peerToPeer");


            PeerNetworkClient proxy = new PeerNetworkClient();

            proxy.GetName();



            Console.ReadLine();
            Console.WriteLine("Hit [Enter] to exit.");
        }
Beispiel #26
0
 private static IEnumerable <PeerNodeData <T> > GetDatas(PeerNameRecordCollection peerNameRecords)
 {
     return(peerNameRecords.Select(record => new PeerNodeData <T>(record)));
 }
Beispiel #27
0
 private void AddLog(PeerNameRecordCollection peerNameRecordCollection)
 {
     foreach (PeerNameRecord peerNameRecord in peerNameRecordCollection)
     {
         AddLog(peerNameRecord);
     }
 }
Beispiel #28
0
        public PeerNameRecordCollection Resolve(PeerName peerName, Cloud cloud, int maxRecords)
        {
            
            //---------------------------------------------------
            //Check arguments
            //---------------------------------------------------
            if (peerName == null)
            {
                throw new ArgumentNullException(SR.GetString(SR.Pnrp_PeerNameCantBeNull), "peerName");
            }

            if (maxRecords <= 0)
            {
                throw new ArgumentOutOfRangeException("maxRecords", SR.GetString(SR.Pnrp_MaxRecordsParameterMustBeGreaterThanZero));
            }

            //---------------------------------------------------
            //Assume all clouds if the clould passed is null?
            //---------------------------------------------------
            if (cloud == null)
            {
                cloud = Cloud.Available;
            }
            
            //---------------------------------------------------
            //Demand CAS permissions
            //---------------------------------------------------
            PnrpPermission.UnrestrictedPnrpPermission.Demand();

            //---------------------------------------------------------------
            //No perf hit here, real native call happens only one time if it 
            //did not already happen
            //---------------------------------------------------------------
            UnsafeP2PNativeMethods.PnrpStartup();

            //---------------------------------------------------------------
            //Trace log
            //---------------------------------------------------------------
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Sync Resolve called with PeerName: {0}, Cloud: {1}, MaxRecords {2}", peerName, cloud, maxRecords);

            SafePeerData shEndPointInfoArray;
            string NativeCloudName = cloud.InternalName;
            UInt32 ActualCountOfEndPoints = (UInt32)maxRecords;
            int result = UnsafeP2PNativeMethods.PeerPnrpResolve(peerName.ToString(), 
                                                                NativeCloudName, 
                                                                ref ActualCountOfEndPoints, 
                                                                out shEndPointInfoArray);
            if (result != 0)
            {
                throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotStartNameResolution), result);
            }

            //---------------------------------------------------
            //If there are no endpoints returned, return 
            //an empty PeerNameRecord Collection
            //---------------------------------------------------
            PeerNameRecordCollection PeerNameRecords = new PeerNameRecordCollection();
            if (ActualCountOfEndPoints != 0)
            {
                try
                {
                    unsafe
                    {
                        IntPtr pEndPointInfoArray = shEndPointInfoArray.DangerousGetHandle();
                        PEER_PNRP_ENDPOINT_INFO* pEndPoints = (PEER_PNRP_ENDPOINT_INFO*)pEndPointInfoArray;
                        for (int i = 0; i < ActualCountOfEndPoints; i++)
                        {
                            PeerNameRecord record = new PeerNameRecord();
                            PEER_PNRP_ENDPOINT_INFO* pEndPointInfo = &pEndPoints[i];
                            record.PeerName = new PeerName(Marshal.PtrToStringUni(pEndPointInfo->pwszPeerName));
                            string comment = Marshal.PtrToStringUni(pEndPointInfo->pwszComment);
                            if (comment != null && comment.Length > 0)
                            {
                                record.Comment = comment;
                            }
    
                            if (pEndPointInfo->payLoad.cbPayload != 0)
                            {
                                record.Data = new byte[pEndPointInfo->payLoad.cbPayload];
                                Marshal.Copy(pEndPointInfo->payLoad.pbPayload, record.Data, 0, (int)pEndPointInfo->payLoad.cbPayload);
    
                            }
                            //record.EndPointList = new IPEndPoint[pEndPointInfo->cAddresses];
                            IntPtr ppSOCKADDRs = pEndPointInfo->ArrayOfSOCKADDRIN6Pointers;
                            for (UInt32 j = 0; j < pEndPointInfo->cAddresses; j++)
                            {
                                IntPtr pSOCKADDR = Marshal.ReadIntPtr(ppSOCKADDRs);
    
                                byte[] AddressFamilyBuffer = new byte[2];
                                Marshal.Copy(pSOCKADDR, AddressFamilyBuffer, 0, 2);
                                int addressFamily = 0;
    #if BIGENDIAN
                                addressFamily = AddressFamilyBuffer[1] + ((int)AddressFamilyBuffer[0] << 8);
    #else
                                addressFamily = AddressFamilyBuffer[0] + ((int)AddressFamilyBuffer[1] << 8);
    #endif
                                byte[] buffer = new byte[((AddressFamily)addressFamily == AddressFamily.InterNetwork) ? SystemNetHelpers.IPv4AddressSize : SystemNetHelpers.IPv6AddressSize];
                                Marshal.Copy(pSOCKADDR, buffer, 0, buffer.Length);
                                IPEndPoint ipe = SystemNetHelpers.IPEndPointFromSOCKADDRBuffer(buffer);
                                record.EndPointCollection.Add(ipe);
                                ppSOCKADDRs = (IntPtr)((long)ppSOCKADDRs + Marshal.SizeOf(typeof(IntPtr)));
                            }
                            //----------------------------------
                            //Dump for trace
                            //----------------------------------
                            record.TracePeerNameRecord();
                            //----------------------------------                
                            //Add to collection
                            //----------------------------------
                            PeerNameRecords.Add(record);
                        }
                    }
                }
                finally
                {
                    shEndPointInfoArray.Dispose();
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Sync Resolve returnig with PeerNameRecord count :{0}", PeerNameRecords.Count);
            return PeerNameRecords;
        }