Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        private static PeerNameRecordCollection Resolve(string serviceName)
        {
            var resolver = new PeerNameResolver();
            var peerName = new PeerName(serviceName, PeerNameType.Unsecured);

            return(resolver.Resolve(peerName));
        }
Exemplo n.º 5
0
        public List <PeerInfo> ResolveByPeerHostName(string peerHostName)
        {
            try
            {
                if (string.IsNullOrEmpty(peerHostName))
                {
                    throw new ArgumentException("Cannot have a null or empty host peer name.");
                }

                PeerNameResolver resolver   = new PeerNameResolver();
                List <PeerInfo>  foundPeers = new List <PeerInfo>();
                var resolvedName            = resolver.Resolve(new PeerName(peerHostName, PeerNameType.Unsecured), Cloud.AllLinkLocal);
                foreach (var foundItem in resolvedName)
                {
                    foreach (var endPointInfo in foundItem.EndPointCollection)
                    {
                        PeerInfo peerInfo = new PeerInfo(foundItem.PeerName.PeerHostName, foundItem.PeerName.Classifier, endPointInfo.Port);
                        peerInfo.Comment = foundItem.Comment;
                        foundPeers.Add(peerInfo);
                    }
                }
                return(foundPeers);
            }
            catch (PeerToPeerException px)
            {
                throw new Exception(px.InnerException.Message);
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
0
        public List <NetworkHost> Find(string name)
        {
            if (type != NetworkTypes.Client)
            {
                throw new Exception("Only allowed for client!");
            }

            var hosts = new List <NetworkHost>();

            peerNameResolver = new PeerNameResolver();
            peerName         = new PeerName(name, PeerNameType.Unsecured);
            var results = peerNameResolver.Resolve(peerName);

            foreach (var record in results)
            {
                var host = new NetworkHost(record.Comment);
                foreach (var endpoint in record.EndPointCollection)
                {
                    if (endpoint.AddressFamily == AddressFamily.InterNetwork)
                    {
                        Console.WriteLine(string.Format("Found EndPoint {0}:{1}", endpoint.Address, endpoint.Port));
                        host.endpoints.Add(endpoint);
                    }
                }

                if (host.endpoints.Count != 0)
                {
                    hosts.Add(host);
                }
            }

            return(hosts);
        }
Exemplo n.º 8
0
        public PeerNameRecordCollection LookupRegistration(PeerName peerName)
        {
            if (peerName == null)
            {
                throw new ArgumentException("Cannot have null or empty peerName");
            }

            return(_resolver.Resolve(peerName));
        }
Exemplo n.º 9
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);
                    }
                }
            }
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        /// <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);
                }
            }
        }
Exemplo n.º 12
0
        static IEnumerable <IPEndPoint> Resolve(string name)
        {
            Console.Write("resolving " + name + "...");
            var peername = new PeerName(name, PeerNameType.Unsecured);

            try {
                IPEndPoint[] result = resolver.Resolve(peername)
                                      .SelectMany(record => record.EndPointCollection)
                                      // .Where(ep => ep.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                                      .ToArray();
                Console.WriteLine(result.Length + " entries");
                return(result);
            } catch (PeerToPeerException e) {
                Console.WriteLine("resolve failed: " + e.Message);
                return(new IPEndPoint[] { new IPEndPoint(IPAddress.Loopback, 4079) });
            }
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        public PeerNameResult ResolveHostName(string hostPeerName)
        {
            if (string.IsNullOrEmpty(hostPeerName))
            {
                throw new ArgumentException("Cannot have a null or empty host peer name.");
            }

            PeerNameResolver resolver = new PeerNameResolver();

            var results = resolver.Resolve(new PeerName(hostPeerName, PeerNameType.Unsecured), Cloud.Global);

            if (results == null || results.Count == 0)
            {
                throw new PeerToPeerException(string.Format("Unable to resolve {0}", hostPeerName));
            }

            return(new PeerNameResult(results[0].PeerName.PeerHostName, results[0].EndPointCollection[0].Port));
        }
Exemplo n.º 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();
        }
Exemplo n.º 16
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...");
            }
        }
Exemplo n.º 17
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();
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
0
        /// <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);
                    }
                }
            }
        }
Exemplo n.º 20
0
        // 搜索资源
        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);
                    }
                }
            }
        }
Exemplo n.º 21
0
        public void Send(PeerName peer, byte[] data)
        {
            var results = _peerResolver.Resolve(peer);

            var count = 1;

            foreach (var 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(Encoding.ASCII.GetString(record.Data));
                }

                Console.WriteLine();
                Console.WriteLine("Endpoints:");

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

                count++;
            }
        }
Exemplo n.º 22
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);
                        }
                    }
                }
            }
        }
Exemplo n.º 23
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.");
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 4)
                {
#if DEBUG
                    Console.WriteLine("invalid arguments");
                    Console.ReadLine();
#endif
                    return;
                }

                string pipeName         = args[0];
                string connectionTarget = args[1];
                string portNoStr        = args[2];
                string param3           = args[3];
#if DEBUG
                Console.WriteLine($"{nameof(pipeName)}={pipeName},{nameof(connectionTarget)}={connectionTarget},{nameof(portNoStr)}={portNoStr},{nameof(param3)}={param3}");
#endif

                int      portNo   = int.Parse(portNoStr);
                PeerName peerName = null;
                Cloud    cloud    = null;

                switch (connectionTarget.ToLower())
                {
                case "global":
                    cloud    = Cloud.Global;
                    peerName = PeerName.CreateFromPeerHostName(param3);
                    break;

                case "local":
                    cloud    = Cloud.AllLinkLocal;
                    peerName = new PeerName(param3, PeerNameType.Secured);
                    break;

                default:
                    throw new ArgumentException("invalid argument", "param 1");
                }

                using (var namedPipePipeToPeer = new NamedPipeClientStream(".", pipeName + "PipeToPeer"))
                {
                    namedPipePipeToPeer.Connect();
#if DEBUG
                    Console.WriteLine("connect named pipe 'PipeToPeer'");
#endif
                    using (var namedPipePeerToPipe = new NamedPipeClientStream(".", pipeName + "PeerToPipe"))
                    {
                        namedPipePeerToPipe.Connect();
#if DEBUG
                        Console.WriteLine("connect named pipe 'PeerToPipe'");
#endif
                        var peerNameResolver = new PeerNameResolver();

                        while (namedPipePipeToPeer.IsConnected && namedPipePeerToPipe.IsConnected)
                        {
                            int result = namedPipePipeToPeer.ReadByte();
                            if (result < 0)
                            {
                                break;
                            }
#if DEBUG
                            Console.WriteLine("read");
#endif
                            // 読み込み実行
                            if (result == 0xFF)
                            {
#if DEBUG
                                Console.WriteLine("resolve start");
#endif
                                foreach (var record in peerNameResolver.Resolve(peerName, cloud))
                                {
                                    namedPipePeerToPipe.Write(record.Data, 0, record.Data.Length);
                                    namedPipePeerToPipe.Flush();
                                    namedPipePeerToPipe.WaitForPipeDrain();
#if DEBUG
                                    var buffer = record.Data;
                                    var length = record.Data.Length;
                                    Console.WriteLine($"{nameof(buffer)}[{length}]={BitConverter.ToString(buffer, 0, length).Replace("-", string.Empty)}");
#endif
                                }

                                namedPipePeerToPipe.WriteByte(0xFF);//終端
                                namedPipePeerToPipe.Flush();
                                namedPipePeerToPipe.WaitForPipeDrain();

#if DEBUG
                                Console.WriteLine("resolve end");
#endif
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex);
                Console.ReadLine();
#endif
            }
#if DEBUG
            Console.WriteLine("end");
#endif
        }